]> git.lyx.org Git - lyx.git/blob - src/insets/InsetGraphicsParams.cpp
nullptr
[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 (InsetInfo)
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 (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
150                 if (!float_equal(convert<double>(scale), 100.0, 0.05))
151                         os << "\tscale " << scale << '\n';
152         } else {
153                 if (!width.zero())
154                         os << "\twidth " << width.asString() << '\n';
155                 if (!height.zero())
156                         os << "\theight " << height.asString() << '\n';
157         }
158
159         if (keepAspectRatio)
160                 os << "\tkeepAspectRatio\n";
161         if (draft)                      // draft mode
162                 os << "\tdraft\n";
163         if (scaleBeforeRotation)
164                 os << "\tscaleBeforeRotation\n";
165
166         if (!bbox.empty())              // bounding box
167                 os << "\tBoundingBox " << bbox << '\n';
168         if (clip)                       // clip image
169                 os << "\tclip\n";
170
171         if (!rotateAngle.empty()
172                 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
173                 os << "\trotateAngle " << rotateAngle << '\n';
174         if (!rotateOrigin.empty())
175                 os << "\trotateOrigin " << rotateOrigin << '\n';
176         if (!special.empty())
177                 os << "\tspecial " << special << '\n';
178         if (!groupId.empty())
179                 os << "\tgroupId "<< groupId << '\n';
180 }
181
182
183 bool InsetGraphicsParams::Read(Lexer & lex, string const & token,
184                                Buffer const & buf, bool allowOrigin)
185 {
186         if (token == "filename") {
187                 lex.eatLine();
188                 if (allowOrigin)
189                         filename = buf.getReferencedFileName(lex.getString());
190                 else
191                         filename.set(lex.getString(), buf.filePath());
192         } else if (token == "lyxscale") {
193                 lex.next();
194                 lyxscale = lex.getInteger();
195         } else if (token == "display") {
196                 lex.next();
197                 display = lex.getString() != "false";
198         } else if (token == "scale") {
199                 lex.next();
200                 scale = lex.getString();
201         } else if (token == "width") {
202                 lex.next();
203                 width = Length(lex.getString());
204                 scale = string();
205         } else if (token == "height") {
206                 lex.next();
207                 height = Length(lex.getString());
208                 scale = string();
209         } else if (token == "keepAspectRatio") {
210                 keepAspectRatio = true;
211         } else if (token == "draft") {
212                 draft = true;
213         } else if (token == "scaleBeforeRotation") {
214                 scaleBeforeRotation = true;
215         } else if (token == "BoundingBox") {
216                 lex.next();
217                 bbox.xl = Length(lex.getString());
218                 lex.next();
219                 bbox.yb = Length(lex.getString());
220                 lex.next();
221                 bbox.xr = Length(lex.getString());
222                 lex.next();
223                 bbox.yt = Length(lex.getString());
224         } else if (token == "clip") {
225                 clip = true;
226         } else if (token == "rotateAngle") {
227                 lex.next();
228                 rotateAngle = lex.getString();
229         } else if (token == "rotateOrigin") {
230                 lex.next();
231                 rotateOrigin=lex.getString();
232         } else if (token == "special") {
233                 lex.eatLine();
234                 special = lex.getString();
235         } else if (token == "groupId") {
236                 lex.eatLine();
237                 groupId = lex.getString();
238
239         // catch and ignore following two old-format tokens and their arguments.
240         // e.g. "size_kind scale" clashes with the setting of the
241         // "scale <value>" keyword.
242         } else if (token == "size_kind" || token == "lyxsize_kind") {
243                 lex.next();
244                 lex.getString();
245
246         } else {
247                 // If it's none of the above, it's not ours.
248                 return false;
249         }
250         return true;
251 }
252
253
254 graphics::Params InsetGraphicsParams::as_grfxParams() const
255 {
256         graphics::Params pars;
257         pars.filename = filename;
258         pars.scale = lyxscale;
259         pars.angle = convert<double>(rotateAngle);
260
261         if (clip) {
262                 pars.bb = bbox;
263
264                 // Get the original Bounding Box from the file
265                 string const tmp = graphics::readBB_from_PSFile(filename);
266                 LYXERR(Debug::GRAPHICS, "BB_from_File: " << tmp);
267                 if (!tmp.empty()) {
268                         unsigned int const bb_orig_xl = convert<unsigned int>(token(tmp, ' ', 0));
269                         unsigned int const bb_orig_yb = convert<unsigned int>(token(tmp, ' ', 1));
270
271                         // new pars.bb values must be >= zero
272                         if (pars.bb.xl.inBP() > static_cast<int>(bb_orig_xl))
273                                 pars.bb.xl = Length(pars.bb.xl.inBP() - bb_orig_xl, Length::BP);
274                         else
275                                 pars.bb.xl = Length();
276
277                         if (pars.bb.xr.inBP() > static_cast<int>(bb_orig_xl))
278                                 pars.bb.xr = Length(pars.bb.xr.inBP() - bb_orig_xl, Length::BP);
279                         else
280                                 pars.bb.xr = Length();
281
282                         if (pars.bb.yb.inBP() > static_cast<int>(bb_orig_yb))
283                                 pars.bb.yb = Length(pars.bb.yb.inBP() - bb_orig_yb, Length::BP);
284                         else
285                                 pars.bb.yb = Length();
286
287                         if (pars.bb.yt.inBP() > static_cast<int>(bb_orig_yb))
288                                 pars.bb.yt = Length(pars.bb.yt.inBP() - bb_orig_yb, Length::BP);
289                         else
290                                 pars.bb.yt = Length();
291                 }
292
293                 // Paranoia check.
294                 if (pars.bb.xr.inBP() < pars.bb.xl.inBP()
295                     || pars.bb.yt.inBP() < pars.bb.yb.inBP()) {
296                         pars.bb.xl = Length();
297                         pars.bb.xr = Length();
298                         pars.bb.yb = Length();
299                         pars.bb.yt = Length();
300                 }
301         }
302
303         pars.display = display;
304
305         // Override the above if we're not using a gui
306         if (!use_gui)
307                 pars.display = false;
308
309         return pars;
310 }
311
312
313 } // namespace lyx