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 void InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
55 // Are we assigning the object into itself?
62 void InsetGraphicsParams::init()
65 lyxscale = 100; // lyx scaling in percentage
66 display = true; // may be overriden by display mode in preferences
67 scale = string("100"); // output scaling in percentage
70 keepAspectRatio = false; // for LaTeX output
71 draft = false; // draft mode
72 scaleBeforeRotation = false; // scale image before rotating
74 bbox = graphics::BoundingBox(); // bounding box
75 clip = false; // clip image
77 rotateAngle = "0"; // angle of rotation in degrees
78 rotateOrigin.erase(); // Origin of rotation
79 special.erase(); // additional userdefined stuff
84 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
86 filename = igp.filename;
87 lyxscale = igp.lyxscale;
88 display = igp.display;
92 keepAspectRatio = igp.keepAspectRatio;
94 scaleBeforeRotation = igp.scaleBeforeRotation;
99 rotateAngle = igp.rotateAngle;
100 rotateOrigin = igp.rotateOrigin;
101 special = igp.special;
102 groupId = igp.groupId;
106 bool operator==(InsetGraphicsParams const & left,
107 InsetGraphicsParams const & right)
109 return left.filename == right.filename &&
110 left.lyxscale == right.lyxscale &&
111 left.display == right.display &&
112 left.scale == right.scale &&
113 left.width == right.width &&
114 left.height == right.height &&
115 left.keepAspectRatio == right.keepAspectRatio &&
116 left.draft == right.draft &&
117 left.scaleBeforeRotation == right.scaleBeforeRotation &&
119 left.bbox == right.bbox &&
120 left.clip == right.clip &&
122 left.rotateAngle == right.rotateAngle &&
123 left.rotateOrigin == right.rotateOrigin &&
124 left.special == right.special &&
125 left.groupId == right.groupId;
129 bool operator!=(InsetGraphicsParams const & left,
130 InsetGraphicsParams const & right)
132 return !(left == right);
136 void InsetGraphicsParams::Write(ostream & os, Buffer const & buffer) const
138 // Do not write the default values
139 if (!filename.empty())
140 os << "\tfilename " << filename.outputFileName(buffer.filePath()) << '\n';
142 os << "\tlyxscale " << lyxscale << '\n';
144 os << "\tdisplay false\n";
145 if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
146 if (!float_equal(convert<double>(scale), 100.0, 0.05))
147 os << "\tscale " << scale << '\n';
150 os << "\twidth " << width.asString() << '\n';
152 os << "\theight " << height.asString() << '\n';
156 os << "\tkeepAspectRatio\n";
157 if (draft) // draft mode
159 if (scaleBeforeRotation)
160 os << "\tscaleBeforeRotation\n";
162 if (!bbox.empty()) // bounding box
163 os << "\tBoundingBox " << bbox << '\n';
164 if (clip) // clip image
167 if (!rotateAngle.empty()
168 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
169 os << "\trotateAngle " << rotateAngle << '\n';
170 if (!rotateOrigin.empty())
171 os << "\trotateOrigin " << rotateOrigin << '\n';
172 if (!special.empty())
173 os << "\tspecial " << special << '\n';
174 if (!groupId.empty())
175 os << "\tgroupId "<< groupId << '\n';
179 bool InsetGraphicsParams::Read(Lexer & lex, string const & token, string const & bufpath)
181 if (token == "filename") {
183 filename.set(lex.getString(), bufpath);
184 } else if (token == "lyxscale") {
186 lyxscale = lex.getInteger();
187 } else if (token == "display") {
189 string const type = lex.getString();
190 display = lex.getString() != "false";
191 } else if (token == "scale") {
193 scale = lex.getString();
194 } else if (token == "width") {
196 width = Length(lex.getString());
198 } else if (token == "height") {
200 height = Length(lex.getString());
202 } else if (token == "keepAspectRatio") {
203 keepAspectRatio = true;
204 } else if (token == "draft") {
206 } else if (token == "scaleBeforeRotation") {
207 scaleBeforeRotation = true;
208 } else if (token == "BoundingBox") {
210 bbox.xl = Length(lex.getString());
212 bbox.yb = Length(lex.getString());
214 bbox.xr = Length(lex.getString());
216 bbox.yt = Length(lex.getString());
217 } else if (token == "clip") {
219 } else if (token == "rotateAngle") {
221 rotateAngle = lex.getString();
222 } else if (token == "rotateOrigin") {
224 rotateOrigin=lex.getString();
225 } else if (token == "special") {
227 special = lex.getString();
228 } else if (token == "groupId") {
230 groupId = lex.getString();
232 // catch and ignore following two old-format tokens and their arguments.
233 // e.g. "size_kind scale" clashes with the setting of the
234 // "scale <value>" keyword.
235 } else if (token == "size_kind" || token == "lyxsize_kind") {
240 // If it's none of the above, it's not ours.
247 graphics::Params InsetGraphicsParams::as_grfxParams() const
249 graphics::Params pars;
250 pars.filename = filename;
251 pars.scale = lyxscale;
252 pars.angle = convert<double>(rotateAngle);
257 // Get the original Bounding Box from the file
258 string const tmp = graphics::readBB_from_PSFile(filename);
259 LYXERR(Debug::GRAPHICS, "BB_from_File: " << tmp);
261 unsigned int const bb_orig_xl = convert<unsigned int>(token(tmp, ' ', 0));
262 unsigned int const bb_orig_yb = convert<unsigned int>(token(tmp, ' ', 1));
264 // new pars.bb values must be >= zero
265 if (pars.bb.xl.inBP() > static_cast<int>(bb_orig_xl))
266 pars.bb.xl = Length(pars.bb.xl.inBP() - bb_orig_xl, Length::BP);
268 pars.bb.xl = Length();
270 if (pars.bb.xr.inBP() > static_cast<int>(bb_orig_xl))
271 pars.bb.xr = Length(pars.bb.xr.inBP() - bb_orig_xl, Length::BP);
273 pars.bb.xr = Length();
275 if (pars.bb.yb.inBP() > static_cast<int>(bb_orig_yb))
276 pars.bb.yb = Length(pars.bb.yb.inBP() - bb_orig_yb, Length::BP);
278 pars.bb.yb = Length();
280 if (pars.bb.yt.inBP() > static_cast<int>(bb_orig_yb))
281 pars.bb.yt = Length(pars.bb.yt.inBP() - bb_orig_yb, Length::BP);
283 pars.bb.yt = Length();
287 int const width = pars.bb.xr.inBP() - pars.bb.xl.inBP();
288 int const height = pars.bb.yt.inBP() - pars.bb.yb.inBP();
290 if (width < 0 || height < 0) {
291 pars.bb.xl = Length();
292 pars.bb.xr = Length();
293 pars.bb.yb = Length();
294 pars.bb.yt = Length();
298 pars.display = display;
300 // Override the above if we're not using a gui
302 pars.display = false;