2003-08-23 00:17:00 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file lyxlength.C
|
|
|
|
|
* 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
|
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
* \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>
|
|
|
|
|
|
|
|
|
|
#include "lyxlength.h"
|
2001-12-05 08:04:20 +00:00
|
|
|
|
#include "lengthcommon.h"
|
2002-01-17 23:09:31 +00:00
|
|
|
|
#include "lyxrc.h"
|
2001-12-02 23:47:06 +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
|
|
|
|
|
2001-12-02 23:47:06 +00:00
|
|
|
|
|
2003-09-05 18:02:24 +00:00
|
|
|
|
using std::ostringstream;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
2003-09-05 18:02:24 +00:00
|
|
|
|
|
2001-12-02 23:47:06 +00:00
|
|
|
|
|
|
|
|
|
LyXLength::LyXLength()
|
2002-12-04 02:57:14 +00:00
|
|
|
|
: val_(0), unit_(LyXLength::UNIT_NONE)
|
2001-12-02 23:47:06 +00:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LyXLength::LyXLength(double v, LyXLength::UNIT u)
|
|
|
|
|
: val_(v), unit_(u)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LyXLength::LyXLength(string const & data)
|
2001-12-15 16:24:40 +00:00
|
|
|
|
: val_(0), unit_(LyXLength::PT)
|
2001-12-02 23:47:06 +00:00
|
|
|
|
{
|
|
|
|
|
LyXLength 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_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string const LyXLength::asString() const
|
|
|
|
|
{
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string const LyXLength::asLatexString() const
|
|
|
|
|
{
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double LyXLength::value() const
|
|
|
|
|
{
|
|
|
|
|
return val_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LyXLength::UNIT LyXLength::unit() const
|
|
|
|
|
{
|
|
|
|
|
return unit_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LyXLength::value(double v)
|
|
|
|
|
{
|
|
|
|
|
val_ = v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LyXLength::unit(LyXLength::UNIT u)
|
|
|
|
|
{
|
|
|
|
|
unit_ = u;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
|
bool LyXLength::zero() const
|
2001-12-05 08:04:20 +00:00
|
|
|
|
{
|
|
|
|
|
return val_ == 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-12-04 02:57:14 +00:00
|
|
|
|
bool LyXLength::empty() const
|
|
|
|
|
{
|
|
|
|
|
return unit_ == LyXLength::UNIT_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-10-24 18:31:47 +00:00
|
|
|
|
int LyXLength::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
|
2006-10-07 16:15:06 +00:00
|
|
|
|
// theApp->fontLoader().metrics(LyXFont(LyXFont::ALL_SANE)).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_) {
|
|
|
|
|
case LyXLength::SP:
|
|
|
|
|
// Scaled point: sp = 1/65536 pt
|
|
|
|
|
result = zoom * dpi * val_
|
|
|
|
|
/ (72.27 * 65536); // 4736286.7
|
|
|
|
|
break;
|
|
|
|
|
case LyXLength::PT:
|
|
|
|
|
// Point: 1 pt = 1/72.27 inch
|
|
|
|
|
result = zoom * dpi * val_
|
|
|
|
|
/ 72.27; // 72.27
|
|
|
|
|
break;
|
|
|
|
|
case LyXLength::BP:
|
|
|
|
|
// Big point: 1 bp = 1/72 inch
|
|
|
|
|
result = zoom * dpi * val_
|
|
|
|
|
/ 72; // 72
|
|
|
|
|
break;
|
|
|
|
|
case LyXLength::DD:
|
|
|
|
|
// Didot: 1157dd = 1238 pt?
|
|
|
|
|
result = zoom * dpi * val_
|
|
|
|
|
/ (72.27 / (0.376 * 2.845)); // 67.559735
|
|
|
|
|
break;
|
|
|
|
|
case LyXLength::MM:
|
|
|
|
|
// Millimeter: 1 mm = 1/25.4 inch
|
|
|
|
|
result = zoom * dpi * val_
|
|
|
|
|
/ 25.4; // 25.4
|
|
|
|
|
break;
|
|
|
|
|
case LyXLength::PC:
|
|
|
|
|
// Pica: 1 pc = 12 pt
|
|
|
|
|
result = zoom * dpi * val_
|
|
|
|
|
/ (72.27 / 12); // 6.0225
|
|
|
|
|
break;
|
|
|
|
|
case LyXLength::CC:
|
|
|
|
|
// Cicero: 1 cc = 12 dd
|
|
|
|
|
result = zoom * dpi * val_
|
|
|
|
|
/ (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
|
|
|
|
|
break;
|
|
|
|
|
case LyXLength::CM:
|
|
|
|
|
// Centimeter: 1 cm = 1/2.54 inch
|
|
|
|
|
result = zoom * dpi * val_
|
|
|
|
|
/ 2.54; // 2.54
|
|
|
|
|
break;
|
|
|
|
|
case LyXLength::IN:
|
|
|
|
|
// Inch
|
|
|
|
|
result = zoom * dpi * val_;
|
|
|
|
|
break;
|
|
|
|
|
case LyXLength::EX:
|
|
|
|
|
// 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;
|
2002-10-17 13:15:25 +00:00
|
|
|
|
case LyXLength::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;
|
2002-10-17 13:15:25 +00:00
|
|
|
|
case LyXLength::MU:
|
|
|
|
|
// 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;
|
2002-08-24 22:02:30 +00:00
|
|
|
|
case LyXLength::PCW: // Always % of workarea
|
|
|
|
|
case LyXLength::PTW:
|
|
|
|
|
case LyXLength::PLW:
|
2002-10-24 18:31:47 +00:00
|
|
|
|
result = val_ * text_width / 100;
|
|
|
|
|
break;
|
|
|
|
|
case LyXLength::PPW:
|
|
|
|
|
// 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;
|
2002-08-24 22:02:30 +00:00
|
|
|
|
case LyXLength::PTH:
|
2002-10-24 18:31:47 +00:00
|
|
|
|
result = val_ * text_width * 1.787 / 100;
|
|
|
|
|
break;
|
2002-08-24 22:02:30 +00:00
|
|
|
|
case LyXLength::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;
|
2002-01-17 23:09:31 +00:00
|
|
|
|
case LyXLength::UNIT_NONE:
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-04-11 18:36:18 +00:00
|
|
|
|
int LyXLength::inBP() const
|
|
|
|
|
{
|
|
|
|
|
// return any LyXLength value as a one with
|
|
|
|
|
// the PostScript point, called bp (big points)
|
|
|
|
|
double result = 0.0;
|
|
|
|
|
switch (unit_) {
|
|
|
|
|
case LyXLength::CM:
|
|
|
|
|
// 1bp = 0.2835cm
|
|
|
|
|
result = val_ * 28.346;
|
|
|
|
|
break;
|
|
|
|
|
case LyXLength::MM:
|
|
|
|
|
// 1bp = 0.02835mm
|
|
|
|
|
result = val_ * 2.8346;
|
|
|
|
|
break;
|
|
|
|
|
case LyXLength::IN:
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-12-02 23:47:06 +00:00
|
|
|
|
bool operator==(LyXLength const & l1, LyXLength const & l2)
|
|
|
|
|
{
|
|
|
|
|
return l1.value() == l2.value() && l1.unit() == l2.unit();
|
|
|
|
|
}
|
2001-12-12 12:07:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool operator!=(LyXLength const & l1, LyXLength const & l2)
|
|
|
|
|
{
|
|
|
|
|
return !(l1 == l2);
|
|
|
|
|
}
|