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