]> git.lyx.org Git - lyx.git/blob - src/insets/InsetGraphicsParams.cpp
Fix GRAPHICS_EDIT of InsetGraphics
[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 "LyX.h" // for use_gui
17 #include "Lexer.h"
18 #include "LyXRC.h"
19 #include "Buffer.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 = graphics::DefaultDisplay; // display mode; see 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 }
83
84
85 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
86 {
87         filename = igp.filename;
88         lyxscale = igp.lyxscale;
89         display = igp.display;
90         scale = igp.scale;
91         width = igp.width;
92         height = igp.height;
93         keepAspectRatio = igp.keepAspectRatio;
94         draft = igp.draft;
95         noUnzip = igp.noUnzip;
96         scaleBeforeRotation = igp.scaleBeforeRotation;
97
98         bb = igp.bb;
99         clip = igp.clip;
100
101         rotateAngle = igp.rotateAngle;
102         rotateOrigin = igp.rotateOrigin;
103         special = igp.special;
104 }
105
106
107 bool operator==(InsetGraphicsParams const & left,
108                 InsetGraphicsParams const & right)
109 {
110         return left.filename == right.filename &&
111             left.lyxscale == right.lyxscale &&
112             left.display == right.display &&
113             left.scale == right.scale &&
114             left.width == right.width &&
115             left.height == right.height &&
116             left.keepAspectRatio == right.keepAspectRatio &&
117             left.draft == right.draft &&
118             left.noUnzip == right.noUnzip &&
119             left.scaleBeforeRotation == right.scaleBeforeRotation &&
120
121             left.bb == right.bb &&
122             left.clip == right.clip &&
123
124             left.rotateAngle == right.rotateAngle &&
125             left.rotateOrigin == right.rotateOrigin &&
126             left.special == right.special;
127 }
128
129
130 bool operator!=(InsetGraphicsParams const & left,
131                 InsetGraphicsParams const & right)
132 {
133         return  !(left == right);
134 }
135
136
137 void InsetGraphicsParams::Write(ostream & os, Buffer const & buffer) const
138 {
139         // Do not write the default values
140         if (!filename.empty()) {
141                 os << "\tfilename " << filename.outputFilename(buffer.filePath()) << '\n';
142                 os << "\tembed " << (filename.embedded() ? filename.inzipName() : "\"\"") << '\n';
143         }
144         if (lyxscale != 100)
145                 os << "\tlyxscale " << lyxscale << '\n';
146         if (display != graphics::DefaultDisplay)
147                 os << "\tdisplay " << graphics::displayTranslator().find(display) << '\n';
148         if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
149                 if (!float_equal(convert<double>(scale), 100.0, 0.05))
150                         os << "\tscale " << scale << '\n';
151         } else {
152                 if (!width.zero())
153                         os << "\twidth " << width.asString() << '\n';
154                 if (!height.zero())
155                         os << "\theight " << height.asString() << '\n';
156         }
157
158         if (keepAspectRatio)
159                 os << "\tkeepAspectRatio\n";
160         if (draft)                      // draft mode
161                 os << "\tdraft\n";
162         if (noUnzip)
163                 os << "\tnoUnzip\n";
164         if (scaleBeforeRotation)
165                 os << "\tscaleBeforeRotation\n";
166
167         if (!bb.empty())                // bounding box
168                 os << "\tBoundingBox " << bb << '\n';
169         if (clip)                       // clip image
170                 os << "\tclip\n";
171
172         if (!rotateAngle.empty()
173                 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
174                 os << "\trotateAngle " << rotateAngle << '\n';
175         if (!rotateOrigin.empty())
176                 os << "\trotateOrigin " << rotateOrigin << '\n';
177         if (!special.empty())
178                 os << "\tspecial " << special << '\n';
179 }
180
181
182 bool InsetGraphicsParams::Read(Lexer & lex, string const & token, string const & bufpath)
183 {
184         if (token == "filename") {
185                 lex.eatLine();
186                 filename.set(lex.getString(), bufpath);
187         } else if (token == "embed") {
188                 lex.next();
189                 string const name = lex.getString();
190                 filename.setInzipName(name);
191                 filename.setEmbed(!name.empty());
192         } else if (token == "lyxscale") {
193                 lex.next();
194                 lyxscale = lex.getInteger();
195         } else if (token == "display") {
196                 lex.next();
197                 string const type = lex.getString();
198                 display = graphics::displayTranslator().find(type);
199         } else if (token == "scale") {
200                 lex.next();
201                 scale = lex.getString();
202         } else if (token == "width") {
203                 lex.next();
204                 width = Length(lex.getString());
205                 scale = string();
206         } else if (token == "height") {
207                 lex.next();
208                 height = Length(lex.getString());
209                 scale = string();
210         } else if (token == "keepAspectRatio") {
211                 keepAspectRatio = true;
212         } else if (token == "draft") {
213                 draft = true;
214         } else if (token == "noUnzip") {
215                 noUnzip = true;
216         } else if (token == "scaleBeforeRotation") {
217                 scaleBeforeRotation = true;
218         } else if (token == "BoundingBox") {
219                 bb.erase();
220                 for (int i = 0; i < 4; ++i) {
221                         if (i != 0)
222                                 bb += ' ';
223                         lex.next();
224                         bb += lex.getString();
225                 }
226         } else if (token == "clip") {
227                 clip = true;
228         } else if (token == "rotateAngle") {
229                 lex.next();
230                 rotateAngle = lex.getString();
231         } else if (token == "rotateOrigin") {
232                 lex.next();
233                 rotateOrigin=lex.getString();
234         } else if (token == "special") {
235                 lex.eatLine();
236                 special = lex.getString();
237
238         // catch and ignore following two old-format tokens and their arguments.
239         // e.g. "size_kind scale" clashes with the setting of the
240         // "scale <value>" keyword.
241         } else if (token == "size_kind" || token == "lyxsize_kind") {
242                 lex.next();
243                 lex.getString();
244
245         } else {
246                 // If it's none of the above, it's not ours.
247                 return false;
248         }
249         return true;
250 }
251
252
253 graphics::Params InsetGraphicsParams::as_grfxParams() const
254 {
255         graphics::Params pars;
256         pars.filename = filename.availableFile();
257         pars.icon = filename.embedded() ? "pin.png" : "";
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         if (display == graphics::DefaultDisplay) {
307                 pars.display = graphics::DisplayType(lyxrc.display_graphics);
308         } else {
309                 pars.display = display;
310         }
311
312         // Override the above if we're not using a gui
313         if (!use_gui)
314                 pars.display = graphics::NoDisplay;
315
316         return pars;
317 }
318
319
320 } // namespace lyx