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