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"
16 #include "support/debug.h"
17 #include "LyX.h" // for use_gui
22 #include "graphics/GraphicsParams.h"
23 #include "graphics/GraphicsTypes.h"
25 #include "support/convert.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 = graphics::DefaultDisplay; // display mode; see 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 subcaption = false; // subfigure
82 subcaptionText.erase(); // subfigure caption
83 special.erase(); // additional userdefined stuff
87 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
89 filename = igp.filename;
90 lyxscale = igp.lyxscale;
91 display = igp.display;
95 keepAspectRatio = igp.keepAspectRatio;
97 noUnzip = igp.noUnzip;
98 scaleBeforeRotation = igp.scaleBeforeRotation;
103 rotateAngle = igp.rotateAngle;
104 rotateOrigin = igp.rotateOrigin;
105 subcaption = igp.subcaption;
106 subcaptionText = igp.subcaptionText;
107 special = igp.special;
111 bool operator==(InsetGraphicsParams const & left,
112 InsetGraphicsParams const & right)
114 return left.filename == right.filename &&
115 left.lyxscale == right.lyxscale &&
116 left.display == right.display &&
117 left.scale == right.scale &&
118 left.width == right.width &&
119 left.height == right.height &&
120 left.keepAspectRatio == right.keepAspectRatio &&
121 left.draft == right.draft &&
122 left.noUnzip == right.noUnzip &&
123 left.scaleBeforeRotation == right.scaleBeforeRotation &&
125 left.bb == right.bb &&
126 left.clip == right.clip &&
128 left.rotateAngle == right.rotateAngle &&
129 left.rotateOrigin == right.rotateOrigin &&
130 left.subcaption == right.subcaption &&
131 left.subcaptionText == right.subcaptionText &&
132 left.special == right.special;
136 bool operator!=(InsetGraphicsParams const & left,
137 InsetGraphicsParams const & right)
139 return !(left == right);
143 void InsetGraphicsParams::Write(ostream & os, Buffer const & buffer) const
145 // Do not write the default values
146 if (!filename.empty()) {
147 os << "\tfilename " << filename.outputFilename(buffer.filePath()) << '\n';
148 os << "\tembed " << (filename.embedded() ? filename.inzipName() : "\"\"") << '\n';
151 os << "\tlyxscale " << lyxscale << '\n';
152 if (display != graphics::DefaultDisplay)
153 os << "\tdisplay " << graphics::displayTranslator().find(display) << '\n';
154 if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
155 if (!float_equal(convert<double>(scale), 100.0, 0.05))
156 os << "\tscale " << scale << '\n';
159 os << "\twidth " << width.asString() << '\n';
161 os << "\theight " << height.asString() << '\n';
165 os << "\tkeepAspectRatio\n";
166 if (draft) // draft mode
170 if (scaleBeforeRotation)
171 os << "\tscaleBeforeRotation\n";
173 if (!bb.empty()) // bounding box
174 os << "\tBoundingBox " << bb << '\n';
175 if (clip) // clip image
178 if (!rotateAngle.empty()
179 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
180 os << "\trotateAngle " << rotateAngle << '\n';
181 if (!rotateOrigin.empty())
182 os << "\trotateOrigin " << rotateOrigin << '\n';
184 os << "\tsubcaption\n";
185 if (!subcaptionText.empty())
186 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
187 if (!special.empty())
188 os << "\tspecial " << special << '\n';
192 bool InsetGraphicsParams::Read(Lexer & lex, string const & token, string const & bufpath)
194 if (token == "filename") {
196 filename.set(lex.getString(), bufpath);
197 } else if (token == "inzipName") {
198 // this option is currently ignored because only files in
199 // or under current document path is embeddable, and their
200 // inzipName is automatically determined.
202 } else if (token == "embed") {
204 string const name = lex.getString();
205 filename.setInzipName(name);
206 filename.setEmbed(!name.empty());
207 } else if (token == "lyxscale") {
209 lyxscale = lex.getInteger();
210 } else if (token == "display") {
212 string const type = lex.getString();
213 display = graphics::displayTranslator().find(type);
214 } else if (token == "scale") {
216 scale = lex.getString();
217 } else if (token == "width") {
219 width = Length(lex.getString());
221 } else if (token == "height") {
223 height = Length(lex.getString());
225 } else if (token == "keepAspectRatio") {
226 keepAspectRatio = true;
227 } else if (token == "draft") {
229 } else if (token == "noUnzip") {
231 } else if (token == "scaleBeforeRotation") {
232 scaleBeforeRotation = true;
233 } else if (token == "BoundingBox") {
235 for (int i = 0; i < 4; ++i) {
239 bb += lex.getString();
241 } else if (token == "clip") {
243 } else if (token == "rotateAngle") {
245 rotateAngle = lex.getString();
246 } else if (token == "rotateOrigin") {
248 rotateOrigin=lex.getString();
249 } else if (token == "subcaption") {
251 } else if (token == "subcaptionText") {
253 string sub = lex.getString();
254 // strip surrounding " "
255 subcaptionText = sub.substr(1, sub.length() - 2);
256 } else if (token == "special") {
258 special = lex.getString();
260 // catch and ignore following two old-format tokens and their arguments.
261 // e.g. "size_kind scale" clashes with the setting of the
262 // "scale <value>" keyword.
263 } else if (token == "size_kind" || token == "lyxsize_kind") {
268 // If it's none of the above, it's not ours.
275 graphics::Params InsetGraphicsParams::as_grfxParams() const
277 graphics::Params pars;
278 pars.filename = filename.availableFile();
279 pars.icon = filename.embedded() ? "pin.png" : "";
280 pars.scale = lyxscale;
281 pars.angle = convert<double>(rotateAngle);
286 // Get the original Bounding Box from the file
287 string const tmp = readBB_from_PSFile(filename);
288 LYXERR(Debug::GRAPHICS, "BB_from_File: " << tmp);
290 // FIXME: why not convert to unsigned int? (Lgb)
291 unsigned int const bb_orig_xl = convert<int>(token(tmp, ' ', 0));
292 unsigned int const bb_orig_yb = convert<int>(token(tmp, ' ', 1));
294 // new pars.bb values must be >= zero
295 if (pars.bb.xl > bb_orig_xl)
296 pars.bb.xl -= bb_orig_xl;
300 if (pars.bb.xr > bb_orig_xl)
301 pars.bb.xr -= bb_orig_xl;
305 if (pars.bb.yb > bb_orig_yb)
306 pars.bb.yb -= bb_orig_yb;
310 if (pars.bb.yt > bb_orig_yb)
311 pars.bb.yt -= bb_orig_yb;
317 int const width = pars.bb.xr - pars.bb.xl;
318 int const height = pars.bb.yt - pars.bb.yb;
320 if (width < 0 || height < 0) {
328 if (display == graphics::DefaultDisplay) {
329 pars.display = graphics::DisplayType(lyxrc.display_graphics);
331 pars.display = display;
334 // Override the above if we're not using a gui
336 pars.display = graphics::NoDisplay;