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