2 * \file insetgraphicsParams.C
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"
20 #include "frontends/lyx_gui.h"
22 #include "graphics/GraphicsParams.h"
24 #include "support/filetools.h"
25 #include "support/lyxlib.h"
26 #include "support/lstrings.h"
27 #include "support/translator.h"
29 using lyx::support::float_equal;
30 using lyx::support::readBB_from_PSFile;
31 using lyx::support::strToInt;
32 using lyx::support::token;
38 InsetGraphicsParams::InsetGraphicsParams()
44 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
46 // I decided to skip the initialization since the copy will overwrite
54 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
56 // Are we assigning the object into itself?
64 void InsetGraphicsParams::init()
67 lyxscale = 100; // lyx scaling in percentage
68 display = lyx::graphics::DefaultDisplay; // display mode; see preferences
69 scale = 100.0; // output scaling in percentage
72 keepAspectRatio = false; // for LaTeX output
73 draft = false; // draft mode
74 noUnzip = false; // unzip files
76 bb = string(); // bounding box
77 clip = false; // clip image
79 rotateAngle = 0.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;
102 rotateAngle = igp.rotateAngle;
103 rotateOrigin = igp.rotateOrigin;
104 subcaption = igp.subcaption;
105 subcaptionText = igp.subcaptionText;
106 special = igp.special;
110 bool operator==(InsetGraphicsParams const & left,
111 InsetGraphicsParams const & right)
113 if (left.filename == right.filename &&
114 left.lyxscale == right.lyxscale &&
115 left.display == right.display &&
116 left.scale == right.scale &&
117 left.width == right.width &&
118 left.height == right.height &&
119 left.keepAspectRatio == right.keepAspectRatio &&
120 left.draft == right.draft &&
121 left.noUnzip == right.noUnzip &&
124 left.bb == right.bb &&
125 left.clip == right.clip &&
127 float_equal(left.rotateAngle, right.rotateAngle, 0.001) &&
128 left.rotateOrigin == right.rotateOrigin &&
129 left.subcaption == right.subcaption &&
130 left.subcaptionText == right.subcaptionText &&
131 left.special == right.special
139 bool operator!=(InsetGraphicsParams const & left,
140 InsetGraphicsParams const & right)
142 return !(left == right);
146 void InsetGraphicsParams::Write(ostream & os, string const & bufpath) const
148 // Do not write the default values
150 if (!filename.empty()) {
151 os << "\tfilename " << filename.outputFilename(bufpath) << '\n';
154 os << "\tlyxscale " << lyxscale << '\n';
155 if (display != lyx::graphics::DefaultDisplay)
156 os << "\tdisplay " << lyx::graphics::displayTranslator().find(display) << '\n';
157 if (!float_equal(scale, 0.0, 0.05)) {
158 if (!float_equal(scale, 100.0, 0.05))
159 os << "\tscale " << scale << '\n';
162 os << "\twidth " << width.asString() << '\n';
164 os << "\theight " << height.asString() << '\n';
168 os << "\tkeepAspectRatio\n";
169 if (draft) // draft mode
174 if (!bb.empty()) // bounding box
175 os << "\tBoundingBox " << bb << '\n';
176 if (clip) // clip image
179 if (rotateAngle != 0.0)
180 os << "\trotateAngle " << rotateAngle << '\n';
181 if (!rotateOrigin.empty())
182 os << "\trotateOrigin " << rotateOrigin << '\n';
184 os << "\tsubcaption\n";
185 if (!subcaptionText.empty())
186 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
187 if (!special.empty())
188 os << "\tspecial " << special << '\n';
192 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token, string const & bufpath)
194 if (token == "filename") {
196 filename.set(lex.getString(), bufpath);
197 } else if (token == "lyxscale") {
199 lyxscale = lex.getInteger();
200 } else if (token == "display") {
202 string const type = lex.getString();
203 display = lyx::graphics::displayTranslator().find(type);
204 } else if (token == "scale") {
206 scale = lex.getFloat();
207 } else if (token == "width") {
209 width = LyXLength(lex.getString());
211 } else if (token == "height") {
213 height = LyXLength(lex.getString());
215 } else if (token == "keepAspectRatio") {
216 keepAspectRatio = true;
217 } else if (token == "draft") {
219 } else if (token == "noUnzip") {
221 } else if (token == "BoundingBox") {
223 for (int i = 0; i < 4; ++i) {
227 bb += lex.getString();
229 } else if (token == "clip") {
231 } else if (token == "rotateAngle") {
233 rotateAngle = lex.getFloat();
234 } else if (token == "rotateOrigin") {
236 rotateOrigin=lex.getString();
237 } else if (token == "subcaption") {
239 } else if (token == "subcaptionText") {
241 string sub = lex.getString();
242 // strip surrounding " "
243 subcaptionText = sub.substr(1, sub.length() - 2);
244 } else if (token == "special") {
246 special = lex.getString();
248 // catch and ignore following two old-format tokens and their arguments.
249 // e.g. "size_kind scale" clashes with the setting of the
250 // "scale <value>" keyword.
251 } else if (token == "size_kind" || token == "lyxsize_kind") {
256 // If it's none of the above, it's not ours.
263 lyx::graphics::Params InsetGraphicsParams::as_grfxParams() const
265 lyx::graphics::Params pars;
266 pars.filename = filename.absFilename();
267 pars.scale = lyxscale;
268 pars.angle = rotateAngle;
273 // Get the original Bounding Box from the file
274 string const tmp = readBB_from_PSFile(filename.absFilename());
275 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
277 unsigned int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
278 unsigned int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
280 // new pars.bb values must be >= zero
281 if (pars.bb.xl > bb_orig_xl)
282 pars.bb.xl -= bb_orig_xl;
286 if (pars.bb.xr > bb_orig_xl)
287 pars.bb.xr -= bb_orig_xl;
291 if (pars.bb.yb > bb_orig_yb)
292 pars.bb.yb -= bb_orig_yb;
296 if (pars.bb.yt > bb_orig_yb)
297 pars.bb.yt -= bb_orig_yb;
303 int const width = pars.bb.xr - pars.bb.xl;
304 int const height = pars.bb.yt - pars.bb.yb;
306 if (width < 0 || height < 0) {
314 if (display == lyx::graphics::DefaultDisplay) {
315 pars.display = lyxrc.display_graphics;
317 pars.display = display;
320 // Override the above if we're not using a gui
321 if (!lyx_gui::use_gui) {
322 pars.display = lyx::graphics::NoDisplay;