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