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_type = DEFAULT_SIZE; // do nothing
100 lyxsize_type = DEFAULT_SIZE; // do nothing
101 keepAspectRatio = false; // only for latex
102 rotate = false; // Rotating
103 rotateOrigin = "center"; // Origin
104 rotateAngle = 0.0; // in degrees
105 special = string(); // userdefined stuff
108 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
110 filename = igp.filename;
114 display = igp.display;
115 subcaption = igp.subcaption;
116 subcaptionText = igp.subcaptionText;
117 noUnzip = igp.noUnzip;
118 keepAspectRatio = igp.keepAspectRatio;
122 size_type = igp.size_type;
123 lyxsize_type = igp.lyxsize_type;
124 lyxwidth = igp.lyxwidth;
125 lyxheight = igp.lyxheight;
126 lyxscale = igp.lyxscale;
128 rotateOrigin = igp.rotateOrigin;
129 rotateAngle = igp.rotateAngle;
130 special = igp.special;
133 bool operator==(InsetGraphicsParams const & left,
134 InsetGraphicsParams const & right)
136 if (left.filename == right.filename &&
137 left.bb == right.bb &&
138 left.draft == right.draft &&
139 left.clip == right.clip &&
140 left.display == right.display &&
141 left.subcaption == right.subcaption &&
142 left.noUnzip == right.noUnzip &&
143 left.subcaptionText == right.subcaptionText &&
144 left.keepAspectRatio == right.keepAspectRatio &&
145 left.width == right.width &&
146 left.height == right.height &&
147 left.scale == right.scale &&
148 left.size_type == right.size_type &&
149 left.lyxsize_type == right.lyxsize_type &&
150 left.lyxwidth == right.lyxwidth &&
151 left.lyxheight == right.lyxheight &&
152 left.lyxscale == right.lyxscale &&
153 left.rotate == right.rotate &&
154 left.rotateOrigin == right.rotateOrigin &&
155 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
156 left.special == right.special)
163 bool operator!=(InsetGraphicsParams const & left,
164 InsetGraphicsParams const & right)
166 return !(left == right);
170 void InsetGraphicsParams::Write(ostream & os) const
172 // If there is no filename, write nothing for it.
173 if (!filename.empty()) {
174 os << "\tfilename " << filename << '\n';
176 if (!bb.empty()) // bounding box
177 os << "\tBoundingBox " << bb << '\n';
178 if (clip) // clip image
180 if (draft) // draft mode
182 // Save the display type for the view inside lyx
183 os << "\tdisplay " << displayTranslator.find(display) << '\n';
184 // Save the subcaption status
186 os << "\tsubcaption\n";
187 if (!subcaptionText.empty())
188 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
191 // we always need the size type
193 // 1: width/height combination
195 os << "\tsize_type " << size_type << '\n';
197 os << "\twidth " << width.asString() << '\n';
199 os << "\theight " << height.asString() << '\n';
201 os << "\tscale " << scale << '\n';
203 os << "\tkeepAspectRatio\n";
206 if (rotateAngle != 0.0)
207 os << "\trotateAngle " << rotateAngle << '\n';
208 if (!rotateOrigin.empty())
209 os << "\trotateOrigin " << rotateOrigin << '\n';
210 if (!special.empty())
211 os << "\tspecial " << special << '\n';
212 // the values for the view in lyx
213 os << "\tlyxsize_type " << lyxsize_type << '\n';
214 if (!lyxwidth.zero()) // the lyx-viewsize
215 os << "\tlyxwidth " << lyxwidth.asString() << '\n';
216 if (!lyxheight.zero())
217 os << "\tlyxheight " << lyxheight.asString();
219 os << "\tlyxscale " << lyxscale << '\n';
223 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token)
225 if (token == "filename") {
227 filename = lex.getString();
228 } else if (token == "BoundingBox") {
229 for (int i=0; i<4 ;i++) {
231 bb += (lex.getString()+" ");
233 } else if (token == "clip") {
235 } else if (token == "draft") {
237 } else if (token == "display") {
239 string const type = lex.getString();
240 display = displayTranslator.find(type);
241 } else if (token == "subcaption") {
243 } else if (token == "subcaptionText") {
245 subcaptionText = lex.getString();
246 } else if (token == "noUnzip") {
248 } else if (token == "size_type") {
250 switch (lex.getInteger()) {
252 size_type = DEFAULT_SIZE;
261 } else if (token == "width") {
263 width = LyXLength(lex.getString());
264 } else if (token == "height") {
266 height = LyXLength(lex.getString());
267 } else if (token == "keepAspectRatio") {
268 keepAspectRatio = true;
269 } else if (token == "scale") {
271 scale = lex.getInteger();
272 } else if (token == "rotate") {
274 } else if (token == "rotateAngle") {
276 rotateAngle = lex.getFloat();
277 } else if (token == "rotateOrigin") {
279 rotateOrigin=lex.getString();
280 } else if (token == "lyxsize_type") {
282 switch (lex.getInteger()) {
284 lyxsize_type = DEFAULT_SIZE;
290 lyxsize_type = SCALE;
293 } else if (token == "lyxwidth") {
295 lyxwidth = LyXLength(lex.getString());
296 } else if (token == "lyxheight") {
298 lyxheight = LyXLength(lex.getString());
299 } else if (token == "lyxscale") {
301 lyxscale = lex.getInteger();
302 } else if (token == "special") {
304 special = lex.getString();
305 } else { // If it's none of the above, its not ours.
312 grfx::Params InsetGraphicsParams::as_grfxParams(string const & filepath) const
319 pars.filename = filename;
321 if (!filepath.empty()) {
322 pars.filename = MakeAbsPath(pars.filename, filepath);
328 // Get the original Bounding Box from the file
329 string const tmp = readBB_from_PSFile(filename);
330 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
332 int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
333 int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
335 pars.bb.xl -= bb_orig_xl;
336 pars.bb.xr -= bb_orig_xl;
337 pars.bb.yb -= bb_orig_yb;
338 pars.bb.yt -= bb_orig_yb;
341 pars.bb.xl = std::max(0, pars.bb.xl);
342 pars.bb.xr = std::max(0, pars.bb.xr);
343 pars.bb.yb = std::max(0, pars.bb.yb);
344 pars.bb.yt = std::max(0, pars.bb.yt);
347 int const width = pars.bb.xr - pars.bb.xl;
348 int const height = pars.bb.yt - pars.bb.yb;
350 if (width < 0 || height < 0) {
359 pars.angle = int(rotateAngle);
361 if (display == InsetGraphicsParams::DEFAULT) {
363 if (lyxrc.display_graphics == "mono")
364 pars.display = grfx::MonochromeDisplay;
365 else if (lyxrc.display_graphics == "gray")
366 pars.display = grfx::GrayscaleDisplay;
367 else if (lyxrc.display_graphics == "color")
368 pars.display = grfx::ColorDisplay;
370 pars.display = grfx::NoDisplay;
372 } else if (display == InsetGraphicsParams::NONE) {
373 pars.display = grfx::NoDisplay;
375 } else if (display == InsetGraphicsParams::MONOCHROME) {
376 pars.display = grfx::MonochromeDisplay;
378 } else if (display == InsetGraphicsParams::GRAYSCALE) {
379 pars.display = grfx::GrayscaleDisplay;
381 } else if (display == InsetGraphicsParams::COLOR) {
382 pars.display = grfx::ColorDisplay;
385 // Override the above if we're not using a gui
386 if (!lyxrc.use_gui) {
387 pars.display = grfx::NoDisplay;
390 if (lyxsize_type == InsetGraphicsParams::SCALE) {
391 pars.scale = lyxscale;
393 } else if (lyxsize_type == InsetGraphicsParams::WH) {
394 if (!lyxwidth.zero())
395 pars.width = lyxwidth.inPixels(1, 1);
396 if (!lyxheight.zero())
397 pars.height = lyxheight.inPixels(1, 1);
399 // inPixels returns a value scaled by lyxrc.zoom.
400 // We want, therefore, to undo this.
401 double const scaling_factor = 100.0 / double(lyxrc.zoom);
402 pars.width = uint(scaling_factor * pars.width);
403 pars.height = uint(scaling_factor * pars.height);