2 * \file InsetGraphicsParams.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
14 #include "InsetGraphicsParams.h"
17 #include "LyX.h" // for use_gui
21 #include "graphics/epstools.h"
22 #include "graphics/GraphicsTypes.h"
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"
33 using namespace lyx::support;
38 InsetGraphicsParams::InsetGraphicsParams()
44 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
46 // I decided to skip the initialization since the copy will overwrite
53 InsetGraphicsParams & InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
55 // Are we assigning the object into itself?
63 void InsetGraphicsParams::init()
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
71 keepAspectRatio = false; // for LaTeX output
72 draft = false; // draft mode
73 scaleBeforeRotation = false; // scale image before rotating
75 bbox = graphics::BoundingBox(); // bounding box
76 clip = false; // clip image
77 darkModeSensitive = false; // dark mode dependency (InsetInfo)
79 rotateAngle = "0"; // angle of rotation in degrees
80 rotateOrigin.erase(); // Origin of rotation
81 special.erase(); // additional userdefined stuff
86 void InsetGraphicsParams::copy(InsetGraphicsParams const & params)
88 filename = params.filename;
89 lyxscale = params.lyxscale;
90 display = params.display;
93 height = params.height;
94 keepAspectRatio = params.keepAspectRatio;
96 scaleBeforeRotation = params.scaleBeforeRotation;
100 darkModeSensitive = params.darkModeSensitive;
102 rotateAngle = params.rotateAngle;
103 rotateOrigin = params.rotateOrigin;
104 special = params.special;
105 groupId = params.groupId;
109 bool operator==(InsetGraphicsParams const & left,
110 InsetGraphicsParams const & right)
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 &&
122 left.bbox == right.bbox &&
123 left.clip == right.clip &&
124 left.darkModeSensitive == right.darkModeSensitive &&
126 left.rotateAngle == right.rotateAngle &&
127 left.rotateOrigin == right.rotateOrigin &&
128 left.special == right.special &&
129 left.groupId == right.groupId;
133 bool operator!=(InsetGraphicsParams const & left,
134 InsetGraphicsParams const & right)
136 return !(left == right);
140 void InsetGraphicsParams::Write(ostream & os, Buffer const & buffer) const
142 // Do not write the default values
143 if (!filename.empty())
144 os << "\tfilename " << filename.outputFileName(buffer.filePath()) << '\n';
146 os << "\tlyxscale " << lyxscale << '\n';
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';
154 os << "\twidth " << width.asString() << '\n';
156 os << "\theight " << height.asString() << '\n';
160 os << "\tkeepAspectRatio\n";
161 if (draft) // draft mode
163 if (scaleBeforeRotation)
164 os << "\tscaleBeforeRotation\n";
166 if (!bbox.empty()) // bounding box
167 os << "\tBoundingBox " << bbox << '\n';
168 if (clip) // clip image
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';
183 bool InsetGraphicsParams::Read(Lexer & lex, string const & token,
184 Buffer const & buf, bool allowOrigin)
186 if (token == "filename") {
189 filename = buf.getReferencedFileName(lex.getString());
191 filename.set(lex.getString(), buf.filePath());
192 } else if (token == "lyxscale") {
194 lyxscale = lex.getInteger();
195 } else if (token == "display") {
197 display = lex.getString() != "false";
198 } else if (token == "scale") {
200 scale = lex.getString();
201 } else if (token == "width") {
203 width = Length(lex.getString());
205 } else if (token == "height") {
207 height = Length(lex.getString());
209 } else if (token == "keepAspectRatio") {
210 keepAspectRatio = true;
211 } else if (token == "draft") {
213 } else if (token == "scaleBeforeRotation") {
214 scaleBeforeRotation = true;
215 } else if (token == "BoundingBox") {
217 bbox.xl = Length(lex.getString());
219 bbox.yb = Length(lex.getString());
221 bbox.xr = Length(lex.getString());
223 bbox.yt = Length(lex.getString());
224 } else if (token == "clip") {
226 } else if (token == "rotateAngle") {
228 rotateAngle = lex.getString();
229 } else if (token == "rotateOrigin") {
231 rotateOrigin=lex.getString();
232 } else if (token == "special") {
234 special = lex.getString();
235 } else if (token == "groupId") {
237 groupId = lex.getString();
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") {
247 // If it's none of the above, it's not ours.
254 graphics::Params InsetGraphicsParams::as_grfxParams() const
256 graphics::Params pars;
257 pars.filename = filename;
258 pars.scale = lyxscale;
259 pars.angle = convert<double>(rotateAngle);
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);
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));
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);
275 pars.bb.xl = Length();
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);
280 pars.bb.xr = Length();
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);
285 pars.bb.yb = Length();
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);
290 pars.bb.yt = Length();
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();
303 pars.display = display;
305 // Override the above if we're not using a gui
307 pars.display = false;