]> git.lyx.org Git - lyx.git/blob - src/insets/InsetGraphicsParams.cpp
Andre's s/getTextClass/textClass/ cleanup.
[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 "LyX.h" // for use_gui
17 #include "Lexer.h"
18 #include "LyXRC.h"
19 #include "Buffer.h"
20
21 #include "graphics/GraphicsParams.h"
22 #include "graphics/GraphicsTypes.h"
23
24 #include "support/convert.h"
25 #include "support/debug.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 << "\tembed " << (filename.embedded() ? filename.inzipName() : "\"\"") << '\n';
149         }
150         if (lyxscale != 100)
151                 os << "\tlyxscale " << lyxscale << '\n';
152         if (display != graphics::DefaultDisplay)
153                 os << "\tdisplay " << graphics::displayTranslator().find(display) << '\n';
154         if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
155                 if (!float_equal(convert<double>(scale), 100.0, 0.05))
156                         os << "\tscale " << scale << '\n';
157         } else {
158                 if (!width.zero())
159                         os << "\twidth " << width.asString() << '\n';
160                 if (!height.zero())
161                         os << "\theight " << height.asString() << '\n';
162         }
163
164         if (keepAspectRatio)
165                 os << "\tkeepAspectRatio\n";
166         if (draft)                      // draft mode
167                 os << "\tdraft\n";
168         if (noUnzip)
169                 os << "\tnoUnzip\n";
170         if (scaleBeforeRotation)
171                 os << "\tscaleBeforeRotation\n";
172
173         if (!bb.empty())                // bounding box
174                 os << "\tBoundingBox " << bb << '\n';
175         if (clip)                       // clip image
176                 os << "\tclip\n";
177
178         if (!rotateAngle.empty()
179                 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
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(Lexer & 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 == "inzipName") {
198                 // this option is currently ignored because only files in
199                 // or under current document path is embeddable, and their
200                 // inzipName is automatically determined.
201                 lex.eatLine();
202         } else if (token == "embed") {
203                 lex.next();
204                 string const name = lex.getString();
205                 filename.setInzipName(name);
206                 filename.setEmbed(!name.empty());
207         } else if (token == "lyxscale") {
208                 lex.next();
209                 lyxscale = lex.getInteger();
210         } else if (token == "display") {
211                 lex.next();
212                 string const type = lex.getString();
213                 display = graphics::displayTranslator().find(type);
214         } else if (token == "scale") {
215                 lex.next();
216                 scale = lex.getString();
217         } else if (token == "width") {
218                 lex.next();
219                 width = Length(lex.getString());
220                 scale = string();
221         } else if (token == "height") {
222                 lex.next();
223                 height = Length(lex.getString());
224                 scale = string();
225         } else if (token == "keepAspectRatio") {
226                 keepAspectRatio = true;
227         } else if (token == "draft") {
228                 draft = true;
229         } else if (token == "noUnzip") {
230                 noUnzip = true;
231         } else if (token == "scaleBeforeRotation") {
232                 scaleBeforeRotation = true;
233         } else if (token == "BoundingBox") {
234                 bb.erase();
235                 for (int i = 0; i < 4; ++i) {
236                         if (i != 0)
237                                 bb += ' ';
238                         lex.next();
239                         bb += lex.getString();
240                 }
241         } else if (token == "clip") {
242                 clip = true;
243         } else if (token == "rotateAngle") {
244                 lex.next();
245                 rotateAngle = lex.getString();
246         } else if (token == "rotateOrigin") {
247                 lex.next();
248                 rotateOrigin=lex.getString();
249         } else if (token == "subcaption") {
250                 subcaption = true;
251         } else if (token == "subcaptionText") {
252                 lex.eatLine();
253                 string sub = lex.getString();
254                 // strip surrounding " "
255                 subcaptionText = sub.substr(1, sub.length() - 2);
256         } else if (token == "special") {
257                 lex.eatLine();
258                 special = lex.getString();
259
260         // catch and ignore following two old-format tokens and their arguments.
261         // e.g. "size_kind scale" clashes with the setting of the
262         // "scale <value>" keyword.
263         } else if (token == "size_kind" || token == "lyxsize_kind") {
264                 lex.next();
265                 lex.getString();
266
267         } else {
268                 // If it's none of the above, it's not ours.
269                 return false;
270         }
271         return true;
272 }
273
274
275 graphics::Params InsetGraphicsParams::as_grfxParams() const
276 {
277         graphics::Params pars;
278         pars.filename = filename.availableFile();
279         pars.icon = filename.embedded() ? "pin.png" : "";
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