]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
eb7d423074722edcd4d85b9edd43b99580420730
[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< grfx::DisplayType, string >
45 displayTranslator(grfx::DefaultDisplay, "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(grfx::DefaultDisplay, "default");
58                 displayTranslator.addPair(grfx::MonochromeDisplay, "monochrome");
59                 displayTranslator.addPair(grfx::GrayscaleDisplay, "grayscale");
60                 displayTranslator.addPair(grfx::ColorDisplay, "color");
61                 displayTranslator.addPair(grfx::NoDisplay, "none");
62                 
63                 // backward compatibility for old lyxrc.display_graphics
64                 displayTranslator.addPair(grfx::MonochromeDisplay, "mono");
65                 displayTranslator.addPair(grfx::GrayscaleDisplay, "gray");
66                 displayTranslator.addPair(grfx::NoDisplay, "no");
67         }
68 }
69
70 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
71 {
72         // I decided to skip the initialization since the copy will overwrite
73         // everything anyway.
74         //    init();
75         copy(igp);
76 }
77
78 InsetGraphicsParams &
79 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
80 {
81         // Are we assigning the object into itself?
82         if (this == &params)
83                 return * this;
84         copy(params);
85         return *this;
86 }
87
88 void InsetGraphicsParams::init()
89 {
90         subcaptionText = filename = string();
91         lyxscale = 100;                 // lyx scaling in percentage
92         display = grfx::DefaultDisplay; // see pref
93         scale = 100;                    // output scaling in percentage
94         width = LyXLength();
95         height = LyXLength();
96         keepAspectRatio = false;        // for latex
97         draft = false;                  // draft mode
98         noUnzip = false;                // unzip files
99
100         bb = string();                  // bounding box
101         clip = false;                   // clip image
102
103         rotateOrigin = "leftBaseline";  // Origin
104         rotateAngle = 0.0;              // in degrees
105         subcaption = false;             // subfigure
106         subcaptionText = string();      // subfigure caption
107         special = string();             // userdefined stuff
108 }
109
110 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
111 {
112         filename = igp.filename;
113         lyxscale = igp.lyxscale;
114         display = igp.display;
115         scale = igp.scale;
116         width = igp.width;
117         height = igp.height;
118         keepAspectRatio = igp.keepAspectRatio;
119         draft = igp.draft;
120         noUnzip = igp.noUnzip;
121
122         bb = igp.bb;
123         clip = igp.clip;
124
125         rotateAngle = igp.rotateAngle;
126         rotateOrigin = igp.rotateOrigin;
127         subcaption = igp.subcaption;
128         subcaptionText = igp.subcaptionText;
129         special = igp.special;
130 }
131
132 bool operator==(InsetGraphicsParams const & left,
133                 InsetGraphicsParams const & right)
134 {
135         if (left.filename == right.filename &&
136             left.lyxscale == right.lyxscale &&
137             left.display == right.display &&
138             left.scale == right.scale &&
139             left.width == right.width &&
140             left.height == right.height &&
141             left.keepAspectRatio == right.keepAspectRatio &&
142             left.draft == right.draft &&
143             left.noUnzip == right.noUnzip &&
144
145
146             left.bb == right.bb &&
147             left.clip == right.clip &&
148
149             lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001) &&
150             left.rotateOrigin == right.rotateOrigin &&
151             left.subcaption == right.subcaption &&
152             left.subcaptionText == right.subcaptionText &&
153             left.special == right.special
154            )
155                 return true;
156
157         return false;
158 }
159
160 bool operator!=(InsetGraphicsParams const & left,
161                 InsetGraphicsParams const & right)
162 {
163         return  !(left == right);
164 }
165
166 void InsetGraphicsParams::Write(ostream & os) const
167 {
168         // Do not write the default values
169
170         if (!filename.empty()) {
171                 os << "\tfilename " << filename << '\n';
172         }
173         if (lyxscale != 100)
174                 os << "\tlyxscale " << lyxscale << '\n';
175         if (display != grfx::DefaultDisplay)
176                 os << "\tdisplay " << displayTranslator.find(display) << '\n';
177         if (scale) {
178                 if (scale != 100)
179                         os << "\tscale " << scale << '\n';
180         } else {
181                 if (!width.zero())
182                         os << "\twidth " << width.asString() << '\n';
183         }
184
185         if (!height.zero())
186                 os << "\theight " << height.asString() << '\n';
187         if (keepAspectRatio)
188                 os << "\tkeepAspectRatio\n";
189         if (draft)                      // draft mode
190                 os << "\tdraft\n";
191         if (noUnzip)
192                 os << "\tnoUnzip\n";
193
194         if (!bb.empty())                // bounding box
195                 os << "\tBoundingBox " << bb << '\n';
196         if (clip)                       // clip image
197                 os << "\tclip\n";
198
199         if (rotateAngle != 0.0)
200                 os << "\trotateAngle " << rotateAngle << '\n';
201         if (rotateOrigin != "leftBaseline")
202                 os << "\trotateOrigin " << rotateOrigin << '\n';
203         if (subcaption)
204                 os << "\tsubcaption\n";
205         if (!subcaptionText.empty())
206                 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
207         if (!special.empty())
208                 os << "\tspecial " << special << '\n';
209 }
210
211
212 bool InsetGraphicsParams::Read(LyXLex & lex, string const & token)
213 {
214         if (token == "filename") {
215                 lex.eatLine();
216                 filename = lex.getString();
217         } else if (token == "lyxscale") {
218                 lex.next();
219                 lyxscale = lex.getInteger();
220         } else if (token == "display") {
221                 lex.next();
222                 string const type = lex.getString();
223                 display = displayTranslator.find(type);
224         } else if (token == "scale") {
225                 lex.next();
226                 scale = lex.getInteger();
227         } else if (token == "width") {
228                 lex.next();
229                 width = LyXLength(lex.getString());
230                 scale = 0;
231         } else if (token == "height") {
232                 lex.next();
233                 height = LyXLength(lex.getString());
234         } else if (token == "keepAspectRatio") {
235                 keepAspectRatio = true;
236         } else if (token == "draft") {
237                 draft = true;
238         } else if (token == "noUnzip") {
239                 noUnzip = true;
240         } else if (token == "BoundingBox") {
241                 for (int i=0; i<4 ;i++) {
242                         lex.next();
243                         bb += (lex.getString()+" ");
244                 }
245         } else if (token == "clip") {
246                 clip = true;
247         } else if (token == "rotateAngle") {
248                 lex.next();
249                 rotateAngle = lex.getFloat();
250         } else if (token == "rotateOrigin") {
251                 lex.next();
252                 rotateOrigin=lex.getString();
253         } else if (token == "subcaption") {
254                 subcaption = true;
255         } else if (token == "subcaptionText") {
256                 lex.next();
257                 subcaptionText = lex.getString();
258         } else if (token == "special") {
259                 lex.eatLine();
260                 special = lex.getString();
261
262         // catch and ignore following two old-format tokens and their arguments.
263         // e.g. "size_kind scale" clashes with the setting of the "scale" keyword.
264         } else if (token == "size_kind" || token == "lyxsize_kind") {
265                 lex.next();
266                 lex.getString();
267         } else {        // If it's none of the above, it's not ours.
268                 return false;
269         }
270         return true;
271 }
272
273
274 grfx::Params InsetGraphicsParams::as_grfxParams(string const & filepath) const
275 {
276         grfx::Params pars;
277         pars.filename = filename;
278         pars.scale = lyxscale;
279         pars.angle = rotateAngle;
280
281         if (!filepath.empty())
282                 pars.filename = MakeAbsPath(pars.filename, filepath);
283
284         if (clip) {
285                 pars.bb = bb;
286
287                 // Get the original Bounding Box from the file
288                 string const tmp = readBB_from_PSFile(filename);
289                 lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
290                 if (!tmp.empty()) {
291                         unsigned int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
292                         unsigned int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
293
294                         // new pars.bb values must be >= zero 
295                         if  (pars.bb.xl > bb_orig_xl)
296                                 pars.bb.xl -= bb_orig_xl;
297                         else
298                                 pars.bb.xl = 0;
299                         
300                         if (pars.bb.xr > bb_orig_xl)
301                                 pars.bb.xr -= bb_orig_xl;
302                         else
303                                 pars.bb.xr = 0;
304                                 
305                         if (pars.bb.yb > bb_orig_yb)
306                                 pars.bb.yb -= bb_orig_yb;
307                         else
308                                 pars.bb.yb = 0;
309                                 
310                         if (pars.bb.yt > bb_orig_yb)
311                                 pars.bb.yt -= bb_orig_yb;
312                         else
313                                 pars.bb.yt = 0;
314                 }
315
316                 // Paranoia check.
317                 int const width  = pars.bb.xr - pars.bb.xl;
318                 int const height = pars.bb.yt - pars.bb.yb;
319
320                 if (width  < 0 || height < 0) {
321                         pars.bb.xl = 0;
322                         pars.bb.xr = 0;
323                         pars.bb.yb = 0;
324                         pars.bb.yt = 0;
325                 }
326         }
327         
328         string mode;
329         if (display != grfx::DefaultDisplay)
330                 mode = displayTranslator.find(display);
331         else
332                 mode = displayTranslator.find(lyxrc.display_graphics);
333         pars.display = displayTranslator.find(mode);
334         
335         // Override the above if we're not using a gui
336         if (!lyxrc.use_gui) {
337                 pars.display = grfx::NoDisplay;
338         }
339
340         return pars;
341 }