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