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