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