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