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