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/GraphicsParams.h"
22 #include "graphics/GraphicsTypes.h"
24 #include "support/convert.h"
25 #include "support/debug.h"
26 #include "support/filetools.h"
27 #include "support/lyxlib.h"
28 #include "support/lstrings.h"
29 #include "support/Translator.h"
34 using namespace lyx::support;
39 InsetGraphicsParams::InsetGraphicsParams()
45 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
47 // I decided to skip the initialization since the copy will overwrite
54 void InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
56 // Are we assigning the object into itself?
63 void InsetGraphicsParams::init()
66 lyxscale = 100; // lyx scaling in percentage
67 display = true; // may be overriden 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 noUnzip = false; // unzip files
74 scaleBeforeRotation = false; // scale image before rotating
76 bb = string(); // bounding box
77 clip = false; // clip image
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 & igp)
88 filename = igp.filename;
89 lyxscale = igp.lyxscale;
90 display = igp.display;
94 keepAspectRatio = igp.keepAspectRatio;
96 noUnzip = igp.noUnzip;
97 scaleBeforeRotation = igp.scaleBeforeRotation;
102 rotateAngle = igp.rotateAngle;
103 rotateOrigin = igp.rotateOrigin;
104 special = igp.special;
105 groupId = igp.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.noUnzip == right.noUnzip &&
121 left.scaleBeforeRotation == right.scaleBeforeRotation &&
123 left.bb == right.bb &&
124 left.clip == right.clip &&
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
165 if (scaleBeforeRotation)
166 os << "\tscaleBeforeRotation\n";
168 if (!bb.empty()) // bounding box
169 os << "\tBoundingBox " << bb << '\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, string const & bufpath)
187 if (token == "filename") {
189 filename.set(lex.getString(), bufpath);
190 } else if (token == "lyxscale") {
192 lyxscale = lex.getInteger();
193 } else if (token == "display") {
195 string const type = lex.getString();
196 display = lex.getString() != "false";
197 } else if (token == "scale") {
199 scale = lex.getString();
200 } else if (token == "width") {
202 width = Length(lex.getString());
204 } else if (token == "height") {
206 height = Length(lex.getString());
208 } else if (token == "keepAspectRatio") {
209 keepAspectRatio = true;
210 } else if (token == "draft") {
212 } else if (token == "noUnzip") {
214 } else if (token == "scaleBeforeRotation") {
215 scaleBeforeRotation = true;
216 } else if (token == "BoundingBox") {
218 for (int i = 0; i < 4; ++i) {
222 bb += 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 = readBB_from_PSFile(filename);
266 LYXERR(Debug::GRAPHICS, "BB_from_File: " << tmp);
268 // FIXME: why not convert to unsigned int? (Lgb)
269 unsigned int const bb_orig_xl = convert<int>(token(tmp, ' ', 0));
270 unsigned int const bb_orig_yb = convert<int>(token(tmp, ' ', 1));
272 // new pars.bb values must be >= zero
273 if (pars.bb.xl > bb_orig_xl)
274 pars.bb.xl -= bb_orig_xl;
278 if (pars.bb.xr > bb_orig_xl)
279 pars.bb.xr -= bb_orig_xl;
283 if (pars.bb.yb > bb_orig_yb)
284 pars.bb.yb -= bb_orig_yb;
288 if (pars.bb.yt > bb_orig_yb)
289 pars.bb.yt -= bb_orig_yb;
295 int const width = pars.bb.xr - pars.bb.xl;
296 int const height = pars.bb.yt - pars.bb.yb;
298 if (width < 0 || height < 0) {
306 pars.display = display;
308 // Override the above if we're not using a gui
310 pars.display = false;