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