]> git.lyx.org Git - features.git/blob - src/insets/InsetGraphicsParams.cpp
Store InsetGraphics bounding box in parsed form
[features.git] / src / insets / InsetGraphicsParams.cpp
1 /**
2  * \file InsetGraphicsParams.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Baruch Even
7  * \author Herbert Voß
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetGraphicsParams.h"
15
16 #include "Buffer.h"
17 #include "LyX.h" // for use_gui
18 #include "Lexer.h"
19 #include "LyXRC.h"
20
21 #include "graphics/epstools.h"
22 #include "graphics/GraphicsTypes.h"
23
24 #include "support/convert.h"
25 #include "support/debug.h"
26 #include "support/lyxlib.h"
27 #include "support/lstrings.h"
28 #include "support/Translator.h"
29
30 #include <ostream>
31
32 using namespace std;
33 using namespace lyx::support;
34
35 namespace lyx {
36
37
38 InsetGraphicsParams::InsetGraphicsParams()
39 {
40         init();
41 }
42
43
44 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
45 {
46         // I decided to skip the initialization since the copy will overwrite
47         // everything anyway.
48         //    init();
49         copy(igp);
50 }
51
52
53 void InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
54 {
55         // Are we assigning the object into itself?
56         if (this == &params)
57                 return;
58         copy(params);
59 }
60
61
62 void InsetGraphicsParams::init()
63 {
64         filename.erase();
65         lyxscale = 100;                 // lyx scaling in percentage
66         display = true;         // may be overriden by display mode in preferences
67         scale = string("100");                  // output scaling in percentage
68         width = Length();
69         height = Length();
70         keepAspectRatio = false;        // for LaTeX output
71         draft = false;                  // draft mode
72         scaleBeforeRotation = false;    // scale image before rotating
73
74         bbox = graphics::BoundingBox(); // bounding box
75         clip = false;                   // clip image
76
77         rotateAngle = "0";              // angle of rotation in degrees
78         rotateOrigin.erase();           // Origin of rotation
79         special.erase();                // additional userdefined stuff
80         groupId.clear();
81 }
82
83
84 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
85 {
86         filename = igp.filename;
87         lyxscale = igp.lyxscale;
88         display = igp.display;
89         scale = igp.scale;
90         width = igp.width;
91         height = igp.height;
92         keepAspectRatio = igp.keepAspectRatio;
93         draft = igp.draft;
94         scaleBeforeRotation = igp.scaleBeforeRotation;
95
96         bbox = igp.bbox;
97         clip = igp.clip;
98
99         rotateAngle = igp.rotateAngle;
100         rotateOrigin = igp.rotateOrigin;
101         special = igp.special;
102         groupId = igp.groupId;
103 }
104
105
106 bool operator==(InsetGraphicsParams const & left,
107                 InsetGraphicsParams const & right)
108 {
109         return left.filename == right.filename &&
110             left.lyxscale == right.lyxscale &&
111             left.display == right.display &&
112             left.scale == right.scale &&
113             left.width == right.width &&
114             left.height == right.height &&
115             left.keepAspectRatio == right.keepAspectRatio &&
116             left.draft == right.draft &&
117             left.scaleBeforeRotation == right.scaleBeforeRotation &&
118
119             left.bbox == right.bbox &&
120             left.clip == right.clip &&
121
122             left.rotateAngle == right.rotateAngle &&
123             left.rotateOrigin == right.rotateOrigin &&
124             left.special == right.special &&
125             left.groupId == right.groupId;
126 }
127
128
129 bool operator!=(InsetGraphicsParams const & left,
130                 InsetGraphicsParams const & right)
131 {
132         return  !(left == right);
133 }
134
135
136 void InsetGraphicsParams::Write(ostream & os, Buffer const & buffer) const
137 {
138         // Do not write the default values
139         if (!filename.empty())
140                 os << "\tfilename " << filename.outputFileName(buffer.filePath()) << '\n';
141         if (lyxscale != 100)
142                 os << "\tlyxscale " << lyxscale << '\n';
143         if (!display)
144                 os << "\tdisplay false\n";
145         if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
146                 if (!float_equal(convert<double>(scale), 100.0, 0.05))
147                         os << "\tscale " << scale << '\n';
148         } else {
149                 if (!width.zero())
150                         os << "\twidth " << width.asString() << '\n';
151                 if (!height.zero())
152                         os << "\theight " << height.asString() << '\n';
153         }
154
155         if (keepAspectRatio)
156                 os << "\tkeepAspectRatio\n";
157         if (draft)                      // draft mode
158                 os << "\tdraft\n";
159         if (scaleBeforeRotation)
160                 os << "\tscaleBeforeRotation\n";
161
162         if (!bbox.empty())              // bounding box
163                 os << "\tBoundingBox " << bbox << '\n';
164         if (clip)                       // clip image
165                 os << "\tclip\n";
166
167         if (!rotateAngle.empty()
168                 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
169                 os << "\trotateAngle " << rotateAngle << '\n';
170         if (!rotateOrigin.empty())
171                 os << "\trotateOrigin " << rotateOrigin << '\n';
172         if (!special.empty())
173                 os << "\tspecial " << special << '\n';
174         if (!groupId.empty())
175                 os << "\tgroupId "<< groupId << '\n';
176 }
177
178
179 bool InsetGraphicsParams::Read(Lexer & lex, string const & token, string const & bufpath)
180 {
181         if (token == "filename") {
182                 lex.eatLine();
183                 filename.set(lex.getString(), bufpath);
184         } else if (token == "lyxscale") {
185                 lex.next();
186                 lyxscale = lex.getInteger();
187         } else if (token == "display") {
188                 lex.next();
189                 string const type = lex.getString();
190                 display = lex.getString() != "false";
191         } else if (token == "scale") {
192                 lex.next();
193                 scale = lex.getString();
194         } else if (token == "width") {
195                 lex.next();
196                 width = Length(lex.getString());
197                 scale = string();
198         } else if (token == "height") {
199                 lex.next();
200                 height = Length(lex.getString());
201                 scale = string();
202         } else if (token == "keepAspectRatio") {
203                 keepAspectRatio = true;
204         } else if (token == "draft") {
205                 draft = true;
206         } else if (token == "scaleBeforeRotation") {
207                 scaleBeforeRotation = true;
208         } else if (token == "BoundingBox") {
209                 lex.next();
210                 bbox.xl = Length(lex.getString());
211                 lex.next();
212                 bbox.yb = Length(lex.getString());
213                 lex.next();
214                 bbox.xr = Length(lex.getString());
215                 lex.next();
216                 bbox.yt = Length(lex.getString());
217         } else if (token == "clip") {
218                 clip = true;
219         } else if (token == "rotateAngle") {
220                 lex.next();
221                 rotateAngle = lex.getString();
222         } else if (token == "rotateOrigin") {
223                 lex.next();
224                 rotateOrigin=lex.getString();
225         } else if (token == "special") {
226                 lex.eatLine();
227                 special = lex.getString();
228         } else if (token == "groupId") {
229                 lex.eatLine();
230                 groupId = lex.getString();
231
232         // catch and ignore following two old-format tokens and their arguments.
233         // e.g. "size_kind scale" clashes with the setting of the
234         // "scale <value>" keyword.
235         } else if (token == "size_kind" || token == "lyxsize_kind") {
236                 lex.next();
237                 lex.getString();
238
239         } else {
240                 // If it's none of the above, it's not ours.
241                 return false;
242         }
243         return true;
244 }
245
246
247 graphics::Params InsetGraphicsParams::as_grfxParams() const
248 {
249         graphics::Params pars;
250         pars.filename = filename;
251         pars.scale = lyxscale;
252         pars.angle = convert<double>(rotateAngle);
253
254         if (clip) {
255                 pars.bb = bbox;
256
257                 // Get the original Bounding Box from the file
258                 string const tmp = graphics::readBB_from_PSFile(filename);
259                 LYXERR(Debug::GRAPHICS, "BB_from_File: " << tmp);
260                 if (!tmp.empty()) {
261                         unsigned int const bb_orig_xl = convert<unsigned int>(token(tmp, ' ', 0));
262                         unsigned int const bb_orig_yb = convert<unsigned int>(token(tmp, ' ', 1));
263
264                         // new pars.bb values must be >= zero
265                         if (pars.bb.xl.inBP() > static_cast<int>(bb_orig_xl))
266                                 pars.bb.xl = Length(pars.bb.xl.inBP() - bb_orig_xl, Length::BP);
267                         else
268                                 pars.bb.xl = Length();
269
270                         if (pars.bb.xr.inBP() > static_cast<int>(bb_orig_xl))
271                                 pars.bb.xr = Length(pars.bb.xr.inBP() - bb_orig_xl, Length::BP);
272                         else
273                                 pars.bb.xr = Length();
274
275                         if (pars.bb.yb.inBP() > static_cast<int>(bb_orig_yb))
276                                 pars.bb.yb = Length(pars.bb.yb.inBP() - bb_orig_yb, Length::BP);
277                         else
278                                 pars.bb.yb = Length();
279
280                         if (pars.bb.yt.inBP() > static_cast<int>(bb_orig_yb))
281                                 pars.bb.yt = Length(pars.bb.yt.inBP() - bb_orig_yb, Length::BP);
282                         else
283                                 pars.bb.yt = Length();
284                 }
285
286                 // Paranoia check.
287                 int const width  = pars.bb.xr.inBP() - pars.bb.xl.inBP();
288                 int const height = pars.bb.yt.inBP() - pars.bb.yb.inBP();
289
290                 if (width  < 0 || height < 0) {
291                         pars.bb.xl = Length();
292                         pars.bb.xr = Length();
293                         pars.bb.yb = Length();
294                         pars.bb.yt = Length();
295                 }
296         }
297
298         pars.display = display;
299
300         // Override the above if we're not using a gui
301         if (!use_gui)
302                 pars.display = false;
303
304         return pars;
305 }
306
307
308 } // namespace lyx