]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsParams.C
(Herbert): use inBP rather than inPixels + further manipulation as this
[lyx.git] / src / graphics / GraphicsParams.C
1 /*
2  * \file GraphicsParams.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Angus Leeming <a.leeming@ic.ac.uk>
7  */
8
9 #include <config.h>
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "GraphicsParams.h"
16 #include "insets/insetgraphicsParams.h"
17 #include "lyxrc.h"
18 #include "debug.h"
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
21 #include "support/LAssert.h"
22
23 namespace grfx {
24
25 GParams::GParams(InsetGraphicsParams const & iparams, string const & filepath)
26         : width(0),
27           height(0),
28           scale(0),
29           angle(0)
30 {
31         filename = iparams.filename;
32         if (!filepath.empty()) {
33                 filename = MakeAbsPath(filename, filepath);
34         }
35
36         if (iparams.clip) {
37                 bb = iparams.bb;
38
39                 // Get the original Bounding Box from the file
40                 string const tmp = readBB_from_PSFile(filename);
41                 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
42                 if (!tmp.empty()) {
43                         int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
44                         int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
45
46                         bb.xl -= bb_orig_xl;
47                         bb.xr -= bb_orig_xl;
48                         bb.yb -= bb_orig_yb;
49                         bb.yt -= bb_orig_yb;
50                 }
51
52                 bb.xl = std::max(0, bb.xl);
53                 bb.xr = std::max(0, bb.xr);
54                 bb.yb = std::max(0, bb.yb);
55                 bb.yt = std::max(0, bb.yt);
56
57                 // Paranoia check.
58                 int const width  = bb.xr - bb.xl;
59                 int const height = bb.yt - bb.yb;
60
61                 if (width  < 0 || height < 0) {
62                         bb.xl = 0;
63                         bb.xr = 0;
64                         bb.yb = 0;
65                         bb.yt = 0;
66                 }
67         }
68         
69         if (iparams.rotate)
70                 angle = int(iparams.rotateAngle);
71
72         if (iparams.display == InsetGraphicsParams::DEFAULT) {
73
74                 if (lyxrc.display_graphics == "mono")
75                         display = MONOCHROME;
76                 else if (lyxrc.display_graphics == "gray")
77                         display = GRAYSCALE;
78                 else if (lyxrc.display_graphics == "color")
79                         display = COLOR;
80                 else
81                         display = NONE;
82
83         } else if (iparams.display == InsetGraphicsParams::NONE) {
84                 display = NONE;
85
86         } else if (iparams.display == InsetGraphicsParams::MONOCHROME) {
87                 display = MONOCHROME;
88
89         } else if (iparams.display == InsetGraphicsParams::GRAYSCALE) {
90                 display = GRAYSCALE;
91
92         } else if (iparams.display == InsetGraphicsParams::COLOR) {
93                 display = COLOR;
94         }
95
96         // Override the above if we're not using a gui
97         if (!lyxrc.use_gui) {
98                 display = NONE;
99         }
100
101         if (iparams.lyxsize_type == InsetGraphicsParams::SCALE) {
102                 scale = iparams.lyxscale;
103
104         } else if (iparams.lyxsize_type == InsetGraphicsParams::WH) {
105                 if (!iparams.lyxwidth.zero())
106                         width  = iparams.lyxwidth.inBP();
107                 if (!iparams.lyxheight.zero())
108                         height = iparams.lyxheight.inBP();
109         }
110 }
111
112
113 bool operator==(GParams const & a, GParams const & b)
114 {
115         return (a.filename == b.filename &&
116                 a.display  == b.display &&
117                 a.bb       == b.bb &&
118                 a.width    == b.width &&
119                 a.height   == b.height &&
120                 a.scale    == b.scale &&
121                 a.angle    == b.angle);
122 }
123
124
125 bool operator!=(GParams const & a, GParams const & b)
126 {
127         return !(a == b);
128 }
129
130
131 BoundingBox::BoundingBox()
132         : xl(0), yb(0), xr(0), yt(0)
133 {}
134
135
136 BoundingBox::BoundingBox(string const & bb)
137         : xl(0), yb(0), xr(0), yt(0)
138 {
139         if (bb.empty())
140                 return;
141
142         std::istringstream is(bb.c_str());
143         string a, b, c, d;
144         is >> a >> b >> c >> d;
145
146         // inBP returns the length in Postscript points.
147         // Note further that there are 72 Postscript pixels per inch.
148         int const xl_tmp = LyXLength(a).inBP();
149         int const yb_tmp = LyXLength(b).inBP();
150         int const xr_tmp = LyXLength(c).inBP();
151         int const yt_tmp = LyXLength(d).inBP();
152
153         if (xr_tmp <= xl_tmp || yt_tmp <= yb_tmp)
154                 return;
155
156         xl = xl_tmp;
157         yb = yb_tmp;
158         xr = xr_tmp;
159         yt = yt_tmp;
160 }
161
162
163 bool BoundingBox::empty() const
164 {
165         return (!xl && !yb && !xr && !yt);
166 }
167
168
169 bool operator==(BoundingBox const & a, BoundingBox const & b)
170 {
171         return (a.xl == b.xl &&
172                 a.yb == b.yb &&
173                 a.xr == b.xr &&
174                 a.yt == b.yt);
175 }
176
177
178 bool operator!=(BoundingBox const & a, BoundingBox const & b)
179 {
180         return !(a == b);
181 }
182
183 } // namespace grfx