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