]> git.lyx.org Git - lyx.git/blob - src/insets/InsetGraphicsParams.cpp
9f6ffbb901ecbca5e387869c83d887a247582622
[lyx.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 InsetGraphicsParams & InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
54 {
55         // Are we assigning the object into itself?
56         if (this == &params)
57                 return *this;
58         copy(params);
59         return *this;
60 }
61
62
63 void InsetGraphicsParams::init()
64 {
65         filename.erase();
66         lyxscale = 100;                 // lyx scaling in percentage
67         display = true;         // may be overridden by display mode in preferences
68         scale = string("100");                  // output scaling in percentage
69         width = Length();
70         height = Length();
71         keepAspectRatio = false;        // for LaTeX output
72         draft = false;                  // draft mode
73         scaleBeforeRotation = false;    // scale image before rotating
74
75         bbox = graphics::BoundingBox(); // bounding box
76         clip = false;                   // clip image
77         darkModeSensitive = false;      // dark mode dependency
78
79         rotateAngle = "0";              // angle of rotation in degrees
80         rotateOrigin.erase();           // Origin of rotation
81         special.erase();                // additional userdefined stuff
82         groupId.clear();
83 }
84
85
86 void InsetGraphicsParams::copy(InsetGraphicsParams const & params)
87 {
88         filename = params.filename;
89         lyxscale = params.lyxscale;
90         display = params.display;
91         scale = params.scale;
92         width = params.width;
93         height = params.height;
94         keepAspectRatio = params.keepAspectRatio;
95         draft = params.draft;
96         scaleBeforeRotation = params.scaleBeforeRotation;
97
98         bbox = params.bbox;
99         clip = params.clip;
100         darkModeSensitive = params.darkModeSensitive;
101
102         rotateAngle = params.rotateAngle;
103         rotateOrigin = params.rotateOrigin;
104         special = params.special;
105         groupId = params.groupId;
106 }
107
108
109 bool operator==(InsetGraphicsParams const & left,
110                 InsetGraphicsParams const & right)
111 {
112         return left.filename == right.filename &&
113             left.lyxscale == right.lyxscale &&
114             left.display == right.display &&
115             left.scale == right.scale &&
116             left.width == right.width &&
117             left.height == right.height &&
118             left.keepAspectRatio == right.keepAspectRatio &&
119             left.draft == right.draft &&
120             left.scaleBeforeRotation == right.scaleBeforeRotation &&
121
122             left.bbox == right.bbox &&
123             left.clip == right.clip &&
124             left.darkModeSensitive == right.darkModeSensitive &&
125
126             left.rotateAngle == right.rotateAngle &&
127             left.rotateOrigin == right.rotateOrigin &&
128             left.special == right.special &&
129             left.groupId == right.groupId;
130 }
131
132
133 bool operator!=(InsetGraphicsParams const & left,
134                 InsetGraphicsParams const & right)
135 {
136         return  !(left == right);
137 }
138
139
140 void InsetGraphicsParams::Write(ostream & os, Buffer const & buffer) const
141 {
142         // Do not write the default values
143         if (!filename.empty())
144                 os << "\tfilename " << filename.outputFileName(buffer.filePath()) << '\n';
145         if (lyxscale != 100)
146                 os << "\tlyxscale " << lyxscale << '\n';
147         if (!display)
148                 os << "\tdisplay false\n";
149         if (darkModeSensitive)
150                 os << "\tdarkModeSensitive\n";
151         if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
152                 if (!float_equal(convert<double>(scale), 100.0, 0.05))
153                         os << "\tscale " << scale << '\n';
154         } else {
155                 if (!width.zero())
156                         os << "\twidth " << width.asString() << '\n';
157                 if (!height.zero())
158                         os << "\theight " << height.asString() << '\n';
159         }
160
161         if (keepAspectRatio)
162                 os << "\tkeepAspectRatio\n";
163         if (draft)                      // draft mode
164                 os << "\tdraft\n";
165         if (scaleBeforeRotation)
166                 os << "\tscaleBeforeRotation\n";
167
168         if (!bbox.empty())              // bounding box
169                 os << "\tBoundingBox " << bbox << '\n';
170         if (clip)                       // clip image
171                 os << "\tclip\n";
172
173         if (!rotateAngle.empty()
174                 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
175                 os << "\trotateAngle " << rotateAngle << '\n';
176         if (!rotateOrigin.empty())
177                 os << "\trotateOrigin " << rotateOrigin << '\n';
178         if (!special.empty())
179                 os << "\tspecial " << special << '\n';
180         if (!groupId.empty())
181                 os << "\tgroupId "<< groupId << '\n';
182 }
183
184
185 bool InsetGraphicsParams::Read(Lexer & lex, string const & token,
186                                Buffer const & buf, bool allowOrigin)
187 {
188         if (token == "filename") {
189                 lex.eatLine();
190                 if (allowOrigin)
191                         filename = buf.getReferencedFileName(lex.getString());
192                 else
193                         filename.set(lex.getString(), buf.filePath());
194         } else if (token == "lyxscale") {
195                 lex.next();
196                 lyxscale = lex.getInteger();
197         } else if (token == "display") {
198                 lex.next();
199                 display = lex.getString() != "false";
200         } else if (token == "darkModeSensitive") {
201                 darkModeSensitive = true;
202         } else if (token == "scale") {
203                 lex.next();
204                 scale = lex.getString();
205         } else if (token == "width") {
206                 lex.next();
207                 width = Length(lex.getString());
208                 scale = string();
209         } else if (token == "height") {
210                 lex.next();
211                 height = Length(lex.getString());
212                 scale = string();
213         } else if (token == "keepAspectRatio") {
214                 keepAspectRatio = true;
215         } else if (token == "draft") {
216                 draft = true;
217         } else if (token == "scaleBeforeRotation") {
218                 scaleBeforeRotation = true;
219         } else if (token == "BoundingBox") {
220                 lex.next();
221                 bbox.xl = Length(lex.getString());
222                 lex.next();
223                 bbox.yb = Length(lex.getString());
224                 lex.next();
225                 bbox.xr = Length(lex.getString());
226                 lex.next();
227                 bbox.yt = Length(lex.getString());
228         } else if (token == "clip") {
229                 clip = true;
230         } else if (token == "rotateAngle") {
231                 lex.next();
232                 rotateAngle = lex.getString();
233         } else if (token == "rotateOrigin") {
234                 lex.next();
235                 rotateOrigin=lex.getString();
236         } else if (token == "special") {
237                 lex.eatLine();
238                 special = lex.getString();
239         } else if (token == "groupId") {
240                 lex.eatLine();
241                 groupId = lex.getString();
242
243         // catch and ignore following two old-format tokens and their arguments.
244         // e.g. "size_kind scale" clashes with the setting of the
245         // "scale <value>" keyword.
246         } else if (token == "size_kind" || token == "lyxsize_kind") {
247                 lex.next();
248                 lex.getString();
249
250         } else {
251                 // If it's none of the above, it's not ours.
252                 return false;
253         }
254         return true;
255 }
256
257
258 graphics::Params InsetGraphicsParams::as_grfxParams() const
259 {
260         graphics::Params pars;
261         pars.filename = filename;
262         pars.scale = lyxscale;
263         pars.angle = convert<double>(rotateAngle);
264
265         if (clip) {
266                 pars.bb = bbox;
267
268                 // Get the original Bounding Box from the file
269                 string const tmp = graphics::readBB_from_PSFile(filename);
270                 LYXERR(Debug::GRAPHICS, "BB_from_File: " << tmp);
271                 if (!tmp.empty()) {
272                         unsigned int const bb_orig_xl = convert<unsigned int>(token(tmp, ' ', 0));
273                         unsigned int const bb_orig_yb = convert<unsigned int>(token(tmp, ' ', 1));
274
275                         // new pars.bb values must be >= zero
276                         if (pars.bb.xl.inBP() > static_cast<int>(bb_orig_xl))
277                                 pars.bb.xl = Length(pars.bb.xl.inBP() - bb_orig_xl, Length::BP);
278                         else
279                                 pars.bb.xl = Length();
280
281                         if (pars.bb.xr.inBP() > static_cast<int>(bb_orig_xl))
282                                 pars.bb.xr = Length(pars.bb.xr.inBP() - bb_orig_xl, Length::BP);
283                         else
284                                 pars.bb.xr = Length();
285
286                         if (pars.bb.yb.inBP() > static_cast<int>(bb_orig_yb))
287                                 pars.bb.yb = Length(pars.bb.yb.inBP() - bb_orig_yb, Length::BP);
288                         else
289                                 pars.bb.yb = Length();
290
291                         if (pars.bb.yt.inBP() > static_cast<int>(bb_orig_yb))
292                                 pars.bb.yt = Length(pars.bb.yt.inBP() - bb_orig_yb, Length::BP);
293                         else
294                                 pars.bb.yt = Length();
295                 }
296
297                 // Paranoia check.
298                 if (pars.bb.xr.inBP() < pars.bb.xl.inBP()
299                     || pars.bb.yt.inBP() < pars.bb.yb.inBP()) {
300                         pars.bb.xl = Length();
301                         pars.bb.xr = Length();
302                         pars.bb.yb = Length();
303                         pars.bb.yt = Length();
304                 }
305         }
306
307         pars.display = display;
308
309         // Override the above if we're not using a gui
310         if (!use_gui)
311                 pars.display = false;
312
313         return pars;
314 }
315
316
317 } // namespace lyx