]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
Assume unknown formats are EPS, hacky solution before implementing a real one.
[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  *          This file Copyright 2000 Baruch Even
9  * ================================================= */
10
11 #include <config.h> 
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif 
16
17 #include "insetgraphicsParams.h"
18
19 #include "support/translator.h"
20 #include "support/filetools.h"
21 #include "support/lyxlib.h"
22
23 #include "support/LAssert.h"
24
25 namespace {
26
27 /// This variable keeps a tab on whether the translator was set with the
28 /// translations.
29 bool translatorsSet = false;
30
31 /// This is the translator between the Resize enum and corresponding lyx
32 /// file strings.
33 Translator< InsetGraphicsParams::Resize, string >
34 resizeTranslator(InsetGraphicsParams::DEFAULT_SIZE, "default");
35
36 /// This is the translator between the Origin enum and corresponding lyx
37 /// file strings.
38 Translator< InsetGraphicsParams::Origin, string >
39 originTranslator(InsetGraphicsParams::DEFAULT, "default");
40
41 /// This is the translator between the Display enum and corresponding lyx
42 /// file strings.
43 Translator< InsetGraphicsParams::DisplayType, string >
44 displayTranslator(InsetGraphicsParams::MONOCHROME, "monochrome");
45
46 } // namespace anon
47
48
49 InsetGraphicsParams::InsetGraphicsParams()
50 {
51         init();
52
53         // Set translators
54         if (! translatorsSet) {
55                 translatorsSet = true;
56
57                 // Fill the resize translator
58                 resizeTranslator.addPair(DEFAULT_SIZE, "default");
59                 resizeTranslator.addPair(CM, "cm");
60                 resizeTranslator.addPair(INCH, "inch");
61                 resizeTranslator.addPair(PERCENT_PAGE, "percentOfPage");
62                 resizeTranslator.addPair(PERCENT_COLUMN, "percentOfColumn");
63
64                 // Fill the origin translator
65                 originTranslator.addPair(DEFAULT, "default");
66                 originTranslator.addPair(LEFTTOP, "leftTop");
67                 originTranslator.addPair(LEFTCENTER, "leftCenter");
68                 originTranslator.addPair(LEFTBASELINE, "leftBaseLine");
69                 originTranslator.addPair(LEFTBOTTOM, "leftBottom");
70                 originTranslator.addPair(CENTERTOP, "centerTop");
71                 originTranslator.addPair(CENTER, "center");
72                 originTranslator.addPair(CENTERBASELINE, "centerBaseLine");
73                 originTranslator.addPair(CENTERBOTTOM, "centerBottom");
74                 originTranslator.addPair(RIGHTTOP, "rightTop");
75                 originTranslator.addPair(RIGHTCENTER, "rightCenter");
76                 originTranslator.addPair(RIGHTBASELINE, "rightBaseLine");
77                 originTranslator.addPair(RIGHTBOTTOM, "rightBottom");
78                 originTranslator.addPair(REFERENCE_POINT, "referencePoint");
79
80                 // Fill the display translator
81                 displayTranslator.addPair(MONOCHROME, "monochrome");
82                 displayTranslator.addPair(GRAYSCALE, "grayscale");
83                 displayTranslator.addPair(COLOR, "color");
84                 displayTranslator.addPair(NONE, "none");
85         }
86
87 }
88
89
90 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
91 {
92         // I decided to skip the initialization since the copy will overwrite
93         // everything anyway.
94         //    init();
95         copy(igp);
96 }
97
98 InsetGraphicsParams &
99 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
100 {
101         // Are we assigning the object into itself?
102         if (this == &params)
103                 return * this;
104
105         copy(params);
106         return *this;
107 }
108
109 void InsetGraphicsParams::init()
110 {
111         subcaptionText = filename = string();
112         display = MONOCHROME;
113         subcaption = false;
114         keepAspectRatio = true;
115         widthResize = DEFAULT_SIZE;
116         widthSize = 0.0;
117         heightResize = DEFAULT_SIZE;
118         heightSize = 0.0;
119         rotateOrigin = DEFAULT;
120         rotateAngle = 0.0;
121
122         testInvariant();
123 }
124
125 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
126 {
127         filename = igp.filename;
128         display = igp.display;
129         subcaption = igp.subcaption;
130         subcaptionText = igp.subcaptionText;
131         keepAspectRatio = igp.keepAspectRatio;
132         widthResize = igp.widthResize;
133         widthSize = igp.widthSize;
134         heightResize = igp.heightResize;
135         heightSize = igp.heightSize;
136         rotateOrigin = igp.rotateOrigin;
137         rotateAngle = igp.rotateAngle;
138
139         testInvariant();
140 }
141
142 void InsetGraphicsParams::testInvariant() const
143 {
144         // Filename might be empty (when the dialog is first created).
145         // Assert(!filename.empty());
146
147         lyx::Assert(display == COLOR ||
148                display == MONOCHROME ||
149                display == GRAYSCALE ||
150                display == NONE
151               );
152
153         lyx::Assert(widthResize == DEFAULT_SIZE ||
154                widthResize == CM ||
155                widthResize == INCH ||
156                widthResize == PERCENT_PAGE ||
157                widthResize == PERCENT_COLUMN
158               );
159
160         lyx::Assert(heightResize == DEFAULT_SIZE ||
161                heightResize == CM ||
162                heightResize == INCH ||
163                heightResize == PERCENT_PAGE
164               );
165
166         lyx::Assert(widthSize >= 0.0);
167         lyx::Assert(heightSize >= 0.0);
168
169         // Angle is in degrees and ranges -360 < angle < 360
170         // The reason for this is that in latex there is a meaning for the
171         // different angles and they are not necessarliy interchangeable,
172         // it depends on the rotation origin.
173         lyx::Assert(rotateAngle < 360.0);
174         lyx::Assert(rotateAngle > -360.0);
175
176 }
177
178 bool operator==(InsetGraphicsParams const & left,
179                 InsetGraphicsParams const & right)
180 {
181         if (left.filename == right.filename &&
182                 left.display == right.display &&
183                 left.subcaption == right.subcaption &&
184                 left.subcaptionText == right.subcaptionText &&
185                 left.keepAspectRatio == right.keepAspectRatio &&
186                 left.widthResize == right.widthResize &&
187                 left.widthSize == right.widthSize &&
188                 left.heightResize == right.heightResize &&
189                 left.heightSize == right.heightSize &&
190                 left.rotateOrigin == right.rotateOrigin &&
191                 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001)
192            )
193                 return true;
194
195         return false;
196 }
197
198 bool operator!=(InsetGraphicsParams const & left,
199                 InsetGraphicsParams const & right)
200 {
201         return  !(left == right);
202 }
203
204
205 namespace {
206
207 void writeResize(ostream & os, string const & key,
208                         InsetGraphicsParams::Resize resize, double size)
209 {
210         os << ' ' << key << "Resize ";
211
212         os << resizeTranslator.find(resize);
213         os << ' ' << key << ' ' << size << '\n';
214 }
215
216 void writeOrigin(ostream & os,
217                         InsetGraphicsParams::Origin origin)
218 {
219         os << " rotateOrigin " << originTranslator.find(origin);
220         os << '\n';
221 }
222
223 } // namespace anon
224
225
226 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
227 {
228         // If there is no filename, write nothing for it.
229         if (! filename.empty()) {
230                 os << "filename "
231                 << MakeRelPath(filename, OnlyPath(buf->fileName()))
232                 << '\n';
233         }
234
235         // Save the display type
236         os << " display " << displayTranslator.find(display) << '\n';
237
238         // Save the subcaption status
239         if (subcaption)
240                 os << " subcaption";
241
242         if (! subcaptionText.empty())
243                 os << " subcaptionText \"" << subcaptionText << '\"' << '\n';
244
245         writeResize(os, "width", widthResize, widthSize);
246         writeResize(os, "height", heightResize, heightSize);
247
248         writeOrigin(os, rotateOrigin);
249         if (lyx::float_equal(rotateAngle, 0.0, 0.001))
250                 os << " rotateAngle " << rotateAngle << '\n';
251 }
252
253
254 namespace {
255
256 void readResize(InsetGraphicsParams * igp, bool height,
257                        string const & token)
258 {
259         InsetGraphicsParams::Resize resize = InsetGraphicsParams::DEFAULT_SIZE;
260
261         resize = resizeTranslator.find(token);
262
263         if (height)
264                 igp->heightResize = resize;
265         else
266                 igp->widthResize = resize;
267 }
268
269
270 void readOrigin(InsetGraphicsParams * igp, string const & token)
271 {
272         // TODO: complete this function.
273         igp->rotateOrigin = originTranslator.find(token);
274 }
275
276 } // namespace anon
277
278
279 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
280                                string const& token)
281 {
282         if (token == "filename") {
283                 lex.next();
284                 filename = lex.GetString();
285
286                 if (!filename.empty()) {
287                         // Make the filename with absolute directory.
288                         filename = MakeAbsPath(filename, OnlyPath(buf->fileName()));
289                 }
290         } else if (token == "display") {
291                 lex.next();
292                 string const type = lex.GetString();
293
294                 display = displayTranslator.find(type);
295         } else if (token == "subcaption") {
296                 subcaption = true;
297         } else if (token == "subcaptionText") {
298                 lex.next();
299                 subcaptionText = lex.GetString();
300         } else if (token == "widthResize") {
301                 lex.next();
302                 string const token = lex.GetString();
303
304                 readResize(this, false, token);
305         } else if (token == "width") {
306                 lex.next();
307                 widthSize = lex.GetFloat();
308         } else if (token == "heightResize") {
309                 lex.next();
310                 string const token = lex.GetString();
311
312                 readResize(this, true, token);
313         } else if (token == "height") {
314                 lex.next();
315                 heightSize = lex.GetFloat();
316         } else if (token == "rotateOrigin") {
317                 lex.next();
318                 string const token = lex.GetString();
319
320                 readOrigin(this, token);
321         } else if (token == "rotateAngle") {
322                 lex.next();
323                 rotateAngle = lex.GetFloat();
324         } else {
325                 // If it's none of the above, its not ours.
326                 return false;
327         }
328
329         return true;
330 }