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"
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::MONOCHROME, "monochrome");
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(MONOCHROME, "monochrome");
70 displayTranslator.addPair(GRAYSCALE, "grayscale");
71 displayTranslator.addPair(COLOR, "color");
72 displayTranslator.addPair(NONE, "none");
77 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
79 // I decided to skip the initialization since the copy will overwrite
86 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
88 // Are we assigning the object into itself?
95 void InsetGraphicsParams::init()
97 subcaptionText = filename = string();
98 bb = string(); // bounding box
99 draft = false; // draft mode
100 clip = false; // clip image
101 display = MONOCHROME; // LyX-View
102 subcaption = false; // subfigure
103 width = LyXLength(); // set to 0pt
104 height = LyXLength();
105 lyxwidth = LyXLength(); // for the view in lyx
106 lyxheight = LyXLength();
108 size_type = DEFAULT_SIZE;
109 keepAspectRatio = false;
110 rotateOrigin = string(); //
111 rotateAngle = 0.0; // in degrees
112 special = string(); // userdefined stuff
117 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
119 filename = igp.filename;
123 display = igp.display;
124 subcaption = igp.subcaption;
125 subcaptionText = igp.subcaptionText;
126 keepAspectRatio = igp.keepAspectRatio;
130 size_type = igp.size_type;
131 lyxwidth = igp.lyxwidth;
132 lyxheight = igp.lyxheight;
133 rotateOrigin = igp.rotateOrigin;
134 rotateAngle = igp.rotateAngle;
135 special = igp.special;
140 void InsetGraphicsParams::testInvariant() const
142 // Filename might be empty (when the dialog is first created).
143 // Assert(!filename.empty());
144 lyx::Assert(display == COLOR ||
145 display == MONOCHROME ||
146 display == GRAYSCALE ||
149 // Angle is in degrees and ranges -360 < angle < 360
150 // The reason for this is that in latex there is a meaning for the
151 // different angles and they are not necessarliy interchangeable,
152 // it depends on the rotation origin.
153 lyx::Assert(rotateAngle < 360.0);
154 lyx::Assert(rotateAngle > -360.0);
158 bool operator==(InsetGraphicsParams const & left,
159 InsetGraphicsParams const & right)
161 if (left.filename == right.filename &&
162 left.bb == right.bb &&
163 left.draft == right.draft &&
164 left.clip == right.clip &&
165 left.display == right.display &&
166 left.subcaption == right.subcaption &&
167 left.subcaptionText == right.subcaptionText &&
168 left.keepAspectRatio == right.keepAspectRatio &&
169 left.width == right.width &&
170 left.height == right.height &&
171 left.scale == right.scale &&
172 left.size_type == right.size_type &&
173 left.lyxwidth == right.lyxwidth &&
174 left.lyxheight == right.lyxheight &&
175 left.rotateOrigin == right.rotateOrigin &&
176 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
177 left.special == right.special)
184 bool operator!=(InsetGraphicsParams const & left,
185 InsetGraphicsParams const & right)
187 return !(left == right);
191 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
193 // If there is no filename, write nothing for it.
194 if (! filename.empty()) {
196 << MakeRelPath(filename, buf->filePath())
199 if (!bb.empty()) // bounding box
200 os << "\tBoundingBox " << bb << '\n';
201 if (clip) // clip image
203 if (draft) // draft mode
205 // Save the display type
206 os << "\tdisplay " << displayTranslator.find(display) << '\n';
207 // Save the subcaption status
209 os << "\tsubcaption\n";
210 if (!subcaptionText.empty())
211 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
212 // we always need the size type
214 // 1: width/height combination
216 os << "\tsize_type " << size_type << '\n';
218 os << "\twidth " << width.asString() << '\n';
220 os << "\theight " << height.asString() << '\n';
222 os << "\tscale " << scale << '\n';
224 os << "\tkeepAspectRatio\n";
225 if (!lyx::float_equal(rotateAngle, 0.0, 0.001))
226 os << "\trotateAngle " << rotateAngle << '\n';
227 if (!rotateOrigin.empty())
228 os << "\trotateOrigin " << rotateOrigin << '\n';
229 if (!special.empty())
230 os << "\tspecial " << special << '\n';
231 if (!lyxwidth.zero()) // the lyx-viewsize
232 os << "\tlyxwidth " << lyxwidth.asString() << '\n';
233 if (!lyxheight.zero())
234 os << "\tlyxheight " << lyxheight.asString();
238 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
241 if (token == "filename") {
243 filename = lex.getString();
244 if (!filename.empty()) {
245 // Make the filename with absolute directory.
246 filename = MakeAbsPath(filename, buf->filePath());
248 } else if (token == "BoundingBox") {
249 for (int i=0; i<4 ;i++) {
251 bb += (lex.getString()+" ");
253 } else if (token == "clip") {
255 } else if (token == "draft") {
257 } else if (token == "display") {
259 string const type = lex.getString();
260 display = displayTranslator.find(type);
261 } else if (token == "subcaption") {
263 } else if (token == "subcaptionText") {
265 subcaptionText = lex.getString();
266 } else if (token == "widthResize") {
268 string const token = lex.getString();
269 if (token == "scale") {
271 scale = lex.getInteger();
275 width = convertResizeValue(token, lex);
279 } else if (token == "size_type") {
281 switch (lex.getInteger()) {
282 case 0 : size_type = DEFAULT_SIZE;
284 case 1 : size_type = WH;
286 case 2 : size_type = SCALE;
288 } else if (token == "width") {
290 width = LyXLength(lex.getString());
292 } else if (token == "heightResize") {
294 height = convertResizeValue(lex.getString(), lex);
295 } else if (token == "height") {
297 height = LyXLength(lex.getString());
299 } else if (token == "keepAspectRatio") {
300 keepAspectRatio = true;
301 } else if (token == "rotateAngle") {
303 rotateAngle = lex.getFloat();
304 } else if (token == "rotateOrigin") {
306 rotateOrigin=lex.getString();
307 } else if (token == "lyxwidth") {
309 lyxwidth = LyXLength(lex.getString());
310 } else if (token == "lyxheight") {
312 lyxheight = LyXLength(lex.getString());
314 // If it's none of the above, its not ours.