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/debug.h"
25 #include "support/docstream.h"
26 #include "support/lstrings.h"
27 #include "support/lyxlib.h"
33 using namespace lyx::support;
38 /////////////////////////////////////////////////////////////////////
42 /////////////////////////////////////////////////////////////////////
45 : val_(0), unit_(Length::UNIT_NONE)
49 Length::Length(double v, Length::UNIT u)
54 Length::Length(string const & data)
55 : val_(0), unit_(Length::UNIT_NONE)
59 if (!isValidLength(data, &tmp))
60 return; // should raise an exception
67 string const Length::asString() const
70 if (unit_ != UNIT_NONE)
71 os << formatFPNumber(val_) << unit_name[unit_]; // setw?
76 docstring const Length::asDocstring() const
79 if (unit_ != UNIT_NONE)
80 os << from_ascii(formatFPNumber(val_))
81 << from_ascii(unit_name[unit_]); // setw?
86 string const Length::asLatexString() const
89 // Do not allow scientific notation (e.g. 1.2e+03), since this is not valid
93 os << formatFPNumber(val_ / 100.0) << "\\textwidth";
96 os << formatFPNumber(val_ / 100.0) << "\\columnwidth";
99 os << formatFPNumber(val_ / 100.0) << "\\paperwidth";
102 os << formatFPNumber(val_ / 100.0) << "\\linewidth";
105 os << formatFPNumber(val_ / 100.0) << "\\textheight";
108 os << formatFPNumber(val_ / 100.0) << "\\paperheight";
111 os << formatFPNumber(val_ / 100.0) << "\\baselineskip";
116 os << formatFPNumber(val_) << unit_name[unit_];
123 string const Length::asHTMLString() const
131 os << formatFPNumber(val_) << "pt";
139 os << formatFPNumber(val_) << unit_name[unit_];
142 os << formatFPNumber(val_ / 12.0) << "pt";
145 os << formatFPNumber(val_ / 18.0) << "em";
154 // what it's a percentage of probably won't make sense for HTML,
155 // so we'll assume people have chosen these appropriately
156 os << formatFPNumber(val_) << '%';
166 double Length::value() const
172 Length::UNIT Length::unit() const
178 void Length::value(double v)
184 void Length::unit(Length::UNIT u)
190 bool Length::zero() const
196 bool Length::empty() const
198 return unit_ == Length::UNIT_NONE;
202 int Length::inPixels(int text_width, int em_width_base) const
204 // Zoom factor specified by user in percent
205 double const zoom = lyxrc.currentZoom / 100.0; // [percent]
207 // DPI setting for monitor: pixels/inch
208 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
210 double const em_width_in = (em_width_base > 0)
211 ? em_width_base / (zoom * dpi)
213 // A different estimate for em_width is
214 // theFontMetrics(FontInfo(sane_font)).em()
215 // but this estimate might not be more accurate as the screen font
216 // is different then the latex font.
218 // Pixel values are scaled so that the ratio
219 // between lengths and font sizes on the screen
220 // is the same as on paper.
222 double const text_width_in = text_width / (zoom * dpi);
223 double const result = zoom * dpi * inInch(text_width_in, em_width_in);
224 return support::iround(result);
228 double Length::inInch(double text_width, double em_width) const
234 // Scaled point: sp = 1/65536 pt
235 result = val_ / (72.27 * 65536); // 4736286.7
238 // Point: 1 pt = 1/72.27 inch
239 result = val_ / 72.27; // 72.27
242 // Big point: 1 bp = 1/72 inch
243 result = val_ / 72; // 72
246 // Didot: 1157dd = 1238 pt?
247 result = val_ / (72.27 / (0.376 * 2.845)); // 67.559735
250 // Millimeter: 1 mm = 1/25.4 inch
251 result = val_ / 25.4; // 25.4
254 // Pica: 1 pc = 12 pt
255 result = val_ / (72.27 / 12); // 6.0225
258 // Cicero: 1 cc = 12 dd
260 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
263 // Centimeter: 1 cm = 1/2.54 inch
264 result = val_ / 2.54; // 2.54
271 // Ex: The height of an "x"
272 // 0.4305 is the ration between 1ex and 1em in cmr10
273 result = val_ * em_width * 0.4305;
276 // Em: The width of an "m"
277 result = val_ * em_width;
280 // math unit = 1/18em
281 result = val_ * em_width / 18;
283 case Length::PCW: // Always % of workarea
286 result = val_ * text_width / 100;
289 // paperwidth/textwidth is 1.7 for A4 paper with default margins
290 result = val_ * text_width * 1.7 / 100;
293 result = val_ * text_width * 1.787 / 100;
296 result = val_ * text_width * 2.2 / 100;
299 // baselineskip is approximately 1.2 times the font size for the cmr fonts
300 // The value actually depends on the current paragraph (see TextMetrics::setRowHeight),
301 // but we do not have this information here.
302 result = val_ * em_width * 1.2 / 100;
304 case Length::UNIT_NONE:
305 result = 0; // this cannot happen
312 int Length::inPixels(MetricsBase const & base) const
314 FontInfo fi = base.font;
315 if (unit_ == Length::MU)
316 // mu is 1/18th of an em in the math symbol font
317 fi.setFamily(SYMBOL_FAMILY);
319 // Math style is only taken into account in the case of mu
320 fi.setStyle(LM_ST_TEXT);
321 return inPixels(base.textwidth, theFontMetrics(fi).em());
325 int Length::inBP() const
327 // return any Length value as a one with
328 // the PostScript point, called bp (big points)
330 double const text_width_in = 210.0 / 2.54; // assume A4
331 double const em_width_in = 10.0 / 72.27;
332 double result = 72.0 * inInch(text_width_in, em_width_in);
333 return support::iround(result);
337 Length::UNIT Length::defaultUnit()
339 return lyxrc.default_length_unit;
344 bool operator==(Length const & l1, Length const & l2)
346 return l1.value() == l2.value() && l1.unit() == l2.unit();
350 bool operator!=(Length const & l1, Length const & l2)
356 /////////////////////////////////////////////////////////////////////
360 /////////////////////////////////////////////////////////////////////
363 GlueLength::GlueLength(Length const & len)
368 GlueLength::GlueLength(Length const & len, Length const & plus,
369 Length const & minus)
370 : len_(len), plus_(plus), minus_(minus)
374 GlueLength::GlueLength(string const & data)
376 if (!isValidGlueLength(data, this))
377 LYXERR0("Invalid glue length " + data);
381 string const GlueLength::asString() const
386 ostringstream buffer;
388 buffer << formatFPNumber(len_.value());
390 if (plus_.zero() && minus_.zero()) {
391 buffer << unit_name[len_.unit()];
397 if (len_.unit() != plus_.unit())
398 buffer << unit_name[len_.unit()];
399 buffer << '+' << formatFPNumber(plus_.value());
400 buffer << unit_name[plus_.unit()];
404 // just len and minus
406 if (len_.unit() != minus_.unit())
407 buffer << unit_name[len_.unit()];
408 buffer << '-' << formatFPNumber(minus_.value());
409 buffer << unit_name[minus_.unit()];
413 // ok, len, plus AND minus
416 if (minus_ == plus_) {
417 if (len_.unit() != minus_.unit())
418 buffer << unit_name[len_.unit()];
419 buffer << "+-" << formatFPNumber(minus_.value());
420 buffer << unit_name[minus_.unit()];
424 // this is so rare a case, why bother minimising units ?
426 buffer << unit_name[len_.unit()];
427 buffer << '+' << formatFPNumber(plus_.value()) << unit_name[plus_.unit()];
428 buffer << '-' << formatFPNumber(minus_.value()) << unit_name[minus_.unit()];
434 string const GlueLength::asLatexString() const
436 ostringstream buffer;
437 // use Length::asLatexString() to handle also the percent lengths
438 buffer << len_.Length::asLatexString();
440 buffer << " plus " << plus_.Length::asLatexString();
442 buffer << " minus " << minus_.Length::asLatexString();
447 Length const & GlueLength::len() const
453 Length const & GlueLength::plus() const
459 Length const & GlueLength::minus() const
465 bool operator==(GlueLength const & l1, GlueLength const & l2)
467 return l1.len() == l2.len()
468 && l1.plus() == l2.plus()
469 && l1.minus() == l2.minus();
473 bool operator!=(GlueLength const & l1, GlueLength const & l2)