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"
22 #include "graphics/GraphicsCache.h"
24 #include "support/translator.h"
25 #include "support/filetools.h"
26 #include "support/lyxlib.h"
27 #include "support/LOstream.h"
28 #include "support/LAssert.h"
29 #include "support/lstrings.h"
39 /// This variable keeps a tab on whether the translator was set with the
41 bool translatorsSet = false;
43 /// This is the translator between the Display enum and corresponding lyx
45 Translator< InsetGraphicsParams::DisplayType, string >
46 displayTranslator(InsetGraphicsParams::DEFAULT, "default");
51 InsetGraphicsParams::InsetGraphicsParams()
55 if (! translatorsSet) {
56 translatorsSet = true;
57 // Fill the display translator
58 displayTranslator.addPair(DEFAULT, "default");
59 displayTranslator.addPair(MONOCHROME, "monochrome");
60 displayTranslator.addPair(GRAYSCALE, "grayscale");
61 displayTranslator.addPair(COLOR, "color");
62 displayTranslator.addPair(NONE, "none");
67 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
69 // I decided to skip the initialization since the copy will overwrite
76 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
78 // Are we assigning the object into itself?
85 void InsetGraphicsParams::init()
87 subcaptionText = filename = string();
88 bb = string(); // bounding box
89 draft = false; // draft mode
90 clip = false; // clip image
91 display = DEFAULT; // see pref
92 subcaption = false; // subfigure
93 noUnzip = false; // unzip files
94 width = LyXLength(); // set to 0pt
96 lyxwidth = LyXLength(); // for the view in lyx
97 lyxheight = LyXLength(); // also set to 0pt
98 scale = 0; // unit is %
99 lyxscale = 0; // same for lyxview
100 size_type = DEFAULT_SIZE; // do nothing
101 lyxsize_type = DEFAULT_SIZE; // do nothing
102 keepLyXAspectRatio = false; // only for LyXview
103 keepAspectRatio = false; // only for latex
104 rotate = false; // Rotating
105 rotateOrigin = "center"; // Origin
106 rotateAngle = 0.0; // in degrees
107 special = string(); // userdefined stuff
110 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
112 filename = igp.filename;
116 display = igp.display;
117 subcaption = igp.subcaption;
118 subcaptionText = igp.subcaptionText;
119 noUnzip = igp.noUnzip;
120 keepAspectRatio = igp.keepAspectRatio;
124 size_type = igp.size_type;
125 lyxsize_type = igp.lyxsize_type;
126 lyxwidth = igp.lyxwidth;
127 lyxheight = igp.lyxheight;
128 keepLyXAspectRatio = igp.keepLyXAspectRatio;
129 lyxscale = igp.lyxscale;
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_type == right.size_type &&
152 left.lyxsize_type == right.lyxsize_type &&
153 left.lyxwidth == right.lyxwidth &&
154 left.lyxheight == right.lyxheight &&
155 left.keepLyXAspectRatio == right.keepLyXAspectRatio &&
156 left.lyxscale == right.lyxscale &&
157 left.rotate == right.rotate &&
158 left.rotateOrigin == right.rotateOrigin &&
159 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
160 left.special == right.special)
167 bool operator!=(InsetGraphicsParams const & left,
168 InsetGraphicsParams const & right)
170 return !(left == right);
174 void InsetGraphicsParams::Write(ostream & os) const
176 // If there is no filename, write nothing for it.
177 if (!filename.empty()) {
178 os << "\tfilename " << filename << '\n';
180 if (!bb.empty()) // bounding box
181 os << "\tBoundingBox " << bb << '\n';
182 if (clip) // clip image
184 if (draft) // draft mode
186 // Save the display type for the view inside lyx
187 os << "\tdisplay " << displayTranslator.find(display) << '\n';
188 // Save the subcaption status
190 os << "\tsubcaption\n";
191 if (!subcaptionText.empty())
192 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
195 // we always need the size type
197 // 1: width/height combination
199 os << "\tsize_type " << size_type << '\n';
201 os << "\twidth " << width.asString() << '\n';
203 os << "\theight " << height.asString() << '\n';
205 os << "\tscale " << scale << '\n';
207 os << "\tkeepAspectRatio\n";
210 if (rotateAngle != 0.0)
211 os << "\trotateAngle " << rotateAngle << '\n';
212 if (!rotateOrigin.empty())
213 os << "\trotateOrigin " << rotateOrigin << '\n';
214 if (!special.empty())
215 os << "\tspecial " << special << '\n';
216 // the values for the view in lyx
217 os << "\tlyxsize_type " << lyxsize_type << '\n';
218 if (!lyxwidth.zero()) // the lyx-viewsize
219 os << "\tlyxwidth " << lyxwidth.asString() << '\n';
220 if (!lyxheight.zero())
221 os << "\tlyxheight " << lyxheight.asString();
222 if (keepLyXAspectRatio)
223 os << "\tkeepLyXAspectRatio\n";
225 os << "\tlyxscale " << lyxscale << '\n';
229 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token)
231 if (token == "filename") {
233 filename = lex.getString();
234 } else if (token == "BoundingBox") {
235 for (int i=0; i<4 ;i++) {
237 bb += (lex.getString()+" ");
239 } else if (token == "clip") {
241 } else if (token == "draft") {
243 } else if (token == "display") {
245 string const type = lex.getString();
246 display = displayTranslator.find(type);
247 } else if (token == "subcaption") {
249 } else if (token == "subcaptionText") {
251 subcaptionText = lex.getString();
252 } else if (token == "noUnzip") {
254 } else if (token == "size_type") {
256 switch (lex.getInteger()) {
258 size_type = DEFAULT_SIZE;
267 } else if (token == "width") {
269 width = LyXLength(lex.getString());
270 } else if (token == "height") {
272 height = LyXLength(lex.getString());
273 } else if (token == "keepAspectRatio") {
274 keepAspectRatio = true;
275 } else if (token == "scale") {
277 scale = lex.getInteger();
278 } else if (token == "rotate") {
280 } else if (token == "rotateAngle") {
282 rotateAngle = lex.getFloat();
283 } else if (token == "rotateOrigin") {
285 rotateOrigin=lex.getString();
286 } else if (token == "lyxsize_type") {
288 switch (lex.getInteger()) {
290 lyxsize_type = DEFAULT_SIZE;
296 lyxsize_type = SCALE;
299 } else if (token == "lyxwidth") {
301 lyxwidth = LyXLength(lex.getString());
302 } else if (token == "lyxheight") {
304 lyxheight = LyXLength(lex.getString());
305 } else if (token == "keepLyXAspectRatio") {
306 keepLyXAspectRatio = true;
307 } else if (token == "lyxscale") {
309 lyxscale = lex.getInteger();
310 } else if (token == "special") {
312 special = lex.getString();
313 } else { // If it's none of the above, its not ours.
320 grfx::Params InsetGraphicsParams::as_grfxParams(string const & filepath) const
327 pars.filename = filename;
329 if (!filepath.empty()) {
330 pars.filename = MakeAbsPath(pars.filename, filepath);
336 // Get the original Bounding Box from the file
337 string const tmp = readBB_from_PSFile(filename);
338 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
340 int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
341 int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
343 pars.bb.xl -= bb_orig_xl;
344 pars.bb.xr -= bb_orig_xl;
345 pars.bb.yb -= bb_orig_yb;
346 pars.bb.yt -= bb_orig_yb;
349 pars.bb.xl = std::max(0, pars.bb.xl);
350 pars.bb.xr = std::max(0, pars.bb.xr);
351 pars.bb.yb = std::max(0, pars.bb.yb);
352 pars.bb.yt = std::max(0, pars.bb.yt);
355 int const width = pars.bb.xr - pars.bb.xl;
356 int const height = pars.bb.yt - pars.bb.yb;
358 if (width < 0 || height < 0) {
367 pars.angle = int(rotateAngle);
369 if (display == InsetGraphicsParams::DEFAULT) {
371 if (lyxrc.display_graphics == "mono")
372 pars.display = grfx::MonochromeDisplay;
373 else if (lyxrc.display_graphics == "gray")
374 pars.display = grfx::GrayscaleDisplay;
375 else if (lyxrc.display_graphics == "color")
376 pars.display = grfx::ColorDisplay;
378 pars.display = grfx::NoDisplay;
380 } else if (display == InsetGraphicsParams::NONE) {
381 pars.display = grfx::NoDisplay;
383 } else if (display == InsetGraphicsParams::MONOCHROME) {
384 pars.display = grfx::MonochromeDisplay;
386 } else if (display == InsetGraphicsParams::GRAYSCALE) {
387 pars.display = grfx::GrayscaleDisplay;
389 } else if (display == InsetGraphicsParams::COLOR) {
390 pars.display = grfx::ColorDisplay;
393 // Override the above if we're not using a gui
394 if (!lyxrc.use_gui) {
395 pars.display = grfx::NoDisplay;
398 if (lyxsize_type == InsetGraphicsParams::SCALE) {
399 pars.scale = lyxscale;
401 } else if (lyxsize_type == InsetGraphicsParams::WH) {
402 if (!lyxwidth.zero())
403 pars.width = lyxwidth.inPixels(1, 1);
404 if (!lyxheight.zero())
405 pars.height = lyxheight.inPixels(1, 1);
407 // inPixels returns a value scaled by lyxrc.zoom.
408 // We want, therefore, to undo this.
409 double const scaling_factor = 100.0 / double(lyxrc.zoom);
410 pars.width = uint(scaling_factor * pars.width);
411 pars.height = uint(scaling_factor * pars.height);
414 #warning Angus, could you please adapt this code? (JMarc)
415 if (keepLyXAspectRatio) {
416 // get the imagesize from the cache
417 grfx::Cache & gc = grfx::Cache::get();
418 float const rw = gc.raw_width(filename);
419 float const rh = gc.raw_height(filename);
420 float const ratio = (rw > 0.001) ? rh/rw : 1.0;
421 lyxerr[Debug::GRAPHICS]
422 << "Value of LyXAspectRatio: "
423 << ratio << std::endl;
424 if (!lyxwidth.zero() && !lyxheight.zero()) {
426 height = int(ratio * width);
428 width = int(ratio * height);
429 } else if (lyxwidth.zero())
430 width = int(ratio * height);
431 else if (lyxheight.zero())
432 height = int(ratio * width);