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"
28 LyXLength::LyXLength()
29 : val_(0), unit_(LyXLength::PT)
33 LyXLength::LyXLength(double v, LyXLength::UNIT u)
38 #ifndef NO_PEXTRA_REALLY
39 // compatibility stuff < version 1.2.0pre and for
40 // "old" 1.2.0 files before the pre
42 string const convertOldRelLength(string const & oldLength)
44 // we can have only one or none of the following
45 if (oldLength.find("c%") != string::npos) {
46 return subst(oldLength,"c%","col%");
48 } else if (oldLength.find("t%") != string::npos) {
49 if (oldLength.find("text%") != string::npos)
52 return subst(oldLength,"t%","text%");
54 } else if (oldLength.find("l%") != string::npos) {
55 if (oldLength.find("col%") != string::npos)
58 return subst(oldLength,"l%","line%");
60 } else if (oldLength.find("p%") != string::npos)
61 return subst(oldLength,"p%","page%");
68 LyXLength::LyXLength(string const & data)
69 : val_(0), unit_(LyXLength::PT)
73 #ifndef NO_PEXTRA_REALLY
74 // this is needed for 1.1.x minipages with width like %t
75 if (!isValidLength (convertOldRelLength(data), &tmp))
77 if (!isValidLength (data, &tmp))
79 if (!isValidLength (convertOldRelLength(data), &tmp))
80 return; // should raise an exception
87 string const LyXLength::asString() const
90 buffer << val_ << unit_name[unit_]; // setw?
91 return buffer.str().c_str();
95 string const LyXLength::asLatexString() const
100 buffer << abs(static_cast<int>(val_/100)) << "."
101 << abs(static_cast<int>(val_)%100) << "\\textwidth";
104 buffer << abs(static_cast<int>(val_/100)) << "."
105 << abs(static_cast<int>(val_)%100) << "\\columnwidth";
108 buffer << abs(static_cast<int>(val_/100)) << "."
109 << abs(static_cast<int>(val_)%100) << "\\paperwidth";
112 buffer << abs(static_cast<int>(val_/100)) << "."
113 << abs(static_cast<int>(val_)%100) << "\\linewidth";
116 buffer << val_ << unit_name[unit_]; // setw?
119 return buffer.str().c_str();
123 double LyXLength::value() const
129 LyXLength::UNIT LyXLength::unit() const
135 void LyXLength::value(double v)
141 void LyXLength::unit(LyXLength::UNIT u)
147 bool LyXLength::zero() const
153 int LyXLength::inPixels(int default_width, int default_height) const
155 // Zoom factor specified by user in percent
156 double const zoom = lyxrc.zoom / 100.0; // [percent]
158 // DPI setting for monitor: pixels/inch
159 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
161 // Pixel values are scaled so that the ratio
162 // between lengths and font sizes on the screen
163 // is the same as on paper.
165 // we don't care about sign of value, we
166 // display negative space with text too
168 int val_sign = val_ < 0.0 ? -1 : 1;
172 // Scaled point: sp = 1/65536 pt
173 result = zoom * dpi * val_
174 / (72.27 * 65536); // 4736286.7
177 // Point: 1 pt = 1/72.27 inch
178 result = zoom * dpi * val_
182 // Big point: 1 bp = 1/72 inch
183 result = zoom * dpi * val_
187 // Didot: 1157dd = 1238 pt?
188 result = zoom * dpi * val_
189 / (72.27 / (0.376 * 2.845)); // 67.559735
192 // Millimeter: 1 mm = 1/25.4 inch
193 result = zoom * dpi * val_
197 // Pica: 1 pc = 12 pt
198 result = zoom * dpi * val_
199 / (72.27 / 12); // 6.0225
202 // Cicero: 1 cc = 12 dd
203 result = zoom * dpi * val_
204 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
207 // Centimeter: 1 cm = 1/2.54 inch
208 result = zoom * dpi * val_
213 result = zoom * dpi * val_;
216 // Ex: The height of an "x"
217 result = zoom * val_ * default_height / 2; // what to / width?
219 case LyXLength::EM: // what to / width?
220 // Em: The width of an "m"
221 result = zoom * val_ * default_height / 2; // Why 2?
223 case LyXLength::MU: // This is probably only allowed in
225 result = zoom * val_ * default_height;
227 case LyXLength::PW: // Always % of workarea
231 result = val_ * default_width / 100;
233 case LyXLength::UNIT_NONE:
234 result = 0; // this cannot happen
237 return static_cast<int>(result * val_sign + 0.5);
241 bool operator==(LyXLength const & l1, LyXLength const & l2)
243 return l1.value() == l2.value() && l1.unit() == l2.unit();
247 bool operator!=(LyXLength const & l1, LyXLength const & l2)