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 "graphics/GraphicsParams.h"
23 #include "support/convert.h"
24 #include "support/filetools.h"
25 #include "support/lyxlib.h"
26 #include "support/lstrings.h"
27 #include "support/Translator.h"
32 using support::float_equal;
33 using support::readBB_from_PSFile;
40 InsetGraphicsParams::InsetGraphicsParams()
46 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
48 // I decided to skip the initialization since the copy will overwrite
56 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
58 // Are we assigning the object into itself?
66 void InsetGraphicsParams::init()
69 lyxscale = 100; // lyx scaling in percentage
70 display = graphics::DefaultDisplay; // display mode; see preferences
71 scale = string("100"); // output scaling in percentage
74 keepAspectRatio = false; // for LaTeX output
75 draft = false; // draft mode
76 noUnzip = false; // unzip files
77 scaleBeforeRotation = false; // scale image before rotating
79 bb = string(); // bounding box
80 clip = false; // clip image
82 rotateAngle = "0"; // angle of rotation in degrees
83 rotateOrigin.erase(); // Origin of rotation
84 subcaption = false; // subfigure
85 subcaptionText.erase(); // subfigure caption
86 special.erase(); // additional userdefined stuff
90 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
92 filename = igp.filename;
93 lyxscale = igp.lyxscale;
94 display = igp.display;
98 keepAspectRatio = igp.keepAspectRatio;
100 noUnzip = igp.noUnzip;
101 scaleBeforeRotation = igp.scaleBeforeRotation;
106 rotateAngle = igp.rotateAngle;
107 rotateOrigin = igp.rotateOrigin;
108 subcaption = igp.subcaption;
109 subcaptionText = igp.subcaptionText;
110 special = igp.special;
114 bool operator==(InsetGraphicsParams const & left,
115 InsetGraphicsParams const & right)
117 if (left.filename == right.filename &&
118 left.lyxscale == right.lyxscale &&
119 left.display == right.display &&
120 left.scale == right.scale &&
121 left.width == right.width &&
122 left.height == right.height &&
123 left.keepAspectRatio == right.keepAspectRatio &&
124 left.draft == right.draft &&
125 left.noUnzip == right.noUnzip &&
126 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
144 bool operator!=(InsetGraphicsParams const & left,
145 InsetGraphicsParams const & right)
147 return !(left == right);
151 void InsetGraphicsParams::Write(ostream & os, string const & bufpath) const
153 // Do not write the default values
155 if (!filename.empty()) {
156 os << "\tfilename " << filename.outputFilename(bufpath) << '\n';
159 os << "\tlyxscale " << lyxscale << '\n';
160 if (display != graphics::DefaultDisplay)
161 os << "\tdisplay " << graphics::displayTranslator().find(display) << '\n';
162 if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
163 if (!float_equal(convert<double>(scale), 100.0, 0.05))
164 os << "\tscale " << scale << '\n';
167 os << "\twidth " << width.asString() << '\n';
169 os << "\theight " << height.asString() << '\n';
173 os << "\tkeepAspectRatio\n";
174 if (draft) // draft mode
178 if (scaleBeforeRotation)
179 os << "\tscaleBeforeRotation\n";
181 if (!bb.empty()) // bounding box
182 os << "\tBoundingBox " << bb << '\n';
183 if (clip) // clip image
186 if (!rotateAngle.empty()
187 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
188 os << "\trotateAngle " << rotateAngle << '\n';
189 if (!rotateOrigin.empty())
190 os << "\trotateOrigin " << rotateOrigin << '\n';
192 os << "\tsubcaption\n";
193 if (!subcaptionText.empty())
194 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
195 if (!special.empty())
196 os << "\tspecial " << special << '\n';
200 bool InsetGraphicsParams::Read(Lexer & lex, string const & token, string const & bufpath)
202 if (token == "filename") {
204 filename.set(lex.getString(), bufpath);
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;
277 pars.scale = lyxscale;
278 pars.angle = convert<double>(rotateAngle);
283 // Get the original Bounding Box from the file
284 string const tmp = readBB_from_PSFile(filename);
285 LYXERR(Debug::GRAPHICS) << "BB_from_File: " << tmp << std::endl;
288 # warning why not convert to unsigned int? (Lgb)
290 unsigned int const bb_orig_xl = convert<int>(token(tmp, ' ', 0));
291 unsigned int const bb_orig_yb = convert<int>(token(tmp, ' ', 1));
293 // new pars.bb values must be >= zero
294 if (pars.bb.xl > bb_orig_xl)
295 pars.bb.xl -= bb_orig_xl;
299 if (pars.bb.xr > bb_orig_xl)
300 pars.bb.xr -= bb_orig_xl;
304 if (pars.bb.yb > bb_orig_yb)
305 pars.bb.yb -= bb_orig_yb;
309 if (pars.bb.yt > bb_orig_yb)
310 pars.bb.yt -= bb_orig_yb;
316 int const width = pars.bb.xr - pars.bb.xl;
317 int const height = pars.bb.yt - pars.bb.yb;
319 if (width < 0 || height < 0) {
327 if (display == graphics::DefaultDisplay) {
328 pars.display = lyxrc.display_graphics;
330 pars.display = display;
333 // Override the above if we're not using a gui
335 pars.display = graphics::NoDisplay;