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/convert.h"
25 #include "support/filetools.h"
26 #include "support/lyxlib.h"
27 #include "support/lstrings.h"
28 #include "support/translator.h"
30 using lyx::support::float_equal;
31 using lyx::support::readBB_from_PSFile;
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 = string(); // 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"; // 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 left.rotateAngle == right.rotateAngle &&
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 (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
158 if (!float_equal(convert<double>(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.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(LyXLex & lex, string const & token, string const & bufpath)
195 if (token == "filename") {
197 filename.set(lex.getString(), bufpath);
198 } else if (token == "lyxscale") {
200 lyxscale = lex.getInteger();
201 } else if (token == "display") {
203 string const type = lex.getString();
204 display = lyx::graphics::displayTranslator().find(type);
205 } else if (token == "scale") {
207 scale = lex.getString();
208 } else if (token == "width") {
210 width = LyXLength(lex.getString());
212 } else if (token == "height") {
214 height = LyXLength(lex.getString());
216 } else if (token == "keepAspectRatio") {
217 keepAspectRatio = true;
218 } else if (token == "draft") {
220 } else if (token == "noUnzip") {
222 } else if (token == "BoundingBox") {
224 for (int i = 0; i < 4; ++i) {
228 bb += lex.getString();
230 } else if (token == "clip") {
232 } else if (token == "rotateAngle") {
234 rotateAngle = lex.getString();
235 } else if (token == "rotateOrigin") {
237 rotateOrigin=lex.getString();
238 } else if (token == "subcaption") {
240 } else if (token == "subcaptionText") {
242 string sub = lex.getString();
243 // strip surrounding " "
244 subcaptionText = sub.substr(1, sub.length() - 2);
245 } else if (token == "special") {
247 special = lex.getString();
249 // catch and ignore following two old-format tokens and their arguments.
250 // e.g. "size_kind scale" clashes with the setting of the
251 // "scale <value>" keyword.
252 } else if (token == "size_kind" || token == "lyxsize_kind") {
257 // If it's none of the above, it's not ours.
264 lyx::graphics::Params InsetGraphicsParams::as_grfxParams() const
266 lyx::graphics::Params pars;
267 pars.filename = filename.absFilename();
268 pars.scale = lyxscale;
269 pars.angle = convert<double>(rotateAngle);
274 // Get the original Bounding Box from the file
275 string const tmp = readBB_from_PSFile(filename.absFilename());
276 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
279 # warning why not convert to unsigned int? (Lgb)
281 unsigned int const bb_orig_xl = convert<int>(token(tmp, ' ', 0));
282 unsigned int const bb_orig_yb = convert<int>(token(tmp, ' ', 1));
284 // new pars.bb values must be >= zero
285 if (pars.bb.xl > bb_orig_xl)
286 pars.bb.xl -= bb_orig_xl;
290 if (pars.bb.xr > bb_orig_xl)
291 pars.bb.xr -= bb_orig_xl;
295 if (pars.bb.yb > bb_orig_yb)
296 pars.bb.yb -= bb_orig_yb;
300 if (pars.bb.yt > bb_orig_yb)
301 pars.bb.yt -= bb_orig_yb;
307 int const width = pars.bb.xr - pars.bb.xl;
308 int const height = pars.bb.yt - pars.bb.yb;
310 if (width < 0 || height < 0) {
318 if (display == lyx::graphics::DefaultDisplay) {
319 pars.display = lyxrc.display_graphics;
321 pars.display = display;
324 // Override the above if we're not using a gui
325 if (!lyx_gui::use_gui) {
326 pars.display = lyx::graphics::NoDisplay;