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 = (em_width_base > 0)
205 : 10*(dpi/72.27)*zoom;
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.
219 // Scaled point: sp = 1/65536 pt
220 result = zoom * dpi * val_
221 / (72.27 * 65536); // 4736286.7
224 // Point: 1 pt = 1/72.27 inch
225 result = zoom * dpi * val_
229 // Big point: 1 bp = 1/72 inch
230 result = zoom * dpi * val_
234 // Didot: 1157dd = 1238 pt?
235 result = zoom * dpi * val_
236 / (72.27 / (0.376 * 2.845)); // 67.559735
239 // Millimeter: 1 mm = 1/25.4 inch
240 result = zoom * dpi * val_
244 // Pica: 1 pc = 12 pt
245 result = zoom * dpi * val_
246 / (72.27 / 12); // 6.0225
249 // Cicero: 1 cc = 12 dd
250 result = zoom * dpi * val_
251 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
254 // Centimeter: 1 cm = 1/2.54 inch
255 result = zoom * dpi * val_
260 result = zoom * dpi * val_;
263 // Ex: The height of an "x"
264 // 0.4305 is the ration between 1ex and 1em in cmr10
265 result = val_ * em_width * 0.4305;
268 // Em: The width of an "m"
269 result = val_ * em_width;
272 // math unit = 1/18em
273 result = val_ * em_width / 18;
275 case Length::PCW: // Always % of workarea
278 result = val_ * text_width / 100;
281 // paperwidth/textwidth is 1.7 for A4 paper with default margins
282 result = val_ * text_width * 1.7 / 100;
285 result = val_ * text_width * 1.787 / 100;
288 result = val_ * text_width * 2.2 / 100;
290 case Length::UNIT_NONE:
291 result = 0; // this cannot happen
294 return static_cast<int>(result + ((result >= 0) ? 0.5 : -0.5));
298 int Length::inPixels(MetricsBase const & base) const
300 return inPixels(base.textwidth, theFontMetrics(base.font).em());
304 int Length::inBP() const
306 // return any Length value as a one with
307 // the PostScript point, called bp (big points)
312 result = val_ * 28.346;
316 result = val_ * 2.8346;
320 result = val_ * 72.0;
323 // no other than bp possible
327 return static_cast<int>(result + 0.5);
331 Length::UNIT Length::defaultUnit()
333 return lyxrc.default_length_unit;
338 bool operator==(Length const & l1, Length const & l2)
340 return l1.value() == l2.value() && l1.unit() == l2.unit();
344 bool operator!=(Length const & l1, Length const & l2)
350 /////////////////////////////////////////////////////////////////////
354 /////////////////////////////////////////////////////////////////////
357 GlueLength::GlueLength(Length const & len)
362 GlueLength::GlueLength(Length const & len, Length const & plus,
363 Length const & minus)
364 : len_(len), plus_(plus), minus_(minus)
368 GlueLength::GlueLength(string const & data)
370 isValidGlueLength(data, this);
374 string const GlueLength::asString() const
379 ostringstream buffer;
381 buffer << formatFPNumber(len_.value());
383 if (plus_.zero() && minus_.zero()) {
384 buffer << unit_name[len_.unit()];
390 if (len_.unit() != plus_.unit())
391 buffer << unit_name[len_.unit()];
392 buffer << '+' << formatFPNumber(plus_.value());
393 buffer << unit_name[plus_.unit()];
397 // just len and minus
399 if (len_.unit() != minus_.unit())
400 buffer << unit_name[len_.unit()];
401 buffer << '-' << formatFPNumber(minus_.value());
402 buffer << unit_name[minus_.unit()];
406 // ok, len, plus AND minus
409 if (minus_ == plus_) {
410 if (len_.unit() != minus_.unit())
411 buffer << unit_name[len_.unit()];
412 buffer << "+-" << formatFPNumber(minus_.value());
413 buffer << unit_name[minus_.unit()];
417 // this is so rare a case, why bother minimising units ?
419 buffer << unit_name[len_.unit()];
420 buffer << '+' << formatFPNumber(plus_.value()) << unit_name[plus_.unit()];
421 buffer << '-' << formatFPNumber(minus_.value()) << unit_name[minus_.unit()];
427 string const GlueLength::asLatexString() const
429 ostringstream buffer;
430 // use Length::asLatexString() to handle also the percent lengths
431 buffer << len_.Length::asLatexString();
433 buffer << " plus " << plus_.Length::asLatexString();
435 buffer << " minus " << minus_.Length::asLatexString();
440 Length const & GlueLength::len() const
446 Length const & GlueLength::plus() const
452 Length const & GlueLength::minus() const
458 bool operator==(GlueLength const & l1, GlueLength const & l2)
460 return l1.len() == l2.len()
461 && l1.plus() == l2.plus()
462 && l1.minus() == l2.minus();
466 bool operator!=(GlueLength const & l1, GlueLength const & l2)