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 "EmbeddedFiles.h"
23 #include "graphics/GraphicsParams.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 support::float_equal;
35 using support::readBB_from_PSFile;
37 using support::DocFileName;
43 InsetGraphicsParams::InsetGraphicsParams()
49 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
51 // I decided to skip the initialization since the copy will overwrite
59 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
61 // Are we assigning the object into itself?
69 void InsetGraphicsParams::init()
72 lyxscale = 100; // lyx scaling in percentage
73 display = graphics::DefaultDisplay; // display mode; see preferences
74 scale = string("100"); // output scaling in percentage
77 keepAspectRatio = false; // for LaTeX output
78 draft = false; // draft mode
79 noUnzip = false; // unzip files
80 scaleBeforeRotation = false; // scale image before rotating
82 bb = string(); // bounding box
83 clip = false; // clip image
85 rotateAngle = "0"; // angle of rotation in degrees
86 rotateOrigin.erase(); // Origin of rotation
87 subcaption = false; // subfigure
88 subcaptionText.erase(); // subfigure caption
89 special.erase(); // additional userdefined stuff
93 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
95 filename = igp.filename;
96 lyxscale = igp.lyxscale;
97 display = igp.display;
101 keepAspectRatio = igp.keepAspectRatio;
103 noUnzip = igp.noUnzip;
104 scaleBeforeRotation = igp.scaleBeforeRotation;
109 rotateAngle = igp.rotateAngle;
110 rotateOrigin = igp.rotateOrigin;
111 subcaption = igp.subcaption;
112 subcaptionText = igp.subcaptionText;
113 special = igp.special;
117 bool operator==(InsetGraphicsParams const & left,
118 InsetGraphicsParams const & right)
120 if (left.filename == right.filename &&
121 left.lyxscale == right.lyxscale &&
122 left.display == right.display &&
123 left.scale == right.scale &&
124 left.width == right.width &&
125 left.height == right.height &&
126 left.keepAspectRatio == right.keepAspectRatio &&
127 left.draft == right.draft &&
128 left.noUnzip == right.noUnzip &&
129 left.scaleBeforeRotation == right.scaleBeforeRotation &&
132 left.bb == right.bb &&
133 left.clip == right.clip &&
135 left.rotateAngle == right.rotateAngle &&
136 left.rotateOrigin == right.rotateOrigin &&
137 left.subcaption == right.subcaption &&
138 left.subcaptionText == right.subcaptionText &&
139 left.special == right.special
147 bool operator!=(InsetGraphicsParams const & left,
148 InsetGraphicsParams const & right)
150 return !(left == right);
154 void InsetGraphicsParams::Write(ostream & os, Buffer const & buffer) const
156 // Do not write the default values
158 if (!filename.empty()) {
159 // when we save, we still use the original filename
160 EmbeddedFiles::EmbeddedFileList::const_iterator it =
161 buffer.embeddedFiles().find(filename.toFilesystemEncoding());
162 if (it != buffer.embeddedFiles().end())
163 os << "\tfilename " << DocFileName(it->absFilename()).outputFilename(buffer.filePath()) << '\n';
165 os << "\tfilename " << filename.outputFilename(buffer.filePath()) << '\n';
168 os << "\tlyxscale " << lyxscale << '\n';
169 if (display != graphics::DefaultDisplay)
170 os << "\tdisplay " << graphics::displayTranslator().find(display) << '\n';
171 if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
172 if (!float_equal(convert<double>(scale), 100.0, 0.05))
173 os << "\tscale " << scale << '\n';
176 os << "\twidth " << width.asString() << '\n';
178 os << "\theight " << height.asString() << '\n';
182 os << "\tkeepAspectRatio\n";
183 if (draft) // draft mode
187 if (scaleBeforeRotation)
188 os << "\tscaleBeforeRotation\n";
190 if (!bb.empty()) // bounding box
191 os << "\tBoundingBox " << bb << '\n';
192 if (clip) // clip image
195 if (!rotateAngle.empty()
196 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
197 os << "\trotateAngle " << rotateAngle << '\n';
198 if (!rotateOrigin.empty())
199 os << "\trotateOrigin " << rotateOrigin << '\n';
201 os << "\tsubcaption\n";
202 if (!subcaptionText.empty())
203 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
204 if (!special.empty())
205 os << "\tspecial " << special << '\n';
209 bool InsetGraphicsParams::Read(Lexer & lex, string const & token, string const & bufpath)
211 if (token == "filename") {
213 filename.set(lex.getString(), bufpath);
214 } else if (token == "lyxscale") {
216 lyxscale = lex.getInteger();
217 } else if (token == "display") {
219 string const type = lex.getString();
220 display = graphics::displayTranslator().find(type);
221 } else if (token == "scale") {
223 scale = lex.getString();
224 } else if (token == "width") {
226 width = Length(lex.getString());
228 } else if (token == "height") {
230 height = Length(lex.getString());
232 } else if (token == "keepAspectRatio") {
233 keepAspectRatio = true;
234 } else if (token == "draft") {
236 } else if (token == "noUnzip") {
238 } else if (token == "scaleBeforeRotation") {
239 scaleBeforeRotation = true;
240 } else if (token == "BoundingBox") {
242 for (int i = 0; i < 4; ++i) {
246 bb += lex.getString();
248 } else if (token == "clip") {
250 } else if (token == "rotateAngle") {
252 rotateAngle = lex.getString();
253 } else if (token == "rotateOrigin") {
255 rotateOrigin=lex.getString();
256 } else if (token == "subcaption") {
258 } else if (token == "subcaptionText") {
260 string sub = lex.getString();
261 // strip surrounding " "
262 subcaptionText = sub.substr(1, sub.length() - 2);
263 } else if (token == "special") {
265 special = lex.getString();
267 // catch and ignore following two old-format tokens and their arguments.
268 // e.g. "size_kind scale" clashes with the setting of the
269 // "scale <value>" keyword.
270 } else if (token == "size_kind" || token == "lyxsize_kind") {
275 // If it's none of the above, it's not ours.
282 graphics::Params InsetGraphicsParams::as_grfxParams() const
284 graphics::Params pars;
285 pars.filename = filename;
286 pars.scale = lyxscale;
287 pars.angle = convert<double>(rotateAngle);
292 // Get the original Bounding Box from the file
293 string const tmp = readBB_from_PSFile(filename);
294 LYXERR(Debug::GRAPHICS) << "BB_from_File: " << tmp << std::endl;
296 // FIXME: why not convert to unsigned int? (Lgb)
297 unsigned int const bb_orig_xl = convert<int>(token(tmp, ' ', 0));
298 unsigned int const bb_orig_yb = convert<int>(token(tmp, ' ', 1));
300 // new pars.bb values must be >= zero
301 if (pars.bb.xl > bb_orig_xl)
302 pars.bb.xl -= bb_orig_xl;
306 if (pars.bb.xr > bb_orig_xl)
307 pars.bb.xr -= bb_orig_xl;
311 if (pars.bb.yb > bb_orig_yb)
312 pars.bb.yb -= bb_orig_yb;
316 if (pars.bb.yt > bb_orig_yb)
317 pars.bb.yt -= bb_orig_yb;
323 int const width = pars.bb.xr - pars.bb.xl;
324 int const height = pars.bb.yt - pars.bb.yb;
326 if (width < 0 || height < 0) {
334 if (display == graphics::DefaultDisplay) {
335 pars.display = lyxrc.display_graphics;
337 pars.display = display;
340 // Override the above if we're not using a gui
342 pars.display = graphics::NoDisplay;