summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvisvgm/dvisvgm-1.4/src/Length.cpp
blob: aae20b74e59ff660052676b87b0e796596cd9d86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*************************************************************************
** Length.cpp                                                           **
**                                                                      **
** This file is part of dvisvgm -- the DVI to SVG converter             **
** Copyright (C) 2005-2013 Martin Gieseking <martin.gieseking@uos.de>   **
**                                                                      **
** This program is free software; you can redistribute it and/or        **
** modify it under the terms of the GNU General Public License as       **
** published by the Free Software Foundation; either version 3 of       **
** the License, or (at your option) any later version.                  **
**                                                                      **
** This program is distributed in the hope that it will be useful, but  **
** WITHOUT ANY WARRANTY; without even the implied warranty of           **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         **
** GNU General Public License for more details.                         **
**                                                                      **
** You should have received a copy of the GNU General Public License    **
** along with this program; if not, see <http://www.gnu.org/licenses/>. **
*************************************************************************/

#include <sstream>
#include "InputReader.h"
#include "Length.h"

#define UNIT(c1,c2) ((c1 << 8)|c2)

using namespace std;


void Length::set (const string &len) {
	switch (len.length()) {
		case 0: 
			_pt = 0; 
			break;
		case 1:
			if (isdigit(len[0]))
				_pt = len[0] - '0';
			else
				throw UnitException("invalid length: "+len);
			break;
		default:
			istringstream iss(len);
			StreamInputReader ir(iss);
			double val; 
			if (!ir.parseDouble(val))
				throw UnitException("invalid length: "+len);
			string unit = ir.getWord();
			set(val, unit);
	}
}


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;
	}
}


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);
}