diff options
author | Akira Kakuto <kakuto@fuk.kindai.ac.jp> | 2016-07-04 22:07:54 +0000 |
---|---|---|
committer | Akira Kakuto <kakuto@fuk.kindai.ac.jp> | 2016-07-04 22:07:54 +0000 |
commit | ab399d16f49b986d1de514b2fb8434872decf91d (patch) | |
tree | 6d3d0faca673d7aa1c0f48ca679ce7841a82e3df /Build/source/texk/dvisvgm/dvisvgm-src/src/Length.cpp | |
parent | dd2749b37f7b6a9976869e29a028eeab80d686df (diff) |
dvisvgm 1.16
git-svn-id: svn://tug.org/texlive/trunk@41628 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Build/source/texk/dvisvgm/dvisvgm-src/src/Length.cpp')
-rw-r--r-- | Build/source/texk/dvisvgm/dvisvgm-src/src/Length.cpp | 124 |
1 files changed, 94 insertions, 30 deletions
diff --git a/Build/source/texk/dvisvgm/dvisvgm-src/src/Length.cpp b/Build/source/texk/dvisvgm/dvisvgm-src/src/Length.cpp index 8036e1e7b9b..8ab8120daab 100644 --- a/Build/source/texk/dvisvgm/dvisvgm-src/src/Length.cpp +++ b/Build/source/texk/dvisvgm/dvisvgm-src/src/Length.cpp @@ -18,33 +18,39 @@ ** along with this program; if not, see <http://www.gnu.org/licenses/>. ** *************************************************************************/ -#include <config.h> #include <sstream> #include "InputReader.h" #include "Length.h" -#define UNIT(c1,c2) ((c1 << 8)|c2) - using namespace std; +const double Length::pt2in = 1.0/72.27; +const double Length::pt2bp = pt2in*72; +const double Length::pt2cm = pt2in*2.54; +const double Length::pt2mm = pt2cm*10; +const double Length::pt2pc = 1.0/12; +const double Length::pt2dd = 1157.0/1238; +const double Length::pt2cc = pt2dd/12; +const double Length::pt2sp = 65536.0; + -void Length::set (const string &len) { - switch (len.length()) { +void Length::set (const string &lenstr) { + switch (lenstr.length()) { case 0: _pt = 0; break; case 1: - if (isdigit(len[0])) - _pt = len[0] - '0'; + if (isdigit(lenstr[0])) + _pt = lenstr[0] - '0'; else - throw UnitException("invalid length: "+len); + throw UnitException(string("invalid length: ")+lenstr); break; default: - istringstream iss(len); + istringstream iss(lenstr); StreamInputReader ir(iss); double val; if (!ir.parseDouble(val)) - throw UnitException("invalid length: "+len); + throw UnitException(string("invalid length: ")+lenstr); string unit = ir.getWord(); set(val, unit); } @@ -54,12 +60,83 @@ void Length::set (const string &len) { void Length::set (double val, Unit unit) { switch (unit) { case PT: _pt = val; break; - case BP: _pt = val*72.27/72; break; - case IN: _pt = val*72.27; break; - case CM: _pt = val/2.54*72.27; break; - case MM: _pt = val/25.4*72.27; break; - case PC: _pt = val/12*72.27; break; + case BP: _pt = val/pt2bp; break; + case IN: _pt = val/pt2in; break; + case CM: _pt = val/pt2cm; break; + case MM: _pt = val/pt2mm; break; + case PC: _pt = val/pt2pc; break; + case DD: _pt = val/pt2dd; break; + case CC: _pt = val/pt2cc; break; + case SP: _pt = val/pt2sp; break; + default: + // this isn't supposed to happen + ostringstream oss; + oss << "invalid length unit: (" << unit << ")"; + throw UnitException(oss.str()); + } +} + + +double Length::get (Unit unit) const { + switch (unit) { + case PT: return pt(); + case BP: return bp(); + case IN: return in(); + case CM: return cm(); + case MM: return mm(); + case PC: return pc(); + case DD: return dd(); + case CC: return cc(); + case SP: return sp(); + } + // this isn't supposed to happen + ostringstream oss; + oss << "invalid length unit: (" << unit << ")"; + throw UnitException(oss.str()); +} + + +string Length::toString (Unit unit) const { + ostringstream oss; + oss << get(unit) << unitToString(unit); + return oss.str(); +} + + +#define UNIT(c1, c2) ((c1 << 8)|c2) + +Length::Unit Length::stringToUnit (const std::string &unitstr) { + if (unitstr.length() == 2) { + switch (UNIT(unitstr[0], unitstr[1])) { + case UNIT('p','t'): return PT; + case UNIT('b','p'): return BP; + case UNIT('i','n'): return IN; + case UNIT('c','m'): return CM; + case UNIT('m','m'): return MM; + case UNIT('p','c'): return PC; + case UNIT('d','d'): return DD; + case UNIT('c','c'): return CC; + case UNIT('s','p'): return SP; + } + } + throw UnitException(string("invalid length unit: ")+unitstr); +} + + +string Length::unitToString (Unit unit) { + switch (unit) { + case PT: return "pt"; + case BP: return "bp"; + case IN: return "in"; + case CM: return "cm"; + case MM: return "mm"; + case PC: return "pc"; + case DD: return "dd"; + case CC: return "cc"; + case SP: return "sp"; } + // this isn't supposed to happen + return "??"; } @@ -67,19 +144,6 @@ void Length::set (double val, string unitstr) { if (unitstr.empty()) unitstr = "pt"; else if (unitstr.length() != 2) - throw UnitException("invalid length unit: "+unitstr); - - Unit unit; - switch (UNIT(unitstr[0], unitstr[1])) { - case UNIT('p','t'): unit = PT; break; - case UNIT('b','p'): unit = BP; break; - case UNIT('i','n'): unit = IN; break; - case UNIT('c','m'): unit = CM; break; - case UNIT('m','m'): unit = MM; break; - case UNIT('p','c'): unit = PC; break; - default: - throw UnitException("invalid length unit: "+unitstr); - } - set(val, unit); + throw UnitException(string("invalid length unit: ")+unitstr); + set(val, stringToUnit(unitstr)); } - |