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 #pragma implementation
18 #include "insetgraphicsParams.h"
20 #include "graphics/GraphicsParams.h"
22 #include "support/filetools.h"
23 #include "support/lyxlib.h"
24 #include "support/LOstream.h"
25 #include "support/LAssert.h"
26 #include "support/lstrings.h"
33 InsetGraphicsParams::InsetGraphicsParams()
38 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
40 // I decided to skip the initialization since the copy will overwrite
47 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
49 // Are we assigning the object into itself?
56 void InsetGraphicsParams::init()
59 lyxscale = 100; // lyx scaling in percentage
60 display = grfx::DefaultDisplay; // display mode; see preferences
61 scale = 100.0; // output scaling in percentage
64 keepAspectRatio = false; // for LaTeX output
65 draft = false; // draft mode
66 noUnzip = false; // unzip files
68 bb = string(); // bounding box
69 clip = false; // clip image
71 rotateAngle = 0.0; // angle of rotation in degrees
72 rotateOrigin.erase(); // Origin of rotation
73 subcaption = false; // subfigure
74 subcaptionText.erase(); // subfigure caption
75 special.erase(); // additional userdefined stuff
78 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
80 filename = igp.filename;
81 lyxscale = igp.lyxscale;
82 display = igp.display;
86 keepAspectRatio = igp.keepAspectRatio;
88 noUnzip = igp.noUnzip;
93 rotateAngle = igp.rotateAngle;
94 rotateOrigin = igp.rotateOrigin;
95 subcaption = igp.subcaption;
96 subcaptionText = igp.subcaptionText;
97 special = igp.special;
100 bool operator==(InsetGraphicsParams const & left,
101 InsetGraphicsParams const & right)
103 if (left.filename == right.filename &&
104 left.lyxscale == right.lyxscale &&
105 left.display == right.display &&
106 left.scale == right.scale &&
107 left.width == right.width &&
108 left.height == right.height &&
109 left.keepAspectRatio == right.keepAspectRatio &&
110 left.draft == right.draft &&
111 left.noUnzip == right.noUnzip &&
114 left.bb == right.bb &&
115 left.clip == right.clip &&
117 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001) &&
118 left.rotateOrigin == right.rotateOrigin &&
119 left.subcaption == right.subcaption &&
120 left.subcaptionText == right.subcaptionText &&
121 left.special == right.special
128 bool operator!=(InsetGraphicsParams const & left,
129 InsetGraphicsParams const & right)
131 return !(left == right);
134 void InsetGraphicsParams::Write(ostream & os) const
136 // Do not write the default values
138 if (!filename.empty()) {
139 os << "\tfilename " << filename << '\n';
142 os << "\tlyxscale " << lyxscale << '\n';
143 if (display != grfx::DefaultDisplay)
144 os << "\tdisplay " << grfx::displayTranslator.find(display) << '\n';
145 if (!lyx::float_equal(scale, 0.0, 0.05)) {
146 if (!lyx::float_equal(scale, 100.0, 0.05))
147 os << "\tscale " << scale << '\n';
150 os << "\twidth " << width.asString() << '\n';
152 os << "\theight " << height.asString() << '\n';
156 os << "\tkeepAspectRatio\n";
157 if (draft) // draft mode
162 if (!bb.empty()) // bounding box
163 os << "\tBoundingBox " << bb << '\n';
164 if (clip) // clip image
167 if (rotateAngle != 0.0)
168 os << "\trotateAngle " << rotateAngle << '\n';
169 if (!rotateOrigin.empty())
170 os << "\trotateOrigin " << rotateOrigin << '\n';
172 os << "\tsubcaption\n";
173 if (!subcaptionText.empty())
174 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
175 if (!special.empty())
176 os << "\tspecial " << special << '\n';
180 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token)
182 if (token == "filename") {
184 filename = lex.getString();
185 } else if (token == "lyxscale") {
187 lyxscale = lex.getInteger();
188 } else if (token == "display") {
190 string const type = lex.getString();
191 display = grfx::displayTranslator.find(type);
192 } else if (token == "scale") {
194 scale = lex.getFloat();
195 } else if (token == "width") {
197 width = LyXLength(lex.getString());
199 } else if (token == "height") {
201 height = LyXLength(lex.getString());
203 } else if (token == "keepAspectRatio") {
204 keepAspectRatio = true;
205 } else if (token == "draft") {
207 } else if (token == "noUnzip") {
209 } else if (token == "BoundingBox") {
211 for (int i = 0; i < 4; ++i) {
215 bb += lex.getString();
217 } else if (token == "clip") {
219 } else if (token == "rotateAngle") {
221 rotateAngle = lex.getFloat();
222 } else if (token == "rotateOrigin") {
224 rotateOrigin=lex.getString();
225 } else if (token == "subcaption") {
227 } else if (token == "subcaptionText") {
229 subcaptionText = lex.getString();
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 (!lyxrc.use_gui) {
311 pars.display = grfx::NoDisplay;