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