2 * \file InsetGraphicsParams.cpp
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"
16 #include "LyX.h" // for use_gui
21 #include "graphics/GraphicsParams.h"
22 #include "graphics/GraphicsTypes.h"
24 #include "support/convert.h"
25 #include "support/debug.h"
26 #include "support/filetools.h"
27 #include "support/lyxlib.h"
28 #include "support/lstrings.h"
29 #include "support/Translator.h"
34 using namespace lyx::support;
39 InsetGraphicsParams::InsetGraphicsParams()
45 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
47 // I decided to skip the initialization since the copy will overwrite
54 void InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
56 // Are we assigning the object into itself?
63 void InsetGraphicsParams::init()
66 lyxscale = 100; // lyx scaling in percentage
67 display = graphics::DefaultDisplay; // display mode; see preferences
68 scale = string("100"); // output scaling in percentage
71 keepAspectRatio = false; // for LaTeX output
72 draft = false; // draft mode
73 noUnzip = false; // unzip files
74 scaleBeforeRotation = false; // scale image before rotating
76 bb = string(); // bounding box
77 clip = false; // clip image
79 rotateAngle = "0"; // angle of rotation in degrees
80 rotateOrigin.erase(); // Origin of rotation
81 special.erase(); // additional userdefined stuff
85 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
87 filename = igp.filename;
88 lyxscale = igp.lyxscale;
89 display = igp.display;
93 keepAspectRatio = igp.keepAspectRatio;
95 noUnzip = igp.noUnzip;
96 scaleBeforeRotation = igp.scaleBeforeRotation;
101 rotateAngle = igp.rotateAngle;
102 rotateOrigin = igp.rotateOrigin;
103 special = igp.special;
107 bool operator==(InsetGraphicsParams const & left,
108 InsetGraphicsParams const & right)
110 return left.filename == right.filename &&
111 left.lyxscale == right.lyxscale &&
112 left.display == right.display &&
113 left.scale == right.scale &&
114 left.width == right.width &&
115 left.height == right.height &&
116 left.keepAspectRatio == right.keepAspectRatio &&
117 left.draft == right.draft &&
118 left.noUnzip == right.noUnzip &&
119 left.scaleBeforeRotation == right.scaleBeforeRotation &&
121 left.bb == right.bb &&
122 left.clip == right.clip &&
124 left.rotateAngle == right.rotateAngle &&
125 left.rotateOrigin == right.rotateOrigin &&
126 left.special == right.special;
130 bool operator!=(InsetGraphicsParams const & left,
131 InsetGraphicsParams const & right)
133 return !(left == right);
137 void InsetGraphicsParams::Write(ostream & os, Buffer const & buffer) const
139 // Do not write the default values
140 if (!filename.empty())
141 os << "\tfilename " << filename.outputFilename(buffer.filePath()) << '\n';
143 os << "\tlyxscale " << lyxscale << '\n';
144 if (display != graphics::DefaultDisplay)
145 os << "\tdisplay " << graphics::displayTranslator().find(display) << '\n';
146 if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
147 if (!float_equal(convert<double>(scale), 100.0, 0.05))
148 os << "\tscale " << scale << '\n';
151 os << "\twidth " << width.asString() << '\n';
153 os << "\theight " << height.asString() << '\n';
157 os << "\tkeepAspectRatio\n";
158 if (draft) // draft mode
162 if (scaleBeforeRotation)
163 os << "\tscaleBeforeRotation\n";
165 if (!bb.empty()) // bounding box
166 os << "\tBoundingBox " << bb << '\n';
167 if (clip) // clip image
170 if (!rotateAngle.empty()
171 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
172 os << "\trotateAngle " << rotateAngle << '\n';
173 if (!rotateOrigin.empty())
174 os << "\trotateOrigin " << rotateOrigin << '\n';
175 if (!special.empty())
176 os << "\tspecial " << special << '\n';
180 bool InsetGraphicsParams::Read(Lexer & lex, string const & token, string const & bufpath)
182 if (token == "filename") {
184 filename.set(lex.getString(), bufpath);
185 } else if (token == "embed") {
187 string const name = lex.getString();
188 filename.setInzipName(name);
189 filename.setEmbed(!name.empty());
190 } else if (token == "lyxscale") {
192 lyxscale = lex.getInteger();
193 } else if (token == "display") {
195 string const type = lex.getString();
196 display = graphics::displayTranslator().find(type);
197 } else if (token == "scale") {
199 scale = lex.getString();
200 } else if (token == "width") {
202 width = Length(lex.getString());
204 } else if (token == "height") {
206 height = Length(lex.getString());
208 } else if (token == "keepAspectRatio") {
209 keepAspectRatio = true;
210 } else if (token == "draft") {
212 } else if (token == "noUnzip") {
214 } else if (token == "scaleBeforeRotation") {
215 scaleBeforeRotation = true;
216 } else if (token == "BoundingBox") {
218 for (int i = 0; i < 4; ++i) {
222 bb += lex.getString();
224 } else if (token == "clip") {
226 } else if (token == "rotateAngle") {
228 rotateAngle = lex.getString();
229 } else if (token == "rotateOrigin") {
231 rotateOrigin=lex.getString();
232 } else if (token == "special") {
234 special = lex.getString();
236 // catch and ignore following two old-format tokens and their arguments.
237 // e.g. "size_kind scale" clashes with the setting of the
238 // "scale <value>" keyword.
239 } else if (token == "size_kind" || token == "lyxsize_kind") {
244 // If it's none of the above, it's not ours.
251 graphics::Params InsetGraphicsParams::as_grfxParams() const
253 graphics::Params pars;
254 pars.filename = filename.availableFile();
255 pars.icon = filename.embedded() ? "pin.png" : "";
256 pars.scale = lyxscale;
257 pars.angle = convert<double>(rotateAngle);
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);
266 // FIXME: why not convert to unsigned int? (Lgb)
267 unsigned int const bb_orig_xl = convert<int>(token(tmp, ' ', 0));
268 unsigned int const bb_orig_yb = convert<int>(token(tmp, ' ', 1));
270 // new pars.bb values must be >= zero
271 if (pars.bb.xl > bb_orig_xl)
272 pars.bb.xl -= bb_orig_xl;
276 if (pars.bb.xr > bb_orig_xl)
277 pars.bb.xr -= bb_orig_xl;
281 if (pars.bb.yb > bb_orig_yb)
282 pars.bb.yb -= bb_orig_yb;
286 if (pars.bb.yt > bb_orig_yb)
287 pars.bb.yt -= bb_orig_yb;
293 int const width = pars.bb.xr - pars.bb.xl;
294 int const height = pars.bb.yt - pars.bb.yb;
296 if (width < 0 || height < 0) {
304 if (display == graphics::DefaultDisplay) {
305 pars.display = graphics::DisplayType(lyxrc.display_graphics);
307 pars.display = display;
310 // Override the above if we're not using a gui
312 pars.display = graphics::NoDisplay;