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.
19 #include "lengthcommon.h"
22 #include "support/docstream.h"
29 using std::ostringstream;
33 /////////////////////////////////////////////////////////////////////
37 /////////////////////////////////////////////////////////////////////
40 : val_(0), unit_(Length::UNIT_NONE)
44 Length::Length(double v, Length::UNIT u)
49 Length::Length(string const & data)
50 : val_(0), unit_(Length::PT)
54 if (!isValidLength(data, &tmp))
55 return; // should raise an exception
62 string const Length::asString() const
65 os << val_ << unit_name[unit_]; // setw?
70 docstring const Length::asDocstring() const
73 os << val_ << unit_name[unit_]; // setw?
78 string const Length::asLatexString() const
83 os << val_ / 100.0 << "\\textwidth";
86 os << val_ / 100.0 << "\\columnwidth";
89 os << val_ / 100.0 << "\\paperwidth";
92 os << val_ / 100.0 << "\\linewidth";
95 os << val_ / 100.0 << "\\paperheight";
98 os << val_ / 100.0 << "\\textheight";
101 os << val_ << unit_name[unit_];
108 double Length::value() const
114 Length::UNIT Length::unit() const
120 void Length::value(double v)
126 void Length::unit(Length::UNIT u)
132 bool Length::zero() const
138 bool Length::empty() const
140 return unit_ == Length::UNIT_NONE;
144 int Length::inPixels(int text_width, int em_width_base) const
146 // Zoom factor specified by user in percent
147 double const zoom = lyxrc.zoom / 100.0; // [percent]
149 // DPI setting for monitor: pixels/inch
150 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
152 double const em_width = (em_width_base > 0)
154 : 10*(dpi/72.27)*zoom;
155 // A different estimate for em_width is
156 // theFontMetrics(Font(Font::ALL_SANE)).width('M')
157 // but this estimate might not be more accurate as the screen font
158 // is different then the latex font.
160 // Pixel values are scaled so that the ratio
161 // between lengths and font sizes on the screen
162 // is the same as on paper.
168 // Scaled point: sp = 1/65536 pt
169 result = zoom * dpi * val_
170 / (72.27 * 65536); // 4736286.7
173 // Point: 1 pt = 1/72.27 inch
174 result = zoom * dpi * val_
178 // Big point: 1 bp = 1/72 inch
179 result = zoom * dpi * val_
183 // Didot: 1157dd = 1238 pt?
184 result = zoom * dpi * val_
185 / (72.27 / (0.376 * 2.845)); // 67.559735
188 // Millimeter: 1 mm = 1/25.4 inch
189 result = zoom * dpi * val_
193 // Pica: 1 pc = 12 pt
194 result = zoom * dpi * val_
195 / (72.27 / 12); // 6.0225
198 // Cicero: 1 cc = 12 dd
199 result = zoom * dpi * val_
200 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
203 // Centimeter: 1 cm = 1/2.54 inch
204 result = zoom * dpi * val_
209 result = zoom * dpi * val_;
212 // Ex: The height of an "x"
213 // 0.4305 is the ration between 1ex and 1em in cmr10
214 result = val_ * em_width * 0.4305;
217 // Em: The width of an "m"
218 result = val_ * em_width;
221 // math unit = 1/18em
222 result = val_ * em_width / 18;
224 case Length::PCW: // Always % of workarea
227 result = val_ * text_width / 100;
230 // paperwidth/textwidth is 1.7 for A4 paper with default margins
231 result = val_ * text_width * 1.7 / 100;
234 result = val_ * text_width * 1.787 / 100;
237 result = val_ * text_width * 2.2 / 100;
239 case Length::UNIT_NONE:
240 result = 0; // this cannot happen
243 return static_cast<int>(result + ((result >= 0) ? 0.5 : -0.5));
247 int Length::inBP() const
249 // return any Length value as a one with
250 // the PostScript point, called bp (big points)
255 result = val_ * 28.346;
259 result = val_ * 2.8346;
263 result = val_ * 72.0;
266 // no other than bp possible
270 return static_cast<int>(result + 0.5);
274 bool operator==(Length const & l1, Length const & l2)
276 return l1.value() == l2.value() && l1.unit() == l2.unit();
280 bool operator!=(Length const & l1, Length const & l2)
286 /////////////////////////////////////////////////////////////////////
290 /////////////////////////////////////////////////////////////////////
293 GlueLength::GlueLength(Length const & len)
298 GlueLength::GlueLength(Length const & len, Length const & plus,
299 Length const & minus)
300 : len_(len), plus_(plus), minus_(minus)
304 GlueLength::GlueLength(string const & data)
306 isValidGlueLength(data, this);
310 string const GlueLength::asString() const
312 ostringstream buffer;
314 buffer << len_.value();
316 if (plus_.zero() && minus_.zero()) {
317 buffer << unit_name[len_.unit()];
323 if (len_.unit() != plus_.unit())
324 buffer << unit_name[len_.unit()];
325 buffer << '+' << plus_.value();
326 buffer << unit_name[plus_.unit()];
330 // just len and minus
332 if (len_.unit() != minus_.unit())
333 buffer << unit_name[len_.unit()];
334 buffer << '-' << minus_.value();
335 buffer << unit_name[minus_.unit()];
339 // ok, len, plus AND minus
342 if (minus_ == plus_) {
343 if (len_.unit() != minus_.unit())
344 buffer << unit_name[len_.unit()];
345 buffer << "+-" << minus_.value();
346 buffer << unit_name[minus_.unit()];
350 // this is so rare a case, why bother minimising units ?
352 buffer << unit_name[len_.unit()];
353 buffer << '+' << plus_.value() << unit_name[plus_.unit()];
354 buffer << '-' << minus_.value() << unit_name[minus_.unit()];
360 string const GlueLength::asLatexString() const
362 ostringstream buffer;
364 buffer << len_.value() << unit_name[len_.unit()];
367 buffer << " plus " << plus_.value() << unit_name[plus_.unit()];
369 buffer << " minus " << minus_.value() << unit_name[minus_.unit()];
374 Length const & GlueLength::len() const
380 Length const & GlueLength::plus() const
386 Length const & GlueLength::minus() const
392 bool operator==(GlueLength const & l1, GlueLength const & l2)
394 return l1.len() == l2.len()
395 && l1.plus() == l2.plus()
396 && l1.minus() == l2.minus();
400 bool operator!=(GlueLength const & l1, GlueLength const & l2)