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"
26 #include "support/lyxlib.h"
32 using namespace lyx::support;
37 /////////////////////////////////////////////////////////////////////
41 /////////////////////////////////////////////////////////////////////
44 : val_(0), unit_(Length::UNIT_NONE)
48 Length::Length(double v, Length::UNIT u)
53 Length::Length(string const & data)
54 : val_(0), unit_(Length::PT)
58 if (!isValidLength(data, &tmp))
59 return; // should raise an exception
66 string const Length::asString() const
69 if (unit_ != UNIT_NONE)
70 os << formatFPNumber(val_) << unit_name[unit_]; // setw?
75 docstring const Length::asDocstring() const
78 if (unit_ != UNIT_NONE)
79 os << from_ascii(formatFPNumber(val_))
80 << from_ascii(unit_name[unit_]); // setw?
85 string const Length::asLatexString() const
88 // Do not allow scientific notation (e.g. 1.2e+03), since this is not valid
92 os << formatFPNumber(val_ / 100.0) << "\\textwidth";
95 os << formatFPNumber(val_ / 100.0) << "\\columnwidth";
98 os << formatFPNumber(val_ / 100.0) << "\\paperwidth";
101 os << formatFPNumber(val_ / 100.0) << "\\linewidth";
104 os << formatFPNumber(val_ / 100.0) << "\\textheight";
107 os << formatFPNumber(val_ / 100.0) << "\\paperheight";
112 os << formatFPNumber(val_) << unit_name[unit_];
119 string const Length::asHTMLString() const
127 os << formatFPNumber(val_) << "pt";
135 os << formatFPNumber(val_) << unit_name[unit_];
138 os << formatFPNumber(val_ / 12.0) << "pt";
141 os << formatFPNumber(val_ / 18.0) << "em";
149 // what it's a percentage of probably won't make sense for HTML,
150 // so we'll assume people have chosen these appropriately
151 os << formatFPNumber(val_) << '%';
161 double Length::value() const
167 Length::UNIT Length::unit() const
173 void Length::value(double v)
179 void Length::unit(Length::UNIT u)
185 bool Length::zero() const
191 bool Length::empty() const
193 return unit_ == Length::UNIT_NONE;
197 int Length::inPixels(int text_width, int em_width_base) const
199 // Zoom factor specified by user in percent
200 double const zoom = lyxrc.zoom / 100.0; // [percent]
202 // DPI setting for monitor: pixels/inch
203 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
205 double const em_width_in = (em_width_base > 0)
206 ? em_width_base / (zoom * dpi)
208 // A different estimate for em_width is
209 // theFontMetrics(FontInfo(sane_font)).em()
210 // but this estimate might not be more accurate as the screen font
211 // is different then the latex font.
213 // Pixel values are scaled so that the ratio
214 // between lengths and font sizes on the screen
215 // is the same as on paper.
217 double const text_width_in = text_width / (zoom * dpi);
218 double const result = zoom * dpi * inInch(text_width_in, em_width_in);
219 return support::iround(result);
223 double Length::inInch(double text_width, double em_width) const
229 // Scaled point: sp = 1/65536 pt
230 result = val_ / (72.27 * 65536); // 4736286.7
233 // Point: 1 pt = 1/72.27 inch
234 result = val_ / 72.27; // 72.27
237 // Big point: 1 bp = 1/72 inch
238 result = val_ / 72; // 72
241 // Didot: 1157dd = 1238 pt?
242 result = val_ / (72.27 / (0.376 * 2.845)); // 67.559735
245 // Millimeter: 1 mm = 1/25.4 inch
246 result = val_ / 25.4; // 25.4
249 // Pica: 1 pc = 12 pt
250 result = val_ / (72.27 / 12); // 6.0225
253 // Cicero: 1 cc = 12 dd
255 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
258 // Centimeter: 1 cm = 1/2.54 inch
259 result = val_ / 2.54; // 2.54
266 // Ex: The height of an "x"
267 // 0.4305 is the ration between 1ex and 1em in cmr10
268 result = val_ * em_width * 0.4305;
271 // Em: The width of an "m"
272 result = val_ * em_width;
275 // math unit = 1/18em
276 result = val_ * em_width / 18;
278 case Length::PCW: // Always % of workarea
281 result = val_ * text_width / 100;
284 // paperwidth/textwidth is 1.7 for A4 paper with default margins
285 result = val_ * text_width * 1.7 / 100;
288 result = val_ * text_width * 1.787 / 100;
291 result = val_ * text_width * 2.2 / 100;
293 case Length::UNIT_NONE:
294 result = 0; // this cannot happen
301 int Length::inPixels(MetricsBase const & base) const
303 FontInfo fi = base.font;
304 if (unit_ == Length::MU)
305 // mu is 1/18th of an em in the math symbol font
306 fi.setFamily(SYMBOL_FAMILY);
308 // Math style is only taken into account in the case of mu
309 fi.setStyle(LM_ST_TEXT);
310 return inPixels(base.textwidth, theFontMetrics(fi).em());
314 int Length::inBP() const
316 // return any Length value as a one with
317 // the PostScript point, called bp (big points)
319 double const text_width_in = 210.0 / 2.54; // assume A4
320 double const em_width_in = 10.0 / 72.27;
321 double result = 72.0 * inInch(text_width_in, em_width_in);
322 return support::iround(result);
326 Length::UNIT Length::defaultUnit()
328 return lyxrc.default_length_unit;
333 bool operator==(Length const & l1, Length const & l2)
335 return l1.value() == l2.value() && l1.unit() == l2.unit();
339 bool operator!=(Length const & l1, Length const & l2)
345 /////////////////////////////////////////////////////////////////////
349 /////////////////////////////////////////////////////////////////////
352 GlueLength::GlueLength(Length const & len)
357 GlueLength::GlueLength(Length const & len, Length const & plus,
358 Length const & minus)
359 : len_(len), plus_(plus), minus_(minus)
363 GlueLength::GlueLength(string const & data)
365 // false positive from coverity
366 // coverity[CHECKED_RETURN]
367 isValidGlueLength(data, this);
371 string const GlueLength::asString() const
376 ostringstream buffer;
378 buffer << formatFPNumber(len_.value());
380 if (plus_.zero() && minus_.zero()) {
381 buffer << unit_name[len_.unit()];
387 if (len_.unit() != plus_.unit())
388 buffer << unit_name[len_.unit()];
389 buffer << '+' << formatFPNumber(plus_.value());
390 buffer << unit_name[plus_.unit()];
394 // just len and minus
396 if (len_.unit() != minus_.unit())
397 buffer << unit_name[len_.unit()];
398 buffer << '-' << formatFPNumber(minus_.value());
399 buffer << unit_name[minus_.unit()];
403 // ok, len, plus AND minus
406 if (minus_ == plus_) {
407 if (len_.unit() != minus_.unit())
408 buffer << unit_name[len_.unit()];
409 buffer << "+-" << formatFPNumber(minus_.value());
410 buffer << unit_name[minus_.unit()];
414 // this is so rare a case, why bother minimising units ?
416 buffer << unit_name[len_.unit()];
417 buffer << '+' << formatFPNumber(plus_.value()) << unit_name[plus_.unit()];
418 buffer << '-' << formatFPNumber(minus_.value()) << unit_name[minus_.unit()];
424 string const GlueLength::asLatexString() const
426 ostringstream buffer;
427 // use Length::asLatexString() to handle also the percent lengths
428 buffer << len_.Length::asLatexString();
430 buffer << " plus " << plus_.Length::asLatexString();
432 buffer << " minus " << minus_.Length::asLatexString();
437 Length const & GlueLength::len() const
443 Length const & GlueLength::plus() const
449 Length const & GlueLength::minus() const
455 bool operator==(GlueLength const & l1, GlueLength const & l2)
457 return l1.len() == l2.len()
458 && l1.plus() == l2.plus()
459 && l1.minus() == l2.minus();
463 bool operator!=(GlueLength const & l1, GlueLength const & l2)