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