]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
redraw fix 1.
[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
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_type = DEFAULT_SIZE;       // do nothing
100         lyxsize_type = DEFAULT_SIZE;    // do nothing
101         keepAspectRatio = false;        // only for latex
102         rotate = false;                 // Rotating
103         rotateOrigin = "center";        // Origin
104         rotateAngle = 0.0;              // in degrees
105         special = string();             // userdefined stuff
106 }
107
108 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
109 {
110         filename = igp.filename;
111         bb = igp.bb;
112         draft = igp.draft;
113         clip = igp.clip;
114         display = igp.display;
115         subcaption = igp.subcaption;
116         subcaptionText = igp.subcaptionText;
117         noUnzip = igp.noUnzip;
118         keepAspectRatio = igp.keepAspectRatio;
119         width = igp.width;
120         height = igp.height;
121         scale = igp.scale;
122         size_type = igp.size_type;
123         lyxsize_type = igp.lyxsize_type;
124         lyxwidth = igp.lyxwidth;
125         lyxheight = igp.lyxheight;
126         lyxscale = igp.lyxscale;
127         rotate = igp.rotate;
128         rotateOrigin = igp.rotateOrigin;
129         rotateAngle = igp.rotateAngle;
130         special = igp.special;
131 }
132
133 bool operator==(InsetGraphicsParams const & left,
134                 InsetGraphicsParams const & right)
135 {
136         if (left.filename == right.filename &&
137             left.bb == right.bb &&
138             left.draft == right.draft &&
139             left.clip == right.clip &&
140             left.display == right.display &&
141             left.subcaption == right.subcaption &&
142             left.noUnzip == right.noUnzip &&
143             left.subcaptionText == right.subcaptionText &&
144             left.keepAspectRatio == right.keepAspectRatio &&
145             left.width == right.width &&
146             left.height == right.height &&
147             left.scale == right.scale &&
148             left.size_type == right.size_type &&
149             left.lyxsize_type == right.lyxsize_type &&
150             left.lyxwidth == right.lyxwidth &&
151             left.lyxheight == right.lyxheight &&
152             left.lyxscale == right.lyxscale &&
153             left.rotate == right.rotate &&
154             left.rotateOrigin == right.rotateOrigin &&
155             lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
156                              left.special == right.special)
157                 )
158                 return true;
159
160         return false;
161 }
162
163 bool operator!=(InsetGraphicsParams const & left,
164                 InsetGraphicsParams const & right)
165 {
166         return  !(left == right);
167 }
168
169
170 void InsetGraphicsParams::Write(ostream & os) const
171 {
172         // If there is no filename, write nothing for it.
173         if (!filename.empty()) {
174                 os << "\tfilename " << filename << '\n';
175         }
176         if (!bb.empty())                // bounding box
177                 os << "\tBoundingBox " << bb << '\n';
178         if (clip)                       // clip image
179                 os << "\tclip\n";
180         if (draft)                      // draft mode
181                 os << "\tdraft\n";
182         // Save the display type for the view inside lyx
183         os << "\tdisplay " << displayTranslator.find(display) << '\n';
184         // Save the subcaption status
185         if (subcaption)
186                 os << "\tsubcaption\n";
187         if (!subcaptionText.empty())
188                 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
189         if (noUnzip)
190                 os << "\tnoUnzip\n";
191         // we always need the size type
192         // 0: no special
193         // 1: width/height combination
194         // 2: scale
195         os << "\tsize_type " <<  size_type << '\n';
196         if (!width.zero())
197                 os << "\twidth " << width.asString() << '\n';
198         if (!height.zero())
199                 os << "\theight " << height.asString() << '\n';
200         if (scale != 0)
201                 os << "\tscale " << scale << '\n';
202         if (keepAspectRatio)
203                 os << "\tkeepAspectRatio\n";
204         if (rotate)
205                 os << "\trotate\n";
206         if (rotateAngle != 0.0)
207                 os << "\trotateAngle " << rotateAngle << '\n';
208         if (!rotateOrigin.empty())
209                 os << "\trotateOrigin " << rotateOrigin << '\n';
210         if (!special.empty())
211                 os << "\tspecial " << special << '\n';
212         // the values for the view in lyx
213         os << "\tlyxsize_type " <<  lyxsize_type << '\n';
214         if (!lyxwidth.zero())           // the lyx-viewsize
215                 os << "\tlyxwidth " << lyxwidth.asString() << '\n';
216         if (!lyxheight.zero())
217                 os << "\tlyxheight " << lyxheight.asString();
218         if (lyxscale != 0)
219                 os << "\tlyxscale " << lyxscale << '\n';
220 }
221
222
223 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token)
224 {
225         if (token == "filename") {
226                 lex.eatLine();
227                 filename = lex.getString();
228         } else if (token == "BoundingBox") {
229                 for (int i=0; i<4 ;i++) {
230                         lex.next();
231                         bb += (lex.getString()+" ");
232                 }
233         } else if (token == "clip") {
234                 clip = true;
235         } else if (token == "draft") {
236                 draft = true;
237         } else if (token == "display") {
238                 lex.next();
239                 string const type = lex.getString();
240                 display = displayTranslator.find(type);
241         } else if (token == "subcaption") {
242                 subcaption = true;
243         } else if (token == "subcaptionText") {
244                 lex.next();
245                 subcaptionText = lex.getString();
246         } else if (token == "noUnzip") {
247                 noUnzip = true;
248         } else if (token == "size_type") {
249                 lex.next();
250                 switch (lex.getInteger()) {
251                 case 0:
252                         size_type = DEFAULT_SIZE;
253                         break;
254                 case 1:
255                         size_type = WH;
256                         break;
257                 case 2:
258                         size_type = SCALE;
259                         break;
260                 }
261         } else if (token == "width") {
262                 lex.next();
263                 width = LyXLength(lex.getString());
264         } else if (token == "height") {
265                 lex.next();
266                 height = LyXLength(lex.getString());
267         } else if (token == "keepAspectRatio") {
268                 keepAspectRatio = true;
269         } else if (token == "scale") {
270                 lex.next();
271                 scale = lex.getInteger();
272         } else if (token == "rotate") {
273                 rotate = true;
274         } else if (token == "rotateAngle") {
275                 lex.next();
276                 rotateAngle = lex.getFloat();
277         } else if (token == "rotateOrigin") {
278                 lex.next();
279                 rotateOrigin=lex.getString();
280         } else if (token == "lyxsize_type") {
281                 lex.next();
282                 switch (lex.getInteger()) {
283                 case 0:
284                         lyxsize_type = DEFAULT_SIZE;
285                         break;
286                 case 1:
287                         lyxsize_type = WH;
288                         break;
289                 case 2:
290                         lyxsize_type = SCALE;
291                         break;
292                 }
293         } else if (token == "lyxwidth") {
294                 lex.next();
295                 lyxwidth = LyXLength(lex.getString());
296         } else if (token == "lyxheight") {
297                 lex.next();
298                 lyxheight = LyXLength(lex.getString());
299         } else if (token == "lyxscale") {
300                 lex.next();
301                 lyxscale = lex.getInteger();
302         } else if (token == "special") {
303                 lex.eatLine();
304                 special = lex.getString();
305         } else {        // If it's none of the above, its not ours.
306                 return false;
307         }
308         return true;
309 }
310
311
312 grfx::Params InsetGraphicsParams::as_grfxParams(string const & filepath) const
313 {
314         grfx::Params pars;
315         pars.width    = 0;
316         pars.height   = 0;
317         pars.scale    = 0;
318         pars.angle    = 0;
319         pars.filename = filename;
320
321         if (!filepath.empty()) {
322                 pars.filename = MakeAbsPath(pars.filename, filepath);
323         }
324
325         if (clip) {
326                 pars.bb = bb;
327
328                 // Get the original Bounding Box from the file
329                 string const tmp = readBB_from_PSFile(filename);
330                 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
331                 if (!tmp.empty()) {
332                         int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
333                         int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
334
335                         pars.bb.xl -= bb_orig_xl;
336                         pars.bb.xr -= bb_orig_xl;
337                         pars.bb.yb -= bb_orig_yb;
338                         pars.bb.yt -= bb_orig_yb;
339                 }
340
341                 pars.bb.xl = std::max(0, pars.bb.xl);
342                 pars.bb.xr = std::max(0, pars.bb.xr);
343                 pars.bb.yb = std::max(0, pars.bb.yb);
344                 pars.bb.yt = std::max(0, pars.bb.yt);
345
346                 // Paranoia check.
347                 int const width  = pars.bb.xr - pars.bb.xl;
348                 int const height = pars.bb.yt - pars.bb.yb;
349
350                 if (width  < 0 || height < 0) {
351                         pars.bb.xl = 0;
352                         pars.bb.xr = 0;
353                         pars.bb.yb = 0;
354                         pars.bb.yt = 0;
355                 }
356         }
357         
358         if (rotate)
359                 pars.angle = int(rotateAngle);
360
361         if (display == InsetGraphicsParams::DEFAULT) {
362
363                 if (lyxrc.display_graphics == "mono")
364                         pars.display = grfx::MonochromeDisplay;
365                 else if (lyxrc.display_graphics == "gray")
366                         pars.display = grfx::GrayscaleDisplay;
367                 else if (lyxrc.display_graphics == "color")
368                         pars.display = grfx::ColorDisplay;
369                 else
370                         pars.display = grfx::NoDisplay;
371
372         } else if (display == InsetGraphicsParams::NONE) {
373                 pars.display = grfx::NoDisplay;
374
375         } else if (display == InsetGraphicsParams::MONOCHROME) {
376                 pars.display = grfx::MonochromeDisplay;
377
378         } else if (display == InsetGraphicsParams::GRAYSCALE) {
379                 pars.display = grfx::GrayscaleDisplay;
380
381         } else if (display == InsetGraphicsParams::COLOR) {
382                 pars.display = grfx::ColorDisplay;
383         }
384
385         // Override the above if we're not using a gui
386         if (!lyxrc.use_gui) {
387                 pars.display = grfx::NoDisplay;
388         }
389
390         if (lyxsize_type == InsetGraphicsParams::SCALE) {
391                 pars.scale = lyxscale;
392
393         } else if (lyxsize_type == InsetGraphicsParams::WH) {
394                 if (!lyxwidth.zero())
395                         pars.width  = lyxwidth.inPixels(1, 1);
396                 if (!lyxheight.zero())
397                         pars.height = lyxheight.inPixels(1, 1);
398
399                 // inPixels returns a value scaled by lyxrc.zoom.
400                 // We want, therefore, to undo this.
401                 double const scaling_factor = 100.0 / double(lyxrc.zoom);
402                 pars.width  = uint(scaling_factor * pars.width);
403                 pars.height = uint(scaling_factor * pars.height);
404         }
405         return pars;
406 }