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"
20 #include "frontends/lyx_gui.h"
22 #include "graphics/GraphicsParams.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::strToInt;
32 using lyx::support::token;
40 /// The translator between the DisplayType and the corresponding lyx string.
41 extern Translator<DisplayType, string> displayTranslator;
46 InsetGraphicsParams::InsetGraphicsParams()
52 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
54 // I decided to skip the initialization since the copy will overwrite
62 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
64 // Are we assigning the object into itself?
72 void InsetGraphicsParams::init()
75 lyxscale = 100; // lyx scaling in percentage
76 display = lyx::graphics::DefaultDisplay; // display mode; see preferences
77 scale = 100.0; // output scaling in percentage
80 keepAspectRatio = false; // for LaTeX output
81 draft = false; // draft mode
82 noUnzip = false; // unzip files
84 bb = string(); // bounding box
85 clip = false; // clip image
87 rotateAngle = 0.0; // angle of rotation in degrees
88 rotateOrigin.erase(); // Origin of rotation
89 subcaption = false; // subfigure
90 subcaptionText.erase(); // subfigure caption
91 special.erase(); // additional userdefined stuff
95 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
97 filename = igp.filename;
98 lyxscale = igp.lyxscale;
99 display = igp.display;
103 keepAspectRatio = igp.keepAspectRatio;
105 noUnzip = igp.noUnzip;
110 rotateAngle = igp.rotateAngle;
111 rotateOrigin = igp.rotateOrigin;
112 subcaption = igp.subcaption;
113 subcaptionText = igp.subcaptionText;
114 special = igp.special;
118 bool operator==(InsetGraphicsParams const & left,
119 InsetGraphicsParams const & right)
121 if (left.filename == right.filename &&
122 left.lyxscale == right.lyxscale &&
123 left.display == right.display &&
124 left.scale == right.scale &&
125 left.width == right.width &&
126 left.height == right.height &&
127 left.keepAspectRatio == right.keepAspectRatio &&
128 left.draft == right.draft &&
129 left.noUnzip == right.noUnzip &&
132 left.bb == right.bb &&
133 left.clip == right.clip &&
135 float_equal(left.rotateAngle, right.rotateAngle, 0.001) &&
136 left.rotateOrigin == right.rotateOrigin &&
137 left.subcaption == right.subcaption &&
138 left.subcaptionText == right.subcaptionText &&
139 left.special == right.special
147 bool operator!=(InsetGraphicsParams const & left,
148 InsetGraphicsParams const & right)
150 return !(left == right);
154 void InsetGraphicsParams::Write(ostream & os, string const & bufpath) const
156 // Do not write the default values
158 if (!filename.empty()) {
159 os << "\tfilename " << filename.outputFilename(bufpath) << '\n';
162 os << "\tlyxscale " << lyxscale << '\n';
163 if (display != lyx::graphics::DefaultDisplay)
164 os << "\tdisplay " << lyx::graphics::displayTranslator.find(display) << '\n';
165 if (!float_equal(scale, 0.0, 0.05)) {
166 if (!float_equal(scale, 100.0, 0.05))
167 os << "\tscale " << scale << '\n';
170 os << "\twidth " << width.asString() << '\n';
172 os << "\theight " << height.asString() << '\n';
176 os << "\tkeepAspectRatio\n";
177 if (draft) // draft mode
182 if (!bb.empty()) // bounding box
183 os << "\tBoundingBox " << bb << '\n';
184 if (clip) // clip image
187 if (rotateAngle != 0.0)
188 os << "\trotateAngle " << rotateAngle << '\n';
189 if (!rotateOrigin.empty())
190 os << "\trotateOrigin " << rotateOrigin << '\n';
192 os << "\tsubcaption\n";
193 if (!subcaptionText.empty())
194 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
195 if (!special.empty())
196 os << "\tspecial " << special << '\n';
200 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token, string const & bufpath)
202 if (token == "filename") {
204 filename.set(lex.getString(), bufpath);
205 } else if (token == "lyxscale") {
207 lyxscale = lex.getInteger();
208 } else if (token == "display") {
210 string const type = lex.getString();
211 display = lyx::graphics::displayTranslator.find(type);
212 } else if (token == "scale") {
214 scale = lex.getFloat();
215 } else if (token == "width") {
217 width = LyXLength(lex.getString());
219 } else if (token == "height") {
221 height = LyXLength(lex.getString());
223 } else if (token == "keepAspectRatio") {
224 keepAspectRatio = true;
225 } else if (token == "draft") {
227 } else if (token == "noUnzip") {
229 } else if (token == "BoundingBox") {
231 for (int i = 0; i < 4; ++i) {
235 bb += lex.getString();
237 } else if (token == "clip") {
239 } else if (token == "rotateAngle") {
241 rotateAngle = lex.getFloat();
242 } else if (token == "rotateOrigin") {
244 rotateOrigin=lex.getString();
245 } else if (token == "subcaption") {
247 } else if (token == "subcaptionText") {
249 string sub = lex.getString();
250 // strip surrounding " "
251 subcaptionText = sub.substr(1, sub.length() - 2);
252 } else if (token == "special") {
254 special = lex.getString();
256 // catch and ignore following two old-format tokens and their arguments.
257 // e.g. "size_kind scale" clashes with the setting of the
258 // "scale <value>" keyword.
259 } else if (token == "size_kind" || token == "lyxsize_kind") {
264 // If it's none of the above, it's not ours.
271 lyx::graphics::Params InsetGraphicsParams::as_grfxParams() const
273 lyx::graphics::Params pars;
274 pars.filename = filename.absFilename();
275 pars.scale = lyxscale;
276 pars.angle = rotateAngle;
281 // Get the original Bounding Box from the file
282 string const tmp = readBB_from_PSFile(filename.absFilename());
283 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
285 unsigned int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
286 unsigned int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
288 // new pars.bb values must be >= zero
289 if (pars.bb.xl > bb_orig_xl)
290 pars.bb.xl -= bb_orig_xl;
294 if (pars.bb.xr > bb_orig_xl)
295 pars.bb.xr -= bb_orig_xl;
299 if (pars.bb.yb > bb_orig_yb)
300 pars.bb.yb -= bb_orig_yb;
304 if (pars.bb.yt > bb_orig_yb)
305 pars.bb.yt -= bb_orig_yb;
311 int const width = pars.bb.xr - pars.bb.xl;
312 int const height = pars.bb.yt - pars.bb.yb;
314 if (width < 0 || height < 0) {
322 if (display == lyx::graphics::DefaultDisplay) {
323 pars.display = lyxrc.display_graphics;
325 pars.display = display;
328 // Override the above if we're not using a gui
329 if (!lyx_gui::use_gui) {
330 pars.display = lyx::graphics::NoDisplay;