- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSizeConverter.php
More file actions
161 lines (128 loc) · 3.39 KB
/
SizeConverter.php
File metadata and controls
161 lines (128 loc) · 3.39 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace Mpdf;
use Psr\Log\LoggerInterface;
use Mpdf\Log\Context as LogContext;
class SizeConverter implements \Psr\Log\LoggerAwareInterface
{
private $dpi;
private $defaultFontSize;
/**
* @var \Mpdf\Mpdf
*/
private $mpdf;
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;
public function __construct($dpi, $defaultFontSize, Mpdf $mpdf, LoggerInterface $logger)
{
$this->dpi = $dpi;
$this->defaultFontSize = $defaultFontSize;
$this->mpdf = $mpdf;
$this->logger = $logger;
}
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* Depends of maxsize value to make % work properly. Usually maxsize == pagewidth
* For text $maxsize = $fontsize
* Setting e.g. margin % will use maxsize (pagewidth) and em will use fontsize
*
* @param mixed $size
* @param mixed $maxsize
* @param mixed $fontsize
* @param mixed $usefontsize Set false for e.g. margins - will ignore fontsize for % values
*
* @return float Final size in mm
*/
public function convert($size = 5, $maxsize = 0, $fontsize = false, $usefontsize = true)
{
$size = trim(strtolower($size));
$res = preg_match('/^(?P<size>[-0-9.,]+([eE]\-?[0-9]+)?)?(?P<unit>[%a-z-]+)?$/', $size, $parts);
if (!$res) {
// ignore definition
$this->logger->warning(sprintf('Invalid size representation "%s"', $size), ['context' => LogContext::CSS_SIZE_CONVERSION]);
}
$unit = !empty($parts['unit']) ? $parts['unit'] : null;
$size = !empty($parts['size']) ? (float) $parts['size'] : 0.0;
switch ($unit) {
case 'mm':
// do nothing
break;
case 'cm':
$size *= 10;
break;
case 'pt':
$size *= 1 / Mpdf::SCALE;
break;
case 'rem':
$size *= $this->mpdf->default_font_size / Mpdf::SCALE;
break;
case '%':
if ($fontsize && $usefontsize) {
$size *= $fontsize / 100;
} else {
$size *= $maxsize / 100;
}
break;
case 'in':
// mm in an inch
$size *= 25.4;
break;
case 'pc':
// PostScript picas
$size *= 38.1 / 9;
break;
case 'ex':
// Approximates "ex" as half of font height
$size *= $this->multiplyFontSize($fontsize, $maxsize, 0.5);
break;
case 'em':
$size *= $this->multiplyFontSize($fontsize, $maxsize, 1);
break;
case 'thin':
$size = 1 * (25.4 / $this->dpi);
break;
case 'medium':
$size = 3 * (25.4 / $this->dpi);
// Commented-out dead code from legacy method
// $size *= $this->multiplyFontSize($fontsize, $maxsize, 1);
break;
case 'thick':
$size = 5 * (25.4 / $this->dpi); // 5 pixel width for table borders
break;
case 'xx-small':
$size *= $this->multiplyFontSize($fontsize, $maxsize, 0.7);
break;
case 'x-small':
$size *= $this->multiplyFontSize($fontsize, $maxsize, 0.77);
break;
case 'small':
$size *= $this->multiplyFontSize($fontsize, $maxsize, 0.86);
break;
case 'large':
$size *= $this->multiplyFontSize($fontsize, $maxsize, 1.2);
break;
case 'x-large':
$size *= $this->multiplyFontSize($fontsize, $maxsize, 1.5);
break;
case 'xx-large':
$size *= $this->multiplyFontSize($fontsize, $maxsize, 2);
break;
case 'px':
default:
$size *= (25.4 / $this->dpi);
break;
}
return $size;
}
private function multiplyFontSize($fontsize, $maxsize, $ratio)
{
if ($fontsize) {
return $fontsize * $ratio;
}
return $maxsize * $ratio;
}
}