2003-08-23 00:17:00 +00:00
|
|
|
/**
|
2007-04-28 12:58:49 +00:00
|
|
|
* \file Length.cpp
|
2003-08-23 00:17:00 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2001-12-02 23:47:06 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* \author Matthias Ettrich
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2003-08-23 00:17:00 +00:00
|
|
|
* \author Jean-Marc Lasgouttes
|
|
|
|
* \author Angus Leeming
|
|
|
|
* \author John Levon
|
|
|
|
* \author Dekel Tsur
|
2002-03-21 17:27:08 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
2001-12-02 23:47:06 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
#include "Length.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "LyXRC.h"
|
2001-12-02 23:47:06 +00:00
|
|
|
|
2006-10-21 19:40:29 +00:00
|
|
|
#include "support/docstream.h"
|
2007-10-24 22:55:02 +00:00
|
|
|
|
2004-07-24 10:55:30 +00:00
|
|
|
#include <sstream>
|
2004-11-04 14:44:35 +00:00
|
|
|
#include <iomanip>
|
2002-04-02 13:23:35 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2001-12-02 23:47:06 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
2001-12-02 23:47:06 +00:00
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Length
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
Length::Length()
|
|
|
|
: val_(0), unit_(Length::UNIT_NONE)
|
2001-12-02 23:47:06 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
Length::Length(double v, Length::UNIT u)
|
2001-12-02 23:47:06 +00:00
|
|
|
: val_(v), unit_(u)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
Length::Length(string const & data)
|
|
|
|
: val_(0), unit_(Length::PT)
|
2001-12-02 23:47:06 +00:00
|
|
|
{
|
2007-04-28 12:58:49 +00:00
|
|
|
Length tmp;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-08-20 20:30:45 +00:00
|
|
|
if (!isValidLength(data, &tmp))
|
2001-12-02 23:47:06 +00:00
|
|
|
return; // should raise an exception
|
|
|
|
|
|
|
|
val_ = tmp.val_;
|
|
|
|
unit_ = tmp.unit_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-31 22:40:34 +00:00
|
|
|
void Length::swap(Length & rhs)
|
|
|
|
{
|
|
|
|
std::swap(val_, rhs.val_);
|
|
|
|
std::swap(unit_, rhs.unit_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
string const Length::asString() const
|
2001-12-02 23:47:06 +00:00
|
|
|
{
|
2004-04-13 06:27:29 +00:00
|
|
|
ostringstream os;
|
|
|
|
os << val_ << unit_name[unit_]; // setw?
|
|
|
|
return os.str();
|
2001-12-02 23:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
docstring const Length::asDocstring() const
|
2006-10-21 19:40:29 +00:00
|
|
|
{
|
|
|
|
odocstringstream os;
|
|
|
|
os << val_ << unit_name[unit_]; // setw?
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
string const Length::asLatexString() const
|
2001-12-02 23:47:06 +00:00
|
|
|
{
|
2004-11-04 14:44:35 +00:00
|
|
|
ostringstream os;
|
2002-02-16 15:59:55 +00:00
|
|
|
switch (unit_) {
|
2002-08-24 22:02:30 +00:00
|
|
|
case PTW:
|
2004-12-29 15:38:48 +00:00
|
|
|
os << val_ / 100.0 << "\\textwidth";
|
2004-04-20 08:51:15 +00:00
|
|
|
break;
|
|
|
|
case PCW:
|
2004-12-29 15:38:48 +00:00
|
|
|
os << val_ / 100.0 << "\\columnwidth";
|
2004-04-20 08:51:15 +00:00
|
|
|
break;
|
|
|
|
case PPW:
|
2004-12-29 15:38:48 +00:00
|
|
|
os << val_ / 100.0 << "\\paperwidth";
|
2004-04-20 08:51:15 +00:00
|
|
|
break;
|
|
|
|
case PLW:
|
2004-12-29 15:38:48 +00:00
|
|
|
os << val_ / 100.0 << "\\linewidth";
|
2004-04-20 08:51:15 +00:00
|
|
|
break;
|
|
|
|
case PPH:
|
2004-12-29 15:38:48 +00:00
|
|
|
os << val_ / 100.0 << "\\paperheight";
|
2004-04-20 08:51:15 +00:00
|
|
|
break;
|
|
|
|
case PTH:
|
2004-12-29 15:38:48 +00:00
|
|
|
os << val_ / 100.0 << "\\textheight";
|
2004-04-13 06:27:29 +00:00
|
|
|
break;
|
2001-12-02 23:47:06 +00:00
|
|
|
default:
|
2004-12-29 15:38:48 +00:00
|
|
|
os << val_ << unit_name[unit_];
|
2004-04-13 06:27:29 +00:00
|
|
|
break;
|
2001-12-02 23:47:06 +00:00
|
|
|
}
|
2004-11-04 14:44:35 +00:00
|
|
|
return os.str();
|
2001-12-02 23:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
double Length::value() const
|
2001-12-02 23:47:06 +00:00
|
|
|
{
|
|
|
|
return val_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
Length::UNIT Length::unit() const
|
2001-12-02 23:47:06 +00:00
|
|
|
{
|
|
|
|
return unit_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
void Length::value(double v)
|
2001-12-02 23:47:06 +00:00
|
|
|
{
|
|
|
|
val_ = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
void Length::unit(Length::UNIT u)
|
2001-12-02 23:47:06 +00:00
|
|
|
{
|
|
|
|
unit_ = u;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
bool Length::zero() const
|
2001-12-05 08:04:20 +00:00
|
|
|
{
|
|
|
|
return val_ == 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
bool Length::empty() const
|
2002-12-04 02:57:14 +00:00
|
|
|
{
|
2007-04-28 12:58:49 +00:00
|
|
|
return unit_ == Length::UNIT_NONE;
|
2002-12-04 02:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
int Length::inPixels(int text_width, int em_width_base) const
|
2002-01-17 23:09:31 +00:00
|
|
|
{
|
|
|
|
// Zoom factor specified by user in percent
|
|
|
|
double const zoom = lyxrc.zoom / 100.0; // [percent]
|
|
|
|
|
|
|
|
// DPI setting for monitor: pixels/inch
|
|
|
|
double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
|
|
|
|
|
2002-10-24 18:31:47 +00:00
|
|
|
double const em_width = (em_width_base > 0)
|
|
|
|
? em_width_base
|
|
|
|
: 10*(dpi/72.27)*zoom;
|
2002-11-04 02:12:42 +00:00
|
|
|
// A different estimate for em_width is
|
2007-10-28 18:51:54 +00:00
|
|
|
// theFontMetrics(FontInfo(sane_font)).width('M')
|
2002-10-24 18:31:47 +00:00
|
|
|
// but this estimate might not be more accurate as the screen font
|
|
|
|
// is different then the latex font.
|
|
|
|
|
2002-01-17 23:09:31 +00:00
|
|
|
// Pixel values are scaled so that the ratio
|
|
|
|
// between lengths and font sizes on the screen
|
|
|
|
// is the same as on paper.
|
|
|
|
|
|
|
|
double result = 0.0;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-01-17 23:09:31 +00:00
|
|
|
switch (unit_) {
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::SP:
|
2002-01-17 23:09:31 +00:00
|
|
|
// Scaled point: sp = 1/65536 pt
|
|
|
|
result = zoom * dpi * val_
|
|
|
|
/ (72.27 * 65536); // 4736286.7
|
|
|
|
break;
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::PT:
|
2002-01-17 23:09:31 +00:00
|
|
|
// Point: 1 pt = 1/72.27 inch
|
|
|
|
result = zoom * dpi * val_
|
|
|
|
/ 72.27; // 72.27
|
|
|
|
break;
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::BP:
|
2002-01-17 23:09:31 +00:00
|
|
|
// Big point: 1 bp = 1/72 inch
|
|
|
|
result = zoom * dpi * val_
|
|
|
|
/ 72; // 72
|
|
|
|
break;
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::DD:
|
2002-01-17 23:09:31 +00:00
|
|
|
// Didot: 1157dd = 1238 pt?
|
|
|
|
result = zoom * dpi * val_
|
|
|
|
/ (72.27 / (0.376 * 2.845)); // 67.559735
|
|
|
|
break;
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::MM:
|
2002-01-17 23:09:31 +00:00
|
|
|
// Millimeter: 1 mm = 1/25.4 inch
|
|
|
|
result = zoom * dpi * val_
|
|
|
|
/ 25.4; // 25.4
|
|
|
|
break;
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::PC:
|
2002-01-17 23:09:31 +00:00
|
|
|
// Pica: 1 pc = 12 pt
|
|
|
|
result = zoom * dpi * val_
|
|
|
|
/ (72.27 / 12); // 6.0225
|
|
|
|
break;
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::CC:
|
2002-01-17 23:09:31 +00:00
|
|
|
// Cicero: 1 cc = 12 dd
|
|
|
|
result = zoom * dpi * val_
|
|
|
|
/ (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
|
|
|
|
break;
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::CM:
|
2002-01-17 23:09:31 +00:00
|
|
|
// Centimeter: 1 cm = 1/2.54 inch
|
|
|
|
result = zoom * dpi * val_
|
|
|
|
/ 2.54; // 2.54
|
|
|
|
break;
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::IN:
|
2002-01-17 23:09:31 +00:00
|
|
|
// Inch
|
|
|
|
result = zoom * dpi * val_;
|
|
|
|
break;
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::EX:
|
2002-01-17 23:09:31 +00:00
|
|
|
// Ex: The height of an "x"
|
2002-10-17 13:15:25 +00:00
|
|
|
// 0.4305 is the ration between 1ex and 1em in cmr10
|
2002-10-24 18:31:47 +00:00
|
|
|
result = val_ * em_width * 0.4305;
|
2002-01-17 23:09:31 +00:00
|
|
|
break;
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::EM:
|
2002-01-17 23:09:31 +00:00
|
|
|
// Em: The width of an "m"
|
2002-10-24 18:31:47 +00:00
|
|
|
result = val_ * em_width;
|
2002-01-17 23:09:31 +00:00
|
|
|
break;
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::MU:
|
2002-10-17 13:15:25 +00:00
|
|
|
// math unit = 1/18em
|
2002-10-24 18:31:47 +00:00
|
|
|
result = val_ * em_width / 18;
|
2002-10-17 13:15:25 +00:00
|
|
|
break;
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::PCW: // Always % of workarea
|
|
|
|
case Length::PTW:
|
|
|
|
case Length::PLW:
|
2002-10-24 18:31:47 +00:00
|
|
|
result = val_ * text_width / 100;
|
|
|
|
break;
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::PPW:
|
2002-10-24 18:31:47 +00:00
|
|
|
// paperwidth/textwidth is 1.7 for A4 paper with default margins
|
|
|
|
result = val_ * text_width * 1.7 / 100;
|
2002-01-17 23:09:31 +00:00
|
|
|
break;
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::PTH:
|
2002-10-24 18:31:47 +00:00
|
|
|
result = val_ * text_width * 1.787 / 100;
|
|
|
|
break;
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::PPH:
|
2002-10-24 18:31:47 +00:00
|
|
|
result = val_ * text_width * 2.2 / 100;
|
2002-07-22 12:36:41 +00:00
|
|
|
break;
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::UNIT_NONE:
|
2002-01-17 23:09:31 +00:00
|
|
|
result = 0; // this cannot happen
|
|
|
|
break;
|
|
|
|
}
|
2002-10-17 13:15:25 +00:00
|
|
|
return static_cast<int>(result + ((result >= 0) ? 0.5 : -0.5));
|
2002-01-17 23:09:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
int Length::inBP() const
|
2002-04-11 18:36:18 +00:00
|
|
|
{
|
2007-04-28 12:58:49 +00:00
|
|
|
// return any Length value as a one with
|
2002-04-11 18:36:18 +00:00
|
|
|
// the PostScript point, called bp (big points)
|
|
|
|
double result = 0.0;
|
|
|
|
switch (unit_) {
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::CM:
|
2002-04-11 18:36:18 +00:00
|
|
|
// 1bp = 0.2835cm
|
|
|
|
result = val_ * 28.346;
|
|
|
|
break;
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::MM:
|
2002-04-11 18:36:18 +00:00
|
|
|
// 1bp = 0.02835mm
|
|
|
|
result = val_ * 2.8346;
|
|
|
|
break;
|
2007-04-28 12:58:49 +00:00
|
|
|
case Length::IN:
|
2002-04-11 18:36:18 +00:00
|
|
|
// 1pt = 1/72in
|
|
|
|
result = val_ * 72.0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// no other than bp possible
|
|
|
|
result = val_;
|
|
|
|
break;
|
|
|
|
}
|
2002-07-12 15:30:50 +00:00
|
|
|
return static_cast<int>(result + 0.5);
|
2002-04-11 18:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-06 21:24:13 +00:00
|
|
|
Length::UNIT Length::defaultUnit()
|
|
|
|
{
|
|
|
|
// FIXME user a proper pref, since we should get rid of
|
|
|
|
// default_papersize in lyxrc.
|
|
|
|
UNIT u = Length::CM;
|
|
|
|
switch (lyxrc.default_papersize) {
|
|
|
|
case PAPER_USLETTER:
|
|
|
|
case PAPER_USLEGAL:
|
|
|
|
case PAPER_USEXECUTIVE:
|
|
|
|
u = Length::IN;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
bool operator==(Length const & l1, Length const & l2)
|
2001-12-02 23:47:06 +00:00
|
|
|
{
|
|
|
|
return l1.value() == l2.value() && l1.unit() == l2.unit();
|
|
|
|
}
|
2001-12-12 12:07:38 +00:00
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
bool operator!=(Length const & l1, Length const & l2)
|
|
|
|
{
|
|
|
|
return !(l1 == l2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// GlueLength
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
GlueLength::GlueLength(Length const & len)
|
|
|
|
: len_(len)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
GlueLength::GlueLength(Length const & len, Length const & plus,
|
|
|
|
Length const & minus)
|
|
|
|
: len_(len), plus_(plus), minus_(minus)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
GlueLength::GlueLength(string const & data)
|
|
|
|
{
|
|
|
|
isValidGlueLength(data, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const GlueLength::asString() const
|
|
|
|
{
|
|
|
|
ostringstream buffer;
|
|
|
|
|
|
|
|
buffer << len_.value();
|
|
|
|
|
|
|
|
if (plus_.zero() && minus_.zero()) {
|
|
|
|
buffer << unit_name[len_.unit()];
|
|
|
|
return buffer.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
// just len and plus
|
|
|
|
if (minus_.zero()) {
|
|
|
|
if (len_.unit() != plus_.unit())
|
|
|
|
buffer << unit_name[len_.unit()];
|
|
|
|
buffer << '+' << plus_.value();
|
|
|
|
buffer << unit_name[plus_.unit()];
|
|
|
|
return buffer.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
// just len and minus
|
|
|
|
if (plus_.zero()) {
|
|
|
|
if (len_.unit() != minus_.unit())
|
|
|
|
buffer << unit_name[len_.unit()];
|
|
|
|
buffer << '-' << minus_.value();
|
|
|
|
buffer << unit_name[minus_.unit()];
|
|
|
|
return buffer.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok, len, plus AND minus
|
|
|
|
|
|
|
|
// len+-
|
|
|
|
if (minus_ == plus_) {
|
|
|
|
if (len_.unit() != minus_.unit())
|
|
|
|
buffer << unit_name[len_.unit()];
|
|
|
|
buffer << "+-" << minus_.value();
|
|
|
|
buffer << unit_name[minus_.unit()];
|
|
|
|
return buffer.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is so rare a case, why bother minimising units ?
|
|
|
|
|
|
|
|
buffer << unit_name[len_.unit()];
|
|
|
|
buffer << '+' << plus_.value() << unit_name[plus_.unit()];
|
|
|
|
buffer << '-' << minus_.value() << unit_name[minus_.unit()];
|
|
|
|
|
|
|
|
return buffer.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const GlueLength::asLatexString() const
|
|
|
|
{
|
|
|
|
ostringstream buffer;
|
|
|
|
|
|
|
|
buffer << len_.value() << unit_name[len_.unit()];
|
|
|
|
|
|
|
|
if (!plus_.zero())
|
|
|
|
buffer << " plus " << plus_.value() << unit_name[plus_.unit()];
|
|
|
|
if (!minus_.zero())
|
|
|
|
buffer << " minus " << minus_.value() << unit_name[minus_.unit()];
|
|
|
|
return buffer.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Length const & GlueLength::len() const
|
|
|
|
{
|
|
|
|
return len_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Length const & GlueLength::plus() const
|
|
|
|
{
|
|
|
|
return plus_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Length const & GlueLength::minus() const
|
|
|
|
{
|
|
|
|
return minus_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator==(GlueLength const & l1, GlueLength const & l2)
|
|
|
|
{
|
|
|
|
return l1.len() == l2.len()
|
|
|
|
&& l1.plus() == l2.plus()
|
|
|
|
&& l1.minus() == l2.minus();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator!=(GlueLength const & l1, GlueLength const & l2)
|
2001-12-12 12:07:38 +00:00
|
|
|
{
|
|
|
|
return !(l1 == l2);
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|