1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
17 #include "lyxlength.h"
18 #include "lengthcommon.h"
21 #include "support/lstrings.h"
29 LyXLength::LyXLength()
30 : val_(0), unit_(LyXLength::PT)
34 LyXLength::LyXLength(double v, LyXLength::UNIT u)
39 #ifndef NO_PEXTRA_REALLY
40 // compatibility stuff < version 1.2.0pre and for
41 // "old" 1.2.0 files before the pre
43 string const convertOldRelLength(string const & oldLength)
45 // we can have only one or none of the following
46 if (oldLength.find("c%") != string::npos) {
47 return subst(oldLength,"c%","col%");
49 } else if (oldLength.find("t%") != string::npos) {
50 if (oldLength.find("text%") != string::npos)
53 return subst(oldLength,"t%","text%");
55 } else if (oldLength.find("l%") != string::npos) {
56 if (oldLength.find("col%") != string::npos)
59 return subst(oldLength,"l%","line%");
61 } else if (oldLength.find("p%") != string::npos)
62 return subst(oldLength,"p%","page%");
69 LyXLength::LyXLength(string const & data)
70 : val_(0), unit_(LyXLength::PT)
74 #ifndef NO_PEXTRA_REALLY
75 // this is needed for 1.1.x minipages with width like %t
76 if (!isValidLength (convertOldRelLength(data), &tmp))
78 if (!isValidLength (data, &tmp))
80 if (!isValidLength (convertOldRelLength(data), &tmp))
81 return; // should raise an exception
88 string const LyXLength::asString() const
91 buffer << val_ << unit_name[unit_]; // setw?
92 return buffer.str().c_str();
96 string const LyXLength::asLatexString() const
101 buffer << abs(static_cast<int>(val_/100)) << "."
102 << abs(static_cast<int>(val_)%100) << "\\textwidth";
105 buffer << abs(static_cast<int>(val_/100)) << "."
106 << abs(static_cast<int>(val_)%100) << "\\columnwidth";
109 buffer << abs(static_cast<int>(val_/100)) << "."
110 << abs(static_cast<int>(val_)%100) << "\\paperwidth";
113 buffer << abs(static_cast<int>(val_/100)) << "."
114 << abs(static_cast<int>(val_)%100) << "\\linewidth";
117 buffer << val_ << unit_name[unit_]; // setw?
120 return buffer.str().c_str();
124 double LyXLength::value() const
130 LyXLength::UNIT LyXLength::unit() const
136 void LyXLength::value(double v)
142 void LyXLength::unit(LyXLength::UNIT u)
148 bool LyXLength::zero() const
154 int LyXLength::inPixels(int default_width, int default_height) const
156 // Zoom factor specified by user in percent
157 double const zoom = lyxrc.zoom / 100.0; // [percent]
159 // DPI setting for monitor: pixels/inch
160 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
162 // Pixel values are scaled so that the ratio
163 // between lengths and font sizes on the screen
164 // is the same as on paper.
166 // we don't care about sign of value, we
167 // display negative space with text too
169 int val_sign = val_ < 0.0 ? -1 : 1;
173 // Scaled point: sp = 1/65536 pt
174 result = zoom * dpi * val_
175 / (72.27 * 65536); // 4736286.7
178 // Point: 1 pt = 1/72.27 inch
179 result = zoom * dpi * val_
183 // Big point: 1 bp = 1/72 inch
184 result = zoom * dpi * val_
188 // Didot: 1157dd = 1238 pt?
189 result = zoom * dpi * val_
190 / (72.27 / (0.376 * 2.845)); // 67.559735
193 // Millimeter: 1 mm = 1/25.4 inch
194 result = zoom * dpi * val_
198 // Pica: 1 pc = 12 pt
199 result = zoom * dpi * val_
200 / (72.27 / 12); // 6.0225
203 // Cicero: 1 cc = 12 dd
204 result = zoom * dpi * val_
205 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
208 // Centimeter: 1 cm = 1/2.54 inch
209 result = zoom * dpi * val_
214 result = zoom * dpi * val_;
217 // Ex: The height of an "x"
218 result = zoom * val_ * default_height / 2; // what to / width?
220 case LyXLength::EM: // what to / width?
221 // Em: The width of an "m"
222 result = zoom * val_ * default_height / 2; // Why 2?
224 case LyXLength::MU: // This is probably only allowed in
226 result = zoom * val_ * default_height;
228 case LyXLength::PW: // Always % of workarea
232 result = val_ * default_width / 100;
234 case LyXLength::UNIT_NONE:
235 result = 0; // this cannot happen
238 return static_cast<int>(result * val_sign + 0.5);
242 int LyXLength::inBP() const
244 // return any LyXLength value as a one with
245 // the PostScript point, called bp (big points)
247 int val_sign = val_ < 0.0 ? -1 : 1;
251 result = val_ * 28.346;
255 result = val_ * 2.8346;
259 result = val_ * 72.0;
262 // no other than bp possible
266 return static_cast<int>(result * val_sign + 0.5);
270 bool operator==(LyXLength const & l1, LyXLength const & l2)
272 return l1.value() == l2.value() && l1.unit() == l2.unit();
276 bool operator!=(LyXLength const & l1, LyXLength const & l2)