]> git.lyx.org Git - features.git/blob - src/insets/insetgraphicsParams.C
latest graphics jumbo patch from herbert
[features.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         } else if (token == "width") {
283                 lex.next();
284                 width = LyXLength(lex.getString());
285         } else if (token == "height") {
286                 lex.next();
287                 height = LyXLength(lex.getString());
288         } else if (token == "keepAspectRatio") {
289                 keepAspectRatio = true;
290         } else if (token == "scale") {
291                 lex.next();
292                 scale = lex.getInteger();
293         } else if (token == "rotate") {
294                 rotate = true;
295         } else if (token == "rotateAngle") {
296                 lex.next();
297                 rotateAngle = lex.getFloat();
298         } else if (token == "rotateOrigin") {
299                 lex.next();
300                 rotateOrigin=lex.getString();
301         } else if (token == "lyxsize_kind") {
302                 lex.next();
303                 lyxsize_kind = getSizeKind(lex.getString());
304         } else if (token == "lyxwidth") {
305                 lex.next();
306                 lyxwidth = LyXLength(lex.getString());
307         } else if (token == "lyxheight") {
308                 lex.next();
309                 lyxheight = LyXLength(lex.getString());
310         } else if (token == "keepLyXAspectRatio") {
311                 keepLyXAspectRatio = true;
312         } else if (token == "lyxscale") {
313                 lex.next();
314                 lyxscale = lex.getInteger();
315         } else if (token == "special") {
316                 lex.eatLine();
317                 special = lex.getString();
318         } else {        // If it's none of the above, its not ours.
319                 return false;
320         }
321         return true;
322 }
323
324
325 grfx::Params InsetGraphicsParams::as_grfxParams(string const & filepath) const
326 {
327         grfx::Params pars;
328         pars.width    = 0;
329         pars.height   = 0;
330         pars.scale    = 0;
331         pars.keepLyXAspectRatio = false;
332         pars.angle    = 0;
333         pars.filename = filename;
334
335         if (!filepath.empty()) {
336                 pars.filename = MakeAbsPath(pars.filename, filepath);
337         }
338
339         if (clip) {
340                 pars.bb = bb;
341
342                 // Get the original Bounding Box from the file
343                 string const tmp = readBB_from_PSFile(filename);
344                 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
345                 if (!tmp.empty()) {
346                         int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
347                         int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
348
349                         pars.bb.xl -= bb_orig_xl;
350                         pars.bb.xr -= bb_orig_xl;
351                         pars.bb.yb -= bb_orig_yb;
352                         pars.bb.yt -= bb_orig_yb;
353                 }
354
355                 pars.bb.xl = std::max(0, pars.bb.xl);
356                 pars.bb.xr = std::max(0, pars.bb.xr);
357                 pars.bb.yb = std::max(0, pars.bb.yb);
358                 pars.bb.yt = std::max(0, pars.bb.yt);
359
360                 // Paranoia check.
361                 int const width  = pars.bb.xr - pars.bb.xl;
362                 int const height = pars.bb.yt - pars.bb.yb;
363
364                 if (width  < 0 || height < 0) {
365                         pars.bb.xl = 0;
366                         pars.bb.xr = 0;
367                         pars.bb.yb = 0;
368                         pars.bb.yt = 0;
369                 }
370         }
371         
372         if (rotate)
373                 pars.angle = int(rotateAngle);
374
375         switch (display) {
376                 case InsetGraphicsParams::NONE:
377                         pars.display = grfx::NoDisplay;
378                 break;
379
380                 case InsetGraphicsParams::MONOCHROME:
381                         pars.display = grfx::MonochromeDisplay;
382                 break;
383
384                 case InsetGraphicsParams::GRAYSCALE: 
385                         pars.display = grfx::GrayscaleDisplay;
386
387                 case InsetGraphicsParams::COLOR:
388                         pars.display = grfx::ColorDisplay;
389                 break;
390
391                 default: {
392                         if (lyxrc.display_graphics == "mono")
393                                 pars.display = grfx::MonochromeDisplay;
394                         else if (lyxrc.display_graphics == "gray")
395                                 pars.display = grfx::GrayscaleDisplay;
396                         else if (lyxrc.display_graphics == "color")
397                                 pars.display = grfx::ColorDisplay;
398                         else
399                                 pars.display = grfx::NoDisplay;
400                 }
401         }
402         
403         // Override the above if we're not using a gui
404         if (!lyxrc.use_gui) {
405                 pars.display = grfx::NoDisplay;
406         }
407
408         if (lyxsize_kind == InsetGraphicsParams::SCALE) {
409                 pars.scale = lyxscale;
410
411         } else if (lyxsize_kind == InsetGraphicsParams::WH) {
412                 pars.width = lyxwidth.inBP();
413                 pars.height = lyxheight.inBP();
414                 pars.keepLyXAspectRatio = keepLyXAspectRatio;
415         }
416         
417         return pars;
418 }