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
63 buffer << abs(static_cast<int>(val_/100)) << "."
64 << abs(static_cast<int>(val_)%100) << "\\columnwidth";
67 buffer << abs(static_cast<int>(val_/100)) << "."
68 << abs(static_cast<int>(val_)%100) << "\\paperwidth";
71 buffer << abs(static_cast<int>(val_/100)) << "."
72 << abs(static_cast<int>(val_)%100) << "\\linewidth";
75 buffer << val_ << unit_name[unit_]; // setw?
78 return buffer.str().c_str();
82 double LyXLength::value() const
88 LyXLength::UNIT LyXLength::unit() const
94 void LyXLength::value(double v)
100 void LyXLength::unit(LyXLength::UNIT u)
106 bool LyXLength::zero() const
112 int LyXLength::inPixels(int default_width, int default_height) const
114 // Zoom factor specified by user in percent
115 double const zoom = lyxrc.zoom / 100.0; // [percent]
117 // DPI setting for monitor: pixels/inch
118 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
120 // Pixel values are scaled so that the ratio
121 // between lengths and font sizes on the screen
122 // is the same as on paper.
124 // we don't care about sign of value, we
125 // display negative space with text too
127 int val_sign = val_ < 0.0 ? -1 : 1;
131 // Scaled point: sp = 1/65536 pt
132 result = zoom * dpi * val_
133 / (72.27 * 65536); // 4736286.7
136 // Point: 1 pt = 1/72.27 inch
137 result = zoom * dpi * val_
141 // Big point: 1 bp = 1/72 inch
142 result = zoom * dpi * val_
146 // Didot: 1157dd = 1238 pt?
147 result = zoom * dpi * val_
148 / (72.27 / (0.376 * 2.845)); // 67.559735
151 // Millimeter: 1 mm = 1/25.4 inch
152 result = zoom * dpi * val_
156 // Pica: 1 pc = 12 pt
157 result = zoom * dpi * val_
158 / (72.27 / 12); // 6.0225
161 // Cicero: 1 cc = 12 dd
162 result = zoom * dpi * val_
163 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
166 // Centimeter: 1 cm = 1/2.54 inch
167 result = zoom * dpi * val_
172 result = zoom * dpi * val_;
175 // Ex: The height of an "x"
176 result = zoom * val_ * default_height / 2; // what to / width?
178 case LyXLength::EM: // what to / width?
179 // Em: The width of an "m"
180 result = zoom * val_ * default_height / 2; // Why 2?
182 case LyXLength::MU: // This is probably only allowed in
184 result = zoom * val_ * default_height;
186 case LyXLength::PW: // Always % of workarea
190 result = val_ * default_width / 100;
192 case LyXLength::UNIT_NONE:
193 result = 0; // this cannot happen
196 return static_cast<int>(result * val_sign + 0.5);
200 bool operator==(LyXLength const & l1, LyXLength const & l2)
202 return l1.value() == l2.value() && l1.unit() == l2.unit();
206 bool operator!=(LyXLength const & l1, LyXLength const & l2)