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
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 (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';
156 os << "\twidth " << width.asString() << '\n';
158 os << "\theight " << height.asString() << '\n';
162 os << "\tkeepAspectRatio\n";
163 if (draft) // draft mode
165 if (scaleBeforeRotation)
166 os << "\tscaleBeforeRotation\n";
168 if (!bbox.empty()) // bounding box
169 os << "\tBoundingBox " << bbox << '\n';
170 if (clip) // clip image
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';
185 bool InsetGraphicsParams::Read(Lexer & lex, string const & token,
186 Buffer const & buf, bool allowOrigin)
188 if (token == "filename") {
191 filename = buf.getReferencedFileName(lex.getString());
193 filename.set(lex.getString(), buf.filePath());
194 } else if (token == "lyxscale") {
196 lyxscale = lex.getInteger();
197 } else if (token == "display") {
199 display = lex.getString() != "false";
200 } else if (token == "darkModeSensitive") {
201 darkModeSensitive = true;
202 } else if (token == "scale") {
204 scale = lex.getString();
205 } else if (token == "width") {
207 width = Length(lex.getString());
209 } else if (token == "height") {
211 height = Length(lex.getString());
213 } else if (token == "keepAspectRatio") {
214 keepAspectRatio = true;
215 } else if (token == "draft") {
217 } else if (token == "scaleBeforeRotation") {
218 scaleBeforeRotation = true;
219 } else if (token == "BoundingBox") {
221 bbox.xl = Length(lex.getString());
223 bbox.yb = Length(lex.getString());
225 bbox.xr = Length(lex.getString());
227 bbox.yt = Length(lex.getString());
228 } else if (token == "clip") {
230 } else if (token == "rotateAngle") {
232 rotateAngle = lex.getString();
233 } else if (token == "rotateOrigin") {
235 rotateOrigin=lex.getString();
236 } else if (token == "special") {
238 special = lex.getString();
239 } else if (token == "groupId") {
241 groupId = lex.getString();
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") {
251 // If it's none of the above, it's not ours.
258 graphics::Params InsetGraphicsParams::as_grfxParams() const
260 graphics::Params pars;
261 pars.filename = filename;
262 pars.scale = lyxscale;
263 pars.angle = convert<double>(rotateAngle);
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);
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));
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);
279 pars.bb.xl = Length();
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);
284 pars.bb.xr = Length();
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);
289 pars.bb.yb = Length();
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);
294 pars.bb.yt = Length();
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();
307 pars.display = display;
309 // Override the above if we're not using a gui
311 pars.display = false;