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"
29 using namespace lyx::support;
34 InsetGraphicsParams::InsetGraphicsParams()
40 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
42 // I decided to skip the initialization since the copy will overwrite
50 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
52 // Are we assigning the object into itself?
60 void InsetGraphicsParams::init()
63 lyxscale = 100; // lyx scaling in percentage
64 display = lyx::graphics::DefaultDisplay; // display mode; see preferences
65 scale = 100.0; // output scaling in percentage
68 keepAspectRatio = false; // for LaTeX output
69 draft = false; // draft mode
70 noUnzip = false; // unzip files
72 bb = string(); // bounding box
73 clip = false; // clip image
75 rotateAngle = 0.0; // angle of rotation in degrees
76 rotateOrigin.erase(); // Origin of rotation
77 subcaption = false; // subfigure
78 subcaptionText.erase(); // subfigure caption
79 special.erase(); // additional userdefined stuff
83 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
85 filename = igp.filename;
86 lyxscale = igp.lyxscale;
87 display = igp.display;
91 keepAspectRatio = igp.keepAspectRatio;
93 noUnzip = igp.noUnzip;
98 rotateAngle = igp.rotateAngle;
99 rotateOrigin = igp.rotateOrigin;
100 subcaption = igp.subcaption;
101 subcaptionText = igp.subcaptionText;
102 special = igp.special;
106 bool operator==(InsetGraphicsParams const & left,
107 InsetGraphicsParams const & right)
109 if (left.filename == right.filename &&
110 left.lyxscale == right.lyxscale &&
111 left.display == right.display &&
112 left.scale == right.scale &&
113 left.width == right.width &&
114 left.height == right.height &&
115 left.keepAspectRatio == right.keepAspectRatio &&
116 left.draft == right.draft &&
117 left.noUnzip == right.noUnzip &&
120 left.bb == right.bb &&
121 left.clip == right.clip &&
123 float_equal(left.rotateAngle, right.rotateAngle, 0.001) &&
124 left.rotateOrigin == right.rotateOrigin &&
125 left.subcaption == right.subcaption &&
126 left.subcaptionText == right.subcaptionText &&
127 left.special == right.special
135 bool operator!=(InsetGraphicsParams const & left,
136 InsetGraphicsParams const & right)
138 return !(left == right);
142 void InsetGraphicsParams::Write(ostream & os, string const & bufpath) const
144 // Do not write the default values
146 if (!filename.empty()) {
147 os << "\tfilename " << MakeRelPath(filename, bufpath) << '\n';
150 os << "\tlyxscale " << lyxscale << '\n';
151 if (display != lyx::graphics::DefaultDisplay)
152 os << "\tdisplay " << lyx::graphics::displayTranslator.find(display) << '\n';
153 if (!float_equal(scale, 0.0, 0.05)) {
154 if (!float_equal(scale, 100.0, 0.05))
155 os << "\tscale " << scale << '\n';
158 os << "\twidth " << width.asString() << '\n';
160 os << "\theight " << height.asString() << '\n';
164 os << "\tkeepAspectRatio\n";
165 if (draft) // draft mode
170 if (!bb.empty()) // bounding box
171 os << "\tBoundingBox " << bb << '\n';
172 if (clip) // clip image
175 if (rotateAngle != 0.0)
176 os << "\trotateAngle " << rotateAngle << '\n';
177 if (!rotateOrigin.empty())
178 os << "\trotateOrigin " << rotateOrigin << '\n';
180 os << "\tsubcaption\n";
181 if (!subcaptionText.empty())
182 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
183 if (!special.empty())
184 os << "\tspecial " << special << '\n';
188 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token, string const & bufpath)
190 if (token == "filename") {
192 filename = MakeAbsPath(lex.getString(), bufpath);
193 } else if (token == "lyxscale") {
195 lyxscale = lex.getInteger();
196 } else if (token == "display") {
198 string const type = lex.getString();
199 display = lyx::graphics::displayTranslator.find(type);
200 } else if (token == "scale") {
202 scale = lex.getFloat();
203 } else if (token == "width") {
205 width = LyXLength(lex.getString());
207 } else if (token == "height") {
209 height = LyXLength(lex.getString());
211 } else if (token == "keepAspectRatio") {
212 keepAspectRatio = true;
213 } else if (token == "draft") {
215 } else if (token == "noUnzip") {
217 } else if (token == "BoundingBox") {
219 for (int i = 0; i < 4; ++i) {
223 bb += lex.getString();
225 } else if (token == "clip") {
227 } else if (token == "rotateAngle") {
229 rotateAngle = lex.getFloat();
230 } else if (token == "rotateOrigin") {
232 rotateOrigin=lex.getString();
233 } else if (token == "subcaption") {
235 } else if (token == "subcaptionText") {
237 string sub = lex.getString();
238 // strip surrounding " "
239 subcaptionText = sub.substr(1, sub.length() - 2);
240 } else if (token == "special") {
242 special = lex.getString();
244 // catch and ignore following two old-format tokens and their arguments.
245 // e.g. "size_kind scale" clashes with the setting of the
246 // "scale <value>" keyword.
247 } else if (token == "size_kind" || token == "lyxsize_kind") {
252 // If it's none of the above, it's not ours.
259 lyx::graphics::Params InsetGraphicsParams::as_grfxParams() const
261 lyx::graphics::Params pars;
262 pars.filename = filename;
263 pars.scale = lyxscale;
264 pars.angle = 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 << std::endl;
273 unsigned int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
274 unsigned int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
276 // new pars.bb values must be >= zero
277 if (pars.bb.xl > bb_orig_xl)
278 pars.bb.xl -= bb_orig_xl;
282 if (pars.bb.xr > bb_orig_xl)
283 pars.bb.xr -= bb_orig_xl;
287 if (pars.bb.yb > bb_orig_yb)
288 pars.bb.yb -= bb_orig_yb;
292 if (pars.bb.yt > bb_orig_yb)
293 pars.bb.yt -= bb_orig_yb;
299 int const width = pars.bb.xr - pars.bb.xl;
300 int const height = pars.bb.yt - pars.bb.yb;
302 if (width < 0 || height < 0) {
310 if (display == lyx::graphics::DefaultDisplay) {
311 pars.display = lyxrc.display_graphics;
313 pars.display = display;
316 // Override the above if we're not using a gui
317 if (!lyx_gui::use_gui) {
318 pars.display = lyx::graphics::NoDisplay;