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