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
15 #include "insetgraphicsParams.h"
17 #include "graphics/GraphicsParams.h"
19 #include "support/filetools.h"
20 #include "support/lyxlib.h"
21 #include "support/LOstream.h"
22 #include "support/LAssert.h"
23 #include "support/lstrings.h"
27 #include "frontends/lyx_gui.h"
31 InsetGraphicsParams::InsetGraphicsParams()
36 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
38 // I decided to skip the initialization since the copy will overwrite
45 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
47 // Are we assigning the object into itself?
54 void InsetGraphicsParams::init()
57 lyxscale = 100; // lyx scaling in percentage
58 display = grfx::DefaultDisplay; // display mode; see preferences
59 scale = 100.0; // output scaling in percentage
62 keepAspectRatio = false; // for LaTeX output
63 draft = false; // draft mode
64 noUnzip = false; // unzip files
66 bb = string(); // bounding box
67 clip = false; // clip image
69 rotateAngle = 0.0; // angle of rotation in degrees
70 rotateOrigin.erase(); // Origin of rotation
71 subcaption = false; // subfigure
72 subcaptionText.erase(); // subfigure caption
73 special.erase(); // additional userdefined stuff
76 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
78 filename = igp.filename;
79 lyxscale = igp.lyxscale;
80 display = igp.display;
84 keepAspectRatio = igp.keepAspectRatio;
86 noUnzip = igp.noUnzip;
91 rotateAngle = igp.rotateAngle;
92 rotateOrigin = igp.rotateOrigin;
93 subcaption = igp.subcaption;
94 subcaptionText = igp.subcaptionText;
95 special = igp.special;
98 bool operator==(InsetGraphicsParams const & left,
99 InsetGraphicsParams const & right)
101 if (left.filename == right.filename &&
102 left.lyxscale == right.lyxscale &&
103 left.display == right.display &&
104 left.scale == right.scale &&
105 left.width == right.width &&
106 left.height == right.height &&
107 left.keepAspectRatio == right.keepAspectRatio &&
108 left.draft == right.draft &&
109 left.noUnzip == right.noUnzip &&
112 left.bb == right.bb &&
113 left.clip == right.clip &&
115 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001) &&
116 left.rotateOrigin == right.rotateOrigin &&
117 left.subcaption == right.subcaption &&
118 left.subcaptionText == right.subcaptionText &&
119 left.special == right.special
126 bool operator!=(InsetGraphicsParams const & left,
127 InsetGraphicsParams const & right)
129 return !(left == right);
132 void InsetGraphicsParams::Write(ostream & os) const
134 // Do not write the default values
136 if (!filename.empty()) {
137 os << "\tfilename " << filename << '\n';
140 os << "\tlyxscale " << lyxscale << '\n';
141 if (display != grfx::DefaultDisplay)
142 os << "\tdisplay " << grfx::displayTranslator.find(display) << '\n';
143 if (!lyx::float_equal(scale, 0.0, 0.05)) {
144 if (!lyx::float_equal(scale, 100.0, 0.05))
145 os << "\tscale " << scale << '\n';
148 os << "\twidth " << width.asString() << '\n';
150 os << "\theight " << height.asString() << '\n';
154 os << "\tkeepAspectRatio\n";
155 if (draft) // draft mode
160 if (!bb.empty()) // bounding box
161 os << "\tBoundingBox " << bb << '\n';
162 if (clip) // clip image
165 if (rotateAngle != 0.0)
166 os << "\trotateAngle " << rotateAngle << '\n';
167 if (!rotateOrigin.empty())
168 os << "\trotateOrigin " << rotateOrigin << '\n';
170 os << "\tsubcaption\n";
171 if (!subcaptionText.empty())
172 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
173 if (!special.empty())
174 os << "\tspecial " << special << '\n';
178 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token)
180 if (token == "filename") {
182 filename = lex.getString();
183 } else if (token == "lyxscale") {
185 lyxscale = lex.getInteger();
186 } else if (token == "display") {
188 string const type = lex.getString();
189 display = grfx::displayTranslator.find(type);
190 } else if (token == "scale") {
192 scale = lex.getFloat();
193 } else if (token == "width") {
195 width = LyXLength(lex.getString());
197 } else if (token == "height") {
199 height = LyXLength(lex.getString());
201 } else if (token == "keepAspectRatio") {
202 keepAspectRatio = true;
203 } else if (token == "draft") {
205 } else if (token == "noUnzip") {
207 } else if (token == "BoundingBox") {
209 for (int i = 0; i < 4; ++i) {
213 bb += lex.getString();
215 } else if (token == "clip") {
217 } else if (token == "rotateAngle") {
219 rotateAngle = lex.getFloat();
220 } else if (token == "rotateOrigin") {
222 rotateOrigin=lex.getString();
223 } else if (token == "subcaption") {
225 } else if (token == "subcaptionText") {
227 string sub = lex.getString();
228 // strip surrounding " "
229 subcaptionText = sub.substr(1, sub.length() - 2);
230 } else if (token == "special") {
232 special = lex.getString();
234 // catch and ignore following two old-format tokens and their arguments.
235 // e.g. "size_kind scale" clashes with the setting of the
236 // "scale <value>" keyword.
237 } else if (token == "size_kind" || token == "lyxsize_kind") {
242 // If it's none of the above, it's not ours.
249 grfx::Params InsetGraphicsParams::as_grfxParams(string const & filepath) const
252 pars.filename = filename;
253 pars.scale = lyxscale;
254 pars.angle = rotateAngle;
256 if (!filepath.empty())
257 pars.filename = MakeAbsPath(pars.filename, filepath);
262 // Get the original Bounding Box from the file
263 string const tmp = readBB_from_PSFile(filename);
264 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
266 unsigned int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
267 unsigned int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
269 // new pars.bb values must be >= zero
270 if (pars.bb.xl > bb_orig_xl)
271 pars.bb.xl -= bb_orig_xl;
275 if (pars.bb.xr > bb_orig_xl)
276 pars.bb.xr -= bb_orig_xl;
280 if (pars.bb.yb > bb_orig_yb)
281 pars.bb.yb -= bb_orig_yb;
285 if (pars.bb.yt > bb_orig_yb)
286 pars.bb.yt -= bb_orig_yb;
292 int const width = pars.bb.xr - pars.bb.xl;
293 int const height = pars.bb.yt - pars.bb.yb;
295 if (width < 0 || height < 0) {
303 if (display == grfx::DefaultDisplay) {
304 pars.display = lyxrc.display_graphics;
306 pars.display = display;
309 // Override the above if we're not using a gui
310 if (!lyx_gui::use_gui) {
311 pars.display = grfx::NoDisplay;