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"
25 #include "support/lstrings.h"
31 using namespace lyx::support;
36 /////////////////////////////////////////////////////////////////////
40 /////////////////////////////////////////////////////////////////////
43 : val_(0), unit_(Length::UNIT_NONE)
47 Length::Length(double v, Length::UNIT u)
52 Length::Length(string const & data)
53 : val_(0), unit_(Length::PT)
57 if (!isValidLength(data, &tmp))
58 return; // should raise an exception
65 string const Length::asString() const
68 if (unit_ != UNIT_NONE)
69 os << formatFPNumber(val_) << unit_name[unit_]; // setw?
74 docstring const Length::asDocstring() const
77 if (unit_ != UNIT_NONE)
78 os << from_ascii(formatFPNumber(val_))
79 << from_ascii(unit_name[unit_]); // setw?
84 string const Length::asLatexString() const
87 // Do not allow scientific notation (e.g. 1.2e+03), since this is not valid
91 os << formatFPNumber(val_ / 100.0) << "\\textwidth";
94 os << formatFPNumber(val_ / 100.0) << "\\columnwidth";
97 os << formatFPNumber(val_ / 100.0) << "\\paperwidth";
100 os << formatFPNumber(val_ / 100.0) << "\\linewidth";
103 os << formatFPNumber(val_ / 100.0) << "\\textheight";
106 os << formatFPNumber(val_ / 100.0) << "\\paperheight";
111 os << formatFPNumber(val_) << unit_name[unit_];
118 string const Length::asHTMLString() const
126 os << formatFPNumber(val_) << "pt";
134 os << formatFPNumber(val_) << unit_name[unit_];
137 os << formatFPNumber(val_ / 12.0) << "pt";
140 os << formatFPNumber(val_ / 18.0) << "em";
148 // what it's a percentage of probably won't make sense for HTML,
149 // so we'll assume people have chosen these appropriately
150 os << formatFPNumber(val_) << '%';
160 double Length::value() const
166 Length::UNIT Length::unit() const
172 void Length::value(double v)
178 void Length::unit(Length::UNIT u)
184 bool Length::zero() const
190 bool Length::empty() const
192 return unit_ == Length::UNIT_NONE;
196 int Length::inPixels(int text_width, int em_width_base) const
198 // Zoom factor specified by user in percent
199 double const zoom = lyxrc.zoom / 100.0; // [percent]
201 // DPI setting for monitor: pixels/inch
202 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
204 double const em_width_in = (em_width_base > 0)
205 ? em_width_base / (zoom * dpi)
207 // A different estimate for em_width is
208 // theFontMetrics(FontInfo(sane_font)).em()
209 // but this estimate might not be more accurate as the screen font
210 // is different then the latex font.
212 // Pixel values are scaled so that the ratio
213 // between lengths and font sizes on the screen
214 // is the same as on paper.
216 double const text_width_in = text_width / (zoom * dpi);
217 double const result = zoom * dpi * inInch(text_width_in, em_width_in);
218 return static_cast<int>(result + ((result >= 0) ? 0.5 : -0.5));
222 double Length::inInch(double text_width, double em_width) const
228 // Scaled point: sp = 1/65536 pt
229 result = val_ / (72.27 * 65536); // 4736286.7
232 // Point: 1 pt = 1/72.27 inch
233 result = val_ / 72.27; // 72.27
236 // Big point: 1 bp = 1/72 inch
237 result = val_ / 72; // 72
240 // Didot: 1157dd = 1238 pt?
241 result = val_ / (72.27 / (0.376 * 2.845)); // 67.559735
244 // Millimeter: 1 mm = 1/25.4 inch
245 result = val_ / 25.4; // 25.4
248 // Pica: 1 pc = 12 pt
249 result = val_ / (72.27 / 12); // 6.0225
252 // Cicero: 1 cc = 12 dd
254 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
257 // Centimeter: 1 cm = 1/2.54 inch
258 result = val_ / 2.54; // 2.54
265 // Ex: The height of an "x"
266 // 0.4305 is the ration between 1ex and 1em in cmr10
267 result = val_ * em_width * 0.4305;
270 // Em: The width of an "m"
271 result = val_ * em_width;
274 // math unit = 1/18em
275 result = val_ * em_width / 18;
277 case Length::PCW: // Always % of workarea
280 result = val_ * text_width / 100;
283 // paperwidth/textwidth is 1.7 for A4 paper with default margins
284 result = val_ * text_width * 1.7 / 100;
287 result = val_ * text_width * 1.787 / 100;
290 result = val_ * text_width * 2.2 / 100;
292 case Length::UNIT_NONE:
293 result = 0; // this cannot happen
300 int Length::inPixels(MetricsBase const & base) const
302 return inPixels(base.textwidth, theFontMetrics(base.font).em());
306 int Length::inBP() const
308 // return any Length value as a one with
309 // the PostScript point, called bp (big points)
311 double const text_width_in = 210.0 / 2.54; // assume A4
312 double const em_width_in = 10.0 / 72.27;
313 double result = 72.0 * inInch(text_width_in, em_width_in);
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 << formatFPNumber(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 << '+' << formatFPNumber(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 << '-' << formatFPNumber(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 << "+-" << formatFPNumber(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 << '+' << formatFPNumber(plus_.value()) << unit_name[plus_.unit()];
408 buffer << '-' << formatFPNumber(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)