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 "support/translator.h"
22 #include "support/filetools.h"
23 #include "support/lyxlib.h"
24 #include "support/LOstream.h"
25 #include "support/LAssert.h"
26 #include "support/lstrings.h"
36 /// This variable keeps a tab on whether the translator was set with the
38 bool translatorsSet = false;
40 /// This is the translator between the Display enum and corresponding lyx
42 Translator< InsetGraphicsParams::DisplayType, string >
43 displayTranslator(InsetGraphicsParams::DEFAULT, "default");
48 InsetGraphicsParams::InsetGraphicsParams()
52 if (! translatorsSet) {
53 translatorsSet = true;
54 // Fill the display translator
55 displayTranslator.addPair(DEFAULT, "default");
56 displayTranslator.addPair(MONOCHROME, "monochrome");
57 displayTranslator.addPair(GRAYSCALE, "grayscale");
58 displayTranslator.addPair(COLOR, "color");
59 displayTranslator.addPair(NONE, "none");
64 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
66 // I decided to skip the initialization since the copy will overwrite
73 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
75 // Are we assigning the object into itself?
82 void InsetGraphicsParams::init()
84 subcaptionText = filename = string();
85 bb = string(); // bounding box
86 draft = false; // draft mode
87 clip = false; // clip image
88 display = DEFAULT; // see pref
89 subcaption = false; // subfigure
90 noUnzip = false; // unzip files
91 width = LyXLength(); // set to 0pt
93 lyxwidth = LyXLength(); // for the view in lyx
94 lyxheight = LyXLength(); // also set to 0pt
95 scale = 0; // unit is %
96 lyxscale = 0; // same for lyxview
97 size_type = DEFAULT_SIZE; // do nothing
98 lyxsize_type = DEFAULT_SIZE; // do nothing
99 keepAspectRatio = false; // only for latex
100 rotate = false; // Rotating
101 rotateOrigin = "center"; // Origin
102 rotateAngle = 0.0; // in degrees
103 special = string(); // userdefined stuff
106 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
108 filename = igp.filename;
112 display = igp.display;
113 subcaption = igp.subcaption;
114 subcaptionText = igp.subcaptionText;
115 noUnzip = igp.noUnzip;
116 keepAspectRatio = igp.keepAspectRatio;
120 size_type = igp.size_type;
121 lyxsize_type = igp.lyxsize_type;
122 lyxwidth = igp.lyxwidth;
123 lyxheight = igp.lyxheight;
124 lyxscale = igp.lyxscale;
126 rotateOrigin = igp.rotateOrigin;
127 rotateAngle = igp.rotateAngle;
128 special = igp.special;
131 bool operator==(InsetGraphicsParams const & left,
132 InsetGraphicsParams const & right)
134 if (left.filename == right.filename &&
135 left.bb == right.bb &&
136 left.draft == right.draft &&
137 left.clip == right.clip &&
138 left.display == right.display &&
139 left.subcaption == right.subcaption &&
140 left.noUnzip == right.noUnzip &&
141 left.subcaptionText == right.subcaptionText &&
142 left.keepAspectRatio == right.keepAspectRatio &&
143 left.width == right.width &&
144 left.height == right.height &&
145 left.scale == right.scale &&
146 left.size_type == right.size_type &&
147 left.lyxsize_type == right.lyxsize_type &&
148 left.lyxwidth == right.lyxwidth &&
149 left.lyxheight == right.lyxheight &&
150 left.lyxscale == right.lyxscale &&
151 left.rotate == right.rotate &&
152 left.rotateOrigin == right.rotateOrigin &&
153 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
154 left.special == right.special)
161 bool operator!=(InsetGraphicsParams const & left,
162 InsetGraphicsParams const & right)
164 return !(left == right);
168 void InsetGraphicsParams::Write(ostream & os) const
170 // If there is no filename, write nothing for it.
171 if (!filename.empty()) {
172 os << "\tfilename " << filename << '\n';
174 if (!bb.empty()) // bounding box
175 os << "\tBoundingBox " << bb << '\n';
176 if (clip) // clip image
178 if (draft) // draft mode
180 // Save the display type for the view inside lyx
181 os << "\tdisplay " << displayTranslator.find(display) << '\n';
182 // Save the subcaption status
184 os << "\tsubcaption\n";
185 if (!subcaptionText.empty())
186 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
189 // we always need the size type
191 // 1: width/height combination
193 os << "\tsize_type " << size_type << '\n';
195 os << "\twidth " << width.asString() << '\n';
197 os << "\theight " << height.asString() << '\n';
199 os << "\tscale " << scale << '\n';
201 os << "\tkeepAspectRatio\n";
204 if (rotateAngle != 0.0)
205 os << "\trotateAngle " << rotateAngle << '\n';
206 if (!rotateOrigin.empty())
207 os << "\trotateOrigin " << rotateOrigin << '\n';
208 if (!special.empty())
209 os << "\tspecial " << special << '\n';
210 // the values for the view in lyx
211 os << "\tlyxsize_type " << lyxsize_type << '\n';
212 if (!lyxwidth.zero()) // the lyx-viewsize
213 os << "\tlyxwidth " << lyxwidth.asString() << '\n';
214 if (!lyxheight.zero())
215 os << "\tlyxheight " << lyxheight.asString();
217 os << "\tlyxscale " << lyxscale << '\n';
221 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token)
223 if (token == "filename") {
225 filename = lex.getString();
226 } else if (token == "BoundingBox") {
227 for (int i=0; i<4 ;i++) {
229 bb += (lex.getString()+" ");
231 } else if (token == "clip") {
233 } else if (token == "draft") {
235 } else if (token == "display") {
237 string const type = lex.getString();
238 display = displayTranslator.find(type);
239 } else if (token == "subcaption") {
241 } else if (token == "subcaptionText") {
243 subcaptionText = lex.getString();
244 } else if (token == "noUnzip") {
246 } else if (token == "size_type") {
248 switch (lex.getInteger()) {
250 size_type = DEFAULT_SIZE;
259 } else if (token == "width") {
261 width = LyXLength(lex.getString());
262 } else if (token == "height") {
264 height = LyXLength(lex.getString());
265 } else if (token == "keepAspectRatio") {
266 keepAspectRatio = true;
267 } else if (token == "scale") {
269 scale = lex.getInteger();
270 } else if (token == "rotate") {
272 } else if (token == "rotateAngle") {
274 rotateAngle = lex.getFloat();
275 } else if (token == "rotateOrigin") {
277 rotateOrigin=lex.getString();
278 } else if (token == "lyxsize_type") {
280 switch (lex.getInteger()) {
282 lyxsize_type = DEFAULT_SIZE;
288 lyxsize_type = SCALE;
291 } else if (token == "lyxwidth") {
293 lyxwidth = LyXLength(lex.getString());
294 } else if (token == "lyxheight") {
296 lyxheight = LyXLength(lex.getString());
297 } else if (token == "lyxscale") {
299 lyxscale = lex.getInteger();
300 } else if (token == "special") {
302 special = lex.getString();
303 } else { // If it's none of the above, its not ours.
310 grfx::GParams InsetGraphicsParams::asGParams(string const & filepath) const
317 pars.filename = filename;
319 if (!filepath.empty()) {
320 pars.filename = MakeAbsPath(pars.filename, filepath);
326 // Get the original Bounding Box from the file
327 string const tmp = readBB_from_PSFile(filename);
328 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
330 int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
331 int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
333 pars.bb.xl -= bb_orig_xl;
334 pars.bb.xr -= bb_orig_xl;
335 pars.bb.yb -= bb_orig_yb;
336 pars.bb.yt -= bb_orig_yb;
339 pars.bb.xl = std::max(0, pars.bb.xl);
340 pars.bb.xr = std::max(0, pars.bb.xr);
341 pars.bb.yb = std::max(0, pars.bb.yb);
342 pars.bb.yt = std::max(0, pars.bb.yt);
345 int const width = pars.bb.xr - pars.bb.xl;
346 int const height = pars.bb.yt - pars.bb.yb;
348 if (width < 0 || height < 0) {
357 pars.angle = int(rotateAngle);
359 if (display == InsetGraphicsParams::DEFAULT) {
361 if (lyxrc.display_graphics == "mono")
362 pars.display = grfx::MonochromeDisplay;
363 else if (lyxrc.display_graphics == "gray")
364 pars.display = grfx::GrayscaleDisplay;
365 else if (lyxrc.display_graphics == "color")
366 pars.display = grfx::ColorDisplay;
368 pars.display = grfx::NoDisplay;
370 } else if (display == InsetGraphicsParams::NONE) {
371 pars.display = grfx::NoDisplay;
373 } else if (display == InsetGraphicsParams::MONOCHROME) {
374 pars.display = grfx::MonochromeDisplay;
376 } else if (display == InsetGraphicsParams::GRAYSCALE) {
377 pars.display = grfx::GrayscaleDisplay;
379 } else if (display == InsetGraphicsParams::COLOR) {
380 pars.display = grfx::ColorDisplay;
383 // Override the above if we're not using a gui
384 if (!lyxrc.use_gui) {
385 pars.display = grfx::NoDisplay;
388 if (lyxsize_type == InsetGraphicsParams::SCALE) {
389 pars.scale = lyxscale;
391 } else if (lyxsize_type == InsetGraphicsParams::WH) {
392 if (!lyxwidth.zero())
393 pars.width = lyxwidth.inPixels(1, 1);
394 if (!lyxheight.zero())
395 pars.height = lyxheight.inPixels(1, 1);
397 // inPixels returns a value scaled by lyxrc.zoom.
398 // We want, therefore, to undo this.
399 double const scaling_factor = 100.0 / double(lyxrc.zoom);
400 pars.width = uint(scaling_factor * pars.width);
401 pars.height = uint(scaling_factor * pars.height);