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.
18 #include "support/Length.h"
22 #include "support/debug.h"
23 #include "support/docstream.h"
24 #include "support/lstrings.h"
25 #include "support/lyxlib.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::UNIT_NONE)
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 << from_ascii(formatFPNumber(val_))
79 << from_ascii(unit_name[unit_]); // setw?
84 string const Length::asLatexString() const
87 // Do not allow scientific notation (e.g. 1.2e+03), since this is not valid
91 os << formatFPNumber(val_ / 100.0) << "\\textwidth";
94 os << formatFPNumber(val_ / 100.0) << "\\columnwidth";
97 os << formatFPNumber(val_ / 100.0) << "\\paperwidth";
100 os << formatFPNumber(val_ / 100.0) << "\\linewidth";
103 os << formatFPNumber(val_ / 100.0) << "\\textheight";
106 os << formatFPNumber(val_ / 100.0) << "\\paperheight";
109 os << formatFPNumber(val_ / 100.0) << "\\baselineskip";
114 os << formatFPNumber(val_) << unit_name[unit_];
121 string const Length::asHTMLString() const
129 os << formatFPNumber(val_) << "pt";
137 os << formatFPNumber(val_) << unit_name[unit_];
140 os << formatFPNumber(val_ / 12.0) << "pt";
143 os << formatFPNumber(val_ / 18.0) << "em";
152 // what it's a percentage of probably won't make sense for HTML,
153 // so we'll assume people have chosen these appropriately
154 os << formatFPNumber(val_) << '%';
164 double Length::value() const
170 Length::UNIT Length::unit() const
176 void Length::value(double v)
182 void Length::unit(Length::UNIT u)
188 bool Length::zero() const
194 bool Length::empty() const
196 return unit_ == Length::UNIT_NONE;
200 int Length::inPixels(int text_width, int em_width_base) const
202 // Zoom factor specified by user in percent
203 double const zoom = lyxrc.currentZoom / 100.0; // [percent]
205 // DPI setting for monitor: pixels/inch
206 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
208 double const em_width_in = (em_width_base > 0)
209 ? em_width_base / (zoom * dpi)
211 // A different estimate for em_width is
212 // theFontMetrics(FontInfo(sane_font)).em()
213 // but this estimate might not be more accurate as the screen font
214 // is different then the latex font.
216 // Pixel values are scaled so that the ratio
217 // between lengths and font sizes on the screen
218 // is the same as on paper.
220 double const text_width_in = text_width / (zoom * dpi);
221 double const result = zoom * dpi * inInch(text_width_in, em_width_in);
222 return support::iround(result);
226 double Length::inInch(double text_width, double em_width) const
232 // Scaled point: sp = 1/65536 pt
233 result = val_ / (72.27 * 65536); // 4736286.7
236 // Point: 1 pt = 1/72.27 inch
237 result = val_ / 72.27; // 72.27
240 // Big point: 1 bp = 1/72 inch
241 result = val_ / 72; // 72
244 // Didot: 1157dd = 1238 pt?
245 result = val_ / (72.27 / (0.376 * 2.845)); // 67.559735
248 // Millimeter: 1 mm = 1/25.4 inch
249 result = val_ / 25.4; // 25.4
252 // Pica: 1 pc = 12 pt
253 result = val_ / (72.27 / 12); // 6.0225
256 // Cicero: 1 cc = 12 dd
258 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
261 // Centimeter: 1 cm = 1/2.54 inch
262 result = val_ / 2.54; // 2.54
269 // Ex: The height of an "x"
270 // 0.4305 is the ration between 1ex and 1em in cmr10
271 result = val_ * em_width * 0.4305;
274 // Em: The width of an "m"
275 result = val_ * em_width;
278 // math unit = 1/18em
279 result = val_ * em_width / 18;
281 case Length::PCW: // Always % of workarea
284 result = val_ * text_width / 100;
287 // paperwidth/textwidth is 1.7 for A4 paper with default margins
288 result = val_ * text_width * 1.7 / 100;
291 result = val_ * text_width * 1.787 / 100;
294 result = val_ * text_width * 2.2 / 100;
297 // baselineskip is approximately 1.2 times the font size for the cmr fonts
298 // The value actually depends on the current paragraph (see TextMetrics::setRowHeight),
299 // but we do not have this information here.
300 result = val_ * em_width * 1.2 / 100;
302 case Length::UNIT_NONE:
303 result = 0; // this cannot happen
310 int Length::inBP() const
312 // return any Length value as a one with
313 // the PostScript point, called bp (big points)
315 double const text_width_in = 210.0 / 2.54; // assume A4
316 double const em_width_in = 10.0 / 72.27;
317 double const result = 72.0 * inInch(text_width_in, em_width_in);
318 return support::iround(result);
322 Length::UNIT Length::defaultUnit()
324 return lyxrc.default_length_unit;
329 bool operator==(Length const & l1, Length const & l2)
331 return l1.value() == l2.value() && l1.unit() == l2.unit();
335 bool operator!=(Length const & l1, Length const & l2)
341 /////////////////////////////////////////////////////////////////////
345 /////////////////////////////////////////////////////////////////////
348 GlueLength::GlueLength(Length const & len)
353 GlueLength::GlueLength(Length const & len, Length const & plus,
354 Length const & minus)
355 : len_(len), plus_(plus), minus_(minus)
359 GlueLength::GlueLength(string const & data)
361 if (!isValidGlueLength(data, this))
362 LYXERR0("Invalid glue length " + data);
366 string const GlueLength::asString() const
371 ostringstream buffer;
373 buffer << formatFPNumber(len_.value());
375 if (plus_.zero() && minus_.zero()) {
376 buffer << unit_name[len_.unit()];
382 if (len_.unit() != plus_.unit())
383 buffer << unit_name[len_.unit()];
384 buffer << '+' << formatFPNumber(plus_.value());
385 buffer << unit_name[plus_.unit()];
389 // just len and minus
391 if (len_.unit() != minus_.unit())
392 buffer << unit_name[len_.unit()];
393 buffer << '-' << formatFPNumber(minus_.value());
394 buffer << unit_name[minus_.unit()];
398 // ok, len, plus AND minus
401 if (minus_ == plus_) {
402 if (len_.unit() != minus_.unit())
403 buffer << unit_name[len_.unit()];
404 buffer << "+-" << formatFPNumber(minus_.value());
405 buffer << unit_name[minus_.unit()];
409 // this is so rare a case, why bother minimising units ?
411 buffer << unit_name[len_.unit()];
412 buffer << '+' << formatFPNumber(plus_.value()) << unit_name[plus_.unit()];
413 buffer << '-' << formatFPNumber(minus_.value()) << unit_name[minus_.unit()];
419 string const GlueLength::asLatexString() const
421 ostringstream buffer;
422 // use Length::asLatexString() to handle also the percent lengths
423 buffer << len_.Length::asLatexString();
425 buffer << " plus " << plus_.Length::asLatexString();
427 buffer << " minus " << minus_.Length::asLatexString();
432 Length const & GlueLength::len() const
438 Length const & GlueLength::plus() const
444 Length const & GlueLength::minus() const
450 bool operator==(GlueLength const & l1, GlueLength const & l2)
452 return l1.len() == l2.len()
453 && l1.plus() == l2.plus()
454 && l1.minus() == l2.minus();
458 bool operator!=(GlueLength const & l1, GlueLength const & l2)