]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
Make it compile when USE_BOOST_FORMAT is unset
[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 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "insetgraphicsParams.h"
19
20 #include "graphics/GraphicsParams.h"
21
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"
27 #include "lyxrc.h"
28 #include "debug.h"
29 #include "lyxlex.h"
30
31 using std::ostream;
32
33 InsetGraphicsParams::InsetGraphicsParams()
34 {
35         init();
36 }
37
38 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
39 {
40         // I decided to skip the initialization since the copy will overwrite
41         // everything anyway.
42         //    init();
43         copy(igp);
44 }
45
46 InsetGraphicsParams &
47 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
48 {
49         // Are we assigning the object into itself?
50         if (this == &params)
51                 return * this;
52         copy(params);
53         return *this;
54 }
55
56 void InsetGraphicsParams::init()
57 {
58         filename.erase();
59         lyxscale = 100;                 // lyx scaling in percentage
60         display = grfx::DefaultDisplay; // display mode; see preferences
61         scale = 100.0;                  // output scaling in percentage
62         width = LyXLength();
63         height = LyXLength();
64         keepAspectRatio = false;        // for LaTeX output
65         draft = false;                  // draft mode
66         noUnzip = false;                // unzip files
67
68         bb = string();                  // bounding box
69         clip = false;                   // clip image
70
71         rotateAngle = 0.0;              // angle of rotation in degrees
72         rotateOrigin.erase();           // Origin of rotation
73         subcaption = false;             // subfigure
74         subcaptionText.erase();         // subfigure caption
75         special.erase();                // additional userdefined stuff
76 }
77
78 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
79 {
80         filename = igp.filename;
81         lyxscale = igp.lyxscale;
82         display = igp.display;
83         scale = igp.scale;
84         width = igp.width;
85         height = igp.height;
86         keepAspectRatio = igp.keepAspectRatio;
87         draft = igp.draft;
88         noUnzip = igp.noUnzip;
89
90         bb = igp.bb;
91         clip = igp.clip;
92
93         rotateAngle = igp.rotateAngle;
94         rotateOrigin = igp.rotateOrigin;
95         subcaption = igp.subcaption;
96         subcaptionText = igp.subcaptionText;
97         special = igp.special;
98 }
99
100 bool operator==(InsetGraphicsParams const & left,
101                 InsetGraphicsParams const & right)
102 {
103         if (left.filename == right.filename &&
104             left.lyxscale == right.lyxscale &&
105             left.display == right.display &&
106             left.scale == right.scale &&
107             left.width == right.width &&
108             left.height == right.height &&
109             left.keepAspectRatio == right.keepAspectRatio &&
110             left.draft == right.draft &&
111             left.noUnzip == right.noUnzip &&
112
113
114             left.bb == right.bb &&
115             left.clip == right.clip &&
116
117             lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001) &&
118             left.rotateOrigin == right.rotateOrigin &&
119             left.subcaption == right.subcaption &&
120             left.subcaptionText == right.subcaptionText &&
121             left.special == right.special
122            )
123                 return true;
124
125         return false;
126 }
127
128 bool operator!=(InsetGraphicsParams const & left,
129                 InsetGraphicsParams const & right)
130 {
131         return  !(left == right);
132 }
133
134 void InsetGraphicsParams::Write(ostream & os) const
135 {
136         // Do not write the default values
137
138         if (!filename.empty()) {
139                 os << "\tfilename " << filename << '\n';
140         }
141         if (lyxscale != 100)
142                 os << "\tlyxscale " << lyxscale << '\n';
143         if (display != grfx::DefaultDisplay)
144                 os << "\tdisplay " << grfx::displayTranslator.find(display) << '\n';
145         if (!lyx::float_equal(scale, 0.0, 0.05)) {
146                 if (!lyx::float_equal(scale, 100.0, 0.05))
147                         os << "\tscale " << scale << '\n';
148         } else {
149                 if (!width.zero())
150                         os << "\twidth " << width.asString() << '\n';
151                 if (!height.zero())
152                         os << "\theight " << height.asString() << '\n';
153         }
154
155         if (keepAspectRatio)
156                 os << "\tkeepAspectRatio\n";
157         if (draft)                      // draft mode
158                 os << "\tdraft\n";
159         if (noUnzip)
160                 os << "\tnoUnzip\n";
161
162         if (!bb.empty())                // bounding box
163                 os << "\tBoundingBox " << bb << '\n';
164         if (clip)                       // clip image
165                 os << "\tclip\n";
166
167         if (rotateAngle != 0.0)
168                 os << "\trotateAngle " << rotateAngle << '\n';
169         if (!rotateOrigin.empty())
170                 os << "\trotateOrigin " << rotateOrigin << '\n';
171         if (subcaption)
172                 os << "\tsubcaption\n";
173         if (!subcaptionText.empty())
174                 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
175         if (!special.empty())
176                 os << "\tspecial " << special << '\n';
177 }
178
179
180 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token)
181 {
182         if (token == "filename") {
183                 lex.eatLine();
184                 filename = lex.getString();
185         } else if (token == "lyxscale") {
186                 lex.next();
187                 lyxscale = lex.getInteger();
188         } else if (token == "display") {
189                 lex.next();
190                 string const type = lex.getString();
191                 display = grfx::displayTranslator.find(type);
192         } else if (token == "scale") {
193                 lex.next();
194                 scale = lex.getFloat();
195         } else if (token == "width") {
196                 lex.next();
197                 width = LyXLength(lex.getString());
198                 scale = 0.0;
199         } else if (token == "height") {
200                 lex.next();
201                 height = LyXLength(lex.getString());
202                 scale = 0.0;
203         } else if (token == "keepAspectRatio") {
204                 keepAspectRatio = true;
205         } else if (token == "draft") {
206                 draft = true;
207         } else if (token == "noUnzip") {
208                 noUnzip = true;
209         } else if (token == "BoundingBox") {
210                 bb.erase();
211                 for (int i = 0; i < 4; ++i) {
212                         if (i != 0)
213                                 bb += " ";
214                         lex.next();
215                         bb += lex.getString();
216                 }
217         } else if (token == "clip") {
218                 clip = true;
219         } else if (token == "rotateAngle") {
220                 lex.next();
221                 rotateAngle = lex.getFloat();
222         } else if (token == "rotateOrigin") {
223                 lex.next();
224                 rotateOrigin=lex.getString();
225         } else if (token == "subcaption") {
226                 subcaption = true;
227         } else if (token == "subcaptionText") {
228                 lex.next();
229                 subcaptionText = lex.getString();
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 (!lyxrc.use_gui) {
311                 pars.display = grfx::NoDisplay;
312         }
313
314         return pars;
315 }