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