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 << formatFPNumber(val_) << unit_name[unit_]; // setw?
83 string const Length::asLatexString() const
86 // Do not allow scientific notation (e.g. 1.2e+03), since this is not valid
90 os << formatFPNumber(val_ / 100.0) << "\\textwidth";
93 os << formatFPNumber(val_ / 100.0) << "\\columnwidth";
96 os << formatFPNumber(val_ / 100.0) << "\\paperwidth";
99 os << formatFPNumber(val_ / 100.0) << "\\linewidth";
102 os << formatFPNumber(val_ / 100.0) << "\\textheight";
105 os << formatFPNumber(val_ / 100.0) << "\\paperheight";
110 os << formatFPNumber(val_) << unit_name[unit_];
117 string const Length::asHTMLString() const
125 os << formatFPNumber(val_) << "pt";
133 os << formatFPNumber(val_) << unit_name[unit_];
136 os << formatFPNumber(val_ / 12.0) << "pt";
139 os << formatFPNumber(val_ / 18.0) << "em";
147 // what it's a percentage of probably won't make sense for HTML,
148 // so we'll assume people have chosen these appropriately
149 os << formatFPNumber(val_) << '%';
159 double Length::value() const
165 Length::UNIT Length::unit() const
171 void Length::value(double v)
177 void Length::unit(Length::UNIT u)
183 bool Length::zero() const
189 bool Length::empty() const
191 return unit_ == Length::UNIT_NONE;
195 int Length::inPixels(int text_width, int em_width_base) const
197 // Zoom factor specified by user in percent
198 double const zoom = lyxrc.zoom / 100.0; // [percent]
200 // DPI setting for monitor: pixels/inch
201 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
203 double const em_width_in = (em_width_base > 0)
204 ? em_width_base / (zoom * dpi)
206 // A different estimate for em_width is
207 // theFontMetrics(FontInfo(sane_font)).em()
208 // but this estimate might not be more accurate as the screen font
209 // is different then the latex font.
211 // Pixel values are scaled so that the ratio
212 // between lengths and font sizes on the screen
213 // is the same as on paper.
215 double const text_width_in = text_width / (zoom * dpi);
216 double const result = zoom * dpi * inInch(text_width_in, em_width_in);
217 return static_cast<int>(result + ((result >= 0) ? 0.5 : -0.5));
221 double Length::inInch(double text_width, double em_width) const
227 // Scaled point: sp = 1/65536 pt
228 result = val_ / (72.27 * 65536); // 4736286.7
231 // Point: 1 pt = 1/72.27 inch
232 result = val_ / 72.27; // 72.27
235 // Big point: 1 bp = 1/72 inch
236 result = val_ / 72; // 72
239 // Didot: 1157dd = 1238 pt?
240 result = val_ / (72.27 / (0.376 * 2.845)); // 67.559735
243 // Millimeter: 1 mm = 1/25.4 inch
244 result = val_ / 25.4; // 25.4
247 // Pica: 1 pc = 12 pt
248 result = val_ / (72.27 / 12); // 6.0225
251 // Cicero: 1 cc = 12 dd
253 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
256 // Centimeter: 1 cm = 1/2.54 inch
257 result = val_ / 2.54; // 2.54
264 // Ex: The height of an "x"
265 // 0.4305 is the ration between 1ex and 1em in cmr10
266 result = val_ * em_width * 0.4305;
269 // Em: The width of an "m"
270 result = val_ * em_width;
273 // math unit = 1/18em
274 result = val_ * em_width / 18;
276 case Length::PCW: // Always % of workarea
279 result = val_ * text_width / 100;
282 // paperwidth/textwidth is 1.7 for A4 paper with default margins
283 result = val_ * text_width * 1.7 / 100;
286 result = val_ * text_width * 1.787 / 100;
289 result = val_ * text_width * 2.2 / 100;
291 case Length::UNIT_NONE:
292 result = 0; // this cannot happen
299 int Length::inPixels(MetricsBase const & base) const
301 return inPixels(base.textwidth, theFontMetrics(base.font).em());
305 int Length::inBP() const
307 // return any Length value as a one with
308 // the PostScript point, called bp (big points)
310 double const text_width_in = 210.0 / 2.54; // assume A4
311 double const em_width_in = 10.0 / 72.27;
312 double result = 72.0 * inInch(text_width_in, em_width_in);
313 return static_cast<int>(result + 0.5);
317 Length::UNIT Length::defaultUnit()
319 return lyxrc.default_length_unit;
324 bool operator==(Length const & l1, Length const & l2)
326 return l1.value() == l2.value() && l1.unit() == l2.unit();
330 bool operator!=(Length const & l1, Length const & l2)
336 /////////////////////////////////////////////////////////////////////
340 /////////////////////////////////////////////////////////////////////
343 GlueLength::GlueLength(Length const & len)
348 GlueLength::GlueLength(Length const & len, Length const & plus,
349 Length const & minus)
350 : len_(len), plus_(plus), minus_(minus)
354 GlueLength::GlueLength(string const & data)
356 isValidGlueLength(data, this);
360 string const GlueLength::asString() const
365 ostringstream buffer;
367 buffer << formatFPNumber(len_.value());
369 if (plus_.zero() && minus_.zero()) {
370 buffer << unit_name[len_.unit()];
376 if (len_.unit() != plus_.unit())
377 buffer << unit_name[len_.unit()];
378 buffer << '+' << formatFPNumber(plus_.value());
379 buffer << unit_name[plus_.unit()];
383 // just len and minus
385 if (len_.unit() != minus_.unit())
386 buffer << unit_name[len_.unit()];
387 buffer << '-' << formatFPNumber(minus_.value());
388 buffer << unit_name[minus_.unit()];
392 // ok, len, plus AND minus
395 if (minus_ == plus_) {
396 if (len_.unit() != minus_.unit())
397 buffer << unit_name[len_.unit()];
398 buffer << "+-" << formatFPNumber(minus_.value());
399 buffer << unit_name[minus_.unit()];
403 // this is so rare a case, why bother minimising units ?
405 buffer << unit_name[len_.unit()];
406 buffer << '+' << formatFPNumber(plus_.value()) << unit_name[plus_.unit()];
407 buffer << '-' << formatFPNumber(minus_.value()) << unit_name[minus_.unit()];
413 string const GlueLength::asLatexString() const
415 ostringstream buffer;
416 // use Length::asLatexString() to handle also the percent lengths
417 buffer << len_.Length::asLatexString();
419 buffer << " plus " << plus_.Length::asLatexString();
421 buffer << " minus " << minus_.Length::asLatexString();
426 Length const & GlueLength::len() const
432 Length const & GlueLength::plus() const
438 Length const & GlueLength::minus() const
444 bool operator==(GlueLength const & l1, GlueLength const & l2)
446 return l1.len() == l2.len()
447 && l1.plus() == l2.plus()
448 && l1.minus() == l2.minus();
452 bool operator!=(GlueLength const & l1, GlueLength const & l2)