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