]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
43d2057e65d5e9b03f0d80e2efbd3e94cadcb262
[lyx.git] / src / insets / insetgraphicsParams.C
1 /* This file is part of
2  * =================================================
3  *
4  *          LyX, The Document Processor
5  *          Copyright 1995 Matthias Ettrich.
6  *          Copyright 1995-2001 The LyX Team.
7  *
8  * \author Baruch Even
9  * \author Herbert Voss <voss@lyx.org>
10  *
11  * ================================================= */
12
13 #include <config.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include "insetgraphicsParams.h"
20
21 #include "support/translator.h"
22 #include "support/filetools.h"
23 #include "support/lyxlib.h"
24 #include "support/LOstream.h"
25 #include "support/LAssert.h"
26 #include "support/lstrings.h"
27 #include "lyxrc.h"
28 #include "debug.h"
29
30
31 using std::ostream;
32
33
34 namespace {
35
36 /// This variable keeps a tab on whether the translator was set with the
37 /// translations.
38 bool translatorsSet = false;
39
40 /// This is the translator between the Display enum and corresponding lyx
41 /// file strings.
42 Translator< InsetGraphicsParams::DisplayType, string >
43 displayTranslator(InsetGraphicsParams::DEFAULT, "default");
44
45 } // namespace anon
46
47
48 InsetGraphicsParams::InsetGraphicsParams()
49 {
50         init();
51         // Set translators
52         if (! translatorsSet) {
53                 translatorsSet = true;
54                 // Fill the display translator
55                 displayTranslator.addPair(DEFAULT, "default");
56                 displayTranslator.addPair(MONOCHROME, "monochrome");
57                 displayTranslator.addPair(GRAYSCALE, "grayscale");
58                 displayTranslator.addPair(COLOR, "color");
59                 displayTranslator.addPair(NONE, "none");
60         }
61 }
62
63
64 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
65 {
66         // I decided to skip the initialization since the copy will overwrite
67         // everything anyway.
68         //    init();
69         copy(igp);
70 }
71
72 InsetGraphicsParams &
73 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
74 {
75         // Are we assigning the object into itself?
76         if (this == &params)
77                 return * this;
78         copy(params);
79         return *this;
80 }
81
82 void InsetGraphicsParams::init()
83 {
84         subcaptionText = filename = string();
85         bb = string();                  // bounding box
86         draft = false;                  // draft mode
87         clip = false;                   // clip image
88         display = DEFAULT;              // see pref
89         subcaption = false;             // subfigure
90         noUnzip = false;                // unzip files
91         width = LyXLength();            // set to 0pt
92         height = LyXLength();
93         lyxwidth = LyXLength();         // for the view in lyx
94         lyxheight = LyXLength();        // also set to 0pt
95         scale = 0;                      // unit is %
96         lyxscale = 0;                   // same for lyxview
97         size_type = DEFAULT_SIZE;       // do nothing
98         lyxsize_type = DEFAULT_SIZE;    // do nothing
99         keepAspectRatio = false;        // only for latex
100         rotate = false;                 // Rotating
101         rotateOrigin = "center";        // Origin
102         rotateAngle = 0.0;              // in degrees
103         special = string();             // userdefined stuff
104 }
105
106 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
107 {
108         filename = igp.filename;
109         bb = igp.bb;
110         draft = igp.draft;
111         clip = igp.clip;
112         display = igp.display;
113         subcaption = igp.subcaption;
114         subcaptionText = igp.subcaptionText;
115         noUnzip = igp.noUnzip;
116         keepAspectRatio = igp.keepAspectRatio;
117         width = igp.width;
118         height = igp.height;
119         scale = igp.scale;
120         size_type = igp.size_type;
121         lyxsize_type = igp.lyxsize_type;
122         lyxwidth = igp.lyxwidth;
123         lyxheight = igp.lyxheight;
124         lyxscale = igp.lyxscale;
125         rotate = igp.rotate;
126         rotateOrigin = igp.rotateOrigin;
127         rotateAngle = igp.rotateAngle;
128         special = igp.special;
129 }
130
131 bool operator==(InsetGraphicsParams const & left,
132                 InsetGraphicsParams const & right)
133 {
134         if (left.filename == right.filename &&
135             left.bb == right.bb &&
136             left.draft == right.draft &&
137             left.clip == right.clip &&
138             left.display == right.display &&
139             left.subcaption == right.subcaption &&
140             left.noUnzip == right.noUnzip &&
141             left.subcaptionText == right.subcaptionText &&
142             left.keepAspectRatio == right.keepAspectRatio &&
143             left.width == right.width &&
144             left.height == right.height &&
145             left.scale == right.scale &&
146             left.size_type == right.size_type &&
147             left.lyxsize_type == right.lyxsize_type &&
148             left.lyxwidth == right.lyxwidth &&
149             left.lyxheight == right.lyxheight &&
150             left.lyxscale == right.lyxscale &&
151             left.rotate == right.rotate &&
152             left.rotateOrigin == right.rotateOrigin &&
153             lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
154                              left.special == right.special)
155                 )
156                 return true;
157
158         return false;
159 }
160
161 bool operator!=(InsetGraphicsParams const & left,
162                 InsetGraphicsParams const & right)
163 {
164         return  !(left == right);
165 }
166
167
168 void InsetGraphicsParams::Write(ostream & os) const
169 {
170         // If there is no filename, write nothing for it.
171         if (!filename.empty()) {
172                 os << "\tfilename " << filename << '\n';
173         }
174         if (!bb.empty())                // bounding box
175                 os << "\tBoundingBox " << bb << '\n';
176         if (clip)                       // clip image
177                 os << "\tclip\n";
178         if (draft)                      // draft mode
179                 os << "\tdraft\n";
180         // Save the display type for the view inside lyx
181         os << "\tdisplay " << displayTranslator.find(display) << '\n';
182         // Save the subcaption status
183         if (subcaption)
184                 os << "\tsubcaption\n";
185         if (!subcaptionText.empty())
186                 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
187         if (noUnzip)
188                 os << "\tnoUnzip\n";
189         // we always need the size type
190         // 0: no special
191         // 1: width/height combination
192         // 2: scale
193         os << "\tsize_type " <<  size_type << '\n';
194         if (!width.zero())
195                 os << "\twidth " << width.asString() << '\n';
196         if (!height.zero())
197                 os << "\theight " << height.asString() << '\n';
198         if (scale != 0)
199                 os << "\tscale " << scale << '\n';
200         if (keepAspectRatio)
201                 os << "\tkeepAspectRatio\n";
202         if (rotate)
203                 os << "\trotate\n";
204         if (rotateAngle != 0.0)
205                 os << "\trotateAngle " << rotateAngle << '\n';
206         if (!rotateOrigin.empty())
207                 os << "\trotateOrigin " << rotateOrigin << '\n';
208         if (!special.empty())
209                 os << "\tspecial " << special << '\n';
210         // the values for the view in lyx
211         os << "\tlyxsize_type " <<  lyxsize_type << '\n';
212         if (!lyxwidth.zero())           // the lyx-viewsize
213                 os << "\tlyxwidth " << lyxwidth.asString() << '\n';
214         if (!lyxheight.zero())
215                 os << "\tlyxheight " << lyxheight.asString();
216         if (lyxscale != 0)
217                 os << "\tlyxscale " << lyxscale << '\n';
218 }
219
220
221 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token)
222 {
223         if (token == "filename") {
224                 lex.eatLine();
225                 filename = lex.getString();
226         } else if (token == "BoundingBox") {
227                 for (int i=0; i<4 ;i++) {
228                         lex.next();
229                         bb += (lex.getString()+" ");
230                 }
231         } else if (token == "clip") {
232                 clip = true;
233         } else if (token == "draft") {
234                 draft = true;
235         } else if (token == "display") {
236                 lex.next();
237                 string const type = lex.getString();
238                 display = displayTranslator.find(type);
239         } else if (token == "subcaption") {
240                 subcaption = true;
241         } else if (token == "subcaptionText") {
242                 lex.next();
243                 subcaptionText = lex.getString();
244         } else if (token == "noUnzip") {
245                 noUnzip = true;
246         } else if (token == "size_type") {
247                 lex.next();
248                 switch (lex.getInteger()) {
249                 case 0:
250                         size_type = DEFAULT_SIZE;
251                         break;
252                 case 1:
253                         size_type = WH;
254                         break;
255                 case 2:
256                         size_type = SCALE;
257                         break;
258                 }
259         } else if (token == "width") {
260                 lex.next();
261                 width = LyXLength(lex.getString());
262         } else if (token == "height") {
263                 lex.next();
264                 height = LyXLength(lex.getString());
265         } else if (token == "keepAspectRatio") {
266                 keepAspectRatio = true;
267         } else if (token == "scale") {
268                 lex.next();
269                 scale = lex.getInteger();
270         } else if (token == "rotate") {
271                 rotate = true;
272         } else if (token == "rotateAngle") {
273                 lex.next();
274                 rotateAngle = lex.getFloat();
275         } else if (token == "rotateOrigin") {
276                 lex.next();
277                 rotateOrigin=lex.getString();
278         } else if (token == "lyxsize_type") {
279                 lex.next();
280                 switch (lex.getInteger()) {
281                 case 0:
282                         lyxsize_type = DEFAULT_SIZE;
283                         break;
284                 case 1:
285                         lyxsize_type = WH;
286                         break;
287                 case 2:
288                         lyxsize_type = SCALE;
289                         break;
290                 }
291         } else if (token == "lyxwidth") {
292                 lex.next();
293                 lyxwidth = LyXLength(lex.getString());
294         } else if (token == "lyxheight") {
295                 lex.next();
296                 lyxheight = LyXLength(lex.getString());
297         } else if (token == "lyxscale") {
298                 lex.next();
299                 lyxscale = lex.getInteger();
300         } else if (token == "special") {
301                 lex.eatLine();
302                 special = lex.getString();
303         } else {        // If it's none of the above, its not ours.
304                 return false;
305         }
306         return true;
307 }
308
309
310 grfx::GParams InsetGraphicsParams::asGParams(string const & filepath) const
311 {
312         grfx::GParams pars;
313         pars.width    = 0;
314         pars.height   = 0;
315         pars.scale    = 0;
316         pars.angle    = 0;
317         pars.filename = filename;
318
319         if (!filepath.empty()) {
320                 pars.filename = MakeAbsPath(pars.filename, filepath);
321         }
322
323         if (clip) {
324                 pars.bb = bb;
325
326                 // Get the original Bounding Box from the file
327                 string const tmp = readBB_from_PSFile(filename);
328                 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
329                 if (!tmp.empty()) {
330                         int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
331                         int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
332
333                         pars.bb.xl -= bb_orig_xl;
334                         pars.bb.xr -= bb_orig_xl;
335                         pars.bb.yb -= bb_orig_yb;
336                         pars.bb.yt -= bb_orig_yb;
337                 }
338
339                 pars.bb.xl = std::max(0, pars.bb.xl);
340                 pars.bb.xr = std::max(0, pars.bb.xr);
341                 pars.bb.yb = std::max(0, pars.bb.yb);
342                 pars.bb.yt = std::max(0, pars.bb.yt);
343
344                 // Paranoia check.
345                 int const width  = pars.bb.xr - pars.bb.xl;
346                 int const height = pars.bb.yt - pars.bb.yb;
347
348                 if (width  < 0 || height < 0) {
349                         pars.bb.xl = 0;
350                         pars.bb.xr = 0;
351                         pars.bb.yb = 0;
352                         pars.bb.yt = 0;
353                 }
354         }
355         
356         if (rotate)
357                 pars.angle = int(rotateAngle);
358
359         if (display == InsetGraphicsParams::DEFAULT) {
360
361                 if (lyxrc.display_graphics == "mono")
362                         pars.display = grfx::GParams::MONOCHROME;
363                 else if (lyxrc.display_graphics == "gray")
364                         pars.display = grfx::GParams::GRAYSCALE;
365                 else if (lyxrc.display_graphics == "color")
366                         pars.display = grfx::GParams::COLOR;
367                 else
368                         pars.display = grfx::GParams::NONE;
369
370         } else if (display == InsetGraphicsParams::NONE) {
371                 pars.display = grfx::GParams::NONE;
372
373         } else if (display == InsetGraphicsParams::MONOCHROME) {
374                 pars.display = grfx::GParams::MONOCHROME;
375
376         } else if (display == InsetGraphicsParams::GRAYSCALE) {
377                 pars.display = grfx::GParams::GRAYSCALE;
378
379         } else if (display == InsetGraphicsParams::COLOR) {
380                 pars.display = grfx::GParams::COLOR;
381         }
382
383         // Override the above if we're not using a gui
384         if (!lyxrc.use_gui) {
385                 pars.display = grfx::GParams::NONE;
386         }
387
388         if (lyxsize_type == InsetGraphicsParams::SCALE) {
389                 pars.scale = lyxscale;
390
391         } else if (lyxsize_type == InsetGraphicsParams::WH) {
392                 if (!lyxwidth.zero())
393                         pars.width  = lyxwidth.inPixels(1, 1);
394                 if (!lyxheight.zero())
395                         pars.height = lyxheight.inPixels(1, 1);
396
397                 // inPixels returns a value scaled by lyxrc.zoom.
398                 // We want, therefore, to undo this.
399                 double const scaling_factor = 100.0 / double(lyxrc.zoom);
400                 pars.width  = uint(scaling_factor * pars.width);
401                 pars.height = uint(scaling_factor * pars.height);
402         }
403         return pars;
404 }