-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitHandler.cpp
118 lines (103 loc) · 2.49 KB
/
UnitHandler.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "UnitHandler.h"
UnitHandler::UnitHandler()
{
}
bool
UnitHandler::Handle(attribute_s *attr, float *output, SVGState *state, float percentage_relation)
{
float x = 0;
char buffer[2];
int result = sscanf(attr->value.String(), "%f%c%c", &x, buffer + 0, buffer + 1);
if (result == 1) {
*output = x;
return true;
} else if (result == 2) {
// we have to work against sscanf here. it understands the e of em and
// ex as the e in 1.0e2 for example and cuts it off.
if (buffer[0] == '%')
x = x / 100 * percentage_relation;
else if (buffer[0] == 'm')
x = x * (state ? state->ResolveValue(state->font_size()) : 12);
else if (buffer[0] == 'x')
x = x * (state ? state->ResolveValue(state->font_size()) / 2 : 6);
*output = x;
return true;
} else if (result == 3) {
switch (buffer[0]) {
case 'p':
if (buffer[1] == 'x')
;// nothing
else if (buffer[1] == 't')
x = x / 72 * DEFAULT_DPI;
else if (buffer[1] == 'c')
x = x / 72 * DEFAULT_DPI * 12;
break;
case 'm':
if (buffer[1] == 'm')
x = x / 25.4 * DEFAULT_DPI;
break;
case 'c':
if (buffer[1] == 'm')
x = x / 2.54 * DEFAULT_DPI;
break;
case 'i':
if (buffer[1] == 'n')
x = x * DEFAULT_DPI;
break;
default: break;
}
*output = x;
return true;
}
return false;
}
bool
UnitHandler::Handle(attribute_s *attr, SourcedValue<float> *output)
{
float x = 0;
char buffer[2];
int result = sscanf(attr->value.String(), "%f%c%c", &x, buffer + 0, buffer + 1);
if (result == 1) {
output->unit = PX_UNIT;
output->value = x;
return true;
} else if (result == 2) {
// we have to work against sscanf here. it understands the e of em and
// ex as the e in 1.0e2 for example and cuts it off.
if (buffer[0] == '%')
output->unit = PE_UNIT;
else if (buffer[0] == 'm')
output->unit = EM_UNIT;
else if (buffer[0] == 'x')
output->unit = EX_UNIT;
output->value = x;
return true;
} else if (result == 3) {
switch (buffer[0]) {
case 'p':
if (buffer[1] == 'x')
output->unit = PX_UNIT;
else if (buffer[1] == 't')
output->unit = PT_UNIT;
else if (buffer[1] == 'c')
output->unit = PC_UNIT;
break;
case 'm':
if (buffer[1] == 'm')
output->unit = MM_UNIT;
break;
case 'c':
if (buffer[1] == 'm')
output->unit = CM_UNIT;
break;
case 'i':
if (buffer[1] == 'n')
output->unit = IN_UNIT;
break;
default: break;
}
output->value = x;
return true;
}
return false;
}