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