]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsParams.C
Revert previous change as it's bollocks. Herbert, your infallibility is
[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 "debug.h"
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
21 #include "support/LAssert.h"
22
23 namespace grfx {
24
25 GParams::GParams(InsetGraphicsParams const & iparams, string const & filepath)
26         : width(0),
27           height(0),
28           scale(0),
29           angle(0)
30 {
31         filename = iparams.filename;
32         if (!filepath.empty()) {
33                 filename = MakeAbsPath(filename, filepath);
34         }
35
36         if (iparams.clip) {
37                 bb = iparams.bb;
38
39                 // Get the original Bounding Box from the file
40                 string const tmp = readBB_from_PSFile(filename);
41                 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
42                 if (!tmp.empty()) {
43                         int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
44                         int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
45
46                         bb.xl -= bb_orig_xl;
47                         bb.xr -= bb_orig_xl;
48                         bb.yb -= bb_orig_yb;
49                         bb.yt -= bb_orig_yb;
50                 }
51
52                 bb.xl = std::max(0, bb.xl);
53                 bb.xr = std::max(0, bb.xr);
54                 bb.yb = std::max(0, bb.yb);
55                 bb.yt = std::max(0, bb.yt);
56
57                 // Paranoia check.
58                 int const width  = bb.xr - bb.xl;
59                 int const height = bb.yt - bb.yb;
60
61                 if (width  < 0 || height < 0) {
62                         bb.xl = 0;
63                         bb.xr = 0;
64                         bb.yb = 0;
65                         bb.yt = 0;
66                 }
67         }
68         
69         if (iparams.rotate)
70                 angle = int(iparams.rotateAngle);
71
72         if (iparams.display == InsetGraphicsParams::DEFAULT) {
73
74                 if (lyxrc.display_graphics == "mono")
75                         display = MONOCHROME;
76                 else if (lyxrc.display_graphics == "gray")
77                         display = GRAYSCALE;
78                 else if (lyxrc.display_graphics == "color")
79                         display = COLOR;
80                 else
81                         display = NONE;
82
83         } else if (iparams.display == InsetGraphicsParams::NONE) {
84                 display = NONE;
85
86         } else if (iparams.display == InsetGraphicsParams::MONOCHROME) {
87                 display = MONOCHROME;
88
89         } else if (iparams.display == InsetGraphicsParams::GRAYSCALE) {
90                 display = GRAYSCALE;
91
92         } else if (iparams.display == InsetGraphicsParams::COLOR) {
93                 display = COLOR;
94         }
95
96         // Override the above if we're not using a gui
97         if (!lyxrc.use_gui) {
98                 display = NONE;
99         }
100
101         if (iparams.lyxsize_type == InsetGraphicsParams::SCALE) {
102                 scale = iparams.lyxscale;
103
104         } else if (iparams.lyxsize_type == InsetGraphicsParams::WH) {
105                 if (!iparams.lyxwidth.zero())
106                         width  = iparams.lyxwidth.inPixels(1, 1);
107                 if (!iparams.lyxheight.zero())
108                         height = iparams.lyxheight.inPixels(1, 1);
109
110                 // inPixels returns a value scaled by lyxrc.zoom.
111                 // We want, therefore, to undo this.
112                 double const scaling_factor = 100.0 / double(lyxrc.zoom);
113                 width  = uint(scaling_factor * width);
114                 height = uint(scaling_factor * height);
115         }
116 }
117
118
119 bool operator==(GParams const & a, GParams const & b)
120 {
121         return (a.filename == b.filename &&
122                 a.display  == b.display &&
123                 a.bb       == b.bb &&
124                 a.width    == b.width &&
125                 a.height   == b.height &&
126                 a.scale    == b.scale &&
127                 a.angle    == b.angle);
128 }
129
130
131 bool operator!=(GParams const & a, GParams const & b)
132 {
133         return !(a == b);
134 }
135
136
137 BoundingBox::BoundingBox()
138         : xl(0), yb(0), xr(0), yt(0)
139 {}
140
141
142 BoundingBox::BoundingBox(string const & bb)
143         : xl(0), yb(0), xr(0), yt(0)
144 {
145         if (bb.empty())
146                 return;
147
148         std::istringstream is(bb.c_str());
149         string a, b, c, d;
150         is >> a >> b >> c >> d;
151
152         // inBP returns the length in Postscript points.
153         // Note further that there are 72 Postscript pixels per inch.
154         int const xl_tmp = LyXLength(a).inBP();
155         int const yb_tmp = LyXLength(b).inBP();
156         int const xr_tmp = LyXLength(c).inBP();
157         int const yt_tmp = LyXLength(d).inBP();
158
159         if (xr_tmp <= xl_tmp || yt_tmp <= yb_tmp)
160                 return;
161
162         xl = xl_tmp;
163         yb = yb_tmp;
164         xr = xr_tmp;
165         yt = yt_tmp;
166 }
167
168
169 bool BoundingBox::empty() const
170 {
171         return (!xl && !yb && !xr && !yt);
172 }
173
174
175 bool operator==(BoundingBox const & a, BoundingBox const & b)
176 {
177         return (a.xl == b.xl &&
178                 a.yb == b.yb &&
179                 a.xr == b.xr &&
180                 a.yt == b.yt);
181 }
182
183
184 bool operator!=(BoundingBox const & a, BoundingBox const & b)
185 {
186         return !(a == b);
187 }
188
189 } // namespace grfx