]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
Now generate previews when loading the buffer for insets in insets.
[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 } //anon
201
202
203 void InsetGraphicsParams::Write(ostream & os) const
204 {
205         // If there is no filename, write nothing for it.
206         if (!filename.empty()) {
207                 os << "\tfilename " << filename << '\n';
208         }
209         if (!bb.empty())                // bounding box
210                 os << "\tBoundingBox " << bb << '\n';
211         if (clip)                       // clip image
212                 os << "\tclip\n";
213         if (draft)                      // draft mode
214                 os << "\tdraft\n";
215         // Save the display type for the view inside lyx
216         os << "\tdisplay " << displayTranslator.find(display) << '\n';
217         // Save the subcaption status
218         if (subcaption)
219                 os << "\tsubcaption\n";
220         if (!subcaptionText.empty())
221                 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
222         if (noUnzip)
223                 os << "\tnoUnzip\n";
224         os << "\tsize_kind " << getSizeKindStr(size_kind) << '\n';
225         if (!width.zero())
226                 os << "\twidth " << width.asString() << '\n';
227         if (!height.zero())
228                 os << "\theight " << height.asString() << '\n';
229         if (scale != 0)
230                 os << "\tscale " << scale << '\n';
231         if (keepAspectRatio)
232                 os << "\tkeepAspectRatio\n";
233         if (rotate)
234                 os << "\trotate\n";
235         if (rotateAngle != 0.0)
236                 os << "\trotateAngle " << rotateAngle << '\n';
237         if (!rotateOrigin.empty())
238                 os << "\trotateOrigin " << rotateOrigin << '\n';
239         if (!special.empty())
240                 os << "\tspecial " << special << '\n';
241         // the values for the view in lyx
242         os << "\tlyxsize_kind " << getSizeKindStr(lyxsize_kind) << '\n';
243         if (!lyxwidth.zero())           // the lyx-viewsize
244                 os << "\tlyxwidth " << lyxwidth.asString() << '\n';
245         if (!lyxheight.zero())
246                 os << "\tlyxheight " << lyxheight.asString();
247         if (keepLyXAspectRatio)
248                 os << "\tkeepLyXAspectRatio\n";
249         if (lyxscale != 0)
250                 os << "\tlyxscale " << lyxscale << '\n';
251 }
252
253
254 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token)
255 {
256         if (token == "filename") {
257                 lex.eatLine();
258                 filename = lex.getString();
259         } else if (token == "BoundingBox") {
260                 for (int i=0; i<4 ;i++) {
261                         lex.next();
262                         bb += (lex.getString()+" ");
263                 }
264         } else if (token == "clip") {
265                 clip = true;
266         } else if (token == "draft") {
267                 draft = true;
268         } else if (token == "display") {
269                 lex.next();
270                 string const type = lex.getString();
271                 display = displayTranslator.find(type);
272         } else if (token == "subcaption") {
273                 subcaption = true;
274         } else if (token == "subcaptionText") {
275                 lex.next();
276                 subcaptionText = lex.getString();
277         } else if (token == "noUnzip") {
278                 noUnzip = true;
279         } else if (token == "size_kind")  {
280                 lex.next();
281                 size_kind = getSizeKind(lex.getString());
282         // compatibility-stuff 1.20->1.3.0
283         } else if (token == "size_type") {
284                 lex.next();
285                 switch (lex.getInteger()) {
286                 case 0:
287                         size_kind = DEFAULT_SIZE;
288                         break;
289                 case 1:
290                         size_kind = WH;
291                         break;
292                 case 2:
293                         size_kind = SCALE;
294                         break;
295                 }
296         } else if (token == "width") {
297                 lex.next();
298                 width = LyXLength(lex.getString());
299         } else if (token == "height") {
300                 lex.next();
301                 height = LyXLength(lex.getString());
302         } else if (token == "keepAspectRatio") {
303                 keepAspectRatio = true;
304         } else if (token == "scale") {
305                 lex.next();
306                 scale = lex.getInteger();
307         } else if (token == "rotate") {
308                 rotate = true;
309         } else if (token == "rotateAngle") {
310                 lex.next();
311                 rotateAngle = lex.getFloat();
312         } else if (token == "rotateOrigin") {
313                 lex.next();
314                 rotateOrigin=lex.getString();
315         } else if (token == "lyxsize_kind") {
316                 lex.next();
317                 lyxsize_kind = getSizeKind(lex.getString());
318         } else if (token == "lyxwidth") {
319                 lex.next();
320                 lyxwidth = LyXLength(lex.getString());
321         } else if (token == "lyxheight") {
322                 lex.next();
323                 lyxheight = LyXLength(lex.getString());
324         } else if (token == "keepLyXAspectRatio") {
325                 keepLyXAspectRatio = true;
326         } else if (token == "lyxscale") {
327                 lex.next();
328                 lyxscale = lex.getInteger();
329         } else if (token == "special") {
330                 lex.eatLine();
331                 special = lex.getString();
332         } else {        // If it's none of the above, its not ours.
333                 return false;
334         }
335         return true;
336 }
337
338
339 grfx::Params InsetGraphicsParams::as_grfxParams(string const & filepath) const
340 {
341         grfx::Params pars;
342         pars.width    = 0;
343         pars.height   = 0;
344         pars.scale    = 0;
345         pars.keepLyXAspectRatio = false;
346         pars.angle    = 0;
347         pars.filename = filename;
348
349         if (!filepath.empty()) {
350                 pars.filename = MakeAbsPath(pars.filename, filepath);
351         }
352
353         if (clip) {
354                 pars.bb = bb;
355
356                 // Get the original Bounding Box from the file
357                 string const tmp = readBB_from_PSFile(filename);
358                 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
359                 if (!tmp.empty()) {
360                         int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
361                         int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
362
363                         pars.bb.xl -= bb_orig_xl;
364                         pars.bb.xr -= bb_orig_xl;
365                         pars.bb.yb -= bb_orig_yb;
366                         pars.bb.yt -= bb_orig_yb;
367                 }
368
369                 pars.bb.xl = std::max(0, pars.bb.xl);
370                 pars.bb.xr = std::max(0, pars.bb.xr);
371                 pars.bb.yb = std::max(0, pars.bb.yb);
372                 pars.bb.yt = std::max(0, pars.bb.yt);
373
374                 // Paranoia check.
375                 int const width  = pars.bb.xr - pars.bb.xl;
376                 int const height = pars.bb.yt - pars.bb.yb;
377
378                 if (width  < 0 || height < 0) {
379                         pars.bb.xl = 0;
380                         pars.bb.xr = 0;
381                         pars.bb.yb = 0;
382                         pars.bb.yt = 0;
383                 }
384         }
385         
386         if (rotate)
387                 pars.angle = int(rotateAngle);
388
389         switch (display) {
390                 case InsetGraphicsParams::NONE:
391                         pars.display = grfx::NoDisplay;
392                 break;
393
394                 case InsetGraphicsParams::MONOCHROME:
395                         pars.display = grfx::MonochromeDisplay;
396                 break;
397
398                 case InsetGraphicsParams::GRAYSCALE: 
399                         pars.display = grfx::GrayscaleDisplay;
400
401                 case InsetGraphicsParams::COLOR:
402                         pars.display = grfx::ColorDisplay;
403                 break;
404
405                 default: {
406                         if (lyxrc.display_graphics == "mono")
407                                 pars.display = grfx::MonochromeDisplay;
408                         else if (lyxrc.display_graphics == "gray")
409                                 pars.display = grfx::GrayscaleDisplay;
410                         else if (lyxrc.display_graphics == "color")
411                                 pars.display = grfx::ColorDisplay;
412                         else
413                                 pars.display = grfx::NoDisplay;
414                 }
415         }
416         
417         // Override the above if we're not using a gui
418         if (!lyxrc.use_gui) {
419                 pars.display = grfx::NoDisplay;
420         }
421
422         if (lyxsize_kind == InsetGraphicsParams::SCALE) {
423                 pars.scale = lyxscale;
424
425         } else if (lyxsize_kind == InsetGraphicsParams::WH) {
426                 pars.width = lyxwidth.inBP();
427                 pars.height = lyxheight.inBP();
428                 pars.keepLyXAspectRatio = keepLyXAspectRatio;
429         }
430         
431         return pars;
432 }