2 /* This file is part of
3 * =================================================
5 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich.
7 * Copyright 1995-2000 The LyX Team.
9 * This file Copyright 2000 Baruch Even
10 * ================================================= */
13 #pragma implementation
17 #include "insetgraphicsParams.h"
19 #include "support/translator.h"
20 #include "support/filetools.h"
22 #ifdef ENABLE_ASSERTIONS
23 #include "support/LAssert.h"
28 /// This variable keeps a tab on whether the translator was set with the
30 static bool translatorsSet = false;
32 /// This is the translator between the Resize enum and corresponding lyx
34 static Translator<InsetGraphicsParams::Resize, string>
35 resizeTranslator(InsetGraphicsParams::DEFAULT_SIZE, "default");
37 /// This is the translator between the Origin enum and corresponding lyx
39 static Translator<InsetGraphicsParams::Origin, string>
40 originTranslator(InsetGraphicsParams::DEFAULT, "default");
42 /// This is the translator between the Display enum and corresponding lyx
44 static Translator<InsetGraphicsParams::DisplayType, string>
45 displayTranslator(InsetGraphicsParams::MONOCHROME, "monochrome");
49 InsetGraphicsParams::InsetGraphicsParams()
54 if (! translatorsSet) {
55 translatorsSet = true;
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");
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");
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");
90 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
92 // I decided to skip the initialization since the copy will overwrite
98 InsetGraphicsParams const &
99 InsetGraphicsParams::operator=(InsetGraphicsParams const ¶ms)
101 // Are we assigning the object into itself?
109 void InsetGraphicsParams::init()
111 subcaptionText = filename = string();
112 display = MONOCHROME;
113 inlineFigure = false;
115 keepAspectRatio = true;
116 widthResize = DEFAULT_SIZE;
118 heightResize = DEFAULT_SIZE;
120 rotateOrigin = DEFAULT;
123 #ifdef ENABLE_ASSERTION
128 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
130 filename = igp.filename;
131 display = igp.display;
132 inlineFigure = igp.inlineFigure;
133 subcaption = igp.subcaption;
134 subcaptionText = igp.subcaptionText;
135 keepAspectRatio = igp.keepAspectRatio;
136 widthResize = igp.widthResize;
137 widthSize = igp.widthSize;
138 heightResize = igp.heightResize;
139 heightSize = igp.heightSize;
140 rotateOrigin = igp.rotateOrigin;
141 rotateAngle = igp.rotateAngle;
143 #ifdef ENABLE_ASSERTIONS
148 void InsetGraphicsParams::testInvariant() const
150 #ifdef ENABLE_ASSERTIONS
151 // Filename might be empty (when the dialog is first created).
152 // Assert(!filename.empty());
154 Assert(display == COLOR ||
155 display == MONOCHROME ||
156 display == GRAYSCALE ||
160 Assert(widthResize == DEFAULT_SIZE ||
162 widthResize == INCH ||
163 widthResize == PERCENT_PAGE ||
164 widthResize == PERCENT_COLUMN
167 Assert(heightResize == DEFAULT_SIZE ||
168 heightResize == CM ||
169 heightResize == INCH ||
170 heightResize == PERCENT_PAGE
173 Assert(widthSize >= 0.0);
174 Assert(heightSize >= 0.0);
176 // Angle is in degrees and ranges -360 < angle < 360
177 // The reason for this is that in latex there is a meaning for the
178 // different angles and they are not necessarliy interchangeable,
179 // it depends on the rotation origin.
180 Assert(rotateAngle < 360);
181 Assert(rotateAngle > -360);
186 bool operator==(InsetGraphicsParams const & left,
187 InsetGraphicsParams const & right)
189 if (left.filename == right.filename &&
190 left.display == right.display &&
191 left.inlineFigure == right.inlineFigure &&
192 left.subcaption == right.subcaption &&
193 left.subcaptionText == right.subcaptionText &&
194 left.keepAspectRatio == right.keepAspectRatio &&
195 left.widthResize == right.widthResize &&
196 left.widthSize == right.widthSize &&
197 left.heightResize == right.heightResize &&
198 left.heightSize == right.heightSize &&
199 left.rotateOrigin == right.rotateOrigin &&
200 left.rotateAngle == right.rotateAngle
207 static void writeResize(ostream & os, char const * key,
208 InsetGraphicsParams::Resize resize, double size)
210 os << ' ' << key << "Resize ";
212 os << resizeTranslator.find(resize);
214 // Old code, before using translators
216 case InsetGraphicsParams::DEFAULT_SIZE:
220 case InsetGraphicsParams::CM:
224 case InsetGraphicsParams::INCH:
228 case InsetGraphicsParams::PERCENT_PAGE:
229 os << "percentOfPage";
232 case InsetGraphicsParams::PERCENT_COLUMN:
233 os << "percentOfColumnt";
237 os << ' ' << key << ' ' << size << endl;
240 static void writeOrigin(ostream & os,
241 InsetGraphicsParams::Origin origin)
243 os << " rotateOrigin " << originTranslator.find(origin);
248 case InsetGraphicsParams:: DEFAULT:
251 case InsetGraphicsParams:: LEFTTOP:
254 case InsetGraphicsParams:: LEFTCENTER:
257 case InsetGraphicsParams:: LEFTBASELINE:
258 os << "LeftBaseLine";
260 case InsetGraphicsParams:: LEFTBOTTOM:
263 case InsetGraphicsParams:: CENTERTOP:
266 case InsetGraphicsParams:: CENTER:
269 case InsetGraphicsParams:: CENTERBASELINE:
270 os << "CenterBaseLine";
272 case InsetGraphicsParams:: CENTERBOTTOM:
273 os << "CenterBottom";
275 case InsetGraphicsParams:: RIGHTTOP:
278 case InsetGraphicsParams:: RIGHTCENTER:
281 case InsetGraphicsParams:: RIGHTBASELINE:
282 os << "RightBaseLine";
284 case InsetGraphicsParams:: RIGHTBOTTOM:
287 // Current REFERENCE_POINT is aliased to LEFTBASELINE
288 // case InsetGraphicsParams:: REFERENCE_POINT:
295 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
297 // If there is no filename, write nothing for it.
298 if (! filename.empty()) {
300 << MakeRelPath(filename, OnlyPath(buf->fileName()))
304 // Save the display type
305 os << " display " << displayTranslator.find(display) << endl;
327 // Save the inline status
331 // Save the subcaption status
335 if (! subcaptionText.empty())
336 os << " subcaptionText \"" << subcaptionText << '\"' << endl;
338 writeResize(os, "width", widthResize, widthSize);
339 writeResize(os, "height", heightResize, heightSize);
341 writeOrigin(os, rotateOrigin);
342 if (rotateAngle != 0)
343 os << " rotateAngle " << rotateAngle << endl;
346 static void readResize(InsetGraphicsParams * igp, bool height,
347 string const & token)
349 InsetGraphicsParams::Resize resize = InsetGraphicsParams::DEFAULT_SIZE;
351 resize = resizeTranslator.find(token);
353 // Old code, before translator.
354 if (token == "default")
355 resize = InsetGraphicsParams::DEFAULT_SIZE;
356 else if (token == "cm")
357 resize = InsetGraphicsParams::CM;
358 else if (token == "inch")
359 resize = InsetGraphicsParams::INCH;
360 else if (token == "percentOfPage")
361 resize = InsetGraphicsParams::PERCENT_PAGE;
362 else if (token == "percentOfColumn")
363 resize = InsetGraphicsParams::PERCENT_COLUMN;
365 lyxerr << "BUG: When reading resize value of InsetGraphicsParam"
366 " unknown token found '" << token << '\'' << endl;
371 igp->heightResize = resize;
373 igp->widthResize = resize;
376 static void readOrigin(InsetGraphicsParams * igp, string const & token)
377 { // TODO: complete this function.
378 igp->rotateOrigin = originTranslator.find(token);
381 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
384 if (token == "filename") {
386 filename = lex.GetString();
388 if (!filename.empty()) {
389 // Make the filename with absolute directory.
390 filename = MakeAbsPath(filename, OnlyPath(buf->fileName()));
392 } else if (token == "display") {
394 string const type = lex.GetString();
396 display = displayTranslator.find(type);
400 else if (type == "grayscale")
402 else if (type == "monochrome")
403 display = MONOCHROME;
404 else if (type == "none")
407 display = MONOCHROME;
408 lyxerr << "BUG: When reading InsetGraphicsParams"
409 " display has an unknown type " << type << endl;
412 } else if (token == "inline") {
414 } else if (token == "subcaption") {
416 } else if (token == "subcaptionText") {
418 subcaptionText = lex.GetString();
419 } else if (token == "widthResize") {
421 string const token = lex.GetString();
423 readResize(this, false, token);
424 } else if (token == "width") {
426 widthSize = lex.GetFloat();
427 } else if (token == "heightResize") {
429 string const token = lex.GetString();
431 readResize(this, true, token);
432 } else if (token == "height") {
434 heightSize = lex.GetFloat();
435 } else if (token == "rotateOrigin") {
437 string const token = lex.GetString();
439 readOrigin(this, token);
440 } else if (token == "rotateAngle") {
442 rotateAngle = lex.GetInteger();
444 // If it's none of the above, its not ours.