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"
203 void InsetGraphicsParams::Write(ostream & os) const
205 // If there is no filename, write nothing for it.
206 if (!filename.empty()) {
207 os << "\tfilename " << filename << '\n';
209 if (!bb.empty()) // bounding box
210 os << "\tBoundingBox " << bb << '\n';
211 if (clip) // clip image
213 if (draft) // draft mode
215 // Save the display type for the view inside lyx
216 os << "\tdisplay " << displayTranslator.find(display) << '\n';
217 // Save the subcaption status
219 os << "\tsubcaption\n";
220 if (!subcaptionText.empty())
221 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
224 os << "\tsize_kind " << getSizeKindStr(size_kind) << '\n';
226 os << "\twidth " << width.asString() << '\n';
228 os << "\theight " << height.asString() << '\n';
230 os << "\tscale " << scale << '\n';
232 os << "\tkeepAspectRatio\n";
235 if (rotateAngle != 0.0)
236 os << "\trotateAngle " << rotateAngle << '\n';
237 if (!rotateOrigin.empty())
238 os << "\trotateOrigin " << rotateOrigin << '\n';
239 if (!special.empty())
240 os << "\tspecial " << special << '\n';
241 // the values for the view in lyx
242 os << "\tlyxsize_kind " << getSizeKindStr(lyxsize_kind) << '\n';
243 if (!lyxwidth.zero()) // the lyx-viewsize
244 os << "\tlyxwidth " << lyxwidth.asString() << '\n';
245 if (!lyxheight.zero())
246 os << "\tlyxheight " << lyxheight.asString();
247 if (keepLyXAspectRatio)
248 os << "\tkeepLyXAspectRatio\n";
250 os << "\tlyxscale " << lyxscale << '\n';
254 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token)
256 if (token == "filename") {
258 filename = lex.getString();
259 } else if (token == "BoundingBox") {
260 for (int i=0; i<4 ;i++) {
262 bb += (lex.getString()+" ");
264 } else if (token == "clip") {
266 } else if (token == "draft") {
268 } else if (token == "display") {
270 string const type = lex.getString();
271 display = displayTranslator.find(type);
272 } else if (token == "subcaption") {
274 } else if (token == "subcaptionText") {
276 subcaptionText = lex.getString();
277 } else if (token == "noUnzip") {
279 } else if (token == "size_kind") {
281 size_kind = getSizeKind(lex.getString());
282 // compatibility-stuff 1.20->1.3.0
283 } else if (token == "size_type") {
285 switch (lex.getInteger()) {
287 size_kind = DEFAULT_SIZE;
296 } else if (token == "width") {
298 width = LyXLength(lex.getString());
299 } else if (token == "height") {
301 height = LyXLength(lex.getString());
302 } else if (token == "keepAspectRatio") {
303 keepAspectRatio = true;
304 } else if (token == "scale") {
306 scale = lex.getInteger();
307 } else if (token == "rotate") {
309 } else if (token == "rotateAngle") {
311 rotateAngle = lex.getFloat();
312 } else if (token == "rotateOrigin") {
314 rotateOrigin=lex.getString();
315 } else if (token == "lyxsize_kind") {
317 lyxsize_kind = getSizeKind(lex.getString());
318 } else if (token == "lyxwidth") {
320 lyxwidth = LyXLength(lex.getString());
321 } else if (token == "lyxheight") {
323 lyxheight = LyXLength(lex.getString());
324 } else if (token == "keepLyXAspectRatio") {
325 keepLyXAspectRatio = true;
326 } else if (token == "lyxscale") {
328 lyxscale = lex.getInteger();
329 } else if (token == "special") {
331 special = lex.getString();
332 } else { // If it's none of the above, its not ours.
339 grfx::Params InsetGraphicsParams::as_grfxParams(string const & filepath) const
345 pars.keepLyXAspectRatio = false;
347 pars.filename = filename;
349 if (!filepath.empty()) {
350 pars.filename = MakeAbsPath(pars.filename, filepath);
356 // Get the original Bounding Box from the file
357 string const tmp = readBB_from_PSFile(filename);
358 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
360 int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
361 int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
363 pars.bb.xl -= bb_orig_xl;
364 pars.bb.xr -= bb_orig_xl;
365 pars.bb.yb -= bb_orig_yb;
366 pars.bb.yt -= bb_orig_yb;
369 pars.bb.xl = std::max(0, pars.bb.xl);
370 pars.bb.xr = std::max(0, pars.bb.xr);
371 pars.bb.yb = std::max(0, pars.bb.yb);
372 pars.bb.yt = std::max(0, pars.bb.yt);
375 int const width = pars.bb.xr - pars.bb.xl;
376 int const height = pars.bb.yt - pars.bb.yb;
378 if (width < 0 || height < 0) {
387 pars.angle = int(rotateAngle);
390 case InsetGraphicsParams::NONE:
391 pars.display = grfx::NoDisplay;
394 case InsetGraphicsParams::MONOCHROME:
395 pars.display = grfx::MonochromeDisplay;
398 case InsetGraphicsParams::GRAYSCALE:
399 pars.display = grfx::GrayscaleDisplay;
401 case InsetGraphicsParams::COLOR:
402 pars.display = grfx::ColorDisplay;
406 if (lyxrc.display_graphics == "mono")
407 pars.display = grfx::MonochromeDisplay;
408 else if (lyxrc.display_graphics == "gray")
409 pars.display = grfx::GrayscaleDisplay;
410 else if (lyxrc.display_graphics == "color")
411 pars.display = grfx::ColorDisplay;
413 pars.display = grfx::NoDisplay;
417 // Override the above if we're not using a gui
418 if (!lyxrc.use_gui) {
419 pars.display = grfx::NoDisplay;
422 if (lyxsize_kind == InsetGraphicsParams::SCALE) {
423 pars.scale = lyxscale;
425 } else if (lyxsize_kind == InsetGraphicsParams::WH) {
426 pars.width = lyxwidth.inBP();
427 pars.height = lyxheight.inBP();
428 pars.keepLyXAspectRatio = keepLyXAspectRatio;