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"
22 #include "support/translator.h"
23 #include "support/filetools.h"
24 #include "support/lyxlib.h"
25 #include "support/LOstream.h"
26 #include "support/LAssert.h"
30 /// This variable keeps a tab on whether the translator was set with the
32 bool translatorsSet = false;
34 /// This is the translator between the Display enum and corresponding lyx
36 Translator< InsetGraphicsParams::DisplayType, string >
37 displayTranslator(InsetGraphicsParams::DEFAULT, "default");
39 // this is only compatibility stuff for the first 1.2 version
40 // it is obselete until 1.3
41 LyXLength convertResizeValue(string const token, LyXLex & lex) {
43 string value = lex.getString(); // "width" or "height"
44 lex.next(); // anyway not interesting
45 value = lex.getString();
46 if (token == "default")
47 return (LyXLength(value+"pt"));
48 else if (token == "cm")
49 return (LyXLength(value+"cm"));
50 else if (token == "inch")
51 return (LyXLength(value+"in"));
52 else if (token == "percentOfColumn")
53 return (LyXLength(value+"c%"));
54 else if (token == "percentOfPage")
55 return (LyXLength(value+"p%"));
56 else return LyXLength("0pt"); // nothing with figinset
62 InsetGraphicsParams::InsetGraphicsParams()
66 if (! translatorsSet) {
67 translatorsSet = true;
68 // Fill the display translator
69 displayTranslator.addPair(DEFAULT, "default");
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 == DEFAULT ||
154 display == MONOCHROME ||
155 display == GRAYSCALE ||
158 // Angle is in degrees and ranges -360 < angle < 360
159 // The reason for this is that in latex there is a meaning for the
160 // different angles and they are not necessarliy interchangeable,
161 // it depends on the rotation origin.
162 lyx::Assert(rotateAngle < 360.0);
163 lyx::Assert(rotateAngle > -360.0);
167 bool operator==(InsetGraphicsParams const & left,
168 InsetGraphicsParams const & right)
170 if (left.filename == right.filename &&
171 left.bb == right.bb &&
172 left.draft == right.draft &&
173 left.clip == right.clip &&
174 left.display == right.display &&
175 left.subcaption == right.subcaption &&
176 left.subcaptionText == right.subcaptionText &&
177 left.keepAspectRatio == right.keepAspectRatio &&
178 left.width == right.width &&
179 left.height == right.height &&
180 left.scale == right.scale &&
181 left.size_type == right.size_type &&
182 left.lyxwidth == right.lyxwidth &&
183 left.lyxheight == right.lyxheight &&
184 left.rotateOrigin == right.rotateOrigin &&
185 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
186 left.special == right.special)
193 bool operator!=(InsetGraphicsParams const & left,
194 InsetGraphicsParams const & right)
196 return !(left == right);
200 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
202 // If there is no filename, write nothing for it.
203 if (! filename.empty()) {
205 << MakeRelPath(filename, buf->filePath())
208 if (!bb.empty()) // bounding box
209 os << "\tBoundingBox " << bb << '\n';
210 if (clip) // clip image
212 if (draft) // draft mode
214 // Save the display type
215 os << "\tdisplay " << displayTranslator.find(display) << '\n';
216 // Save the subcaption status
218 os << "\tsubcaption\n";
219 if (!subcaptionText.empty())
220 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
221 // we always need the size type
223 // 1: width/height combination
225 os << "\tsize_type " << size_type << '\n';
227 os << "\twidth " << width.asString() << '\n';
229 os << "\theight " << height.asString() << '\n';
231 os << "\tscale " << scale << '\n';
233 os << "\tkeepAspectRatio\n";
234 if (!lyx::float_equal(rotateAngle, 0.0, 0.001))
235 os << "\trotateAngle " << rotateAngle << '\n';
236 if (!rotateOrigin.empty())
237 os << "\trotateOrigin " << rotateOrigin << '\n';
238 if (!special.empty())
239 os << "\tspecial " << special << '\n';
240 if (!lyxwidth.zero()) // the lyx-viewsize
241 os << "\tlyxwidth " << lyxwidth.asString() << '\n';
242 if (!lyxheight.zero())
243 os << "\tlyxheight " << lyxheight.asString();
247 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
250 if (token == "filename") {
252 filename = lex.getString();
253 if (!filename.empty()) {
254 // Make the filename with absolute directory.
255 filename = MakeAbsPath(filename, buf->filePath());
257 } else if (token == "BoundingBox") {
258 for (int i=0; i<4 ;i++) {
260 bb += (lex.getString()+" ");
262 } else if (token == "clip") {
264 } else if (token == "draft") {
266 } else if (token == "display") {
268 string const type = lex.getString();
269 display = displayTranslator.find(type);
270 } else if (token == "subcaption") {
272 } else if (token == "subcaptionText") {
274 subcaptionText = lex.getString();
275 } else if (token == "widthResize") {
277 string const token = lex.getString();
278 if (token == "scale") {
280 scale = lex.getInteger();
284 width = convertResizeValue(token, lex);
288 } else if (token == "size_type") {
290 switch (lex.getInteger()) {
291 case 0 : size_type = DEFAULT_SIZE;
293 case 1 : size_type = WH;
295 case 2 : size_type = SCALE;
297 } else if (token == "width") {
299 width = LyXLength(lex.getString());
301 } else if (token == "heightResize") {
303 height = convertResizeValue(lex.getString(), lex);
304 } else if (token == "height") {
306 height = LyXLength(lex.getString());
308 } else if (token == "keepAspectRatio") {
309 keepAspectRatio = true;
310 } else if (token == "scale") {
312 scale = lex.getInteger();
313 } else if (token == "rotateAngle") {
315 rotateAngle = lex.getFloat();
316 } else if (token == "rotateOrigin") {
318 rotateOrigin=lex.getString();
319 } else if (token == "lyxwidth") {
321 lyxwidth = LyXLength(lex.getString());
322 } else if (token == "lyxheight") {
324 lyxheight = LyXLength(lex.getString());
326 // If it's none of the above, its not ours.