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"
26 LyXLength::LyXLength()
27 : val_(0), unit_(LyXLength::PT)
31 LyXLength::LyXLength(double v, LyXLength::UNIT u)
36 LyXLength::LyXLength(string const & data)
37 : val_(0), unit_(LyXLength::PT)
41 if (!isValidLength (data, &tmp))
42 return; // should raise an exception
49 string const LyXLength::asString() const
52 buffer << val_ << unit_name[unit_]; // setw?
53 return buffer.str().c_str();
57 string const LyXLength::asLatexString() const
62 buffer << abs(static_cast<int>(val_/100)) << "."
63 << abs(static_cast<int>(val_)%100) << "\\textwidth";
66 buffer << abs(static_cast<int>(val_/100)) << "."
67 << abs(static_cast<int>(val_)%100) << "\\columnwidth";
70 buffer << abs(static_cast<int>(val_/100)) << "."
71 << abs(static_cast<int>(val_)%100) << "\\paperwidth";
74 buffer << abs(static_cast<int>(val_/100)) << "."
75 << abs(static_cast<int>(val_)%100) << "\\linewidth";
78 buffer << val_ << unit_name[unit_]; // setw?
81 return buffer.str().c_str();
85 double LyXLength::value() const
91 LyXLength::UNIT LyXLength::unit() const
97 void LyXLength::value(double v)
103 void LyXLength::unit(LyXLength::UNIT u)
109 bool LyXLength::zero() const
115 int LyXLength::inPixels(int default_width, int default_height) const
117 // Zoom factor specified by user in percent
118 double const zoom = lyxrc.zoom / 100.0; // [percent]
120 // DPI setting for monitor: pixels/inch
121 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
123 // Pixel values are scaled so that the ratio
124 // between lengths and font sizes on the screen
125 // is the same as on paper.
127 // we don't care about sign of value, we
128 // display negative space with text too
130 int val_sign = val_ < 0.0 ? -1 : 1;
134 // Scaled point: sp = 1/65536 pt
135 result = zoom * dpi * val_
136 / (72.27 * 65536); // 4736286.7
139 // Point: 1 pt = 1/72.27 inch
140 result = zoom * dpi * val_
144 // Big point: 1 bp = 1/72 inch
145 result = zoom * dpi * val_
149 // Didot: 1157dd = 1238 pt?
150 result = zoom * dpi * val_
151 / (72.27 / (0.376 * 2.845)); // 67.559735
154 // Millimeter: 1 mm = 1/25.4 inch
155 result = zoom * dpi * val_
159 // Pica: 1 pc = 12 pt
160 result = zoom * dpi * val_
161 / (72.27 / 12); // 6.0225
164 // Cicero: 1 cc = 12 dd
165 result = zoom * dpi * val_
166 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
169 // Centimeter: 1 cm = 1/2.54 inch
170 result = zoom * dpi * val_
175 result = zoom * dpi * val_;
178 // Ex: The height of an "x"
179 result = zoom * val_ * default_height / 2; // what to / width?
181 case LyXLength::EM: // what to / width?
182 // Em: The width of an "m"
183 result = zoom * val_ * default_height / 2; // Why 2?
185 case LyXLength::MU: // This is probably only allowed in
187 result = zoom * val_ * default_height;
189 case LyXLength::PW: // Always % of workarea
193 result = val_ * default_width / 100;
195 case LyXLength::UNIT_NONE:
196 result = 0; // this cannot happen
199 return static_cast<int>(result * val_sign + 0.5);
203 bool operator==(LyXLength const & l1, LyXLength const & l2)
205 return l1.value() == l2.value() && l1.unit() == l2.unit();
209 bool operator!=(LyXLength const & l1, LyXLength const & l2)