3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Matthias Ettrich
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
13 * Full author contact details are available in file CREDITS.
18 #include "lyxlength.h"
19 #include "lengthcommon.h"
22 #include "support/docstream.h"
30 using std::ostringstream;
34 LyXLength::LyXLength()
35 : val_(0), unit_(LyXLength::UNIT_NONE)
39 LyXLength::LyXLength(double v, LyXLength::UNIT u)
44 LyXLength::LyXLength(string const & data)
45 : val_(0), unit_(LyXLength::PT)
49 if (!isValidLength(data, &tmp))
50 return; // should raise an exception
57 string const LyXLength::asString() const
60 os << val_ << unit_name[unit_]; // setw?
65 docstring const LyXLength::asDocstring() const
68 os << val_ << unit_name[unit_]; // setw?
73 string const LyXLength::asLatexString() const
78 os << val_ / 100.0 << "\\textwidth";
81 os << val_ / 100.0 << "\\columnwidth";
84 os << val_ / 100.0 << "\\paperwidth";
87 os << val_ / 100.0 << "\\linewidth";
90 os << val_ / 100.0 << "\\paperheight";
93 os << val_ / 100.0 << "\\textheight";
96 os << val_ << unit_name[unit_];
103 double LyXLength::value() const
109 LyXLength::UNIT LyXLength::unit() const
115 void LyXLength::value(double v)
121 void LyXLength::unit(LyXLength::UNIT u)
127 bool LyXLength::zero() const
133 bool LyXLength::empty() const
135 return unit_ == LyXLength::UNIT_NONE;
139 int LyXLength::inPixels(int text_width, int em_width_base) const
141 // Zoom factor specified by user in percent
142 double const zoom = lyxrc.zoom / 100.0; // [percent]
144 // DPI setting for monitor: pixels/inch
145 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
147 double const em_width = (em_width_base > 0)
149 : 10*(dpi/72.27)*zoom;
150 // A different estimate for em_width is
151 // theFontMetrics(LyXFont(LyXFont::ALL_SANE)).width('M')
152 // but this estimate might not be more accurate as the screen font
153 // is different then the latex font.
155 // Pixel values are scaled so that the ratio
156 // between lengths and font sizes on the screen
157 // is the same as on paper.
163 // Scaled point: sp = 1/65536 pt
164 result = zoom * dpi * val_
165 / (72.27 * 65536); // 4736286.7
168 // Point: 1 pt = 1/72.27 inch
169 result = zoom * dpi * val_
173 // Big point: 1 bp = 1/72 inch
174 result = zoom * dpi * val_
178 // Didot: 1157dd = 1238 pt?
179 result = zoom * dpi * val_
180 / (72.27 / (0.376 * 2.845)); // 67.559735
183 // Millimeter: 1 mm = 1/25.4 inch
184 result = zoom * dpi * val_
188 // Pica: 1 pc = 12 pt
189 result = zoom * dpi * val_
190 / (72.27 / 12); // 6.0225
193 // Cicero: 1 cc = 12 dd
194 result = zoom * dpi * val_
195 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
198 // Centimeter: 1 cm = 1/2.54 inch
199 result = zoom * dpi * val_
204 result = zoom * dpi * val_;
207 // Ex: The height of an "x"
208 // 0.4305 is the ration between 1ex and 1em in cmr10
209 result = val_ * em_width * 0.4305;
212 // Em: The width of an "m"
213 result = val_ * em_width;
216 // math unit = 1/18em
217 result = val_ * em_width / 18;
219 case LyXLength::PCW: // Always % of workarea
222 result = val_ * text_width / 100;
225 // paperwidth/textwidth is 1.7 for A4 paper with default margins
226 result = val_ * text_width * 1.7 / 100;
229 result = val_ * text_width * 1.787 / 100;
232 result = val_ * text_width * 2.2 / 100;
234 case LyXLength::UNIT_NONE:
235 result = 0; // this cannot happen
238 return static_cast<int>(result + ((result >= 0) ? 0.5 : -0.5));
242 int LyXLength::inBP() const
244 // return any LyXLength value as a one with
245 // the PostScript point, called bp (big points)
250 result = val_ * 28.346;
254 result = val_ * 2.8346;
258 result = val_ * 72.0;
261 // no other than bp possible
265 return static_cast<int>(result + 0.5);
269 bool operator==(LyXLength const & l1, LyXLength const & l2)
271 return l1.value() == l2.value() && l1.unit() == l2.unit();
275 bool operator!=(LyXLength const & l1, LyXLength const & l2)