]> git.lyx.org Git - lyx.git/blob - src/insets/InsetGraphicsParams.cpp
Fix bug 1749. CAUTION: lyx2lyx not fully working yet (see FIXMEs).
[lyx.git] / src / insets / InsetGraphicsParams.cpp
1 /**
2  * \file InsetGraphicsParams.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Baruch Even
7  * \author Herbert Voß
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetGraphicsParams.h"
15
16 #include "debug.h"
17 #include "LyX.h" // for use_gui
18 #include "Lexer.h"
19 #include "LyXRC.h"
20
21 #include "graphics/GraphicsParams.h"
22
23 #include "support/convert.h"
24 #include "support/filetools.h"
25 #include "support/lyxlib.h"
26 #include "support/lstrings.h"
27 #include "support/Translator.h"
28
29
30 namespace lyx {
31
32 using support::float_equal;
33 using support::readBB_from_PSFile;
34 using support::token;
35
36 using std::string;
37 using std::ostream;
38
39
40 InsetGraphicsParams::InsetGraphicsParams()
41 {
42         init();
43 }
44
45
46 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
47 {
48         // I decided to skip the initialization since the copy will overwrite
49         // everything anyway.
50         //    init();
51         copy(igp);
52 }
53
54
55 InsetGraphicsParams &
56 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
57 {
58         // Are we assigning the object into itself?
59         if (this == &params)
60                 return *this;
61         copy(params);
62         return *this;
63 }
64
65
66 void InsetGraphicsParams::init()
67 {
68         filename.erase();
69         lyxscale = 100;                 // lyx scaling in percentage
70         display = graphics::DefaultDisplay; // display mode; see preferences
71         scale = string("100");                  // output scaling in percentage
72         width = Length();
73         height = Length();
74         keepAspectRatio = false;        // for LaTeX output
75         draft = false;                  // draft mode
76         noUnzip = false;                // unzip files
77         scaleBeforeRotation = false;    // scale image before rotating
78
79         bb = string();                  // bounding box
80         clip = false;                   // clip image
81
82         rotateAngle = "0";              // angle of rotation in degrees
83         rotateOrigin.erase();           // Origin of rotation
84         subcaption = false;             // subfigure
85         subcaptionText.erase();         // subfigure caption
86         special.erase();                // additional userdefined stuff
87 }
88
89
90 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
91 {
92         filename = igp.filename;
93         lyxscale = igp.lyxscale;
94         display = igp.display;
95         scale = igp.scale;
96         width = igp.width;
97         height = igp.height;
98         keepAspectRatio = igp.keepAspectRatio;
99         draft = igp.draft;
100         noUnzip = igp.noUnzip;
101         scaleBeforeRotation = igp.scaleBeforeRotation;
102
103         bb = igp.bb;
104         clip = igp.clip;
105
106         rotateAngle = igp.rotateAngle;
107         rotateOrigin = igp.rotateOrigin;
108         subcaption = igp.subcaption;
109         subcaptionText = igp.subcaptionText;
110         special = igp.special;
111 }
112
113
114 bool operator==(InsetGraphicsParams const & left,
115                 InsetGraphicsParams const & right)
116 {
117         if (left.filename == right.filename &&
118             left.lyxscale == right.lyxscale &&
119             left.display == right.display &&
120             left.scale == right.scale &&
121             left.width == right.width &&
122             left.height == right.height &&
123             left.keepAspectRatio == right.keepAspectRatio &&
124             left.draft == right.draft &&
125             left.noUnzip == right.noUnzip &&
126             left.scaleBeforeRotation == right.scaleBeforeRotation &&
127
128
129             left.bb == right.bb &&
130             left.clip == right.clip &&
131
132             left.rotateAngle == right.rotateAngle &&
133             left.rotateOrigin == right.rotateOrigin &&
134             left.subcaption == right.subcaption &&
135             left.subcaptionText == right.subcaptionText &&
136             left.special == right.special
137            )
138                 return true;
139
140         return false;
141 }
142
143
144 bool operator!=(InsetGraphicsParams const & left,
145                 InsetGraphicsParams const & right)
146 {
147         return  !(left == right);
148 }
149
150
151 void InsetGraphicsParams::Write(ostream & os, string const & bufpath) const
152 {
153         // Do not write the default values
154
155         if (!filename.empty()) {
156                 os << "\tfilename " << filename.outputFilename(bufpath) << '\n';
157         }
158         if (lyxscale != 100)
159                 os << "\tlyxscale " << lyxscale << '\n';
160         if (display != graphics::DefaultDisplay)
161                 os << "\tdisplay " << graphics::displayTranslator().find(display) << '\n';
162         if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
163                 if (!float_equal(convert<double>(scale), 100.0, 0.05))
164                         os << "\tscale " << scale << '\n';
165         } else {
166                 if (!width.zero())
167                         os << "\twidth " << width.asString() << '\n';
168                 if (!height.zero())
169                         os << "\theight " << height.asString() << '\n';
170         }
171
172         if (keepAspectRatio)
173                 os << "\tkeepAspectRatio\n";
174         if (draft)                      // draft mode
175                 os << "\tdraft\n";
176         if (noUnzip)
177                 os << "\tnoUnzip\n";
178         if (scaleBeforeRotation)
179                 os << "\tscaleBeforeRotation\n";
180
181         if (!bb.empty())                // bounding box
182                 os << "\tBoundingBox " << bb << '\n';
183         if (clip)                       // clip image
184                 os << "\tclip\n";
185
186         if (!rotateAngle.empty()
187                 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
188                 os << "\trotateAngle " << rotateAngle << '\n';
189         if (!rotateOrigin.empty())
190                 os << "\trotateOrigin " << rotateOrigin << '\n';
191         if (subcaption)
192                 os << "\tsubcaption\n";
193         if (!subcaptionText.empty())
194                 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
195         if (!special.empty())
196                 os << "\tspecial " << special << '\n';
197 }
198
199
200 bool InsetGraphicsParams::Read(Lexer & lex, string const & token, string const & bufpath)
201 {
202         if (token == "filename") {
203                 lex.eatLine();
204                 filename.set(lex.getString(), bufpath);
205         } else if (token == "lyxscale") {
206                 lex.next();
207                 lyxscale = lex.getInteger();
208         } else if (token == "display") {
209                 lex.next();
210                 string const type = lex.getString();
211                 display = graphics::displayTranslator().find(type);
212         } else if (token == "scale") {
213                 lex.next();
214                 scale = lex.getString();
215         } else if (token == "width") {
216                 lex.next();
217                 width = Length(lex.getString());
218                 scale = string();
219         } else if (token == "height") {
220                 lex.next();
221                 height = Length(lex.getString());
222                 scale = string();
223         } else if (token == "keepAspectRatio") {
224                 keepAspectRatio = true;
225         } else if (token == "draft") {
226                 draft = true;
227         } else if (token == "noUnzip") {
228                 noUnzip = true;
229         } else if (token == "scaleBeforeRotation") {
230                 scaleBeforeRotation = true;
231         } else if (token == "BoundingBox") {
232                 bb.erase();
233                 for (int i = 0; i < 4; ++i) {
234                         if (i != 0)
235                                 bb += ' ';
236                         lex.next();
237                         bb += lex.getString();
238                 }
239         } else if (token == "clip") {
240                 clip = true;
241         } else if (token == "rotateAngle") {
242                 lex.next();
243                 rotateAngle = lex.getString();
244         } else if (token == "rotateOrigin") {
245                 lex.next();
246                 rotateOrigin=lex.getString();
247         } else if (token == "subcaption") {
248                 subcaption = true;
249         } else if (token == "subcaptionText") {
250                 lex.eatLine();
251                 string sub = lex.getString();
252                 // strip surrounding " "
253                 subcaptionText = sub.substr(1, sub.length() - 2);
254         } else if (token == "special") {
255                 lex.eatLine();
256                 special = lex.getString();
257
258         // catch and ignore following two old-format tokens and their arguments.
259         // e.g. "size_kind scale" clashes with the setting of the
260         // "scale <value>" keyword.
261         } else if (token == "size_kind" || token == "lyxsize_kind") {
262                 lex.next();
263                 lex.getString();
264
265         } else {
266                 // If it's none of the above, it's not ours.
267                 return false;
268         }
269         return true;
270 }
271
272
273 graphics::Params InsetGraphicsParams::as_grfxParams() const
274 {
275         graphics::Params pars;
276         pars.filename = filename;
277         pars.scale = lyxscale;
278         pars.angle = convert<double>(rotateAngle);
279
280         if (clip) {
281                 pars.bb = bb;
282
283                 // Get the original Bounding Box from the file
284                 string const tmp = readBB_from_PSFile(filename);
285                 LYXERR(Debug::GRAPHICS) << "BB_from_File: " << tmp << std::endl;
286                 if (!tmp.empty()) {
287 #ifdef WITH_WARNINGS
288 # warning why not convert to unsigned int? (Lgb)
289 #endif
290                         unsigned int const bb_orig_xl = convert<int>(token(tmp, ' ', 0));
291                         unsigned int const bb_orig_yb = convert<int>(token(tmp, ' ', 1));
292
293                         // new pars.bb values must be >= zero
294                         if  (pars.bb.xl > bb_orig_xl)
295                                 pars.bb.xl -= bb_orig_xl;
296                         else
297                                 pars.bb.xl = 0;
298
299                         if (pars.bb.xr > bb_orig_xl)
300                                 pars.bb.xr -= bb_orig_xl;
301                         else
302                                 pars.bb.xr = 0;
303
304                         if (pars.bb.yb > bb_orig_yb)
305                                 pars.bb.yb -= bb_orig_yb;
306                         else
307                                 pars.bb.yb = 0;
308
309                         if (pars.bb.yt > bb_orig_yb)
310                                 pars.bb.yt -= bb_orig_yb;
311                         else
312                                 pars.bb.yt = 0;
313                 }
314
315                 // Paranoia check.
316                 int const width  = pars.bb.xr - pars.bb.xl;
317                 int const height = pars.bb.yt - pars.bb.yb;
318
319                 if (width  < 0 || height < 0) {
320                         pars.bb.xl = 0;
321                         pars.bb.xr = 0;
322                         pars.bb.yb = 0;
323                         pars.bb.yt = 0;
324                 }
325         }
326
327         if (display == graphics::DefaultDisplay) {
328                 pars.display = lyxrc.display_graphics;
329         } else {
330                 pars.display = display;
331         }
332
333         // Override the above if we're not using a gui
334         if (!use_gui) {
335                 pars.display = graphics::NoDisplay;
336         }
337
338         return pars;
339 }
340
341
342 } // namespace lyx