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