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"
25 #include "support/LAssert.h"
33 /// This variable keeps a tab on whether the translator was set with the
35 bool translatorsSet = false;
37 /// This is the translator between the Display enum and corresponding lyx
39 Translator< InsetGraphicsParams::DisplayType, string >
40 displayTranslator(InsetGraphicsParams::DEFAULT, "default");
42 // this is only compatibility stuff for the first 1.2 version
43 // it is obselete until 1.3
44 LyXLength convertResizeValue(string const token, LyXLex & lex) {
46 string value = lex.getString(); // "width" or "height"
47 lex.next(); // anyway not interesting
48 value = lex.getString();
49 if (token == "default")
50 return (LyXLength(value+"pt"));
51 else if (token == "cm")
52 return (LyXLength(value+"cm"));
53 else if (token == "inch")
54 return (LyXLength(value+"in"));
55 else if (token == "percentOfColumn")
56 return (LyXLength(value+"c%"));
57 else if (token == "percentOfPage")
58 return (LyXLength(value+"p%"));
59 else return LyXLength("0pt"); // nothing with figinset
65 InsetGraphicsParams::InsetGraphicsParams()
69 if (! translatorsSet) {
70 translatorsSet = true;
71 // Fill the display translator
72 displayTranslator.addPair(DEFAULT, "default");
73 displayTranslator.addPair(MONOCHROME, "monochrome");
74 displayTranslator.addPair(GRAYSCALE, "grayscale");
75 displayTranslator.addPair(COLOR, "color");
76 displayTranslator.addPair(NONE, "none");
81 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
83 // I decided to skip the initialization since the copy will overwrite
90 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
92 // Are we assigning the object into itself?
99 void InsetGraphicsParams::init()
101 subcaptionText = filename = string();
102 bb = string(); // bounding box
103 draft = false; // draft mode
104 clip = false; // clip image
105 display = DEFAULT; // see pref
106 subcaption = false; // subfigure
107 noUnzip = false; // unzip files
108 width = LyXLength(); // set to 0pt
109 height = LyXLength();
110 lyxwidth = LyXLength(); // for the view in lyx
111 lyxheight = LyXLength(); // also set to 0pt
112 scale = 0; // unit is %
113 lyxscale = 0; // same for lyxview
114 size_type = DEFAULT_SIZE; // do nothing
115 lyxsize_type = DEFAULT_SIZE; // do nothing
116 keepAspectRatio = false; // only for latex
117 rotate = false; // Rotating
118 rotateOrigin = "center"; // Origin
119 rotateAngle = 0.0; // in degrees
120 special = string(); // userdefined stuff
123 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
125 filename = igp.filename;
129 display = igp.display;
130 subcaption = igp.subcaption;
131 subcaptionText = igp.subcaptionText;
132 noUnzip = igp.noUnzip;
133 keepAspectRatio = igp.keepAspectRatio;
137 size_type = igp.size_type;
138 lyxsize_type = igp.lyxsize_type;
139 lyxwidth = igp.lyxwidth;
140 lyxheight = igp.lyxheight;
141 lyxscale = igp.lyxscale;
143 rotateOrigin = igp.rotateOrigin;
144 rotateAngle = igp.rotateAngle;
145 special = igp.special;
148 bool operator==(InsetGraphicsParams const & left,
149 InsetGraphicsParams const & right)
151 if (left.filename == right.filename &&
152 left.bb == right.bb &&
153 left.draft == right.draft &&
154 left.clip == right.clip &&
155 left.display == right.display &&
156 left.subcaption == right.subcaption &&
157 left.noUnzip == right.noUnzip &&
158 left.subcaptionText == right.subcaptionText &&
159 left.keepAspectRatio == right.keepAspectRatio &&
160 left.width == right.width &&
161 left.height == right.height &&
162 left.scale == right.scale &&
163 left.size_type == right.size_type &&
164 left.lyxsize_type == right.lyxsize_type &&
165 left.lyxwidth == right.lyxwidth &&
166 left.lyxheight == right.lyxheight &&
167 left.lyxscale == right.lyxscale &&
168 left.rotate == right.rotate &&
169 left.rotateOrigin == right.rotateOrigin &&
170 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
171 left.special == right.special)
178 bool operator!=(InsetGraphicsParams const & left,
179 InsetGraphicsParams const & right)
181 return !(left == right);
185 void InsetGraphicsParams::Write(ostream & os) const
187 // If there is no filename, write nothing for it.
188 if (!filename.empty()) {
189 os << "\tfilename " << filename << '\n';
191 if (!bb.empty()) // bounding box
192 os << "\tBoundingBox " << bb << '\n';
193 if (clip) // clip image
195 if (draft) // draft mode
197 // Save the display type for the view inside lyx
198 os << "\tdisplay " << displayTranslator.find(display) << '\n';
199 // Save the subcaption status
201 os << "\tsubcaption\n";
202 if (!subcaptionText.empty())
203 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
206 // we always need the size type
208 // 1: width/height combination
210 os << "\tsize_type " << size_type << '\n';
212 os << "\twidth " << width.asString() << '\n';
214 os << "\theight " << height.asString() << '\n';
216 os << "\tscale " << scale << '\n';
218 os << "\tkeepAspectRatio\n";
221 if (!lyx::float_equal(rotateAngle, 0.0, 0.001))
222 os << "\trotateAngle " << rotateAngle << '\n';
223 if (!rotateOrigin.empty())
224 os << "\trotateOrigin " << rotateOrigin << '\n';
225 if (!special.empty())
226 os << "\tspecial " << special << '\n';
227 // the values for the view in lyx
228 os << "\tlyxsize_type " << lyxsize_type << '\n';
229 if (!lyxwidth.zero()) // the lyx-viewsize
230 os << "\tlyxwidth " << lyxwidth.asString() << '\n';
231 if (!lyxheight.zero())
232 os << "\tlyxheight " << lyxheight.asString();
234 os << "\tlyxscale " << lyxscale << '\n';
238 bool InsetGraphicsParams::Read(LyXLex & lex, string const& token)
240 if (token == "filename") {
242 filename = lex.getString();
243 } else if (token == "BoundingBox") {
244 for (int i=0; i<4 ;i++) {
246 bb += (lex.getString()+" ");
248 } else if (token == "clip") {
250 } else if (token == "draft") {
252 } else if (token == "display") {
254 if (IsFileReadable(filename)) {
255 string const type = lex.getString();
256 display = displayTranslator.find(type);
259 } else if (token == "subcaption") {
261 } else if (token == "subcaptionText") {
263 subcaptionText = lex.getString();
264 } else if (token == "noUnzip") {
266 } else if (token == "size_type") {
268 switch (lex.getInteger()) {
269 case 0 : size_type = DEFAULT_SIZE;
271 case 1 : size_type = WH;
273 case 2 : size_type = SCALE;
275 } else if (token == "width") {
277 width = LyXLength(lex.getString());
279 } else if (token == "height") {
281 height = LyXLength(lex.getString());
283 } else if (token == "keepAspectRatio") {
284 keepAspectRatio = true;
285 } else if (token == "scale") {
287 scale = lex.getInteger();
288 } else if (token == "rotate") {
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();
314 // now the compytibility stuff for "old" 1.2.0 files which uses
315 // the first try of the new graphic inset. Can be deleted, when
317 } else if (token == "widthResize") {
319 string const token = lex.getString();
320 if (token == "scale") {
322 scale = lex.getInteger();
326 width = convertResizeValue(token, lex);
330 } else if (token == "heightResize") {
332 height = convertResizeValue(lex.getString(), lex);
333 // end compytibility stuff
334 } else { // If it's none of the above, its not ours.