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