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