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 * ================================================= */
15 #pragma implementation
18 #include "insetgraphicsParams.h"
20 #include "support/translator.h"
21 #include "support/filetools.h"
23 #include "support/LAssert.h"
27 /// This variable keeps a tab on whether the translator was set with the
29 static bool translatorsSet = false;
31 /// This is the translator between the Resize enum and corresponding lyx
33 static Translator < InsetGraphicsParams::Resize, string >
34 resizeTranslator(InsetGraphicsParams::DEFAULT_SIZE, "default");
36 /// This is the translator between the Origin enum and corresponding lyx
38 static Translator < InsetGraphicsParams::Origin, string >
39 originTranslator(InsetGraphicsParams::DEFAULT, "default");
41 /// This is the translator between the Display enum and corresponding lyx
43 static Translator < InsetGraphicsParams::DisplayType, string >
44 displayTranslator(InsetGraphicsParams::MONOCHROME, "monochrome");
48 InsetGraphicsParams::InsetGraphicsParams()
53 if (! translatorsSet) {
54 translatorsSet = true;
56 // Fill the resize translator
57 resizeTranslator.addPair(DEFAULT_SIZE, "default");
58 resizeTranslator.addPair(CM, "cm");
59 resizeTranslator.addPair(INCH, "inch");
60 resizeTranslator.addPair(PERCENT_PAGE, "percentOfPage");
61 resizeTranslator.addPair(PERCENT_COLUMN, "percentOfColumn");
63 // Fill the origin translator
64 originTranslator.addPair(DEFAULT, "default");
65 originTranslator.addPair(LEFTTOP, "leftTop");
66 originTranslator.addPair(LEFTCENTER, "leftCenter");
67 originTranslator.addPair(LEFTBASELINE, "leftBaseLine");
68 originTranslator.addPair(LEFTBOTTOM, "leftBottom");
69 originTranslator.addPair(CENTERTOP, "centerTop");
70 originTranslator.addPair(CENTER, "center");
71 originTranslator.addPair(CENTERBASELINE, "centerBaseLine");
72 originTranslator.addPair(CENTERBOTTOM, "centerBottom");
73 originTranslator.addPair(RIGHTTOP, "rightTop");
74 originTranslator.addPair(RIGHTCENTER, "rightCenter");
75 originTranslator.addPair(RIGHTBASELINE, "rightBaseLine");
76 originTranslator.addPair(RIGHTBOTTOM, "rightBottom");
77 originTranslator.addPair(REFERENCE_POINT, "referencePoint");
79 // Fill the display translator
80 displayTranslator.addPair(MONOCHROME, "monochrome");
81 displayTranslator.addPair(GRAYSCALE, "grayscale");
82 displayTranslator.addPair(COLOR, "color");
83 displayTranslator.addPair(NONE, "none");
89 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
91 // I decided to skip the initialization since the copy will overwrite
97 InsetGraphicsParams const &
98 InsetGraphicsParams::operator=(InsetGraphicsParams const ¶ms)
100 // Are we assigning the object into itself?
108 void InsetGraphicsParams::init()
110 subcaptionText = filename = string();
111 display = MONOCHROME;
112 inlineFigure = false;
114 keepAspectRatio = true;
115 widthResize = DEFAULT_SIZE;
117 heightResize = DEFAULT_SIZE;
119 rotateOrigin = DEFAULT;
125 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
127 filename = igp.filename;
128 display = igp.display;
129 inlineFigure = igp.inlineFigure;
130 subcaption = igp.subcaption;
131 subcaptionText = igp.subcaptionText;
132 keepAspectRatio = igp.keepAspectRatio;
133 widthResize = igp.widthResize;
134 widthSize = igp.widthSize;
135 heightResize = igp.heightResize;
136 heightSize = igp.heightSize;
137 rotateOrigin = igp.rotateOrigin;
138 rotateAngle = igp.rotateAngle;
143 void InsetGraphicsParams::testInvariant() const
145 // Filename might be empty (when the dialog is first created).
146 // Assert(!filename.empty());
148 Assert(display == COLOR ||
149 display == MONOCHROME ||
150 display == GRAYSCALE ||
154 Assert(widthResize == DEFAULT_SIZE ||
156 widthResize == INCH ||
157 widthResize == PERCENT_PAGE ||
158 widthResize == PERCENT_COLUMN
161 Assert(heightResize == DEFAULT_SIZE ||
162 heightResize == CM ||
163 heightResize == INCH ||
164 heightResize == PERCENT_PAGE
167 Assert(widthSize >= 0.0);
168 Assert(heightSize >= 0.0);
170 // Angle is in degrees and ranges -360 < angle < 360
171 // The reason for this is that in latex there is a meaning for the
172 // different angles and they are not necessarliy interchangeable,
173 // it depends on the rotation origin.
174 Assert(rotateAngle < 360);
175 Assert(rotateAngle > -360);
179 bool operator==(InsetGraphicsParams const & left,
180 InsetGraphicsParams const & right)
182 if (left.filename == right.filename &&
183 left.display == right.display &&
184 left.inlineFigure == right.inlineFigure &&
185 left.subcaption == right.subcaption &&
186 left.subcaptionText == right.subcaptionText &&
187 left.keepAspectRatio == right.keepAspectRatio &&
188 left.widthResize == right.widthResize &&
189 left.widthSize == right.widthSize &&
190 left.heightResize == right.heightResize &&
191 left.heightSize == right.heightSize &&
192 left.rotateOrigin == right.rotateOrigin &&
193 left.rotateAngle == right.rotateAngle
200 static void writeResize(ostream & os, char const * key,
201 InsetGraphicsParams::Resize resize, double size)
203 os << ' ' << key << "Resize ";
205 os << resizeTranslator.find(resize);
207 // Old code, before using translators
209 case InsetGraphicsParams::DEFAULT_SIZE:
213 case InsetGraphicsParams::CM:
217 case InsetGraphicsParams::INCH:
221 case InsetGraphicsParams::PERCENT_PAGE:
222 os << "percentOfPage";
225 case InsetGraphicsParams::PERCENT_COLUMN:
226 os << "percentOfColumnt";
230 os << ' ' << key << ' ' << size << endl;
233 static void writeOrigin(ostream & os,
234 InsetGraphicsParams::Origin origin)
236 os << " rotateOrigin " << originTranslator.find(origin);
241 case InsetGraphicsParams:: DEFAULT:
244 case InsetGraphicsParams:: LEFTTOP:
247 case InsetGraphicsParams:: LEFTCENTER:
250 case InsetGraphicsParams:: LEFTBASELINE:
251 os << "LeftBaseLine";
253 case InsetGraphicsParams:: LEFTBOTTOM:
256 case InsetGraphicsParams:: CENTERTOP:
259 case InsetGraphicsParams:: CENTER:
262 case InsetGraphicsParams:: CENTERBASELINE:
263 os << "CenterBaseLine";
265 case InsetGraphicsParams:: CENTERBOTTOM:
266 os << "CenterBottom";
268 case InsetGraphicsParams:: RIGHTTOP:
271 case InsetGraphicsParams:: RIGHTCENTER:
274 case InsetGraphicsParams:: RIGHTBASELINE:
275 os << "RightBaseLine";
277 case InsetGraphicsParams:: RIGHTBOTTOM:
280 // Current REFERENCE_POINT is aliased to LEFTBASELINE
281 // case InsetGraphicsParams:: REFERENCE_POINT:
289 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
291 // If there is no filename, write nothing for it.
292 if (! filename.empty()) {
294 << MakeRelPath(filename, OnlyPath(buf->fileName()))
298 // Save the display type
299 os << " display " << displayTranslator.find(display) << endl;
321 // Save the inline status
325 // Save the subcaption status
329 if (! subcaptionText.empty())
330 os << " subcaptionText \"" << subcaptionText << '\"' << endl;
332 writeResize(os, "width", widthResize, widthSize);
333 writeResize(os, "height", heightResize, heightSize);
335 writeOrigin(os, rotateOrigin);
336 if (rotateAngle != 0)
337 os << " rotateAngle " << rotateAngle << endl;
340 static void readResize(InsetGraphicsParams * igp, bool height,
341 string const & token)
343 InsetGraphicsParams::Resize resize = InsetGraphicsParams::DEFAULT_SIZE;
345 resize = resizeTranslator.find(token);
347 // Old code, before translator.
348 if (token == "default")
349 resize = InsetGraphicsParams::DEFAULT_SIZE;
350 else if (token == "cm")
351 resize = InsetGraphicsParams::CM;
352 else if (token == "inch")
353 resize = InsetGraphicsParams::INCH;
354 else if (token == "percentOfPage")
355 resize = InsetGraphicsParams::PERCENT_PAGE;
356 else if (token == "percentOfColumn")
357 resize = InsetGraphicsParams::PERCENT_COLUMN;
359 lyxerr << "BUG: When reading resize value of InsetGraphicsParam"
360 " unknown token found '" << token << '\'' << endl;
365 igp->heightResize = resize;
367 igp->widthResize = resize;
370 static void readOrigin(InsetGraphicsParams * igp, string const & token)
371 { // TODO: complete this function.
372 igp->rotateOrigin = originTranslator.find(token);
375 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
378 if (token == "filename") {
380 filename = lex.GetString();
382 if (!filename.empty()) {
383 // Make the filename with absolute directory.
384 filename = MakeAbsPath(filename, OnlyPath(buf->fileName()));
386 } else if (token == "display") {
388 string const type = lex.GetString();
390 display = displayTranslator.find(type);
394 else if (type == "grayscale")
396 else if (type == "monochrome")
397 display = MONOCHROME;
398 else if (type == "none")
401 display = MONOCHROME;
402 lyxerr << "BUG: When reading InsetGraphicsParams"
403 " display has an unknown type " << type << endl;
406 } else if (token == "inline") {
408 } else if (token == "subcaption") {
410 } else if (token == "subcaptionText") {
412 subcaptionText = lex.GetString();
413 } else if (token == "widthResize") {
415 string const token = lex.GetString();
417 readResize(this, false, token);
418 } else if (token == "width") {
420 widthSize = lex.GetFloat();
421 } else if (token == "heightResize") {
423 string const token = lex.GetString();
425 readResize(this, true, token);
426 } else if (token == "height") {
428 heightSize = lex.GetFloat();
429 } else if (token == "rotateOrigin") {
431 string const token = lex.GetString();
433 readOrigin(this, token);
434 } else if (token == "rotateAngle") {
436 rotateAngle = lex.GetInteger();
438 // If it's none of the above, its not ours.