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