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
22 #include "graphics/GraphicsParams.h"
24 #include "support/convert.h"
25 #include "support/filetools.h"
26 #include "support/lyxlib.h"
27 #include "support/lstrings.h"
28 #include "support/Translator.h"
33 using support::float_equal;
34 using support::readBB_from_PSFile;
36 using support::DocFileName;
42 InsetGraphicsParams::InsetGraphicsParams()
48 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
50 // I decided to skip the initialization since the copy will overwrite
58 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
60 // Are we assigning the object into itself?
68 void InsetGraphicsParams::init()
71 lyxscale = 100; // lyx scaling in percentage
72 display = graphics::DefaultDisplay; // display mode; see preferences
73 scale = string("100"); // output scaling in percentage
76 keepAspectRatio = false; // for LaTeX output
77 draft = false; // draft mode
78 noUnzip = false; // unzip files
79 scaleBeforeRotation = false; // scale image before rotating
81 bb = string(); // bounding box
82 clip = false; // clip image
84 rotateAngle = "0"; // angle of rotation in degrees
85 rotateOrigin.erase(); // Origin of rotation
86 subcaption = false; // subfigure
87 subcaptionText.erase(); // subfigure caption
88 special.erase(); // additional userdefined stuff
92 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
94 filename = igp.filename;
95 lyxscale = igp.lyxscale;
96 display = igp.display;
100 keepAspectRatio = igp.keepAspectRatio;
102 noUnzip = igp.noUnzip;
103 scaleBeforeRotation = igp.scaleBeforeRotation;
108 rotateAngle = igp.rotateAngle;
109 rotateOrigin = igp.rotateOrigin;
110 subcaption = igp.subcaption;
111 subcaptionText = igp.subcaptionText;
112 special = igp.special;
116 bool operator==(InsetGraphicsParams const & left,
117 InsetGraphicsParams const & right)
119 if (left.filename == right.filename &&
120 left.lyxscale == right.lyxscale &&
121 left.display == right.display &&
122 left.scale == right.scale &&
123 left.width == right.width &&
124 left.height == right.height &&
125 left.keepAspectRatio == right.keepAspectRatio &&
126 left.draft == right.draft &&
127 left.noUnzip == right.noUnzip &&
128 left.scaleBeforeRotation == right.scaleBeforeRotation &&
131 left.bb == right.bb &&
132 left.clip == right.clip &&
134 left.rotateAngle == right.rotateAngle &&
135 left.rotateOrigin == right.rotateOrigin &&
136 left.subcaption == right.subcaption &&
137 left.subcaptionText == right.subcaptionText &&
138 left.special == right.special
146 bool operator!=(InsetGraphicsParams const & left,
147 InsetGraphicsParams const & right)
149 return !(left == right);
153 void InsetGraphicsParams::Write(ostream & os, Buffer const & buffer) const
155 // Do not write the default values
156 if (!filename.empty()) {
157 os << "\tfilename " << filename.outputFilename(buffer.filePath()) << '\n';
158 os << "\tinzipName " << filename.inzipName() << '\n';
159 os << "\tembed " << (filename.embedded() ? "true" : "false") << '\n';
162 os << "\tlyxscale " << lyxscale << '\n';
163 if (display != graphics::DefaultDisplay)
164 os << "\tdisplay " << graphics::displayTranslator().find(display) << '\n';
165 if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
166 if (!float_equal(convert<double>(scale), 100.0, 0.05))
167 os << "\tscale " << scale << '\n';
170 os << "\twidth " << width.asString() << '\n';
172 os << "\theight " << height.asString() << '\n';
176 os << "\tkeepAspectRatio\n";
177 if (draft) // draft mode
181 if (scaleBeforeRotation)
182 os << "\tscaleBeforeRotation\n";
184 if (!bb.empty()) // bounding box
185 os << "\tBoundingBox " << bb << '\n';
186 if (clip) // clip image
189 if (!rotateAngle.empty()
190 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
191 os << "\trotateAngle " << rotateAngle << '\n';
192 if (!rotateOrigin.empty())
193 os << "\trotateOrigin " << rotateOrigin << '\n';
195 os << "\tsubcaption\n";
196 if (!subcaptionText.empty())
197 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
198 if (!special.empty())
199 os << "\tspecial " << special << '\n';
203 bool InsetGraphicsParams::Read(Lexer & lex, string const & token, string const & bufpath)
205 if (token == "filename") {
207 filename.set(lex.getString(), bufpath);
208 } else if (token == "inzipName") {
210 filename.setInzipName(lex.getString());
211 } else if (token == "embed") {
213 filename.setEmbed(lex.getBool());
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;