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/translator.h"
24 #include "support/filetools.h"
25 #include "support/lyxlib.h"
26 #include "support/LOstream.h"
27 #include "support/LAssert.h"
28 #include "support/lstrings.h"
38 /// This variable keeps a tab on whether the translator was set with the
40 bool translatorsSet = false;
42 /// This is the translator between the Display enum and corresponding lyx
44 Translator< grfx::DisplayType, string >
45 displayTranslator(grfx::DefaultDisplay, "default");
50 InsetGraphicsParams::InsetGraphicsParams()
54 if (! translatorsSet) {
55 translatorsSet = true;
56 // Fill the display translator
57 displayTranslator.addPair(grfx::DefaultDisplay, "default");
58 displayTranslator.addPair(grfx::MonochromeDisplay, "monochrome");
59 displayTranslator.addPair(grfx::GrayscaleDisplay, "grayscale");
60 displayTranslator.addPair(grfx::ColorDisplay, "color");
61 displayTranslator.addPair(grfx::NoDisplay, "none");
63 // backward compatibility for old lyxrc.display_graphics
64 displayTranslator.addPair(grfx::MonochromeDisplay, "mono");
65 displayTranslator.addPair(grfx::GrayscaleDisplay, "gray");
66 displayTranslator.addPair(grfx::NoDisplay, "no");
70 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
72 // I decided to skip the initialization since the copy will overwrite
79 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
81 // Are we assigning the object into itself?
88 void InsetGraphicsParams::init()
90 subcaptionText = filename = string();
91 lyxscale = 100; // lyx scaling in percentage
92 display = grfx::DefaultDisplay; // see pref
93 scale = 100; // output scaling in percentage
96 keepAspectRatio = false; // for latex
97 draft = false; // draft mode
98 noUnzip = false; // unzip files
100 bb = string(); // bounding box
101 clip = false; // clip image
103 rotateOrigin = "leftBaseline"; // Origin
104 rotateAngle = 0.0; // in degrees
105 subcaption = false; // subfigure
106 subcaptionText = string(); // subfigure caption
107 special = string(); // userdefined stuff
110 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
112 filename = igp.filename;
113 lyxscale = igp.lyxscale;
114 display = igp.display;
118 keepAspectRatio = igp.keepAspectRatio;
120 noUnzip = igp.noUnzip;
125 rotateAngle = igp.rotateAngle;
126 rotateOrigin = igp.rotateOrigin;
127 subcaption = igp.subcaption;
128 subcaptionText = igp.subcaptionText;
129 special = igp.special;
132 bool operator==(InsetGraphicsParams const & left,
133 InsetGraphicsParams const & right)
135 if (left.filename == right.filename &&
136 left.lyxscale == right.lyxscale &&
137 left.display == right.display &&
138 left.scale == right.scale &&
139 left.width == right.width &&
140 left.height == right.height &&
141 left.keepAspectRatio == right.keepAspectRatio &&
142 left.draft == right.draft &&
143 left.noUnzip == right.noUnzip &&
146 left.bb == right.bb &&
147 left.clip == right.clip &&
149 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001) &&
150 left.rotateOrigin == right.rotateOrigin &&
151 left.subcaption == right.subcaption &&
152 left.subcaptionText == right.subcaptionText &&
153 left.special == right.special
160 bool operator!=(InsetGraphicsParams const & left,
161 InsetGraphicsParams const & right)
163 return !(left == right);
166 void InsetGraphicsParams::Write(ostream & os) const
168 // Do not write the default values
170 if (!filename.empty()) {
171 os << "\tfilename " << filename << '\n';
174 os << "\tlyxscale " << lyxscale << '\n';
175 if (display != grfx::DefaultDisplay)
176 os << "\tdisplay " << displayTranslator.find(display) << '\n';
179 os << "\tscale " << scale << '\n';
182 os << "\twidth " << width.asString() << '\n';
186 os << "\theight " << height.asString() << '\n';
188 os << "\tkeepAspectRatio\n";
189 if (draft) // draft mode
194 if (!bb.empty()) // bounding box
195 os << "\tBoundingBox " << bb << '\n';
196 if (clip) // clip image
199 if (rotateAngle != 0.0)
200 os << "\trotateAngle " << rotateAngle << '\n';
201 if (rotateOrigin != "leftBaseline")
202 os << "\trotateOrigin " << rotateOrigin << '\n';
204 os << "\tsubcaption\n";
205 if (!subcaptionText.empty())
206 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
207 if (!special.empty())
208 os << "\tspecial " << special << '\n';
212 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token)
214 if (token == "filename") {
216 filename = lex.getString();
217 } else if (token == "lyxscale") {
219 lyxscale = lex.getInteger();
220 } else if (token == "display") {
222 string const type = lex.getString();
223 display = displayTranslator.find(type);
224 } else if (token == "scale") {
226 scale = lex.getInteger();
227 } else if (token == "width") {
229 width = LyXLength(lex.getString());
231 } else if (token == "height") {
233 height = LyXLength(lex.getString());
234 } else if (token == "keepAspectRatio") {
235 keepAspectRatio = true;
236 } else if (token == "draft") {
238 } else if (token == "noUnzip") {
240 } else if (token == "BoundingBox") {
241 for (int i=0; i<4 ;i++) {
243 bb += (lex.getString()+" ");
245 } else if (token == "clip") {
247 } else if (token == "rotateAngle") {
249 rotateAngle = lex.getFloat();
250 } else if (token == "rotateOrigin") {
252 rotateOrigin=lex.getString();
253 } else if (token == "subcaption") {
255 } else if (token == "subcaptionText") {
257 subcaptionText = lex.getString();
258 } else if (token == "special") {
260 special = lex.getString();
262 // catch and ignore following two old-format tokens and their arguments.
263 // e.g. "size_kind scale" clashes with the setting of the "scale" keyword.
264 } else if (token == "size_kind" || token == "lyxsize_kind") {
267 } else { // If it's none of the above, it's not ours.
274 grfx::Params InsetGraphicsParams::as_grfxParams(string const & filepath) const
277 pars.filename = filename;
278 pars.scale = lyxscale;
279 pars.angle = rotateAngle;
281 if (!filepath.empty())
282 pars.filename = MakeAbsPath(pars.filename, filepath);
287 // Get the original Bounding Box from the file
288 string const tmp = readBB_from_PSFile(filename);
289 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
291 unsigned int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
292 unsigned int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
294 // new pars.bb values must be >= zero
295 if (pars.bb.xl > bb_orig_xl)
296 pars.bb.xl -= bb_orig_xl;
300 if (pars.bb.xr > bb_orig_xl)
301 pars.bb.xr -= bb_orig_xl;
305 if (pars.bb.yb > bb_orig_yb)
306 pars.bb.yb -= bb_orig_yb;
310 if (pars.bb.yt > bb_orig_yb)
311 pars.bb.yt -= bb_orig_yb;
317 int const width = pars.bb.xr - pars.bb.xl;
318 int const height = pars.bb.yt - pars.bb.yb;
320 if (width < 0 || height < 0) {
329 if (display != grfx::DefaultDisplay)
330 mode = displayTranslator.find(display);
332 mode = displayTranslator.find(lyxrc.display_graphics);
333 pars.display = displayTranslator.find(mode);
335 // Override the above if we're not using a gui
336 if (!lyxrc.use_gui) {
337 pars.display = grfx::NoDisplay;