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 << "\tinzipName " << filename.inzipName() << '\n';
149 os << "\tembed " << (filename.embedded() ? "true" : "false") << '\n';
152 os << "\tlyxscale " << lyxscale << '\n';
153 if (display != graphics::DefaultDisplay)
154 os << "\tdisplay " << graphics::displayTranslator().find(display) << '\n';
155 if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
156 if (!float_equal(convert<double>(scale), 100.0, 0.05))
157 os << "\tscale " << scale << '\n';
160 os << "\twidth " << width.asString() << '\n';
162 os << "\theight " << height.asString() << '\n';
166 os << "\tkeepAspectRatio\n";
167 if (draft) // draft mode
171 if (scaleBeforeRotation)
172 os << "\tscaleBeforeRotation\n";
174 if (!bb.empty()) // bounding box
175 os << "\tBoundingBox " << bb << '\n';
176 if (clip) // clip image
179 if (!rotateAngle.empty()
180 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
181 os << "\trotateAngle " << rotateAngle << '\n';
182 if (!rotateOrigin.empty())
183 os << "\trotateOrigin " << rotateOrigin << '\n';
185 os << "\tsubcaption\n";
186 if (!subcaptionText.empty())
187 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
188 if (!special.empty())
189 os << "\tspecial " << special << '\n';
193 bool InsetGraphicsParams::Read(Lexer & lex, string const & token, string const & bufpath)
195 if (token == "filename") {
197 filename.set(lex.getString(), bufpath);
198 } else if (token == "inzipName") {
200 filename.setInzipName(lex.getString());
201 } else if (token == "embed") {
203 filename.setEmbed(lex.getBool());
204 } else if (token == "lyxscale") {
206 lyxscale = lex.getInteger();
207 } else if (token == "display") {
209 string const type = lex.getString();
210 display = graphics::displayTranslator().find(type);
211 } else if (token == "scale") {
213 scale = lex.getString();
214 } else if (token == "width") {
216 width = Length(lex.getString());
218 } else if (token == "height") {
220 height = Length(lex.getString());
222 } else if (token == "keepAspectRatio") {
223 keepAspectRatio = true;
224 } else if (token == "draft") {
226 } else if (token == "noUnzip") {
228 } else if (token == "scaleBeforeRotation") {
229 scaleBeforeRotation = true;
230 } else if (token == "BoundingBox") {
232 for (int i = 0; i < 4; ++i) {
236 bb += lex.getString();
238 } else if (token == "clip") {
240 } else if (token == "rotateAngle") {
242 rotateAngle = lex.getString();
243 } else if (token == "rotateOrigin") {
245 rotateOrigin=lex.getString();
246 } else if (token == "subcaption") {
248 } else if (token == "subcaptionText") {
250 string sub = lex.getString();
251 // strip surrounding " "
252 subcaptionText = sub.substr(1, sub.length() - 2);
253 } else if (token == "special") {
255 special = lex.getString();
257 // catch and ignore following two old-format tokens and their arguments.
258 // e.g. "size_kind scale" clashes with the setting of the
259 // "scale <value>" keyword.
260 } else if (token == "size_kind" || token == "lyxsize_kind") {
265 // If it's none of the above, it's not ours.
272 graphics::Params InsetGraphicsParams::as_grfxParams() const
274 graphics::Params pars;
275 pars.filename = filename;
276 pars.scale = lyxscale;
277 pars.angle = convert<double>(rotateAngle);
282 // Get the original Bounding Box from the file
283 string const tmp = readBB_from_PSFile(filename);
284 LYXERR(Debug::GRAPHICS, "BB_from_File: " << tmp);
286 // FIXME: why not convert to unsigned int? (Lgb)
287 unsigned int const bb_orig_xl = convert<int>(token(tmp, ' ', 0));
288 unsigned int const bb_orig_yb = convert<int>(token(tmp, ' ', 1));
290 // new pars.bb values must be >= zero
291 if (pars.bb.xl > bb_orig_xl)
292 pars.bb.xl -= bb_orig_xl;
296 if (pars.bb.xr > bb_orig_xl)
297 pars.bb.xr -= bb_orig_xl;
301 if (pars.bb.yb > bb_orig_yb)
302 pars.bb.yb -= bb_orig_yb;
306 if (pars.bb.yt > bb_orig_yb)
307 pars.bb.yt -= bb_orig_yb;
313 int const width = pars.bb.xr - pars.bb.xl;
314 int const height = pars.bb.yt - pars.bb.yb;
316 if (width < 0 || height < 0) {
324 if (display == graphics::DefaultDisplay) {
325 pars.display = graphics::DisplayType(lyxrc.display_graphics);
327 pars.display = display;
330 // Override the above if we're not using a gui
332 pars.display = graphics::NoDisplay;