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"
31 /////////////////////////////////////////////////////////////////////
35 /////////////////////////////////////////////////////////////////////
38 : val_(0), unit_(Length::UNIT_NONE)
42 Length::Length(double v, Length::UNIT u)
47 Length::Length(string const & data)
48 : val_(0), unit_(Length::PT)
52 if (!isValidLength(data, &tmp))
53 return; // should raise an exception
60 string const Length::asString() const
63 if (unit_ != UNIT_NONE)
64 os << val_ << unit_name[unit_]; // setw?
69 docstring const Length::asDocstring() const
72 if (unit_ != UNIT_NONE)
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 << "\\textheight";
98 os << val_ / 100.0 << "\\paperheight";
103 os << val_ << unit_name[unit_];
110 string const Length::asHTMLString() const
126 os << val_ << unit_name[unit_];
129 os << val_/12.0 << "pt";
132 os << val_/18.0 << "em";
140 // what it's a percentage of probably won't make sense for HTML,
141 // so we'll assume people have chosen these appropriately
152 double Length::value() const
158 Length::UNIT Length::unit() const
164 void Length::value(double v)
170 void Length::unit(Length::UNIT u)
176 bool Length::zero() const
182 bool Length::empty() const
184 return unit_ == Length::UNIT_NONE;
188 int Length::inPixels(int text_width, int em_width_base) const
190 // Zoom factor specified by user in percent
191 double const zoom = lyxrc.zoom / 100.0; // [percent]
193 // DPI setting for monitor: pixels/inch
194 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
196 double const em_width = (em_width_base > 0)
198 : 10*(dpi/72.27)*zoom;
199 // A different estimate for em_width is
200 // theFontMetrics(FontInfo(sane_font)).width('M')
201 // but this estimate might not be more accurate as the screen font
202 // is different then the latex font.
204 // Pixel values are scaled so that the ratio
205 // between lengths and font sizes on the screen
206 // is the same as on paper.
212 // Scaled point: sp = 1/65536 pt
213 result = zoom * dpi * val_
214 / (72.27 * 65536); // 4736286.7
217 // Point: 1 pt = 1/72.27 inch
218 result = zoom * dpi * val_
222 // Big point: 1 bp = 1/72 inch
223 result = zoom * dpi * val_
227 // Didot: 1157dd = 1238 pt?
228 result = zoom * dpi * val_
229 / (72.27 / (0.376 * 2.845)); // 67.559735
232 // Millimeter: 1 mm = 1/25.4 inch
233 result = zoom * dpi * val_
237 // Pica: 1 pc = 12 pt
238 result = zoom * dpi * val_
239 / (72.27 / 12); // 6.0225
242 // Cicero: 1 cc = 12 dd
243 result = zoom * dpi * val_
244 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
247 // Centimeter: 1 cm = 1/2.54 inch
248 result = zoom * dpi * val_
253 result = zoom * dpi * val_;
256 // Ex: The height of an "x"
257 // 0.4305 is the ration between 1ex and 1em in cmr10
258 result = val_ * em_width * 0.4305;
261 // Em: The width of an "m"
262 result = val_ * em_width;
265 // math unit = 1/18em
266 result = val_ * em_width / 18;
268 case Length::PCW: // Always % of workarea
271 result = val_ * text_width / 100;
274 // paperwidth/textwidth is 1.7 for A4 paper with default margins
275 result = val_ * text_width * 1.7 / 100;
278 result = val_ * text_width * 1.787 / 100;
281 result = val_ * text_width * 2.2 / 100;
283 case Length::UNIT_NONE:
284 result = 0; // this cannot happen
287 return static_cast<int>(result + ((result >= 0) ? 0.5 : -0.5));
291 int Length::inBP() const
293 // return any Length value as a one with
294 // the PostScript point, called bp (big points)
299 result = val_ * 28.346;
303 result = val_ * 2.8346;
307 result = val_ * 72.0;
310 // no other than bp possible
314 return static_cast<int>(result + 0.5);
318 Length::UNIT Length::defaultUnit()
320 return lyxrc.default_length_unit;
325 bool operator==(Length const & l1, Length const & l2)
327 return l1.value() == l2.value() && l1.unit() == l2.unit();
331 bool operator!=(Length const & l1, Length const & l2)
337 /////////////////////////////////////////////////////////////////////
341 /////////////////////////////////////////////////////////////////////
344 GlueLength::GlueLength(Length const & len)
349 GlueLength::GlueLength(Length const & len, Length const & plus,
350 Length const & minus)
351 : len_(len), plus_(plus), minus_(minus)
355 GlueLength::GlueLength(string const & data)
357 isValidGlueLength(data, this);
361 string const GlueLength::asString() const
366 ostringstream buffer;
368 buffer << len_.value();
370 if (plus_.zero() && minus_.zero()) {
371 buffer << unit_name[len_.unit()];
377 if (len_.unit() != plus_.unit())
378 buffer << unit_name[len_.unit()];
379 buffer << '+' << plus_.value();
380 buffer << unit_name[plus_.unit()];
384 // just len and minus
386 if (len_.unit() != minus_.unit())
387 buffer << unit_name[len_.unit()];
388 buffer << '-' << minus_.value();
389 buffer << unit_name[minus_.unit()];
393 // ok, len, plus AND minus
396 if (minus_ == plus_) {
397 if (len_.unit() != minus_.unit())
398 buffer << unit_name[len_.unit()];
399 buffer << "+-" << minus_.value();
400 buffer << unit_name[minus_.unit()];
404 // this is so rare a case, why bother minimising units ?
406 buffer << unit_name[len_.unit()];
407 buffer << '+' << plus_.value() << unit_name[plus_.unit()];
408 buffer << '-' << minus_.value() << unit_name[minus_.unit()];
414 string const GlueLength::asLatexString() const
416 ostringstream buffer;
417 // use Length::asLatexString() to handle also the percent lengths
418 buffer << len_.Length::asLatexString();
420 buffer << " plus " << plus_.Length::asLatexString();
422 buffer << " minus " << minus_.Length::asLatexString();
427 Length const & GlueLength::len() const
433 Length const & GlueLength::plus() const
439 Length const & GlueLength::minus() const
445 bool operator==(GlueLength const & l1, GlueLength const & l2)
447 return l1.len() == l2.len()
448 && l1.plus() == l2.plus()
449 && l1.minus() == l2.minus();
453 bool operator!=(GlueLength const & l1, GlueLength const & l2)