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"
30 InsetGraphicsParams::InsetGraphicsParams()
35 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
37 // I decided to skip the initialization since the copy will overwrite
44 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
46 // Are we assigning the object into itself?
53 void InsetGraphicsParams::init()
56 lyxscale = 100; // lyx scaling in percentage
57 display = grfx::DefaultDisplay; // display mode; see preferences
58 scale = 100.0; // output scaling in percentage
61 keepAspectRatio = false; // for LaTeX output
62 draft = false; // draft mode
63 noUnzip = false; // unzip files
65 bb = string(); // bounding box
66 clip = false; // clip image
68 rotateAngle = 0.0; // angle of rotation in degrees
69 rotateOrigin.erase(); // Origin of rotation
70 subcaption = false; // subfigure
71 subcaptionText.erase(); // subfigure caption
72 special.erase(); // additional userdefined stuff
75 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
77 filename = igp.filename;
78 lyxscale = igp.lyxscale;
79 display = igp.display;
83 keepAspectRatio = igp.keepAspectRatio;
85 noUnzip = igp.noUnzip;
90 rotateAngle = igp.rotateAngle;
91 rotateOrigin = igp.rotateOrigin;
92 subcaption = igp.subcaption;
93 subcaptionText = igp.subcaptionText;
94 special = igp.special;
97 bool operator==(InsetGraphicsParams const & left,
98 InsetGraphicsParams const & right)
100 if (left.filename == right.filename &&
101 left.lyxscale == right.lyxscale &&
102 left.display == right.display &&
103 left.scale == right.scale &&
104 left.width == right.width &&
105 left.height == right.height &&
106 left.keepAspectRatio == right.keepAspectRatio &&
107 left.draft == right.draft &&
108 left.noUnzip == right.noUnzip &&
111 left.bb == right.bb &&
112 left.clip == right.clip &&
114 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001) &&
115 left.rotateOrigin == right.rotateOrigin &&
116 left.subcaption == right.subcaption &&
117 left.subcaptionText == right.subcaptionText &&
118 left.special == right.special
125 bool operator!=(InsetGraphicsParams const & left,
126 InsetGraphicsParams const & right)
128 return !(left == right);
131 void InsetGraphicsParams::Write(ostream & os) const
133 // Do not write the default values
135 if (!filename.empty()) {
136 os << "\tfilename " << filename << '\n';
139 os << "\tlyxscale " << lyxscale << '\n';
140 if (display != grfx::DefaultDisplay)
141 os << "\tdisplay " << grfx::displayTranslator.find(display) << '\n';
142 if (!lyx::float_equal(scale, 0.0, 0.05)) {
143 if (!lyx::float_equal(scale, 100.0, 0.05))
144 os << "\tscale " << scale << '\n';
147 os << "\twidth " << width.asString() << '\n';
149 os << "\theight " << height.asString() << '\n';
153 os << "\tkeepAspectRatio\n";
154 if (draft) // draft mode
159 if (!bb.empty()) // bounding box
160 os << "\tBoundingBox " << bb << '\n';
161 if (clip) // clip image
164 if (rotateAngle != 0.0)
165 os << "\trotateAngle " << rotateAngle << '\n';
166 if (!rotateOrigin.empty())
167 os << "\trotateOrigin " << rotateOrigin << '\n';
169 os << "\tsubcaption\n";
170 if (!subcaptionText.empty())
171 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
172 if (!special.empty())
173 os << "\tspecial " << special << '\n';
177 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token)
179 if (token == "filename") {
181 filename = lex.getString();
182 } else if (token == "lyxscale") {
184 lyxscale = lex.getInteger();
185 } else if (token == "display") {
187 string const type = lex.getString();
188 display = grfx::displayTranslator.find(type);
189 } else if (token == "scale") {
191 scale = lex.getFloat();
192 } else if (token == "width") {
194 width = LyXLength(lex.getString());
196 } else if (token == "height") {
198 height = LyXLength(lex.getString());
200 } else if (token == "keepAspectRatio") {
201 keepAspectRatio = true;
202 } else if (token == "draft") {
204 } else if (token == "noUnzip") {
206 } else if (token == "BoundingBox") {
208 for (int i = 0; i < 4; ++i) {
212 bb += lex.getString();
214 } else if (token == "clip") {
216 } else if (token == "rotateAngle") {
218 rotateAngle = lex.getFloat();
219 } else if (token == "rotateOrigin") {
221 rotateOrigin=lex.getString();
222 } else if (token == "subcaption") {
224 } else if (token == "subcaptionText") {
226 subcaptionText = lex.getString();
227 } else if (token == "special") {
229 special = lex.getString();
231 // catch and ignore following two old-format tokens and their arguments.
232 // e.g. "size_kind scale" clashes with the setting of the
233 // "scale <value>" keyword.
234 } else if (token == "size_kind" || token == "lyxsize_kind") {
239 // If it's none of the above, it's not ours.
246 grfx::Params InsetGraphicsParams::as_grfxParams(string const & filepath) const
249 pars.filename = filename;
250 pars.scale = lyxscale;
251 pars.angle = rotateAngle;
253 if (!filepath.empty())
254 pars.filename = MakeAbsPath(pars.filename, filepath);
259 // Get the original Bounding Box from the file
260 string const tmp = readBB_from_PSFile(filename);
261 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
263 unsigned int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
264 unsigned int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
266 // new pars.bb values must be >= zero
267 if (pars.bb.xl > bb_orig_xl)
268 pars.bb.xl -= bb_orig_xl;
272 if (pars.bb.xr > bb_orig_xl)
273 pars.bb.xr -= bb_orig_xl;
277 if (pars.bb.yb > bb_orig_yb)
278 pars.bb.yb -= bb_orig_yb;
282 if (pars.bb.yt > bb_orig_yb)
283 pars.bb.yt -= bb_orig_yb;
289 int const width = pars.bb.xr - pars.bb.xl;
290 int const height = pars.bb.yt - pars.bb.yb;
292 if (width < 0 || height < 0) {
300 if (display == grfx::DefaultDisplay) {
301 pars.display = lyxrc.display_graphics;
303 pars.display = display;
306 // Override the above if we're not using a gui
307 if (!lyxrc.use_gui) {
308 pars.display = grfx::NoDisplay;