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