]> git.lyx.org Git - features.git/blob - src/graphics/GraphicsParams.C
f63b28b0b05c1c1398fe51a20ba5ee5874828146
[features.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         string tmp1;
118         string tmp2 = split(bb, tmp1, ' ');
119         if (!isValidLength(tmp1))
120                 return;
121
122         LyXLength const length_xl(tmp1);
123
124         tmp2 = split(tmp2, tmp1, ' ');
125         if (!isValidLength(tmp1))
126                 return;
127
128         LyXLength const length_yb(tmp1);
129
130         tmp2 = split(tmp2, tmp1, ' ');
131         if (!isValidLength(tmp1) || !isValidLength(tmp2))
132                 return;
133
134         LyXLength const length_xr(tmp1);
135         LyXLength const length_yt(tmp2);
136
137         // inPixels returns the length in inches, scaled by
138         // lyxrc.dpi and lyxrc.zoom.
139         // We want, therefore, to undo all this lyxrc nonsense because we
140         // want the bounding box in Postscript pixels.
141         // Note further that there are 72 Postscript pixels per inch.
142         double const scaling_factor = 7200.0 / (lyxrc.dpi * lyxrc.zoom);
143         unsigned int const xl_tmp =
144                 uint(scaling_factor * length_xl.inPixels(1, 1));
145         unsigned int const yb_tmp =
146                 uint(scaling_factor * length_yb.inPixels(1, 1));
147         unsigned int const xr_tmp =
148                 uint(scaling_factor * length_xr.inPixels(1, 1));
149         unsigned int const yt_tmp =
150                 uint(scaling_factor * length_yt.inPixels(1, 1));
151
152         if (xr <= xl || yt <= yb)
153                 return;
154
155         xl = xl_tmp;
156         yb = yb_tmp;
157         xr = xr_tmp;
158         yt = yt_tmp;
159 }
160
161
162 bool BoundingBox::empty() const
163 {
164         return (!xl && !yb && !xr && !yt);
165 }
166
167
168 bool operator==(BoundingBox const & a, BoundingBox const & b)
169 {
170         return (a.xl == b.xl &&
171                 a.yb == b.yb &&
172                 a.xr == b.xr &&
173                 a.yt == b.yt);
174 }
175
176
177 bool operator!=(BoundingBox const & a, BoundingBox const & b)
178 {
179         return !(a == b);
180 }
181
182 } // namespace grfx