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"
32 InsetGraphicsParams::InsetGraphicsParams()
38 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
40 // I decided to skip the initialization since the copy will overwrite
48 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
50 // Are we assigning the object into itself?
58 void InsetGraphicsParams::init()
61 lyxscale = 100; // lyx scaling in percentage
62 display = grfx::DefaultDisplay; // display mode; see preferences
63 scale = 100.0; // output scaling in percentage
66 keepAspectRatio = false; // for LaTeX output
67 draft = false; // draft mode
68 noUnzip = false; // unzip files
70 bb = string(); // bounding box
71 clip = false; // clip image
73 rotateAngle = 0.0; // angle of rotation in degrees
74 rotateOrigin.erase(); // Origin of rotation
75 subcaption = false; // subfigure
76 subcaptionText.erase(); // subfigure caption
77 special.erase(); // additional userdefined stuff
81 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
83 filename = igp.filename;
84 lyxscale = igp.lyxscale;
85 display = igp.display;
89 keepAspectRatio = igp.keepAspectRatio;
91 noUnzip = igp.noUnzip;
96 rotateAngle = igp.rotateAngle;
97 rotateOrigin = igp.rotateOrigin;
98 subcaption = igp.subcaption;
99 subcaptionText = igp.subcaptionText;
100 special = igp.special;
104 bool operator==(InsetGraphicsParams const & left,
105 InsetGraphicsParams const & right)
107 if (left.filename == right.filename &&
108 left.lyxscale == right.lyxscale &&
109 left.display == right.display &&
110 left.scale == right.scale &&
111 left.width == right.width &&
112 left.height == right.height &&
113 left.keepAspectRatio == right.keepAspectRatio &&
114 left.draft == right.draft &&
115 left.noUnzip == right.noUnzip &&
118 left.bb == right.bb &&
119 left.clip == right.clip &&
121 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001) &&
122 left.rotateOrigin == right.rotateOrigin &&
123 left.subcaption == right.subcaption &&
124 left.subcaptionText == right.subcaptionText &&
125 left.special == right.special
133 bool operator!=(InsetGraphicsParams const & left,
134 InsetGraphicsParams const & right)
136 return !(left == right);
140 void InsetGraphicsParams::Write(ostream & os, string const & bufpath) const
142 // Do not write the default values
144 if (!filename.empty()) {
145 os << "\tfilename " << MakeRelPath(filename, bufpath) << '\n';
148 os << "\tlyxscale " << lyxscale << '\n';
149 if (display != grfx::DefaultDisplay)
150 os << "\tdisplay " << grfx::displayTranslator.find(display) << '\n';
151 if (!lyx::float_equal(scale, 0.0, 0.05)) {
152 if (!lyx::float_equal(scale, 100.0, 0.05))
153 os << "\tscale " << scale << '\n';
156 os << "\twidth " << width.asString() << '\n';
158 os << "\theight " << height.asString() << '\n';
162 os << "\tkeepAspectRatio\n";
163 if (draft) // draft mode
168 if (!bb.empty()) // bounding box
169 os << "\tBoundingBox " << bb << '\n';
170 if (clip) // clip image
173 if (rotateAngle != 0.0)
174 os << "\trotateAngle " << rotateAngle << '\n';
175 if (!rotateOrigin.empty())
176 os << "\trotateOrigin " << rotateOrigin << '\n';
178 os << "\tsubcaption\n";
179 if (!subcaptionText.empty())
180 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
181 if (!special.empty())
182 os << "\tspecial " << special << '\n';
186 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token, string const & bufpath)
188 if (token == "filename") {
190 filename = MakeAbsPath(lex.getString(), bufpath);
191 } else if (token == "lyxscale") {
193 lyxscale = lex.getInteger();
194 } else if (token == "display") {
196 string const type = lex.getString();
197 display = grfx::displayTranslator.find(type);
198 } else if (token == "scale") {
200 scale = lex.getFloat();
201 } else if (token == "width") {
203 width = LyXLength(lex.getString());
205 } else if (token == "height") {
207 height = LyXLength(lex.getString());
209 } else if (token == "keepAspectRatio") {
210 keepAspectRatio = true;
211 } else if (token == "draft") {
213 } else if (token == "noUnzip") {
215 } else if (token == "BoundingBox") {
217 for (int i = 0; i < 4; ++i) {
221 bb += lex.getString();
223 } else if (token == "clip") {
225 } else if (token == "rotateAngle") {
227 rotateAngle = lex.getFloat();
228 } else if (token == "rotateOrigin") {
230 rotateOrigin=lex.getString();
231 } else if (token == "subcaption") {
233 } else if (token == "subcaptionText") {
235 string sub = lex.getString();
236 // strip surrounding " "
237 subcaptionText = sub.substr(1, sub.length() - 2);
238 } else if (token == "special") {
240 special = lex.getString();
242 // catch and ignore following two old-format tokens and their arguments.
243 // e.g. "size_kind scale" clashes with the setting of the
244 // "scale <value>" keyword.
245 } else if (token == "size_kind" || token == "lyxsize_kind") {
250 // If it's none of the above, it's not ours.
257 grfx::Params InsetGraphicsParams::as_grfxParams() const
260 pars.filename = filename;
261 pars.scale = lyxscale;
262 pars.angle = rotateAngle;
267 // Get the original Bounding Box from the file
268 string const tmp = readBB_from_PSFile(filename);
269 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
271 unsigned int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
272 unsigned int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
274 // new pars.bb values must be >= zero
275 if (pars.bb.xl > bb_orig_xl)
276 pars.bb.xl -= bb_orig_xl;
280 if (pars.bb.xr > bb_orig_xl)
281 pars.bb.xr -= bb_orig_xl;
285 if (pars.bb.yb > bb_orig_yb)
286 pars.bb.yb -= bb_orig_yb;
290 if (pars.bb.yt > bb_orig_yb)
291 pars.bb.yt -= bb_orig_yb;
297 int const width = pars.bb.xr - pars.bb.xl;
298 int const height = pars.bb.yt - pars.bb.yb;
300 if (width < 0 || height < 0) {
308 if (display == grfx::DefaultDisplay) {
309 pars.display = lyxrc.display_graphics;
311 pars.display = display;
314 // Override the above if we're not using a gui
315 if (!lyx_gui::use_gui) {
316 pars.display = grfx::NoDisplay;