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"
20 #include "BufferView.h"
28 LyXLength::LyXLength()
29 : val_(0), unit_(LyXLength::PT)
33 LyXLength::LyXLength(double v, LyXLength::UNIT u)
38 LyXLength::LyXLength(string const & data)
39 : val_(0), unit_(LyXLength::PT)
43 if (!isValidLength (data, &tmp))
44 return; // should raise an exception
51 string const LyXLength::asString() const
54 buffer << val_ << unit_name[unit_]; // setw?
55 return buffer.str().c_str();
59 string const LyXLength::asLatexString() const
65 buffer << abs(static_cast<int>(val_/100)) << "."
66 << abs(static_cast<int>(val_)%100) << "\\columnwidth";
69 buffer << abs(static_cast<int>(val_/100)) << "."
70 << abs(static_cast<int>(val_)%100) << "\\pagewidth";
73 buffer << abs(static_cast<int>(val_/100)) << "."
74 << abs(static_cast<int>(val_)%100) << "\\linewidth";
77 buffer << val_ << unit_name[unit_]; // setw?
80 return buffer.str().c_str();
84 double LyXLength::value() const
90 LyXLength::UNIT LyXLength::unit() const
96 void LyXLength::value(double v)
102 void LyXLength::unit(LyXLength::UNIT u)
108 bool LyXLength::zero() const
114 int LyXLength::inPixels(BufferView const * bv) const
116 // Height of a normal line in pixels (zoom factor considered)
117 int height = bv->text->defaultHeight(); // [pixels]
118 int default_width = bv->workWidth();
120 // Zoom factor specified by user in percent
121 double const zoom = lyxrc.zoom / 100.0; // [percent]
123 // DPI setting for monitor: pixels/inch
124 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
126 // Pixel values are scaled so that the ratio
127 // between lengths and font sizes on the screen
128 // is the same as on paper.
130 // we don't care about sign of value, we
131 // display negative space with text too
133 int val_sign = val_ < 0.0 ? -1 : 1;
137 // Scaled point: sp = 1/65536 pt
138 result = zoom * dpi * val_
139 / (72.27 * 65536); // 4736286.7
142 // Point: 1 pt = 1/72.27 inch
143 result = zoom * dpi * val_
147 // Big point: 1 bp = 1/72 inch
148 result = zoom * dpi * val_
152 // Didot: 1157dd = 1238 pt?
153 result = zoom * dpi * val_
154 / (72.27 / (0.376 * 2.845)); // 67.559735
157 // Millimeter: 1 mm = 1/25.4 inch
158 result = zoom * dpi * val_
162 // Pica: 1 pc = 12 pt
163 result = zoom * dpi * val_
164 / (72.27 / 12); // 6.0225
167 // Cicero: 1 cc = 12 dd
168 result = zoom * dpi * val_
169 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
172 // Centimeter: 1 cm = 1/2.54 inch
173 result = zoom * dpi * val_
178 result = zoom * dpi * val_;
181 // Ex: The height of an "x"
182 result = zoom * val_ * height / 2; // what to / width?
184 case LyXLength::EM: // what to / width?
185 // Em: The width of an "m"
186 result = zoom * val_ * height / 2; // Why 2?
188 case LyXLength::MU: // This is probably only allowed in
190 result = zoom * val_ * height;
192 case LyXLength::PW: // Always % of workarea
196 result = val_ * default_width / 100;
198 case LyXLength::UNIT_NONE:
199 result = 0; // this cannot happen
202 return static_cast<int>(result * val_sign + 0.5);
206 bool operator==(LyXLength const & l1, LyXLength const & l2)
208 return l1.value() == l2.value() && l1.unit() == l2.unit();
212 bool operator!=(LyXLength const & l1, LyXLength const & l2)