font.php
4.93 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
N2Loader::import('libraries.image.color');
N2Loader::import('libraries.parse.parse');
class N2ParseFont
{
/**
* @var array
*/
private $_font;
public function __construct($font) {
$this->_font = json_decode($font, true);
}
/**
* @param string $tab
*
* @return string
*/
public function printTab($tab = '') {
if ($tab == '') $tab = $this->_font['firsttab'];
$style = '';
if (isset($this->_font[$tab])) {
$tab = &$this->_font[$tab];
$extra = '';
if (isset($tab['extra'])) {
$extra = $tab['extra'];
unset($tab['extra']);
}
foreach ($tab AS $k => $v) {
$style .= $this->parse($k, $v);
}
$style .= $this->parse('extra', $extra);
}
return $style;
}
/**
* @param $target
* @param string $source
*/
public function mixinTab($target, $source = '') {
if ($source == '') $source = $this->_font['firsttab'];
$this->_font[$target] = array_merge($this->_font[$source], $this->_font[$target]);
}
/**
* @param $property
* @param $value
*
* @return mixed
*/
public function parse($property, $value) {
$fn = 'parse' . $property;
return $this->$fn($value);
}
/**
* @param $v
*
* @return string
*/
public function parseColor($v) {
$hex = N2Color::hex82hex($v);
$style = 'color: #' . $hex[0] . ';';
if ($hex[1] != 'ff') {
$rgba = N2Color::hex2rgba($v);
$style .= 'color: RGBA(' . $rgba[0] . ',' . $rgba[1] . ',' . $rgba[2] . ',' . round($rgba[3] / 127, 2) . ');';
}
return $style;
}
/**
* @param $v
*
* @return string
*/
public function parseSize($v) {
return 'font-size:' . N2Parse::parse($v, '') . ';';
}
/**
* @param $v
*
* @return string
*/
public function parseTshadow($v) {
$v = N2Parse::parse($v);
$rgba = N2Color::hex2rgba($v[3]);
if ($v[0] == 0 && $v[1] == 0 && $v[2] == 0) return 'text-shadow: none;';
return 'text-shadow: ' . $v[0] . 'px ' . $v[1] . 'px ' . $v[2] . 'px RGBA(' . $rgba[0] . ',' . $rgba[1] . ',' . $rgba[2] . ',' . round($rgba[3] / 127, 2) . ');';
}
/**
* @param $v
*
* @return string
*/
public function parseAfont($v) {
return 'font-family: ' . $this->loadFont($v) . ';';
}
/**
* @param $v
*
* @return string
*/
public function parseLineheight($v) {
if ($v == '') return '';
return 'line-height: ' . $v . ';';
}
/**
* @param $v
*
* @return string
*/
public function parseBold($v) {
if ($v == '1') return 'font-weight: bold;';
return 'font-weight: normal;';
}
/**
* @param $v
*
* @return string
*/
public function parseItalic($v) {
if ($v == '1') return 'font-style: italic;';
return 'font-style: normal;';
}
/**
* @param $v
*
* @return string
*/
public function parseUnderline($v) {
if ($v == '1') return 'text-decoration: underline;';
return 'text-decoration: none;';
}
/**
* @param $v
*
* @return string
*/
public function parsePaddingleft($v) {
/*$transition = '-moz-transition: padding-left 0.4s ease;';
$transition .= '-webkit-transition: padding-left 0.4s ease;';
$transition .= '-o-transition: padding-left 0.4s ease;';
$transition .= 'transition: padding-left 0.4s ease;';
return $transition . 'padding-left: ' . $v . 'px;';*/
return '';
}
/**
* @param $v
*
* @return string
*/
public function parseAlign($v) {
return 'text-align: ' . $v . ';';
}
/**
* @param $v
*
* @return string
*/
public function parsereset($v) {
return '';
}
public function parseextra($v) {
return $v;
}
/**
* @param $families
*
* @return mixed
*/
public function loadFont($families) {
preg_match_all("/google\(.*?family=(.*?)\);\)/", $families, $out, PREG_SET_ORDER);
foreach ($out AS $f) {
preg_match('/(.*?)(:(.*?))?(&subset=(.*))?$/', $f[1], $g);
$family = str_replace('+', ' ', $g[1]);
$styles = 400;
if (isset($g[3]) && !empty($g[3])) {
$styles = $g[3];
}
$subset = 'latin';
if (isset($g[5])) {
$subset = $g[5];
}
N2GoogleFonts::addSubset($subset);
foreach (explode(',', $styles) AS $style) {
N2GoogleFonts::addFont($family, $style);
}
$families = str_replace($f[0], "'" . $family . "'", $families);
}
return $families;
}
}