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