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"
35 /////////////////////////////////////////////////////////////////////
39 /////////////////////////////////////////////////////////////////////
42 : val_(0), unit_(Length::UNIT_NONE)
46 Length::Length(double v, Length::UNIT u)
51 Length::Length(string const & data)
52 : val_(0), unit_(Length::PT)
56 if (!isValidLength(data, &tmp))
57 return; // should raise an exception
64 string const Length::asString() const
67 if (unit_ != UNIT_NONE)
68 os << val_ << unit_name[unit_]; // setw?
73 docstring const Length::asDocstring() const
76 if (unit_ != UNIT_NONE)
77 os << val_ << unit_name[unit_]; // setw?
82 string const Length::asLatexString() const
85 // Do not allow scientific notation (e.g. 1.2e+03), since this is not valid
89 os << support::formatFPNumber(val_ / 100.0) << "\\textwidth";
92 os << support::formatFPNumber(val_ / 100.0) << "\\columnwidth";
95 os << support::formatFPNumber(val_ / 100.0) << "\\paperwidth";
98 os << support::formatFPNumber(val_ / 100.0) << "\\linewidth";
101 os << support::formatFPNumber(val_ / 100.0) << "\\textheight";
104 os << support::formatFPNumber(val_ / 100.0) << "\\paperheight";
109 os << support::formatFPNumber(val_) << unit_name[unit_];
116 string const Length::asHTMLString() const
132 os << val_ << unit_name[unit_];
135 os << val_/12.0 << "pt";
138 os << val_/18.0 << "em";
146 // what it's a percentage of probably won't make sense for HTML,
147 // so we'll assume people have chosen these appropriately
158 double Length::value() const
164 Length::UNIT Length::unit() const
170 void Length::value(double v)
176 void Length::unit(Length::UNIT u)
182 bool Length::zero() const
188 bool Length::empty() const
190 return unit_ == Length::UNIT_NONE;
194 int Length::inPixels(int text_width, int em_width_base) const
196 // Zoom factor specified by user in percent
197 double const zoom = lyxrc.zoom / 100.0; // [percent]
199 // DPI setting for monitor: pixels/inch
200 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
202 double const em_width = (em_width_base > 0)
204 : 10*(dpi/72.27)*zoom;
205 // A different estimate for em_width is
206 // theFontMetrics(FontInfo(sane_font)).em()
207 // but this estimate might not be more accurate as the screen font
208 // is different then the latex font.
210 // Pixel values are scaled so that the ratio
211 // between lengths and font sizes on the screen
212 // is the same as on paper.
218 // Scaled point: sp = 1/65536 pt
219 result = zoom * dpi * val_
220 / (72.27 * 65536); // 4736286.7
223 // Point: 1 pt = 1/72.27 inch
224 result = zoom * dpi * val_
228 // Big point: 1 bp = 1/72 inch
229 result = zoom * dpi * val_
233 // Didot: 1157dd = 1238 pt?
234 result = zoom * dpi * val_
235 / (72.27 / (0.376 * 2.845)); // 67.559735
238 // Millimeter: 1 mm = 1/25.4 inch
239 result = zoom * dpi * val_
243 // Pica: 1 pc = 12 pt
244 result = zoom * dpi * val_
245 / (72.27 / 12); // 6.0225
248 // Cicero: 1 cc = 12 dd
249 result = zoom * dpi * val_
250 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
253 // Centimeter: 1 cm = 1/2.54 inch
254 result = zoom * dpi * val_
259 result = zoom * dpi * val_;
262 // Ex: The height of an "x"
263 // 0.4305 is the ration between 1ex and 1em in cmr10
264 result = val_ * em_width * 0.4305;
267 // Em: The width of an "m"
268 result = val_ * em_width;
271 // math unit = 1/18em
272 result = val_ * em_width / 18;
274 case Length::PCW: // Always % of workarea
277 result = val_ * text_width / 100;
280 // paperwidth/textwidth is 1.7 for A4 paper with default margins
281 result = val_ * text_width * 1.7 / 100;
284 result = val_ * text_width * 1.787 / 100;
287 result = val_ * text_width * 2.2 / 100;
289 case Length::UNIT_NONE:
290 result = 0; // this cannot happen
293 return static_cast<int>(result + ((result >= 0) ? 0.5 : -0.5));
297 int Length::inPixels(MetricsBase const & base) const
299 return inPixels(base.textwidth, theFontMetrics(base.font).em());
303 int Length::inBP() const
305 // return any Length value as a one with
306 // the PostScript point, called bp (big points)
311 result = val_ * 28.346;
315 result = val_ * 2.8346;
319 result = val_ * 72.0;
322 // no other than bp possible
326 return static_cast<int>(result + 0.5);
330 Length::UNIT Length::defaultUnit()
332 return lyxrc.default_length_unit;
337 bool operator==(Length const & l1, Length const & l2)
339 return l1.value() == l2.value() && l1.unit() == l2.unit();
343 bool operator!=(Length const & l1, Length const & l2)
349 /////////////////////////////////////////////////////////////////////
353 /////////////////////////////////////////////////////////////////////
356 GlueLength::GlueLength(Length const & len)
361 GlueLength::GlueLength(Length const & len, Length const & plus,
362 Length const & minus)
363 : len_(len), plus_(plus), minus_(minus)
367 GlueLength::GlueLength(string const & data)
369 isValidGlueLength(data, this);
373 string const GlueLength::asString() const
378 ostringstream buffer;
380 buffer << len_.value();
382 if (plus_.zero() && minus_.zero()) {
383 buffer << unit_name[len_.unit()];
389 if (len_.unit() != plus_.unit())
390 buffer << unit_name[len_.unit()];
391 buffer << '+' << plus_.value();
392 buffer << unit_name[plus_.unit()];
396 // just len and minus
398 if (len_.unit() != minus_.unit())
399 buffer << unit_name[len_.unit()];
400 buffer << '-' << minus_.value();
401 buffer << unit_name[minus_.unit()];
405 // ok, len, plus AND minus
408 if (minus_ == plus_) {
409 if (len_.unit() != minus_.unit())
410 buffer << unit_name[len_.unit()];
411 buffer << "+-" << minus_.value();
412 buffer << unit_name[minus_.unit()];
416 // this is so rare a case, why bother minimising units ?
418 buffer << unit_name[len_.unit()];
419 buffer << '+' << plus_.value() << unit_name[plus_.unit()];
420 buffer << '-' << minus_.value() << unit_name[minus_.unit()];
426 string const GlueLength::asLatexString() const
428 ostringstream buffer;
429 // use Length::asLatexString() to handle also the percent lengths
430 buffer << len_.Length::asLatexString();
432 buffer << " plus " << plus_.Length::asLatexString();
434 buffer << " minus " << minus_.Length::asLatexString();
439 Length const & GlueLength::len() const
445 Length const & GlueLength::plus() const
451 Length const & GlueLength::minus() const
457 bool operator==(GlueLength const & l1, GlueLength const & l2)
459 return l1.len() == l2.len()
460 && l1.plus() == l2.plus()
461 && l1.minus() == l2.minus();
465 bool operator!=(GlueLength const & l1, GlueLength const & l2)