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.
20 #include "MetricsInfo.h"
22 #include "frontends/FontMetrics.h"
24 #include "support/docstream.h"
34 /////////////////////////////////////////////////////////////////////
38 /////////////////////////////////////////////////////////////////////
41 : val_(0), unit_(Length::UNIT_NONE)
45 Length::Length(double v, Length::UNIT u)
50 Length::Length(string const & data)
51 : val_(0), unit_(Length::PT)
55 if (!isValidLength(data, &tmp))
56 return; // should raise an exception
63 string const Length::asString() const
66 if (unit_ != UNIT_NONE)
67 os << val_ << unit_name[unit_]; // setw?
72 docstring const Length::asDocstring() const
75 if (unit_ != UNIT_NONE)
76 os << val_ << unit_name[unit_]; // setw?
81 string const Length::asLatexString() const
86 os << val_ / 100.0 << "\\textwidth";
89 os << val_ / 100.0 << "\\columnwidth";
92 os << val_ / 100.0 << "\\paperwidth";
95 os << val_ / 100.0 << "\\linewidth";
98 os << val_ / 100.0 << "\\textheight";
101 os << val_ / 100.0 << "\\paperheight";
106 os << val_ << unit_name[unit_];
113 string const Length::asHTMLString() const
129 os << val_ << unit_name[unit_];
132 os << val_/12.0 << "pt";
135 os << val_/18.0 << "em";
143 // what it's a percentage of probably won't make sense for HTML,
144 // so we'll assume people have chosen these appropriately
155 double Length::value() const
161 Length::UNIT Length::unit() const
167 void Length::value(double v)
173 void Length::unit(Length::UNIT u)
179 bool Length::zero() const
185 bool Length::empty() const
187 return unit_ == Length::UNIT_NONE;
191 int Length::inPixels(int text_width, int em_width_base) const
193 // Zoom factor specified by user in percent
194 double const zoom = lyxrc.zoom / 100.0; // [percent]
196 // DPI setting for monitor: pixels/inch
197 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
199 double const em_width = (em_width_base > 0)
201 : 10*(dpi/72.27)*zoom;
202 // A different estimate for em_width is
203 // theFontMetrics(FontInfo(sane_font)).em()
204 // but this estimate might not be more accurate as the screen font
205 // is different then the latex font.
207 // Pixel values are scaled so that the ratio
208 // between lengths and font sizes on the screen
209 // is the same as on paper.
215 // Scaled point: sp = 1/65536 pt
216 result = zoom * dpi * val_
217 / (72.27 * 65536); // 4736286.7
220 // Point: 1 pt = 1/72.27 inch
221 result = zoom * dpi * val_
225 // Big point: 1 bp = 1/72 inch
226 result = zoom * dpi * val_
230 // Didot: 1157dd = 1238 pt?
231 result = zoom * dpi * val_
232 / (72.27 / (0.376 * 2.845)); // 67.559735
235 // Millimeter: 1 mm = 1/25.4 inch
236 result = zoom * dpi * val_
240 // Pica: 1 pc = 12 pt
241 result = zoom * dpi * val_
242 / (72.27 / 12); // 6.0225
245 // Cicero: 1 cc = 12 dd
246 result = zoom * dpi * val_
247 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
250 // Centimeter: 1 cm = 1/2.54 inch
251 result = zoom * dpi * val_
256 result = zoom * dpi * val_;
259 // Ex: The height of an "x"
260 // 0.4305 is the ration between 1ex and 1em in cmr10
261 result = val_ * em_width * 0.4305;
264 // Em: The width of an "m"
265 result = val_ * em_width;
268 // math unit = 1/18em
269 result = val_ * em_width / 18;
271 case Length::PCW: // Always % of workarea
274 result = val_ * text_width / 100;
277 // paperwidth/textwidth is 1.7 for A4 paper with default margins
278 result = val_ * text_width * 1.7 / 100;
281 result = val_ * text_width * 1.787 / 100;
284 result = val_ * text_width * 2.2 / 100;
286 case Length::UNIT_NONE:
287 result = 0; // this cannot happen
290 return static_cast<int>(result + ((result >= 0) ? 0.5 : -0.5));
294 int Length::inPixels(MetricsBase const & base) const
296 return inPixels(base.textwidth, theFontMetrics(base.font).em());
300 int Length::inBP() const
302 // return any Length value as a one with
303 // the PostScript point, called bp (big points)
308 result = val_ * 28.346;
312 result = val_ * 2.8346;
316 result = val_ * 72.0;
319 // no other than bp possible
323 return static_cast<int>(result + 0.5);
327 Length::UNIT Length::defaultUnit()
329 return lyxrc.default_length_unit;
334 bool operator==(Length const & l1, Length const & l2)
336 return l1.value() == l2.value() && l1.unit() == l2.unit();
340 bool operator!=(Length const & l1, Length const & l2)
346 /////////////////////////////////////////////////////////////////////
350 /////////////////////////////////////////////////////////////////////
353 GlueLength::GlueLength(Length const & len)
358 GlueLength::GlueLength(Length const & len, Length const & plus,
359 Length const & minus)
360 : len_(len), plus_(plus), minus_(minus)
364 GlueLength::GlueLength(string const & data)
366 isValidGlueLength(data, this);
370 string const GlueLength::asString() const
375 ostringstream buffer;
377 buffer << len_.value();
379 if (plus_.zero() && minus_.zero()) {
380 buffer << unit_name[len_.unit()];
386 if (len_.unit() != plus_.unit())
387 buffer << unit_name[len_.unit()];
388 buffer << '+' << plus_.value();
389 buffer << unit_name[plus_.unit()];
393 // just len and minus
395 if (len_.unit() != minus_.unit())
396 buffer << unit_name[len_.unit()];
397 buffer << '-' << minus_.value();
398 buffer << unit_name[minus_.unit()];
402 // ok, len, plus AND minus
405 if (minus_ == plus_) {
406 if (len_.unit() != minus_.unit())
407 buffer << unit_name[len_.unit()];
408 buffer << "+-" << minus_.value();
409 buffer << unit_name[minus_.unit()];
413 // this is so rare a case, why bother minimising units ?
415 buffer << unit_name[len_.unit()];
416 buffer << '+' << plus_.value() << unit_name[plus_.unit()];
417 buffer << '-' << minus_.value() << unit_name[minus_.unit()];
423 string const GlueLength::asLatexString() const
425 ostringstream buffer;
426 // use Length::asLatexString() to handle also the percent lengths
427 buffer << len_.Length::asLatexString();
429 buffer << " plus " << plus_.Length::asLatexString();
431 buffer << " minus " << minus_.Length::asLatexString();
436 Length const & GlueLength::len() const
442 Length const & GlueLength::plus() const
448 Length const & GlueLength::minus() const
454 bool operator==(GlueLength const & l1, GlueLength const & l2)
456 return l1.len() == l2.len()
457 && l1.plus() == l2.plus()
458 && l1.minus() == l2.minus();
462 bool operator!=(GlueLength const & l1, GlueLength const & l2)