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"
24 #include "support/translator.h"
28 #include "frontends/lyx_gui.h"
30 using namespace lyx::support;
36 /// The translator between the DisplayType and the corresponding lyx string.
37 extern Translator<DisplayType, string> displayTranslator;
42 InsetGraphicsParams::InsetGraphicsParams()
48 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
50 // I decided to skip the initialization since the copy will overwrite
58 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
60 // Are we assigning the object into itself?
68 void InsetGraphicsParams::init()
71 lyxscale = 100; // lyx scaling in percentage
72 display = lyx::graphics::DefaultDisplay; // display mode; see preferences
73 scale = 100.0; // output scaling in percentage
76 keepAspectRatio = false; // for LaTeX output
77 draft = false; // draft mode
78 noUnzip = false; // unzip files
80 bb = string(); // bounding box
81 clip = false; // clip image
83 rotateAngle = 0.0; // angle of rotation in degrees
84 rotateOrigin.erase(); // Origin of rotation
85 subcaption = false; // subfigure
86 subcaptionText.erase(); // subfigure caption
87 special.erase(); // additional userdefined stuff
91 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
93 filename = igp.filename;
94 lyxscale = igp.lyxscale;
95 display = igp.display;
99 keepAspectRatio = igp.keepAspectRatio;
101 noUnzip = igp.noUnzip;
106 rotateAngle = igp.rotateAngle;
107 rotateOrigin = igp.rotateOrigin;
108 subcaption = igp.subcaption;
109 subcaptionText = igp.subcaptionText;
110 special = igp.special;
114 bool operator==(InsetGraphicsParams const & left,
115 InsetGraphicsParams const & right)
117 if (left.filename == right.filename &&
118 left.lyxscale == right.lyxscale &&
119 left.display == right.display &&
120 left.scale == right.scale &&
121 left.width == right.width &&
122 left.height == right.height &&
123 left.keepAspectRatio == right.keepAspectRatio &&
124 left.draft == right.draft &&
125 left.noUnzip == right.noUnzip &&
128 left.bb == right.bb &&
129 left.clip == right.clip &&
131 float_equal(left.rotateAngle, right.rotateAngle, 0.001) &&
132 left.rotateOrigin == right.rotateOrigin &&
133 left.subcaption == right.subcaption &&
134 left.subcaptionText == right.subcaptionText &&
135 left.special == right.special
143 bool operator!=(InsetGraphicsParams const & left,
144 InsetGraphicsParams const & right)
146 return !(left == right);
150 void InsetGraphicsParams::Write(ostream & os, string const & bufpath) const
152 // Do not write the default values
154 if (!filename.empty()) {
155 os << "\tfilename " << filename.outputFilename(bufpath) << '\n';
158 os << "\tlyxscale " << lyxscale << '\n';
159 if (display != lyx::graphics::DefaultDisplay)
160 os << "\tdisplay " << lyx::graphics::displayTranslator.find(display) << '\n';
161 if (!float_equal(scale, 0.0, 0.05)) {
162 if (!float_equal(scale, 100.0, 0.05))
163 os << "\tscale " << scale << '\n';
166 os << "\twidth " << width.asString() << '\n';
168 os << "\theight " << height.asString() << '\n';
172 os << "\tkeepAspectRatio\n";
173 if (draft) // draft mode
178 if (!bb.empty()) // bounding box
179 os << "\tBoundingBox " << bb << '\n';
180 if (clip) // clip image
183 if (rotateAngle != 0.0)
184 os << "\trotateAngle " << rotateAngle << '\n';
185 if (!rotateOrigin.empty())
186 os << "\trotateOrigin " << rotateOrigin << '\n';
188 os << "\tsubcaption\n";
189 if (!subcaptionText.empty())
190 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
191 if (!special.empty())
192 os << "\tspecial " << special << '\n';
196 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token, string const & bufpath)
198 if (token == "filename") {
200 filename.set(lex.getString(), bufpath);
201 } else if (token == "lyxscale") {
203 lyxscale = lex.getInteger();
204 } else if (token == "display") {
206 string const type = lex.getString();
207 display = lyx::graphics::displayTranslator.find(type);
208 } else if (token == "scale") {
210 scale = lex.getFloat();
211 } else if (token == "width") {
213 width = LyXLength(lex.getString());
215 } else if (token == "height") {
217 height = LyXLength(lex.getString());
219 } else if (token == "keepAspectRatio") {
220 keepAspectRatio = true;
221 } else if (token == "draft") {
223 } else if (token == "noUnzip") {
225 } else if (token == "BoundingBox") {
227 for (int i = 0; i < 4; ++i) {
231 bb += lex.getString();
233 } else if (token == "clip") {
235 } else if (token == "rotateAngle") {
237 rotateAngle = lex.getFloat();
238 } else if (token == "rotateOrigin") {
240 rotateOrigin=lex.getString();
241 } else if (token == "subcaption") {
243 } else if (token == "subcaptionText") {
245 string sub = lex.getString();
246 // strip surrounding " "
247 subcaptionText = sub.substr(1, sub.length() - 2);
248 } else if (token == "special") {
250 special = lex.getString();
252 // catch and ignore following two old-format tokens and their arguments.
253 // e.g. "size_kind scale" clashes with the setting of the
254 // "scale <value>" keyword.
255 } else if (token == "size_kind" || token == "lyxsize_kind") {
260 // If it's none of the above, it's not ours.
267 lyx::graphics::Params InsetGraphicsParams::as_grfxParams() const
269 lyx::graphics::Params pars;
270 pars.filename = filename.absFilename();
271 pars.scale = lyxscale;
272 pars.angle = rotateAngle;
277 // Get the original Bounding Box from the file
278 string const tmp = readBB_from_PSFile(filename.absFilename());
279 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
281 unsigned int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
282 unsigned int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
284 // new pars.bb values must be >= zero
285 if (pars.bb.xl > bb_orig_xl)
286 pars.bb.xl -= bb_orig_xl;
290 if (pars.bb.xr > bb_orig_xl)
291 pars.bb.xr -= bb_orig_xl;
295 if (pars.bb.yb > bb_orig_yb)
296 pars.bb.yb -= bb_orig_yb;
300 if (pars.bb.yt > bb_orig_yb)
301 pars.bb.yt -= bb_orig_yb;
307 int const width = pars.bb.xr - pars.bb.xl;
308 int const height = pars.bb.yt - pars.bb.yb;
310 if (width < 0 || height < 0) {
318 if (display == lyx::graphics::DefaultDisplay) {
319 pars.display = lyxrc.display_graphics;
321 pars.display = display;
324 // Override the above if we're not using a gui
325 if (!lyx_gui::use_gui) {
326 pars.display = lyx::graphics::NoDisplay;