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 LyXLength::LyXLength(string const & data)
40 : val_(0), unit_(LyXLength::PT)
44 if (!isValidLength(data, &tmp))
45 return; // should raise an exception
52 string const LyXLength::asString() const
55 buffer << val_ << unit_name[unit_]; // setw?
56 return buffer.str().c_str();
60 string const LyXLength::asLatexString() const
65 buffer << abs(static_cast<int>(val_/100)) << "."
66 << abs(static_cast<int>(val_)%100) << "\\textwidth";
69 buffer << abs(static_cast<int>(val_/100)) << "."
70 << abs(static_cast<int>(val_)%100) << "\\columnwidth";
73 buffer << abs(static_cast<int>(val_/100)) << "."
74 << abs(static_cast<int>(val_)%100) << "\\paperwidth";
77 buffer << abs(static_cast<int>(val_/100)) << "."
78 << abs(static_cast<int>(val_)%100) << "\\linewidth";
81 buffer << abs(static_cast<int>(val_/100)) << "."
82 << abs(static_cast<int>(val_)%100) << "\\paperheight";
85 buffer << abs(static_cast<int>(val_/100)) << "."
86 << abs(static_cast<int>(val_)%100) << "\\textheight";
89 buffer << val_ << unit_name[unit_]; // setw?
92 return buffer.str().c_str();
96 double LyXLength::value() const
102 LyXLength::UNIT LyXLength::unit() const
108 void LyXLength::value(double v)
114 void LyXLength::unit(LyXLength::UNIT u)
120 bool LyXLength::zero() const
126 int LyXLength::inPixels(int default_width, int default_height) const
128 // Zoom factor specified by user in percent
129 double const zoom = lyxrc.zoom / 100.0; // [percent]
131 // DPI setting for monitor: pixels/inch
132 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
134 // Pixel values are scaled so that the ratio
135 // between lengths and font sizes on the screen
136 // is the same as on paper.
138 // we don't care about sign of value, we
139 // display negative space with text too
141 #warning if you don't care than either call this function differently or let it return negative values and call abs() explicitly when needed (Andre')
148 // Scaled point: sp = 1/65536 pt
149 result = zoom * dpi * val_
150 / (72.27 * 65536); // 4736286.7
153 // Point: 1 pt = 1/72.27 inch
154 result = zoom * dpi * val_
158 // Big point: 1 bp = 1/72 inch
159 result = zoom * dpi * val_
163 // Didot: 1157dd = 1238 pt?
164 result = zoom * dpi * val_
165 / (72.27 / (0.376 * 2.845)); // 67.559735
168 // Millimeter: 1 mm = 1/25.4 inch
169 result = zoom * dpi * val_
173 // Pica: 1 pc = 12 pt
174 result = zoom * dpi * val_
175 / (72.27 / 12); // 6.0225
178 // Cicero: 1 cc = 12 dd
179 result = zoom * dpi * val_
180 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
183 // Centimeter: 1 cm = 1/2.54 inch
184 result = zoom * dpi * val_
189 result = zoom * dpi * val_;
192 // Ex: The height of an "x"
193 // 0.4305 is the ration between 1ex and 1em in cmr10
194 result = zoom * val_ * default_height * 0.4305;
197 // Em: The width of an "m"
198 // 1em is approx 10points in cmr10
199 result = zoom * val_ * default_height;
202 // math unit = 1/18em
203 result = val_ * default_height / 18;
205 case LyXLength::PCW: // Always % of workarea
209 result = val_ * default_width / 100;
213 result = val_ * default_height / 100;
215 case LyXLength::UNIT_NONE:
216 result = 0; // this cannot happen
219 return static_cast<int>(result + ((result >= 0) ? 0.5 : -0.5));
223 int LyXLength::inBP() const
225 // return any LyXLength value as a one with
226 // the PostScript point, called bp (big points)
231 result = val_ * 28.346;
235 result = val_ * 2.8346;
239 result = val_ * 72.0;
242 // no other than bp possible
246 return static_cast<int>(result + 0.5);
250 bool operator==(LyXLength const & l1, LyXLength const & l2)
252 return l1.value() == l2.value() && l1.unit() == l2.unit();
256 bool operator!=(LyXLength const & l1, LyXLength const & l2)