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 lyx::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"
29 using lyx::support::float_equal;
30 using lyx::support::readBB_from_PSFile;
31 using lyx::support::token;
37 InsetGraphicsParams::InsetGraphicsParams()
43 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
45 // I decided to skip the initialization since the copy will overwrite
53 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
55 // Are we assigning the object into itself?
63 void InsetGraphicsParams::init()
66 lyxscale = 100; // lyx scaling in percentage
67 display = lyx::graphics::DefaultDisplay; // display mode; see preferences
68 scale = string(); // output scaling in percentage
71 keepAspectRatio = false; // for LaTeX output
72 draft = false; // draft mode
73 noUnzip = false; // unzip files
75 bb = string(); // bounding box
76 clip = false; // clip image
78 rotateAngle = "0"; // angle of rotation in degrees
79 rotateOrigin.erase(); // Origin of rotation
80 subcaption = false; // subfigure
81 subcaptionText.erase(); // subfigure caption
82 special.erase(); // additional userdefined stuff
86 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
88 filename = igp.filename;
89 lyxscale = igp.lyxscale;
90 display = igp.display;
94 keepAspectRatio = igp.keepAspectRatio;
96 noUnzip = igp.noUnzip;
101 rotateAngle = igp.rotateAngle;
102 rotateOrigin = igp.rotateOrigin;
103 subcaption = igp.subcaption;
104 subcaptionText = igp.subcaptionText;
105 special = igp.special;
109 bool operator==(InsetGraphicsParams const & left,
110 InsetGraphicsParams const & right)
112 if (left.filename == right.filename &&
113 left.lyxscale == right.lyxscale &&
114 left.display == right.display &&
115 left.scale == right.scale &&
116 left.width == right.width &&
117 left.height == right.height &&
118 left.keepAspectRatio == right.keepAspectRatio &&
119 left.draft == right.draft &&
120 left.noUnzip == right.noUnzip &&
123 left.bb == right.bb &&
124 left.clip == right.clip &&
126 left.rotateAngle == right.rotateAngle &&
127 left.rotateOrigin == right.rotateOrigin &&
128 left.subcaption == right.subcaption &&
129 left.subcaptionText == right.subcaptionText &&
130 left.special == right.special
138 bool operator!=(InsetGraphicsParams const & left,
139 InsetGraphicsParams const & right)
141 return !(left == right);
145 void InsetGraphicsParams::Write(ostream & os, string const & bufpath) const
147 // Do not write the default values
149 if (!filename.empty()) {
150 os << "\tfilename " << filename.outputFilename(bufpath) << '\n';
153 os << "\tlyxscale " << lyxscale << '\n';
154 if (display != lyx::graphics::DefaultDisplay)
155 os << "\tdisplay " << lyx::graphics::displayTranslator().find(display) << '\n';
156 if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
157 if (!float_equal(convert<double>(scale), 100.0, 0.05))
158 os << "\tscale " << scale << '\n';
161 os << "\twidth " << width.asString() << '\n';
163 os << "\theight " << height.asString() << '\n';
167 os << "\tkeepAspectRatio\n";
168 if (draft) // draft mode
173 if (!bb.empty()) // bounding box
174 os << "\tBoundingBox " << bb << '\n';
175 if (clip) // clip image
178 if (!rotateAngle.empty()
179 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
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.getString();
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.getString();
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 = convert<double>(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;
278 # warning why not convert to unsigned int? (Lgb)
280 unsigned int const bb_orig_xl = convert<int>(token(tmp, ' ', 0));
281 unsigned int const bb_orig_yb = convert<int>(token(tmp, ' ', 1));
283 // new pars.bb values must be >= zero
284 if (pars.bb.xl > bb_orig_xl)
285 pars.bb.xl -= bb_orig_xl;
289 if (pars.bb.xr > bb_orig_xl)
290 pars.bb.xr -= bb_orig_xl;
294 if (pars.bb.yb > bb_orig_yb)
295 pars.bb.yb -= bb_orig_yb;
299 if (pars.bb.yt > bb_orig_yb)
300 pars.bb.yt -= bb_orig_yb;
306 int const width = pars.bb.xr - pars.bb.xl;
307 int const height = pars.bb.yt - pars.bb.yb;
309 if (width < 0 || height < 0) {
317 if (display == lyx::graphics::DefaultDisplay) {
318 pars.display = lyxrc.display_graphics;
320 pars.display = display;
323 // Override the above if we're not using a gui
325 pars.display = lyx::graphics::NoDisplay;