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"
17 #include "lyx_main.h" // for use_gui
21 #include "graphics/GraphicsParams.h"
23 #include "support/convert.h"
24 #include "support/filetools.h"
25 #include "support/lyxlib.h"
26 #include "support/lstrings.h"
27 #include "support/translator.h"
32 using support::float_equal;
33 using support::readBB_from_PSFile;
40 InsetGraphicsParams::InsetGraphicsParams()
46 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
48 // I decided to skip the initialization since the copy will overwrite
56 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
58 // Are we assigning the object into itself?
66 void InsetGraphicsParams::init()
69 lyxscale = 100; // lyx scaling in percentage
70 display = graphics::DefaultDisplay; // display mode; see preferences
71 scale = string(); // output scaling in percentage
74 keepAspectRatio = false; // for LaTeX output
75 draft = false; // draft mode
76 noUnzip = false; // unzip files
78 bb = string(); // bounding box
79 clip = false; // clip image
81 rotateAngle = "0"; // angle of rotation in degrees
82 rotateOrigin.erase(); // Origin of rotation
83 subcaption = false; // subfigure
84 subcaptionText.erase(); // subfigure caption
85 special.erase(); // additional userdefined stuff
89 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
91 filename = igp.filename;
92 lyxscale = igp.lyxscale;
93 display = igp.display;
97 keepAspectRatio = igp.keepAspectRatio;
99 noUnzip = igp.noUnzip;
104 rotateAngle = igp.rotateAngle;
105 rotateOrigin = igp.rotateOrigin;
106 subcaption = igp.subcaption;
107 subcaptionText = igp.subcaptionText;
108 special = igp.special;
112 bool operator==(InsetGraphicsParams const & left,
113 InsetGraphicsParams const & right)
115 if (left.filename == right.filename &&
116 left.lyxscale == right.lyxscale &&
117 left.display == right.display &&
118 left.scale == right.scale &&
119 left.width == right.width &&
120 left.height == right.height &&
121 left.keepAspectRatio == right.keepAspectRatio &&
122 left.draft == right.draft &&
123 left.noUnzip == right.noUnzip &&
126 left.bb == right.bb &&
127 left.clip == right.clip &&
129 left.rotateAngle == right.rotateAngle &&
130 left.rotateOrigin == right.rotateOrigin &&
131 left.subcaption == right.subcaption &&
132 left.subcaptionText == right.subcaptionText &&
133 left.special == right.special
141 bool operator!=(InsetGraphicsParams const & left,
142 InsetGraphicsParams const & right)
144 return !(left == right);
148 void InsetGraphicsParams::Write(ostream & os, string const & bufpath) const
150 // Do not write the default values
152 if (!filename.empty()) {
153 os << "\tfilename " << filename.outputFilename(bufpath) << '\n';
156 os << "\tlyxscale " << lyxscale << '\n';
157 if (display != graphics::DefaultDisplay)
158 os << "\tdisplay " << graphics::displayTranslator().find(display) << '\n';
159 if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
160 if (!float_equal(convert<double>(scale), 100.0, 0.05))
161 os << "\tscale " << scale << '\n';
164 os << "\twidth " << width.asString() << '\n';
166 os << "\theight " << height.asString() << '\n';
170 os << "\tkeepAspectRatio\n";
171 if (draft) // draft mode
176 if (!bb.empty()) // bounding box
177 os << "\tBoundingBox " << bb << '\n';
178 if (clip) // clip image
181 if (!rotateAngle.empty()
182 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
183 os << "\trotateAngle " << rotateAngle << '\n';
184 if (!rotateOrigin.empty())
185 os << "\trotateOrigin " << rotateOrigin << '\n';
187 os << "\tsubcaption\n";
188 if (!subcaptionText.empty())
189 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
190 if (!special.empty())
191 os << "\tspecial " << special << '\n';
195 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token, string const & bufpath)
197 if (token == "filename") {
199 filename.set(lex.getString(), bufpath);
200 } else if (token == "lyxscale") {
202 lyxscale = lex.getInteger();
203 } else if (token == "display") {
205 string const type = lex.getString();
206 display = graphics::displayTranslator().find(type);
207 } else if (token == "scale") {
209 scale = lex.getString();
210 } else if (token == "width") {
212 width = LyXLength(lex.getString());
214 } else if (token == "height") {
216 height = LyXLength(lex.getString());
218 } else if (token == "keepAspectRatio") {
219 keepAspectRatio = true;
220 } else if (token == "draft") {
222 } else if (token == "noUnzip") {
224 } else if (token == "BoundingBox") {
226 for (int i = 0; i < 4; ++i) {
230 bb += lex.getString();
232 } else if (token == "clip") {
234 } else if (token == "rotateAngle") {
236 rotateAngle = lex.getString();
237 } else if (token == "rotateOrigin") {
239 rotateOrigin=lex.getString();
240 } else if (token == "subcaption") {
242 } else if (token == "subcaptionText") {
244 string sub = lex.getString();
245 // strip surrounding " "
246 subcaptionText = sub.substr(1, sub.length() - 2);
247 } else if (token == "special") {
249 special = lex.getString();
251 // catch and ignore following two old-format tokens and their arguments.
252 // e.g. "size_kind scale" clashes with the setting of the
253 // "scale <value>" keyword.
254 } else if (token == "size_kind" || token == "lyxsize_kind") {
259 // If it's none of the above, it's not ours.
266 graphics::Params InsetGraphicsParams::as_grfxParams() const
268 graphics::Params pars;
269 pars.filename = filename.absFilename();
270 pars.scale = lyxscale;
271 pars.angle = convert<double>(rotateAngle);
276 // Get the original Bounding Box from the file
277 string const tmp = readBB_from_PSFile(filename.absFilename());
278 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
281 # warning why not convert to unsigned int? (Lgb)
283 unsigned int const bb_orig_xl = convert<int>(token(tmp, ' ', 0));
284 unsigned int const bb_orig_yb = convert<int>(token(tmp, ' ', 1));
286 // new pars.bb values must be >= zero
287 if (pars.bb.xl > bb_orig_xl)
288 pars.bb.xl -= bb_orig_xl;
292 if (pars.bb.xr > bb_orig_xl)
293 pars.bb.xr -= bb_orig_xl;
297 if (pars.bb.yb > bb_orig_yb)
298 pars.bb.yb -= bb_orig_yb;
302 if (pars.bb.yt > bb_orig_yb)
303 pars.bb.yt -= bb_orig_yb;
309 int const width = pars.bb.xr - pars.bb.xl;
310 int const height = pars.bb.yt - pars.bb.yb;
312 if (width < 0 || height < 0) {
320 if (display == graphics::DefaultDisplay) {
321 pars.display = lyxrc.display_graphics;
323 pars.display = display;
326 // Override the above if we're not using a gui
328 pars.display = graphics::NoDisplay;