1 /* This file is part of
2 * =================================================
4 * LyX, The Document Processor
5 * Copyright 1995 Matthias Ettrich.
6 * Copyright 1995-2001 The LyX Team.
9 * \author Herbert Voss <voss@lyx.org>
11 * ================================================= */
16 #pragma implementation
19 #include "insetgraphicsParams.h"
21 #include "graphics/GraphicsParams.h"
23 #include "support/filetools.h"
24 #include "support/lyxlib.h"
25 #include "support/LOstream.h"
26 #include "support/LAssert.h"
27 #include "support/lstrings.h"
34 InsetGraphicsParams::InsetGraphicsParams()
39 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
41 // I decided to skip the initialization since the copy will overwrite
48 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
50 // Are we assigning the object into itself?
57 void InsetGraphicsParams::init()
59 subcaptionText = filename = string();
60 lyxscale = 100; // lyx scaling in percentage
61 display = grfx::DefaultDisplay; // see pref
62 scale = 100; // output scaling in percentage
65 keepAspectRatio = false; // for latex
66 draft = false; // draft mode
67 noUnzip = false; // unzip files
69 bb = string(); // bounding box
70 clip = false; // clip image
72 rotateOrigin = "leftBaseline"; // Origin
73 rotateAngle = 0.0; // in degrees
74 subcaption = false; // subfigure
75 subcaptionText = string(); // subfigure caption
76 special = string(); // userdefined stuff
79 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
81 filename = igp.filename;
82 lyxscale = igp.lyxscale;
83 display = igp.display;
87 keepAspectRatio = igp.keepAspectRatio;
89 noUnzip = igp.noUnzip;
94 rotateAngle = igp.rotateAngle;
95 rotateOrigin = igp.rotateOrigin;
96 subcaption = igp.subcaption;
97 subcaptionText = igp.subcaptionText;
98 special = igp.special;
101 bool operator==(InsetGraphicsParams const & left,
102 InsetGraphicsParams const & right)
104 if (left.filename == right.filename &&
105 left.lyxscale == right.lyxscale &&
106 left.display == right.display &&
107 left.scale == right.scale &&
108 left.width == right.width &&
109 left.height == right.height &&
110 left.keepAspectRatio == right.keepAspectRatio &&
111 left.draft == right.draft &&
112 left.noUnzip == right.noUnzip &&
115 left.bb == right.bb &&
116 left.clip == right.clip &&
118 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001) &&
119 left.rotateOrigin == right.rotateOrigin &&
120 left.subcaption == right.subcaption &&
121 left.subcaptionText == right.subcaptionText &&
122 left.special == right.special
129 bool operator!=(InsetGraphicsParams const & left,
130 InsetGraphicsParams const & right)
132 return !(left == right);
135 void InsetGraphicsParams::Write(ostream & os) const
137 // Do not write the default values
139 if (!filename.empty()) {
140 os << "\tfilename " << filename << '\n';
143 os << "\tlyxscale " << lyxscale << '\n';
144 if (display != grfx::DefaultDisplay)
145 os << "\tdisplay " << grfx::displayTranslator.find(display) << '\n';
148 os << "\tscale " << scale << '\n';
151 os << "\twidth " << width.asString() << '\n';
155 os << "\theight " << height.asString() << '\n';
157 os << "\tkeepAspectRatio\n";
158 if (draft) // draft mode
163 if (!bb.empty()) // bounding box
164 os << "\tBoundingBox " << bb << '\n';
165 if (clip) // clip image
168 if (rotateAngle != 0.0)
169 os << "\trotateAngle " << rotateAngle << '\n';
170 if (rotateOrigin != "leftBaseline")
171 os << "\trotateOrigin " << rotateOrigin << '\n';
173 os << "\tsubcaption\n";
174 if (!subcaptionText.empty())
175 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
176 if (!special.empty())
177 os << "\tspecial " << special << '\n';
181 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token)
183 if (token == "filename") {
185 filename = lex.getString();
186 } else if (token == "lyxscale") {
188 lyxscale = lex.getInteger();
189 } else if (token == "display") {
191 string const type = lex.getString();
192 display = grfx::displayTranslator.find(type);
193 } else if (token == "scale") {
195 scale = lex.getInteger();
196 } else if (token == "width") {
198 width = LyXLength(lex.getString());
200 } else if (token == "height") {
202 height = LyXLength(lex.getString());
203 } else if (token == "keepAspectRatio") {
204 keepAspectRatio = true;
205 } else if (token == "draft") {
207 } else if (token == "noUnzip") {
209 } else if (token == "BoundingBox") {
210 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 "scale" keyword.
233 } else if (token == "size_kind" || token == "lyxsize_kind") {
236 } else { // If it's none of the above, it's not ours.
243 grfx::Params InsetGraphicsParams::as_grfxParams(string const & filepath) const
246 pars.filename = filename;
247 pars.scale = lyxscale;
248 pars.angle = rotateAngle;
250 if (!filepath.empty())
251 pars.filename = MakeAbsPath(pars.filename, filepath);
256 // Get the original Bounding Box from the file
257 string const tmp = readBB_from_PSFile(filename);
258 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
260 unsigned int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
261 unsigned int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
263 // new pars.bb values must be >= zero
264 if (pars.bb.xl > bb_orig_xl)
265 pars.bb.xl -= bb_orig_xl;
269 if (pars.bb.xr > bb_orig_xl)
270 pars.bb.xr -= bb_orig_xl;
274 if (pars.bb.yb > bb_orig_yb)
275 pars.bb.yb -= bb_orig_yb;
279 if (pars.bb.yt > bb_orig_yb)
280 pars.bb.yt -= bb_orig_yb;
286 int const width = pars.bb.xr - pars.bb.xl;
287 int const height = pars.bb.yt - pars.bb.yb;
289 if (width < 0 || height < 0) {
297 if (display == grfx::DefaultDisplay) {
298 pars.display = lyxrc.display_graphics;
300 pars.display = display;
303 // Override the above if we're not using a gui
304 if (!lyxrc.use_gui) {
305 pars.display = grfx::NoDisplay;