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"
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 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 return 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 &&
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;
142 bool operator!=(InsetGraphicsParams const & left,
143 InsetGraphicsParams const & right)
145 return !(left == right);
149 void InsetGraphicsParams::Write(ostream & os, Buffer const & buffer) const
151 // Do not write the default values
152 if (!filename.empty()) {
153 os << "\tfilename " << filename.outputFilename(buffer.filePath()) << '\n';
154 os << "\tinzipName " << filename.inzipName() << '\n';
155 os << "\tembed " << (filename.embedded() ? "true" : "false") << '\n';
158 os << "\tlyxscale " << lyxscale << '\n';
159 if (display != graphics::DefaultDisplay)
160 os << "\tdisplay " << graphics::displayTranslator().find(display) << '\n';
161 if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
162 if (!float_equal(convert<double>(scale), 100.0, 0.05))
163 os << "\tscale " << scale << '\n';
166 os << "\twidth " << width.asString() << '\n';
168 os << "\theight " << height.asString() << '\n';
172 os << "\tkeepAspectRatio\n";
173 if (draft) // draft mode
177 if (scaleBeforeRotation)
178 os << "\tscaleBeforeRotation\n";
180 if (!bb.empty()) // bounding box
181 os << "\tBoundingBox " << bb << '\n';
182 if (clip) // clip image
185 if (!rotateAngle.empty()
186 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
187 os << "\trotateAngle " << rotateAngle << '\n';
188 if (!rotateOrigin.empty())
189 os << "\trotateOrigin " << rotateOrigin << '\n';
191 os << "\tsubcaption\n";
192 if (!subcaptionText.empty())
193 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
194 if (!special.empty())
195 os << "\tspecial " << special << '\n';
199 bool InsetGraphicsParams::Read(Lexer & lex, string const & token, string const & bufpath)
201 if (token == "filename") {
203 filename.set(lex.getString(), bufpath);
204 } else if (token == "inzipName") {
206 filename.setInzipName(lex.getString());
207 } else if (token == "embed") {
209 filename.setEmbed(lex.getBool());
210 } else if (token == "lyxscale") {
212 lyxscale = lex.getInteger();
213 } else if (token == "display") {
215 string const type = lex.getString();
216 display = graphics::displayTranslator().find(type);
217 } else if (token == "scale") {
219 scale = lex.getString();
220 } else if (token == "width") {
222 width = Length(lex.getString());
224 } else if (token == "height") {
226 height = Length(lex.getString());
228 } else if (token == "keepAspectRatio") {
229 keepAspectRatio = true;
230 } else if (token == "draft") {
232 } else if (token == "noUnzip") {
234 } else if (token == "scaleBeforeRotation") {
235 scaleBeforeRotation = true;
236 } else if (token == "BoundingBox") {
238 for (int i = 0; i < 4; ++i) {
242 bb += lex.getString();
244 } else if (token == "clip") {
246 } else if (token == "rotateAngle") {
248 rotateAngle = lex.getString();
249 } else if (token == "rotateOrigin") {
251 rotateOrigin=lex.getString();
252 } else if (token == "subcaption") {
254 } else if (token == "subcaptionText") {
256 string sub = lex.getString();
257 // strip surrounding " "
258 subcaptionText = sub.substr(1, sub.length() - 2);
259 } else if (token == "special") {
261 special = lex.getString();
263 // catch and ignore following two old-format tokens and their arguments.
264 // e.g. "size_kind scale" clashes with the setting of the
265 // "scale <value>" keyword.
266 } else if (token == "size_kind" || token == "lyxsize_kind") {
271 // If it's none of the above, it's not ours.
278 graphics::Params InsetGraphicsParams::as_grfxParams() const
280 graphics::Params pars;
281 pars.filename = filename;
282 pars.scale = lyxscale;
283 pars.angle = convert<double>(rotateAngle);
288 // Get the original Bounding Box from the file
289 string const tmp = readBB_from_PSFile(filename);
290 LYXERR(Debug::GRAPHICS) << "BB_from_File: " << tmp << std::endl;
292 // FIXME: why not convert to unsigned int? (Lgb)
293 unsigned int const bb_orig_xl = convert<int>(token(tmp, ' ', 0));
294 unsigned int const bb_orig_yb = convert<int>(token(tmp, ' ', 1));
296 // new pars.bb values must be >= zero
297 if (pars.bb.xl > bb_orig_xl)
298 pars.bb.xl -= bb_orig_xl;
302 if (pars.bb.xr > bb_orig_xl)
303 pars.bb.xr -= bb_orig_xl;
307 if (pars.bb.yb > bb_orig_yb)
308 pars.bb.yb -= bb_orig_yb;
312 if (pars.bb.yt > bb_orig_yb)
313 pars.bb.yt -= bb_orig_yb;
319 int const width = pars.bb.xr - pars.bb.xl;
320 int const height = pars.bb.yt - pars.bb.yb;
322 if (width < 0 || height < 0) {
330 if (display == graphics::DefaultDisplay) {
331 pars.display = graphics::DisplayType(lyxrc.display_graphics);
333 pars.display = display;
336 // Override the above if we're not using a gui
338 pars.display = graphics::NoDisplay;