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.
14 #include "insetgraphicsParams.h"
20 #include "frontends/lyx_gui.h"
22 #include "graphics/GraphicsParams.h"
24 #include "support/filetools.h"
25 #include "support/lyxlib.h"
26 #include "support/lstrings.h"
27 #include "support/translator.h"
29 using lyx::support::float_equal;
30 using lyx::support::readBB_from_PSFile;
31 using lyx::support::strToInt;
32 using lyx::support::token;
39 /// The translator between the DisplayType and the corresponding lyx string.
40 extern Translator<DisplayType, string> displayTranslator;
45 InsetGraphicsParams::InsetGraphicsParams()
51 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
53 // I decided to skip the initialization since the copy will overwrite
61 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
63 // Are we assigning the object into itself?
71 void InsetGraphicsParams::init()
74 lyxscale = 100; // lyx scaling in percentage
75 display = lyx::graphics::DefaultDisplay; // display mode; see preferences
76 scale = 100.0; // output scaling in percentage
79 keepAspectRatio = false; // for LaTeX output
80 draft = false; // draft mode
81 noUnzip = false; // unzip files
83 bb = string(); // bounding box
84 clip = false; // clip image
86 rotateAngle = 0.0; // angle of rotation in degrees
87 rotateOrigin.erase(); // Origin of rotation
88 subcaption = false; // subfigure
89 subcaptionText.erase(); // subfigure caption
90 special.erase(); // additional userdefined stuff
94 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
96 filename = igp.filename;
97 lyxscale = igp.lyxscale;
98 display = igp.display;
102 keepAspectRatio = igp.keepAspectRatio;
104 noUnzip = igp.noUnzip;
109 rotateAngle = igp.rotateAngle;
110 rotateOrigin = igp.rotateOrigin;
111 subcaption = igp.subcaption;
112 subcaptionText = igp.subcaptionText;
113 special = igp.special;
117 bool operator==(InsetGraphicsParams const & left,
118 InsetGraphicsParams const & right)
120 if (left.filename == right.filename &&
121 left.lyxscale == right.lyxscale &&
122 left.display == right.display &&
123 left.scale == right.scale &&
124 left.width == right.width &&
125 left.height == right.height &&
126 left.keepAspectRatio == right.keepAspectRatio &&
127 left.draft == right.draft &&
128 left.noUnzip == right.noUnzip &&
131 left.bb == right.bb &&
132 left.clip == right.clip &&
134 float_equal(left.rotateAngle, right.rotateAngle, 0.001) &&
135 left.rotateOrigin == right.rotateOrigin &&
136 left.subcaption == right.subcaption &&
137 left.subcaptionText == right.subcaptionText &&
138 left.special == right.special
146 bool operator!=(InsetGraphicsParams const & left,
147 InsetGraphicsParams const & right)
149 return !(left == right);
153 void InsetGraphicsParams::Write(ostream & os, string const & bufpath) const
155 // Do not write the default values
157 if (!filename.empty()) {
158 os << "\tfilename " << filename.outputFilename(bufpath) << '\n';
161 os << "\tlyxscale " << lyxscale << '\n';
162 if (display != lyx::graphics::DefaultDisplay)
163 os << "\tdisplay " << lyx::graphics::displayTranslator.find(display) << '\n';
164 if (!float_equal(scale, 0.0, 0.05)) {
165 if (!float_equal(scale, 100.0, 0.05))
166 os << "\tscale " << scale << '\n';
169 os << "\twidth " << width.asString() << '\n';
171 os << "\theight " << height.asString() << '\n';
175 os << "\tkeepAspectRatio\n";
176 if (draft) // draft mode
181 if (!bb.empty()) // bounding box
182 os << "\tBoundingBox " << bb << '\n';
183 if (clip) // clip image
186 if (rotateAngle != 0.0)
187 os << "\trotateAngle " << rotateAngle << '\n';
188 if (!rotateOrigin.empty())
189 os << "\trotateOrigin " << rotateOrigin << '\n';
191 os << "\tsubcaption\n";
192 if (!subcaptionText.empty())
193 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
194 if (!special.empty())
195 os << "\tspecial " << special << '\n';
199 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token, string const & bufpath)
201 if (token == "filename") {
203 filename.set(lex.getString(), bufpath);
204 } else if (token == "lyxscale") {
206 lyxscale = lex.getInteger();
207 } else if (token == "display") {
209 string const type = lex.getString();
210 display = lyx::graphics::displayTranslator.find(type);
211 } else if (token == "scale") {
213 scale = lex.getFloat();
214 } else if (token == "width") {
216 width = LyXLength(lex.getString());
218 } else if (token == "height") {
220 height = LyXLength(lex.getString());
222 } else if (token == "keepAspectRatio") {
223 keepAspectRatio = true;
224 } else if (token == "draft") {
226 } else if (token == "noUnzip") {
228 } else if (token == "BoundingBox") {
230 for (int i = 0; i < 4; ++i) {
234 bb += lex.getString();
236 } else if (token == "clip") {
238 } else if (token == "rotateAngle") {
240 rotateAngle = lex.getFloat();
241 } else if (token == "rotateOrigin") {
243 rotateOrigin=lex.getString();
244 } else if (token == "subcaption") {
246 } else if (token == "subcaptionText") {
248 string sub = lex.getString();
249 // strip surrounding " "
250 subcaptionText = sub.substr(1, sub.length() - 2);
251 } else if (token == "special") {
253 special = lex.getString();
255 // catch and ignore following two old-format tokens and their arguments.
256 // e.g. "size_kind scale" clashes with the setting of the
257 // "scale <value>" keyword.
258 } else if (token == "size_kind" || token == "lyxsize_kind") {
263 // If it's none of the above, it's not ours.
270 lyx::graphics::Params InsetGraphicsParams::as_grfxParams() const
272 lyx::graphics::Params pars;
273 pars.filename = filename.absFilename();
274 pars.scale = lyxscale;
275 pars.angle = rotateAngle;
280 // Get the original Bounding Box from the file
281 string const tmp = readBB_from_PSFile(filename.absFilename());
282 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
284 unsigned int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
285 unsigned int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
287 // new pars.bb values must be >= zero
288 if (pars.bb.xl > bb_orig_xl)
289 pars.bb.xl -= bb_orig_xl;
293 if (pars.bb.xr > bb_orig_xl)
294 pars.bb.xr -= bb_orig_xl;
298 if (pars.bb.yb > bb_orig_yb)
299 pars.bb.yb -= bb_orig_yb;
303 if (pars.bb.yt > bb_orig_yb)
304 pars.bb.yt -= bb_orig_yb;
310 int const width = pars.bb.xr - pars.bb.xl;
311 int const height = pars.bb.yt - pars.bb.yb;
313 if (width < 0 || height < 0) {
321 if (display == lyx::graphics::DefaultDisplay) {
322 pars.display = lyxrc.display_graphics;
324 pars.display = display;
327 // Override the above if we're not using a gui
328 if (!lyx_gui::use_gui) {
329 pars.display = lyx::graphics::NoDisplay;