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"
20 #include "support/translator.h"
21 #include "support/filetools.h"
22 #include "support/lyxlib.h"
23 #include "support/LOstream.h"
24 #include "support/LAssert.h"
28 /// This variable keeps a tab on whether the translator was set with the
30 bool translatorsSet = false;
32 /// This is the translator between the Display enum and corresponding lyx
34 Translator< InsetGraphicsParams::DisplayType, string >
35 displayTranslator(InsetGraphicsParams::DEFAULT, "default");
37 // this is only compatibility stuff for the first 1.2 version
38 // it is obselete until 1.3
39 LyXLength convertResizeValue(string const token, LyXLex & lex) {
41 string value = lex.getString(); // "width" or "height"
42 lex.next(); // anyway not interesting
43 value = lex.getString();
44 if (token == "default")
45 return (LyXLength(value+"pt"));
46 else if (token == "cm")
47 return (LyXLength(value+"cm"));
48 else if (token == "inch")
49 return (LyXLength(value+"in"));
50 else if (token == "percentOfColumn")
51 return (LyXLength(value+"c%"));
52 else if (token == "percentOfPage")
53 return (LyXLength(value+"p%"));
54 else return LyXLength("0pt"); // nothing with figinset
60 InsetGraphicsParams::InsetGraphicsParams()
64 if (! translatorsSet) {
65 translatorsSet = true;
66 // Fill the display translator
67 displayTranslator.addPair(DEFAULT, "default");
68 displayTranslator.addPair(MONOCHROME, "monochrome");
69 displayTranslator.addPair(GRAYSCALE, "grayscale");
70 displayTranslator.addPair(COLOR, "color");
71 displayTranslator.addPair(NONE, "none");
76 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
78 // I decided to skip the initialization since the copy will overwrite
85 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
87 // Are we assigning the object into itself?
94 void InsetGraphicsParams::init()
96 subcaptionText = filename = string();
97 bb = string(); // bounding box
98 draft = false; // draft mode
99 clip = false; // clip image
101 subcaption = false; // subfigure
102 width = LyXLength(); // set to 0pt
103 height = LyXLength();
104 lyxwidth = LyXLength(); // for the view in lyx
105 lyxheight = LyXLength();
108 size_type = DEFAULT_SIZE; // do nothing
109 lyxsize_type = DEFAULT_SIZE; // do nothing
110 keepAspectRatio = false; //
111 rotateOrigin = "center"; //
112 rotateAngle = 0.0; // in degrees
113 special = string(); // userdefined stuff
116 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
118 filename = igp.filename;
122 display = igp.display;
123 subcaption = igp.subcaption;
124 subcaptionText = igp.subcaptionText;
125 keepAspectRatio = igp.keepAspectRatio;
129 size_type = igp.size_type;
130 lyxsize_type = igp.lyxsize_type;
131 lyxwidth = igp.lyxwidth;
132 lyxheight = igp.lyxheight;
133 lyxscale = igp.lyxscale;
134 rotateOrigin = igp.rotateOrigin;
135 rotateAngle = igp.rotateAngle;
136 special = igp.special;
139 bool operator==(InsetGraphicsParams const & left,
140 InsetGraphicsParams const & right)
142 if (left.filename == right.filename &&
143 left.bb == right.bb &&
144 left.draft == right.draft &&
145 left.clip == right.clip &&
146 left.display == right.display &&
147 left.subcaption == right.subcaption &&
148 left.subcaptionText == right.subcaptionText &&
149 left.keepAspectRatio == right.keepAspectRatio &&
150 left.width == right.width &&
151 left.height == right.height &&
152 left.scale == right.scale &&
153 left.size_type == right.size_type &&
154 left.lyxsize_type == right.lyxsize_type &&
155 left.lyxwidth == right.lyxwidth &&
156 left.lyxheight == right.lyxheight &&
157 left.lyxscale == right.lyxscale &&
158 left.rotateOrigin == right.rotateOrigin &&
159 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
160 left.special == right.special)
167 bool operator!=(InsetGraphicsParams const & left,
168 InsetGraphicsParams const & right)
170 return !(left == right);
174 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
176 // If there is no filename, write nothing for it.
177 if (! filename.empty()) {
179 << MakeRelPath(filename, buf->filePath())
182 if (!bb.empty()) // bounding box
183 os << "\tBoundingBox " << bb << '\n';
184 if (clip) // clip image
186 if (draft) // draft mode
188 // Save the display type
189 os << "\tdisplay " << displayTranslator.find(display) << '\n';
190 // Save the subcaption status
192 os << "\tsubcaption\n";
193 if (!subcaptionText.empty())
194 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
195 // we always need the size type
197 // 1: width/height combination
199 os << "\tsize_type " << size_type << '\n';
201 os << "\twidth " << width.asString() << '\n';
203 os << "\theight " << height.asString() << '\n';
205 os << "\tscale " << scale << '\n';
207 os << "\tkeepAspectRatio\n";
208 if (!lyx::float_equal(rotateAngle, 0.0, 0.001))
209 os << "\trotateAngle " << rotateAngle << '\n';
210 if (!rotateOrigin.empty())
211 os << "\trotateOrigin " << rotateOrigin << '\n';
212 if (!special.empty())
213 os << "\tspecial " << special << '\n';
214 os << "\tlyxsize_type " << lyxsize_type << '\n';
215 if (!lyxwidth.zero()) // the lyx-viewsize
216 os << "\tlyxwidth " << lyxwidth.asString() << '\n';
217 if (!lyxheight.zero())
218 os << "\tlyxheight " << lyxheight.asString();
220 os << "\tlyxscale " << lyxscale << '\n';
224 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
227 if (token == "filename") {
229 filename = lex.getString();
230 if (!filename.empty()) {
231 // Make the filename with absolute directory.
232 filename = MakeAbsPath(filename, buf->filePath());
234 } else if (token == "BoundingBox") {
235 for (int i=0; i<4 ;i++) {
237 bb += (lex.getString()+" ");
239 } else if (token == "clip") {
241 } else if (token == "draft") {
243 } else if (token == "display") {
245 string const type = lex.getString();
246 display = displayTranslator.find(type);
247 } else if (token == "subcaption") {
249 } else if (token == "subcaptionText") {
251 subcaptionText = lex.getString();
252 } else if (token == "widthResize") {
254 string const token = lex.getString();
255 if (token == "scale") {
257 scale = lex.getInteger();
261 width = convertResizeValue(token, lex);
265 } else if (token == "size_type") {
267 switch (lex.getInteger()) {
268 case 0 : size_type = DEFAULT_SIZE;
270 case 1 : size_type = WH;
272 case 2 : size_type = SCALE;
274 } else if (token == "width") {
276 width = LyXLength(lex.getString());
278 } else if (token == "heightResize") {
280 height = convertResizeValue(lex.getString(), lex);
281 } else if (token == "height") {
283 height = LyXLength(lex.getString());
285 } else if (token == "keepAspectRatio") {
286 keepAspectRatio = true;
287 } else if (token == "scale") {
289 scale = lex.getInteger();
290 } else if (token == "rotateAngle") {
292 rotateAngle = lex.getFloat();
293 } else if (token == "rotateOrigin") {
295 rotateOrigin=lex.getString();
296 } else if (token == "lyxsize_type") {
298 switch (lex.getInteger()) {
299 case 0 : lyxsize_type = DEFAULT_SIZE;
301 case 1 : lyxsize_type = WH;
303 case 2 : lyxsize_type = SCALE;
305 } else if (token == "lyxwidth") {
307 lyxwidth = LyXLength(lex.getString());
308 } else if (token == "lyxheight") {
310 lyxheight = LyXLength(lex.getString());
311 } else if (token == "lyxscale") {
313 lyxscale = lex.getInteger();
315 // If it's none of the above, its not ours.