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"
37 using support::float_equal;
38 using support::readBB_from_PSFile;
40 using support::DocFileName;
43 InsetGraphicsParams::InsetGraphicsParams()
49 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
51 // I decided to skip the initialization since the copy will overwrite
58 void InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
60 // Are we assigning the object into itself?
67 void InsetGraphicsParams::init()
70 lyxscale = 100; // lyx scaling in percentage
71 display = graphics::DefaultDisplay; // display mode; see preferences
72 scale = string("100"); // output scaling in percentage
75 keepAspectRatio = false; // for LaTeX output
76 draft = false; // draft mode
77 noUnzip = false; // unzip files
78 scaleBeforeRotation = false; // scale image before rotating
80 bb = string(); // bounding box
81 clip = false; // clip image
83 rotateAngle = "0"; // angle of rotation in degrees
84 rotateOrigin.erase(); // Origin of rotation
85 subcaption = false; // subfigure
86 subcaptionText.erase(); // subfigure caption
87 special.erase(); // additional userdefined stuff
91 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
93 filename = igp.filename;
94 lyxscale = igp.lyxscale;
95 display = igp.display;
99 keepAspectRatio = igp.keepAspectRatio;
101 noUnzip = igp.noUnzip;
102 scaleBeforeRotation = igp.scaleBeforeRotation;
107 rotateAngle = igp.rotateAngle;
108 rotateOrigin = igp.rotateOrigin;
109 subcaption = igp.subcaption;
110 subcaptionText = igp.subcaptionText;
111 special = igp.special;
115 bool operator==(InsetGraphicsParams const & left,
116 InsetGraphicsParams const & right)
118 return left.filename == right.filename &&
119 left.lyxscale == right.lyxscale &&
120 left.display == right.display &&
121 left.scale == right.scale &&
122 left.width == right.width &&
123 left.height == right.height &&
124 left.keepAspectRatio == right.keepAspectRatio &&
125 left.draft == right.draft &&
126 left.noUnzip == right.noUnzip &&
127 left.scaleBeforeRotation == right.scaleBeforeRotation &&
129 left.bb == right.bb &&
130 left.clip == right.clip &&
132 left.rotateAngle == right.rotateAngle &&
133 left.rotateOrigin == right.rotateOrigin &&
134 left.subcaption == right.subcaption &&
135 left.subcaptionText == right.subcaptionText &&
136 left.special == right.special;
140 bool operator!=(InsetGraphicsParams const & left,
141 InsetGraphicsParams const & right)
143 return !(left == right);
147 void InsetGraphicsParams::Write(ostream & os, Buffer const & buffer) const
149 // Do not write the default values
150 if (!filename.empty()) {
151 os << "\tfilename " << filename.outputFilename(buffer.filePath()) << '\n';
152 os << "\tinzipName " << filename.inzipName() << '\n';
153 os << "\tembed " << (filename.embedded() ? "true" : "false") << '\n';
156 os << "\tlyxscale " << lyxscale << '\n';
157 if (display != graphics::DefaultDisplay)
158 os << "\tdisplay " << graphics::displayTranslator().find(display) << '\n';
159 if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
160 if (!float_equal(convert<double>(scale), 100.0, 0.05))
161 os << "\tscale " << scale << '\n';
164 os << "\twidth " << width.asString() << '\n';
166 os << "\theight " << height.asString() << '\n';
170 os << "\tkeepAspectRatio\n";
171 if (draft) // draft mode
175 if (scaleBeforeRotation)
176 os << "\tscaleBeforeRotation\n";
178 if (!bb.empty()) // bounding box
179 os << "\tBoundingBox " << bb << '\n';
180 if (clip) // clip image
183 if (!rotateAngle.empty()
184 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
185 os << "\trotateAngle " << rotateAngle << '\n';
186 if (!rotateOrigin.empty())
187 os << "\trotateOrigin " << rotateOrigin << '\n';
189 os << "\tsubcaption\n";
190 if (!subcaptionText.empty())
191 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
192 if (!special.empty())
193 os << "\tspecial " << special << '\n';
197 bool InsetGraphicsParams::Read(Lexer & lex, string const & token, string const & bufpath)
199 if (token == "filename") {
201 filename.set(lex.getString(), bufpath);
202 } else if (token == "inzipName") {
204 filename.setInzipName(lex.getString());
205 } else if (token == "embed") {
207 filename.setEmbed(lex.getBool());
208 } else if (token == "lyxscale") {
210 lyxscale = lex.getInteger();
211 } else if (token == "display") {
213 string const type = lex.getString();
214 display = graphics::displayTranslator().find(type);
215 } else if (token == "scale") {
217 scale = lex.getString();
218 } else if (token == "width") {
220 width = Length(lex.getString());
222 } else if (token == "height") {
224 height = Length(lex.getString());
226 } else if (token == "keepAspectRatio") {
227 keepAspectRatio = true;
228 } else if (token == "draft") {
230 } else if (token == "noUnzip") {
232 } else if (token == "scaleBeforeRotation") {
233 scaleBeforeRotation = true;
234 } else if (token == "BoundingBox") {
236 for (int i = 0; i < 4; ++i) {
240 bb += lex.getString();
242 } else if (token == "clip") {
244 } else if (token == "rotateAngle") {
246 rotateAngle = lex.getString();
247 } else if (token == "rotateOrigin") {
249 rotateOrigin=lex.getString();
250 } else if (token == "subcaption") {
252 } else if (token == "subcaptionText") {
254 string sub = lex.getString();
255 // strip surrounding " "
256 subcaptionText = sub.substr(1, sub.length() - 2);
257 } else if (token == "special") {
259 special = lex.getString();
261 // catch and ignore following two old-format tokens and their arguments.
262 // e.g. "size_kind scale" clashes with the setting of the
263 // "scale <value>" keyword.
264 } else if (token == "size_kind" || token == "lyxsize_kind") {
269 // If it's none of the above, it's not ours.
276 graphics::Params InsetGraphicsParams::as_grfxParams() const
278 graphics::Params pars;
279 pars.filename = filename;
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;