]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsParams.C
Store BoundingBox relative to original.
[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 "support/filetools.h"
19 #include "support/lstrings.h"
20 #include "support/LAssert.h"
21
22 namespace grfx {
23
24 GParams::GParams(InsetGraphicsParams const & iparams, string const & filepath)
25         : width(0),
26           height(0),
27           scale(0),
28           angle(0)
29 {
30         filename = iparams.filename;
31         if (!filepath.empty()) {
32                 filename = MakeAbsPath(filename, filepath);
33         }
34
35         if (iparams.clip) {
36                 bb = iparams.bb;
37
38                 // Get the original Bounding Box from the file
39                 string const bb_orig_str = readBB_from_PSFile(filename);
40                 if (!bb_orig_str.empty()) {
41                         BoundingBox bb_orig;
42                         bb_orig.xl = strToInt(token(bb_orig_str, ' ', 0));
43                         bb_orig.yb = strToInt(token(bb_orig_str, ' ', 1));
44                         bb_orig.xr = strToInt(token(bb_orig_str, ' ', 2));
45                         bb_orig.yt = strToInt(token(bb_orig_str, ' ', 3));
46
47                         bb.xl -= bb_orig.xl;
48                         bb.xr -= bb_orig.xl;
49                         bb.yb -= bb_orig.yb;
50                         bb.yt -= bb_orig.yb;
51                 }
52
53                 // Paranoia check.
54                 int const width  = bb.xr - bb.xl;
55                 int const height = bb.yt - bb.yb;
56
57                 if (width  < 0 || height < 0) {
58                         bb.xl = 0;
59                         bb.xr = 0;
60                         bb.yb = 0;
61                         bb.yt = 0;
62                 }
63         }
64         
65         if (iparams.rotate)
66                 angle = int(iparams.rotateAngle);
67
68         if (iparams.display == InsetGraphicsParams::DEFAULT) {
69
70                 if (lyxrc.display_graphics == "mono")
71                         display = MONOCHROME;
72                 else if (lyxrc.display_graphics == "gray")
73                         display = GRAYSCALE;
74                 else if (lyxrc.display_graphics == "color")
75                         display = COLOR;
76                 else
77                         display = NONE;
78
79         } else if (iparams.display == InsetGraphicsParams::NONE) {
80                 display = NONE;
81
82         } else if (iparams.display == InsetGraphicsParams::MONOCHROME) {
83                 display = MONOCHROME;
84
85         } else if (iparams.display == InsetGraphicsParams::GRAYSCALE) {
86                 display = GRAYSCALE;
87
88         } else if (iparams.display == InsetGraphicsParams::COLOR) {
89                 display = COLOR;
90         }
91
92         // Override the above if we're not using a gui
93         if (!lyxrc.use_gui) {
94                 display = NONE;
95         }
96
97         if (iparams.lyxsize_type == InsetGraphicsParams::SCALE) {
98                 scale = iparams.lyxscale;
99
100         } else if (iparams.lyxsize_type == InsetGraphicsParams::WH) {
101                 if (!iparams.lyxwidth.zero())
102                         width  = iparams.lyxwidth.inPixels(1, 1);
103                 if (!iparams.lyxheight.zero())
104                         height = iparams.lyxheight.inPixels(1, 1);
105
106                 // inPixels returns a value scaled by lyxrc.zoom.
107                 // We want, therefore, to undo this.
108                 double const scaling_factor = 100.0 / double(lyxrc.zoom);
109                 width  = uint(scaling_factor * width);
110                 height = uint(scaling_factor * height);
111         }
112 }
113
114
115 bool operator==(GParams const & a, GParams const & b)
116 {
117         return (a.filename == b.filename &&
118                 a.display  == b.display &&
119                 a.bb       == b.bb &&
120                 a.width    == b.width &&
121                 a.height   == b.height &&
122                 a.scale    == b.scale &&
123                 a.angle    == b.angle);
124 }
125
126
127 bool operator!=(GParams const & a, GParams const & b)
128 {
129         return !(a == b);
130 }
131
132
133 BoundingBox::BoundingBox()
134         : xl(0), yb(0), xr(0), yt(0)
135 {}
136
137
138 BoundingBox::BoundingBox(string const & bb)
139         : xl(0), yb(0), xr(0), yt(0)
140 {
141         if (bb.empty())
142                 return;
143
144         std::istringstream is(bb.c_str());
145         string a, b, c, d;
146         is >> a >> b >> c >> d;
147
148         // Don't need to check that the strings are valid LyXLength's
149         // because this is done in the LyXLength c-tor.
150         LyXLength const length_xl(a);
151         LyXLength const length_yb(b);
152         LyXLength const length_xr(c);
153         LyXLength const length_yt(d);
154
155         // inPixels returns the length in inches, scaled by
156         // lyxrc.dpi and lyxrc.zoom.
157         // We want, therefore, to undo all this lyxrc nonsense because we
158         // want the bounding box in Postscript pixels.
159         // Note further that there are 72 Postscript pixels per inch.
160         double const scaling_factor = 7200.0 / (lyxrc.dpi * lyxrc.zoom);
161         int const xl_tmp = int(scaling_factor * length_xl.inPixels(1, 1));
162         int const yb_tmp = int(scaling_factor * length_yb.inPixels(1, 1));
163         int const xr_tmp = int(scaling_factor * length_xr.inPixels(1, 1));
164         int const yt_tmp = int(scaling_factor * length_yt.inPixels(1, 1));
165
166         if (xr_tmp <= xl_tmp || yt_tmp <= yb_tmp)
167                 return;
168
169         xl = xl_tmp;
170         yb = yb_tmp;
171         xr = xr_tmp;
172         yt = yt_tmp;
173 }
174
175
176 bool BoundingBox::empty() const
177 {
178         return (!xl && !yb && !xr && !yt);
179 }
180
181
182 bool operator==(BoundingBox const & a, BoundingBox const & b)
183 {
184         return (a.xl == b.xl &&
185                 a.yb == b.yb &&
186                 a.xr == b.xr &&
187                 a.yt == b.yt);
188 }
189
190
191 bool operator!=(BoundingBox const & a, BoundingBox const & b)
192 {
193         return !(a == b);
194 }
195
196 } // namespace grfx