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< InsetGraphicsParams::DisplayType, string >
45 displayTranslator(InsetGraphicsParams::DEFAULT, "default");
50 InsetGraphicsParams::InsetGraphicsParams()
54 if (! translatorsSet) {
55 translatorsSet = true;
56 // Fill the display translator
57 displayTranslator.addPair(DEFAULT, "default");
58 displayTranslator.addPair(MONOCHROME, "monochrome");
59 displayTranslator.addPair(GRAYSCALE, "grayscale");
60 displayTranslator.addPair(COLOR, "color");
61 displayTranslator.addPair(NONE, "none");
66 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
68 // I decided to skip the initialization since the copy will overwrite
75 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
77 // Are we assigning the object into itself?
84 void InsetGraphicsParams::init()
86 subcaptionText = filename = string();
87 bb = string(); // bounding box
88 draft = false; // draft mode
89 clip = false; // clip image
90 display = DEFAULT; // see pref
91 subcaption = false; // subfigure
92 noUnzip = false; // unzip files
93 width = LyXLength(); // set to 0pt
95 lyxwidth = LyXLength(); // for the view in lyx
96 lyxheight = LyXLength(); // also set to 0pt
97 scale = 0; // unit is %
98 lyxscale = 0; // same for lyxview
99 size_kind = DEFAULT_SIZE; // do nothing
100 lyxsize_kind = DEFAULT_SIZE; // do nothing
101 keepAspectRatio = false; // for latex
102 keepLyXAspectRatio = false; // for lyx
103 rotate = false; // Rotating
104 rotateOrigin = "leftBaseline"; // Origin
105 rotateAngle = 0.0; // in degrees
106 special = string(); // userdefined stuff
109 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
111 filename = igp.filename;
115 display = igp.display;
116 subcaption = igp.subcaption;
117 subcaptionText = igp.subcaptionText;
118 noUnzip = igp.noUnzip;
119 keepAspectRatio = igp.keepAspectRatio;
123 size_kind = igp.size_kind;
124 lyxsize_kind = igp.lyxsize_kind;
125 lyxwidth = igp.lyxwidth;
126 lyxheight = igp.lyxheight;
127 keepLyXAspectRatio = igp.keepLyXAspectRatio;
128 lyxscale = igp.lyxscale;
129 keepLyXAspectRatio = igp.keepLyXAspectRatio;
131 rotateOrigin = igp.rotateOrigin;
132 rotateAngle = igp.rotateAngle;
133 special = igp.special;
136 bool operator==(InsetGraphicsParams const & left,
137 InsetGraphicsParams const & right)
139 if (left.filename == right.filename &&
140 left.bb == right.bb &&
141 left.draft == right.draft &&
142 left.clip == right.clip &&
143 left.display == right.display &&
144 left.subcaption == right.subcaption &&
145 left.noUnzip == right.noUnzip &&
146 left.subcaptionText == right.subcaptionText &&
147 left.keepAspectRatio == right.keepAspectRatio &&
148 left.width == right.width &&
149 left.height == right.height &&
150 left.scale == right.scale &&
151 left.size_kind == right.size_kind &&
152 left.lyxsize_kind == right.lyxsize_kind &&
153 left.lyxwidth == right.lyxwidth &&
154 left.lyxheight == right.lyxheight &&
155 left.keepLyXAspectRatio == right.keepLyXAspectRatio &&
156 left.lyxscale == right.lyxscale &&
157 left.keepLyXAspectRatio == right.keepLyXAspectRatio &&
158 left.rotate == right.rotate &&
159 left.rotateOrigin == right.rotateOrigin &&
160 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
161 left.special == right.special)
168 bool operator!=(InsetGraphicsParams const & left,
169 InsetGraphicsParams const & right)
171 return !(left == right);
177 InsetGraphicsParams::sizeKind getSizeKind(string const & str_in)
179 if (str_in == "width_height")
180 return InsetGraphicsParams::WH;
181 if (str_in == "scale")
182 return InsetGraphicsParams::SCALE;
184 // all other like "original"
185 return InsetGraphicsParams::DEFAULT_SIZE;
189 string const getSizeKindStr(InsetGraphicsParams::sizeKind sK_in)
191 if (sK_in == InsetGraphicsParams::SCALE)
193 if (sK_in == InsetGraphicsParams::WH)
194 return "width_height";
196 // all other like DEFAULT_SIZE"
200 // compatibility-stuff 1.20->1.3.0
201 InsetGraphicsParams::sizeKind getSizeKind(int type)
205 return InsetGraphicsParams::WH;
208 return InsetGraphicsParams::SCALE;
212 return InsetGraphicsParams::DEFAULT_SIZE;
219 void InsetGraphicsParams::Write(ostream & os) const
221 // If there is no filename, write nothing for it.
222 if (!filename.empty()) {
223 os << "\tfilename " << filename << '\n';
225 if (!bb.empty()) // bounding box
226 os << "\tBoundingBox " << bb << '\n';
227 if (clip) // clip image
229 if (draft) // draft mode
231 // Save the display type for the view inside lyx
232 os << "\tdisplay " << displayTranslator.find(display) << '\n';
233 // Save the subcaption status
235 os << "\tsubcaption\n";
236 if (!subcaptionText.empty())
237 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
240 os << "\tsize_kind " << getSizeKindStr(size_kind) << '\n';
242 os << "\twidth " << width.asString() << '\n';
244 os << "\theight " << height.asString() << '\n';
246 os << "\tscale " << scale << '\n';
248 os << "\tkeepAspectRatio\n";
251 if (rotateAngle != 0.0)
252 os << "\trotateAngle " << rotateAngle << '\n';
253 if (!rotateOrigin.empty())
254 os << "\trotateOrigin " << rotateOrigin << '\n';
255 if (!special.empty())
256 os << "\tspecial " << special << '\n';
257 // the values for the view in lyx
258 os << "\tlyxsize_kind " << getSizeKindStr(lyxsize_kind) << '\n';
259 if (!lyxwidth.zero()) // the lyx-viewsize
260 os << "\tlyxwidth " << lyxwidth.asString() << '\n';
261 if (!lyxheight.zero())
262 os << "\tlyxheight " << lyxheight.asString();
263 if (keepLyXAspectRatio)
264 os << "\tkeepLyXAspectRatio\n";
266 os << "\tlyxscale " << lyxscale << '\n';
270 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token)
272 if (token == "filename") {
274 filename = lex.getString();
275 } else if (token == "BoundingBox") {
276 for (int i=0; i<4 ;i++) {
278 bb += (lex.getString()+" ");
280 } else if (token == "clip") {
282 } else if (token == "draft") {
284 } else if (token == "display") {
286 string const type = lex.getString();
287 display = displayTranslator.find(type);
288 } else if (token == "subcaption") {
290 } else if (token == "subcaptionText") {
292 subcaptionText = lex.getString();
293 } else if (token == "noUnzip") {
295 } else if (token == "size_kind") {
297 size_kind = getSizeKind(lex.getString());
298 // compatibility-stuff 1.20->1.3.0
299 } else if (token == "size_type") {
301 size_kind = getSizeKind(lex.getInteger());
302 } else if (token == "width") {
304 width = LyXLength(lex.getString());
305 } else if (token == "height") {
307 height = LyXLength(lex.getString());
308 } else if (token == "keepAspectRatio") {
309 keepAspectRatio = true;
310 } else if (token == "scale") {
312 scale = lex.getInteger();
313 } else if (token == "rotate") {
315 } else if (token == "rotateAngle") {
317 rotateAngle = lex.getFloat();
318 } else if (token == "rotateOrigin") {
320 rotateOrigin=lex.getString();
321 } else if (token == "lyxsize_kind") {
323 lyxsize_kind = getSizeKind(lex.getString());
324 // compatibility-stuff 1.20->1.3.0
325 } else if (token == "lyxsize_type") {
327 lyxsize_kind = getSizeKind(lex.getInteger());
328 } else if (token == "lyxwidth") {
330 lyxwidth = LyXLength(lex.getString());
331 } else if (token == "lyxheight") {
333 lyxheight = LyXLength(lex.getString());
334 } else if (token == "keepLyXAspectRatio") {
335 keepLyXAspectRatio = true;
336 } else if (token == "lyxscale") {
338 lyxscale = lex.getInteger();
339 } else if (token == "special") {
341 special = lex.getString();
342 } else { // If it's none of the above, its not ours.
349 grfx::Params InsetGraphicsParams::as_grfxParams(string const & filepath) const
355 pars.keepLyXAspectRatio = false;
357 pars.filename = filename;
359 if (!filepath.empty()) {
360 pars.filename = MakeAbsPath(pars.filename, filepath);
366 // Get the original Bounding Box from the file
367 string const tmp = readBB_from_PSFile(filename);
368 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
370 int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
371 int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
373 pars.bb.xl -= bb_orig_xl;
374 pars.bb.xr -= bb_orig_xl;
375 pars.bb.yb -= bb_orig_yb;
376 pars.bb.yt -= bb_orig_yb;
379 pars.bb.xl = std::max(0, pars.bb.xl);
380 pars.bb.xr = std::max(0, pars.bb.xr);
381 pars.bb.yb = std::max(0, pars.bb.yb);
382 pars.bb.yt = std::max(0, pars.bb.yt);
385 int const width = pars.bb.xr - pars.bb.xl;
386 int const height = pars.bb.yt - pars.bb.yb;
388 if (width < 0 || height < 0) {
397 pars.angle = int(rotateAngle);
400 case InsetGraphicsParams::NONE:
401 pars.display = grfx::NoDisplay;
404 case InsetGraphicsParams::MONOCHROME:
405 pars.display = grfx::MonochromeDisplay;
408 case InsetGraphicsParams::GRAYSCALE:
409 pars.display = grfx::GrayscaleDisplay;
411 case InsetGraphicsParams::COLOR:
412 pars.display = grfx::ColorDisplay;
416 if (lyxrc.display_graphics == "mono")
417 pars.display = grfx::MonochromeDisplay;
418 else if (lyxrc.display_graphics == "gray")
419 pars.display = grfx::GrayscaleDisplay;
420 else if (lyxrc.display_graphics == "color")
421 pars.display = grfx::ColorDisplay;
423 pars.display = grfx::NoDisplay;
427 // Override the above if we're not using a gui
428 if (!lyxrc.use_gui) {
429 pars.display = grfx::NoDisplay;
432 if (lyxsize_kind == InsetGraphicsParams::SCALE) {
433 pars.scale = lyxscale;
435 } else if (lyxsize_kind == InsetGraphicsParams::WH) {
436 pars.width = lyxwidth.inBP();
437 pars.height = lyxheight.inBP();
438 pars.keepLyXAspectRatio = keepLyXAspectRatio;