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::strToDbl;
32 using lyx::support::strToInt;
33 using lyx::support::token;
39 InsetGraphicsParams::InsetGraphicsParams()
45 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
47 // I decided to skip the initialization since the copy will overwrite
55 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
57 // Are we assigning the object into itself?
65 void InsetGraphicsParams::init()
68 lyxscale = 100; // lyx scaling in percentage
69 display = lyx::graphics::DefaultDisplay; // display mode; see preferences
70 scale = string(); // output scaling in percentage
73 keepAspectRatio = false; // for LaTeX output
74 draft = false; // draft mode
75 noUnzip = false; // unzip files
77 bb = string(); // bounding box
78 clip = false; // clip image
80 rotateAngle = "0"; // angle of rotation in degrees
81 rotateOrigin.erase(); // Origin of rotation
82 subcaption = false; // subfigure
83 subcaptionText.erase(); // subfigure caption
84 special.erase(); // additional userdefined stuff
88 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
90 filename = igp.filename;
91 lyxscale = igp.lyxscale;
92 display = igp.display;
96 keepAspectRatio = igp.keepAspectRatio;
98 noUnzip = igp.noUnzip;
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 if (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 &&
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
140 bool operator!=(InsetGraphicsParams const & left,
141 InsetGraphicsParams const & right)
143 return !(left == right);
147 void InsetGraphicsParams::Write(ostream & os, string const & bufpath) const
149 // Do not write the default values
151 if (!filename.empty()) {
152 os << "\tfilename " << filename.outputFilename(bufpath) << '\n';
155 os << "\tlyxscale " << lyxscale << '\n';
156 if (display != lyx::graphics::DefaultDisplay)
157 os << "\tdisplay " << lyx::graphics::displayTranslator().find(display) << '\n';
158 if (!scale.empty() && !float_equal(strToDbl(scale), 0.0, 0.05)) {
159 if (!float_equal(strToDbl(scale), 100.0, 0.05))
160 os << "\tscale " << scale << '\n';
163 os << "\twidth " << width.asString() << '\n';
165 os << "\theight " << height.asString() << '\n';
169 os << "\tkeepAspectRatio\n";
170 if (draft) // draft mode
175 if (!bb.empty()) // bounding box
176 os << "\tBoundingBox " << bb << '\n';
177 if (clip) // clip image
180 if (!rotateAngle.empty()
181 && !float_equal(strToDbl(rotateAngle), 0.0, 0.001))
182 os << "\trotateAngle " << rotateAngle << '\n';
183 if (!rotateOrigin.empty())
184 os << "\trotateOrigin " << rotateOrigin << '\n';
186 os << "\tsubcaption\n";
187 if (!subcaptionText.empty())
188 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
189 if (!special.empty())
190 os << "\tspecial " << special << '\n';
194 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token, string const & bufpath)
196 if (token == "filename") {
198 filename.set(lex.getString(), bufpath);
199 } else if (token == "lyxscale") {
201 lyxscale = lex.getInteger();
202 } else if (token == "display") {
204 string const type = lex.getString();
205 display = lyx::graphics::displayTranslator().find(type);
206 } else if (token == "scale") {
208 scale = lex.getString();
209 } else if (token == "width") {
211 width = LyXLength(lex.getString());
213 } else if (token == "height") {
215 height = LyXLength(lex.getString());
217 } else if (token == "keepAspectRatio") {
218 keepAspectRatio = true;
219 } else if (token == "draft") {
221 } else if (token == "noUnzip") {
223 } else if (token == "BoundingBox") {
225 for (int i = 0; i < 4; ++i) {
229 bb += lex.getString();
231 } else if (token == "clip") {
233 } else if (token == "rotateAngle") {
235 rotateAngle = lex.getString();
236 } else if (token == "rotateOrigin") {
238 rotateOrigin=lex.getString();
239 } else if (token == "subcaption") {
241 } else if (token == "subcaptionText") {
243 string sub = lex.getString();
244 // strip surrounding " "
245 subcaptionText = sub.substr(1, sub.length() - 2);
246 } else if (token == "special") {
248 special = lex.getString();
250 // catch and ignore following two old-format tokens and their arguments.
251 // e.g. "size_kind scale" clashes with the setting of the
252 // "scale <value>" keyword.
253 } else if (token == "size_kind" || token == "lyxsize_kind") {
258 // If it's none of the above, it's not ours.
265 lyx::graphics::Params InsetGraphicsParams::as_grfxParams() const
267 lyx::graphics::Params pars;
268 pars.filename = filename.absFilename();
269 pars.scale = lyxscale;
270 pars.angle = lyx::support::strToDbl(rotateAngle);
275 // Get the original Bounding Box from the file
276 string const tmp = readBB_from_PSFile(filename.absFilename());
277 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
279 unsigned int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
280 unsigned int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
282 // new pars.bb values must be >= zero
283 if (pars.bb.xl > bb_orig_xl)
284 pars.bb.xl -= bb_orig_xl;
288 if (pars.bb.xr > bb_orig_xl)
289 pars.bb.xr -= bb_orig_xl;
293 if (pars.bb.yb > bb_orig_yb)
294 pars.bb.yb -= bb_orig_yb;
298 if (pars.bb.yt > bb_orig_yb)
299 pars.bb.yt -= bb_orig_yb;
305 int const width = pars.bb.xr - pars.bb.xl;
306 int const height = pars.bb.yt - pars.bb.yb;
308 if (width < 0 || height < 0) {
316 if (display == lyx::graphics::DefaultDisplay) {
317 pars.display = lyxrc.display_graphics;
319 pars.display = display;
322 // Override the above if we're not using a gui
323 if (!lyx_gui::use_gui) {
324 pars.display = lyx::graphics::NoDisplay;