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() ? "true" : "false") << '\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 filename.setEmbed(lex.getBool());
205 } else if (token == "lyxscale") {
207 lyxscale = lex.getInteger();
208 } else if (token == "display") {
210 string const type = lex.getString();
211 display = graphics::displayTranslator().find(type);
212 } else if (token == "scale") {
214 scale = lex.getString();
215 } else if (token == "width") {
217 width = Length(lex.getString());
219 } else if (token == "height") {
221 height = Length(lex.getString());
223 } else if (token == "keepAspectRatio") {
224 keepAspectRatio = true;
225 } else if (token == "draft") {
227 } else if (token == "noUnzip") {
229 } else if (token == "scaleBeforeRotation") {
230 scaleBeforeRotation = true;
231 } else if (token == "BoundingBox") {
233 for (int i = 0; i < 4; ++i) {
237 bb += lex.getString();
239 } else if (token == "clip") {
241 } else if (token == "rotateAngle") {
243 rotateAngle = lex.getString();
244 } else if (token == "rotateOrigin") {
246 rotateOrigin=lex.getString();
247 } else if (token == "subcaption") {
249 } else if (token == "subcaptionText") {
251 string sub = lex.getString();
252 // strip surrounding " "
253 subcaptionText = sub.substr(1, sub.length() - 2);
254 } else if (token == "special") {
256 special = lex.getString();
258 // catch and ignore following two old-format tokens and their arguments.
259 // e.g. "size_kind scale" clashes with the setting of the
260 // "scale <value>" keyword.
261 } else if (token == "size_kind" || token == "lyxsize_kind") {
266 // If it's none of the above, it's not ours.
273 graphics::Params InsetGraphicsParams::as_grfxParams() const
275 graphics::Params pars;
276 pars.filename = filename.availableFile();
277 pars.icon = filename.embedded() ? "pin.png" : "";
278 pars.scale = lyxscale;
279 pars.angle = convert<double>(rotateAngle);
284 // Get the original Bounding Box from the file
285 string const tmp = readBB_from_PSFile(filename);
286 LYXERR(Debug::GRAPHICS, "BB_from_File: " << tmp);
288 // FIXME: why not convert to unsigned int? (Lgb)
289 unsigned int const bb_orig_xl = convert<int>(token(tmp, ' ', 0));
290 unsigned int const bb_orig_yb = convert<int>(token(tmp, ' ', 1));
292 // new pars.bb values must be >= zero
293 if (pars.bb.xl > bb_orig_xl)
294 pars.bb.xl -= bb_orig_xl;
298 if (pars.bb.xr > bb_orig_xl)
299 pars.bb.xr -= bb_orig_xl;
303 if (pars.bb.yb > bb_orig_yb)
304 pars.bb.yb -= bb_orig_yb;
308 if (pars.bb.yt > bb_orig_yb)
309 pars.bb.yt -= bb_orig_yb;
315 int const width = pars.bb.xr - pars.bb.xl;
316 int const height = pars.bb.yt - pars.bb.yb;
318 if (width < 0 || height < 0) {
326 if (display == graphics::DefaultDisplay) {
327 pars.display = graphics::DisplayType(lyxrc.display_graphics);
329 pars.display = display;
332 // Override the above if we're not using a gui
334 pars.display = graphics::NoDisplay;