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';
142 os << "\tembed " << (filename.embedded() ? filename.inzipName() : "\"\"") << '\n';
145 os << "\tlyxscale " << lyxscale << '\n';
146 if (display != graphics::DefaultDisplay)
147 os << "\tdisplay " << graphics::displayTranslator().find(display) << '\n';
148 if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
149 if (!float_equal(convert<double>(scale), 100.0, 0.05))
150 os << "\tscale " << scale << '\n';
153 os << "\twidth " << width.asString() << '\n';
155 os << "\theight " << height.asString() << '\n';
159 os << "\tkeepAspectRatio\n";
160 if (draft) // draft mode
164 if (scaleBeforeRotation)
165 os << "\tscaleBeforeRotation\n";
167 if (!bb.empty()) // bounding box
168 os << "\tBoundingBox " << bb << '\n';
169 if (clip) // clip image
172 if (!rotateAngle.empty()
173 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
174 os << "\trotateAngle " << rotateAngle << '\n';
175 if (!rotateOrigin.empty())
176 os << "\trotateOrigin " << rotateOrigin << '\n';
177 if (!special.empty())
178 os << "\tspecial " << special << '\n';
182 bool InsetGraphicsParams::Read(Lexer & lex, string const & token, string const & bufpath)
184 if (token == "filename") {
186 filename.set(lex.getString(), bufpath);
187 } else if (token == "inzipName") {
188 // this option is currently ignored because only files in
189 // or under current document path is embeddable, and their
190 // inzipName is automatically determined.
192 } else if (token == "embed") {
194 string const name = lex.getString();
195 filename.setInzipName(name);
196 filename.setEmbed(!name.empty());
197 } else if (token == "lyxscale") {
199 lyxscale = lex.getInteger();
200 } else if (token == "display") {
202 string const type = lex.getString();
203 display = graphics::displayTranslator().find(type);
204 } else if (token == "scale") {
206 scale = lex.getString();
207 } else if (token == "width") {
209 width = Length(lex.getString());
211 } else if (token == "height") {
213 height = Length(lex.getString());
215 } else if (token == "keepAspectRatio") {
216 keepAspectRatio = true;
217 } else if (token == "draft") {
219 } else if (token == "noUnzip") {
221 } else if (token == "scaleBeforeRotation") {
222 scaleBeforeRotation = true;
223 } else if (token == "BoundingBox") {
225 for (int i = 0; i < 4; ++i) {
229 bb += lex.getString();
231 } else if (token == "clip") {
233 } else if (token == "rotateAngle") {
235 rotateAngle = lex.getString();
236 } else if (token == "rotateOrigin") {
238 rotateOrigin=lex.getString();
239 } else if (token == "special") {
241 special = lex.getString();
243 // catch and ignore following two old-format tokens and their arguments.
244 // e.g. "size_kind scale" clashes with the setting of the
245 // "scale <value>" keyword.
246 } else if (token == "size_kind" || token == "lyxsize_kind") {
251 // If it's none of the above, it's not ours.
258 graphics::Params InsetGraphicsParams::as_grfxParams() const
260 graphics::Params pars;
261 pars.filename = filename.availableFile();
262 pars.icon = filename.embedded() ? "pin.png" : "";
263 pars.scale = lyxscale;
264 pars.angle = convert<double>(rotateAngle);
269 // Get the original Bounding Box from the file
270 string const tmp = readBB_from_PSFile(filename);
271 LYXERR(Debug::GRAPHICS, "BB_from_File: " << tmp);
273 // FIXME: why not convert to unsigned int? (Lgb)
274 unsigned int const bb_orig_xl = convert<int>(token(tmp, ' ', 0));
275 unsigned int const bb_orig_yb = convert<int>(token(tmp, ' ', 1));
277 // new pars.bb values must be >= zero
278 if (pars.bb.xl > bb_orig_xl)
279 pars.bb.xl -= bb_orig_xl;
283 if (pars.bb.xr > bb_orig_xl)
284 pars.bb.xr -= bb_orig_xl;
288 if (pars.bb.yb > bb_orig_yb)
289 pars.bb.yb -= bb_orig_yb;
293 if (pars.bb.yt > bb_orig_yb)
294 pars.bb.yt -= bb_orig_yb;
300 int const width = pars.bb.xr - pars.bb.xl;
301 int const height = pars.bb.yt - pars.bb.yb;
303 if (width < 0 || height < 0) {
311 if (display == graphics::DefaultDisplay) {
312 pars.display = graphics::DisplayType(lyxrc.display_graphics);
314 pars.display = display;
317 // Override the above if we're not using a gui
319 pars.display = graphics::NoDisplay;