1 /* This file is part of
2 * =================================================
4 * LyX, The Document Processor
5 * Copyright 1995 Matthias Ettrich.
6 * Copyright 1995-2001 The LyX Team.
9 * \author Herbert Voss <voss@lyx.org>
11 * ================================================= */
16 #pragma implementation
19 #include "insetgraphicsParams.h"
21 #include "support/translator.h"
22 #include "support/filetools.h"
23 #include "support/lyxlib.h"
24 #include "support/LOstream.h"
27 #include "support/LAssert.h"
31 /// This variable keeps a tab on whether the translator was set with the
33 bool translatorsSet = false;
35 /// This is the translator between the Display enum and corresponding lyx
37 Translator< InsetGraphicsParams::DisplayType, string >
38 displayTranslator(InsetGraphicsParams::MONOCHROME, "monochrome");
40 // this is only compatibility stuff for the first 1.2 version
41 // it is obselete until 1.3
42 LyXLength convertResizeValue(string const token, LyXLex & lex) {
44 string value = lex.getString(); // "width" or "height"
45 lex.next(); // anyway not interesting
46 value = lex.getString();
47 if (token == "default")
48 return (LyXLength(value+"pt"));
49 else if (token == "cm")
50 return (LyXLength(value+"cm"));
51 else if (token == "inch")
52 return (LyXLength(value+"in"));
53 else if (token == "percentOfColumn")
54 return (LyXLength(value+"c%"));
55 else if (token == "percentOfPage")
56 return (LyXLength(value+"p%"));
57 else return LyXLength("0pt"); // nothing with figinset
63 InsetGraphicsParams::InsetGraphicsParams()
67 if (! translatorsSet) {
68 translatorsSet = true;
69 // Fill the display translator
70 displayTranslator.addPair(MONOCHROME, "monochrome");
71 displayTranslator.addPair(GRAYSCALE, "grayscale");
72 displayTranslator.addPair(COLOR, "color");
73 displayTranslator.addPair(NONE, "none");
78 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
80 // I decided to skip the initialization since the copy will overwrite
87 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
89 // Are we assigning the object into itself?
96 void InsetGraphicsParams::init()
98 subcaptionText = filename = string();
99 bb = string(); // bounding box
100 draft = false; // draft mode
101 clip = false; // clip image
102 if (lyxrc.display_graphics == "mono")
103 display = MONOCHROME;
104 else if (lyxrc.display_graphics == "gray")
106 else if (lyxrc.display_graphics == "color")
110 subcaption = false; // subfigure
111 width = LyXLength(); // set to 0pt
112 height = LyXLength();
113 lyxwidth = LyXLength(); // for the view in lyx
114 lyxheight = LyXLength();
116 size_type = DEFAULT_SIZE; // do nothing
117 keepAspectRatio = false; //
118 rotateOrigin = "center"; //
119 rotateAngle = 0.0; // in degrees
120 special = string(); // userdefined stuff
125 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
127 filename = igp.filename;
131 display = igp.display;
132 subcaption = igp.subcaption;
133 subcaptionText = igp.subcaptionText;
134 keepAspectRatio = igp.keepAspectRatio;
138 size_type = igp.size_type;
139 lyxwidth = igp.lyxwidth;
140 lyxheight = igp.lyxheight;
141 rotateOrigin = igp.rotateOrigin;
142 rotateAngle = igp.rotateAngle;
143 special = igp.special;
148 void InsetGraphicsParams::testInvariant() const
150 // Filename might be empty (when the dialog is first created).
151 // Assert(!filename.empty());
152 lyx::Assert(display == COLOR ||
153 display == MONOCHROME ||
154 display == GRAYSCALE ||
157 // Angle is in degrees and ranges -360 < angle < 360
158 // The reason for this is that in latex there is a meaning for the
159 // different angles and they are not necessarliy interchangeable,
160 // it depends on the rotation origin.
161 lyx::Assert(rotateAngle < 360.0);
162 lyx::Assert(rotateAngle > -360.0);
166 bool operator==(InsetGraphicsParams const & left,
167 InsetGraphicsParams const & right)
169 if (left.filename == right.filename &&
170 left.bb == right.bb &&
171 left.draft == right.draft &&
172 left.clip == right.clip &&
173 left.display == right.display &&
174 left.subcaption == right.subcaption &&
175 left.subcaptionText == right.subcaptionText &&
176 left.keepAspectRatio == right.keepAspectRatio &&
177 left.width == right.width &&
178 left.height == right.height &&
179 left.scale == right.scale &&
180 left.size_type == right.size_type &&
181 left.lyxwidth == right.lyxwidth &&
182 left.lyxheight == right.lyxheight &&
183 left.rotateOrigin == right.rotateOrigin &&
184 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
185 left.special == right.special)
192 bool operator!=(InsetGraphicsParams const & left,
193 InsetGraphicsParams const & right)
195 return !(left == right);
199 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
201 // If there is no filename, write nothing for it.
202 if (! filename.empty()) {
204 << MakeRelPath(filename, buf->filePath())
207 if (!bb.empty()) // bounding box
208 os << "\tBoundingBox " << bb << '\n';
209 if (clip) // clip image
211 if (draft) // draft mode
213 // Save the display type
214 os << "\tdisplay " << displayTranslator.find(display) << '\n';
215 // Save the subcaption status
217 os << "\tsubcaption\n";
218 if (!subcaptionText.empty())
219 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
220 // we always need the size type
222 // 1: width/height combination
224 os << "\tsize_type " << size_type << '\n';
226 os << "\twidth " << width.asString() << '\n';
228 os << "\theight " << height.asString() << '\n';
230 os << "\tscale " << scale << '\n';
232 os << "\tkeepAspectRatio\n";
233 if (!lyx::float_equal(rotateAngle, 0.0, 0.001))
234 os << "\trotateAngle " << rotateAngle << '\n';
235 if (!rotateOrigin.empty())
236 os << "\trotateOrigin " << rotateOrigin << '\n';
237 if (!special.empty())
238 os << "\tspecial " << special << '\n';
239 if (!lyxwidth.zero()) // the lyx-viewsize
240 os << "\tlyxwidth " << lyxwidth.asString() << '\n';
241 if (!lyxheight.zero())
242 os << "\tlyxheight " << lyxheight.asString();
246 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
249 if (token == "filename") {
251 filename = lex.getString();
252 if (!filename.empty()) {
253 // Make the filename with absolute directory.
254 filename = MakeAbsPath(filename, buf->filePath());
256 } else if (token == "BoundingBox") {
257 for (int i=0; i<4 ;i++) {
259 bb += (lex.getString()+" ");
261 } else if (token == "clip") {
263 } else if (token == "draft") {
265 } else if (token == "display") {
267 string const type = lex.getString();
268 display = displayTranslator.find(type);
269 } else if (token == "subcaption") {
271 } else if (token == "subcaptionText") {
273 subcaptionText = lex.getString();
274 } else if (token == "widthResize") {
276 string const token = lex.getString();
277 if (token == "scale") {
279 scale = lex.getInteger();
283 width = convertResizeValue(token, lex);
287 } else if (token == "size_type") {
289 switch (lex.getInteger()) {
290 case 0 : size_type = DEFAULT_SIZE;
292 case 1 : size_type = WH;
294 case 2 : size_type = SCALE;
296 } else if (token == "width") {
298 width = LyXLength(lex.getString());
300 } else if (token == "heightResize") {
302 height = convertResizeValue(lex.getString(), lex);
303 } else if (token == "height") {
305 height = LyXLength(lex.getString());
307 } else if (token == "keepAspectRatio") {
308 keepAspectRatio = true;
309 } else if (token == "scale") {
311 scale = lex.getInteger();
312 } else if (token == "rotateAngle") {
314 rotateAngle = lex.getFloat();
315 } else if (token == "rotateOrigin") {
317 rotateOrigin=lex.getString();
318 } else if (token == "lyxwidth") {
320 lyxwidth = LyXLength(lex.getString());
321 } else if (token == "lyxheight") {
323 lyxheight = LyXLength(lex.getString());
325 // If it's none of the above, its not ours.