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.
21 #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 void Length::swap(Length & rhs)
64 std::swap(val_, rhs.val_);
65 std::swap(unit_, rhs.unit_);
69 string const Length::asString() const
72 os << val_ << unit_name[unit_]; // setw?
77 docstring const Length::asDocstring() const
80 os << val_ << unit_name[unit_]; // setw?
85 string const Length::asLatexString() const
90 os << val_ / 100.0 << "\\textwidth";
93 os << val_ / 100.0 << "\\columnwidth";
96 os << val_ / 100.0 << "\\paperwidth";
99 os << val_ / 100.0 << "\\linewidth";
102 os << val_ / 100.0 << "\\paperheight";
105 os << val_ / 100.0 << "\\textheight";
108 os << val_ << unit_name[unit_];
115 double Length::value() const
121 Length::UNIT Length::unit() const
127 void Length::value(double v)
133 void Length::unit(Length::UNIT u)
139 bool Length::zero() const
145 bool Length::empty() const
147 return unit_ == Length::UNIT_NONE;
151 int Length::inPixels(int text_width, int em_width_base) const
153 // Zoom factor specified by user in percent
154 double const zoom = lyxrc.zoom / 100.0; // [percent]
156 // DPI setting for monitor: pixels/inch
157 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
159 double const em_width = (em_width_base > 0)
161 : 10*(dpi/72.27)*zoom;
162 // A different estimate for em_width is
163 // theFontMetrics(FontInfo(sane_font)).width('M')
164 // but this estimate might not be more accurate as the screen font
165 // is different then the latex font.
167 // Pixel values are scaled so that the ratio
168 // between lengths and font sizes on the screen
169 // is the same as on paper.
175 // Scaled point: sp = 1/65536 pt
176 result = zoom * dpi * val_
177 / (72.27 * 65536); // 4736286.7
180 // Point: 1 pt = 1/72.27 inch
181 result = zoom * dpi * val_
185 // Big point: 1 bp = 1/72 inch
186 result = zoom * dpi * val_
190 // Didot: 1157dd = 1238 pt?
191 result = zoom * dpi * val_
192 / (72.27 / (0.376 * 2.845)); // 67.559735
195 // Millimeter: 1 mm = 1/25.4 inch
196 result = zoom * dpi * val_
200 // Pica: 1 pc = 12 pt
201 result = zoom * dpi * val_
202 / (72.27 / 12); // 6.0225
205 // Cicero: 1 cc = 12 dd
206 result = zoom * dpi * val_
207 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
210 // Centimeter: 1 cm = 1/2.54 inch
211 result = zoom * dpi * val_
216 result = zoom * dpi * val_;
219 // Ex: The height of an "x"
220 // 0.4305 is the ration between 1ex and 1em in cmr10
221 result = val_ * em_width * 0.4305;
224 // Em: The width of an "m"
225 result = val_ * em_width;
228 // math unit = 1/18em
229 result = val_ * em_width / 18;
231 case Length::PCW: // Always % of workarea
234 result = val_ * text_width / 100;
237 // paperwidth/textwidth is 1.7 for A4 paper with default margins
238 result = val_ * text_width * 1.7 / 100;
241 result = val_ * text_width * 1.787 / 100;
244 result = val_ * text_width * 2.2 / 100;
246 case Length::UNIT_NONE:
247 result = 0; // this cannot happen
250 return static_cast<int>(result + ((result >= 0) ? 0.5 : -0.5));
254 int Length::inBP() const
256 // return any Length value as a one with
257 // the PostScript point, called bp (big points)
262 result = val_ * 28.346;
266 result = val_ * 2.8346;
270 result = val_ * 72.0;
273 // no other than bp possible
277 return static_cast<int>(result + 0.5);
281 bool operator==(Length const & l1, Length const & l2)
283 return l1.value() == l2.value() && l1.unit() == l2.unit();
287 bool operator!=(Length const & l1, Length const & l2)
293 /////////////////////////////////////////////////////////////////////
297 /////////////////////////////////////////////////////////////////////
300 GlueLength::GlueLength(Length const & len)
305 GlueLength::GlueLength(Length const & len, Length const & plus,
306 Length const & minus)
307 : len_(len), plus_(plus), minus_(minus)
311 GlueLength::GlueLength(string const & data)
313 isValidGlueLength(data, this);
317 string const GlueLength::asString() const
319 ostringstream buffer;
321 buffer << len_.value();
323 if (plus_.zero() && minus_.zero()) {
324 buffer << unit_name[len_.unit()];
330 if (len_.unit() != plus_.unit())
331 buffer << unit_name[len_.unit()];
332 buffer << '+' << plus_.value();
333 buffer << unit_name[plus_.unit()];
337 // just len and minus
339 if (len_.unit() != minus_.unit())
340 buffer << unit_name[len_.unit()];
341 buffer << '-' << minus_.value();
342 buffer << unit_name[minus_.unit()];
346 // ok, len, plus AND minus
349 if (minus_ == plus_) {
350 if (len_.unit() != minus_.unit())
351 buffer << unit_name[len_.unit()];
352 buffer << "+-" << minus_.value();
353 buffer << unit_name[minus_.unit()];
357 // this is so rare a case, why bother minimising units ?
359 buffer << unit_name[len_.unit()];
360 buffer << '+' << plus_.value() << unit_name[plus_.unit()];
361 buffer << '-' << minus_.value() << unit_name[minus_.unit()];
367 string const GlueLength::asLatexString() const
369 ostringstream buffer;
371 buffer << len_.value() << unit_name[len_.unit()];
374 buffer << " plus " << plus_.value() << unit_name[plus_.unit()];
376 buffer << " minus " << minus_.value() << unit_name[minus_.unit()];
381 Length const & GlueLength::len() const
387 Length const & GlueLength::plus() const
393 Length const & GlueLength::minus() const
399 bool operator==(GlueLength const & l1, GlueLength const & l2)
401 return l1.len() == l2.len()
402 && l1.plus() == l2.plus()
403 && l1.minus() == l2.minus();
407 bool operator!=(GlueLength const & l1, GlueLength const & l2)