-
Notifications
You must be signed in to change notification settings - Fork 3
/
build-font-previews.php
346 lines (289 loc) · 11.8 KB
/
build-font-previews.php
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php declare(strict_types = 1);
namespace Mikk3lRo\vueFontpicker;
ini_set('memory_limit', '5G');
if (!isset($argv[1]) || $argv[1] == 'googlefonts') {
$apiKey = file_exists(__DIR__ . '/GOOGLE_API_KEY') ? file_get_contents(__DIR__ . '/GOOGLE_API_KEY') : '';
if (!is_string($apiKey) || strlen($apiKey) < 20) {
die('Invalid api key - get an api key for google fonts and put it in a file called GOOGLE_API_KEY (or hardcode it in
build-font-previews.php)');
}
$fonts = GoogleFonts::fetchAll(
$apiKey,
__DIR__ . '/font-cache',
);
$outpath = __DIR__ . '/font-preview';
} else {
$fonts = array();
$dummy = array_shift($argv);
$outpath = array_shift($argv);
if (!is_dir(dirname($outpath))) {
die('Need outpath as first argument!');
}
if (!is_dir($outpath)) {
mkdir($outpath);
}
foreach ($argv as $arg) {
// build-font-previews.php "FontName|sans-serif|0,400+0,700+1,400+1,700|/path/to/font.ttf" "Font2|serif|0,400|/path/to/font2.ttf"
if (preg_match('#^(.+)\|(.+)\|((?:[0,1],[0-9]{1,4}\+)*(?:[0,1],[0-9]{1,4}))\|(.+)$#', $arg, $matches)) {
$fontName = $matches[1];
$fontCategory = $matches[2];
$fontVariants = explode('+', $matches[3]);
$fontFile = $matches[4];
if (substr($fontFile, -4) !== '.ttf') {
die('Not a TTF file: ' . $fontFile);
} else if (!file_exists($fontFile)) {
die('File does not exist: ' . $fontFile);
}
$fonts[] = [
'name' => $fontName,
'category' => $fontCategory,
'localFile' => $fontFile,
'variants' => $fontVariants,
];
}
}
}
fontPreviewBuilder::generatePreview($fonts, $outpath);
class GoogleFonts
{
private static $apiKey;
private static $fontPath;
public static function fetchAll($apiKey, $fontPath)
{
self::$fontPath = $fontPath;
self::$apiKey = $apiKey;
if (!file_exists(self::$fontPath)) {
mkdir(self::$fontPath, 0700, true);
}
$fontInfos = self::getFontList();
//$fontInfos = array_slice($fontInfos, 0, 50);
$fonts = [];
foreach ($fontInfos as $num => $font) {
if (isset($font['files']['regular'])) {
$remoteFile = $font['files']['regular'];
} elseif (isset($font['files']['400'])) {
$remoteFile = $font['files']['400'];
} elseif (isset($font['files']['300'])) {
$remoteFile = $font['files']['300'];
} elseif (isset($font['files']['500'])) {
$remoteFile = $font['files']['500'];
} else {
$remoteFile = reset($font['files']);
}
$localFile = self::$fontPath . '/' . strtolower(preg_replace('#[^a-zA-Z0-9\-]#', '', str_replace(' ', '-', $font['family']))) . '.ttf';
if (!file_exists($localFile)) {
file_put_contents($localFile, file_get_contents($remoteFile));
sleep(1);
}
$fonts[] = [
'name' => $font['family'],
'category' => $font['category'],
'localFile' => $localFile,
'variants' => self::shortVariants($font),
];
}
return $fonts;
}
private static function shortVariants($font)
{
$shortVariants = [];
foreach ($font['variants'] as $longVariant) {
if ($longVariant == 'regular') {
$shortVariants[] = '0,400';
} elseif ($longVariant == 'italic') {
$shortVariants[] = '1,400';
} elseif (is_numeric($longVariant)) {
$shortVariants[] = '0,' . $longVariant;
} elseif (preg_match('#^([0-9]+)italic$#', $longVariant, $matches)) {
$shortVariants[] = '1,' . $matches[1];
} else {
die($longVariant);
}
}
return $shortVariants;
}
private static function getFontList()
{
$localJsonFile = self::$fontPath . '/fonts.json';
if (!is_file($localJsonFile) || filemtime($localJsonFile) < time() - 60 * 60 * 24) {
$url = 'https://www.googleapis.com/webfonts/v1/webfonts?key=' . self::$apiKey . '&sort=alpha';
$remoteJson = json_decode(
file_get_contents($url),
true,
);
if (is_array($remoteJson) && isset($remoteJson['items'])) {
file_put_contents($localJsonFile, json_encode($remoteJson));
}
}
$localJson = json_decode(file_get_contents($localJsonFile), true);
if (!is_array($localJson) || !isset($localJson['items'])) {
throw new Exception('Failed to get fonts');
}
$fonts = array_filter($localJson['items'], function ($font) {
//We only want fonts with a latin subset
if (!in_array('latin', $font['subsets'])) {
return false;
}
//A few fonts just don't work for some reason
if (in_array($font['family'], [
'Kumar One',
'Kumar One Outline',
])) {
return false;
}
return true;
});
return array_values($fonts);
}
}
class fontPreviewBuilder {
private static $outputPath;
private static $cellHeight = 40;
private static $sliceSize = 200;
public static function generatePreview($fonts, $outputPath) {
self::$outputPath = $outputPath;
if (!file_exists(self::$outputPath)) {
mkdir(self::$outputPath, 0700, true);
}
foreach ($fonts as $num => &$font) {
$font['sanename'] = strtolower(preg_replace('#[^a-zA-Z0-9\_]#', '', str_replace(' ', '_', $font['name'])));
$font['top'] = ($num % self::$sliceSize) * self::$cellHeight;
}
self::makeJson($fonts);
self::makeImages($fonts);
self::makeCss($fonts);
self::makeHtml($fonts);
}
private static function makeJson($fonts)
{
$json = [];
foreach ($fonts as $font) {
$json[] = [
'category' => $font['category'],
'name' => $font['name'],
'sane' => $font['sanename'],
'variants' => $font['variants'],
];
}
file_put_contents(self::$outputPath . '/fontInfo.json', json_encode($json, JSON_PRETTY_PRINT));
}
private static function makeImages($fonts)
{
for ($i = 0; $i < count($fonts) / self::$sliceSize; $i++) {
echo 'Slice ' . ($i + 1) . '/' . (intdiv(count($fonts), self::$sliceSize) + 1) . "\n";
$slice = array_slice($fonts, $i * self::$sliceSize, self::$sliceSize);
self::makeImage($slice, self::$outputPath . '/sprite.' . ($i + 1));
}
}
private static function makeImage($fonts, $outFile)
{
foreach ([1, 1.5, 2] as $outScale) {
echo $outScale . "x\n";
$scale = $outScale * 2;
$dstW = intval(ceil(600 * $outScale));
$dstH = intval(ceil(self::$cellHeight * count($fonts) * $outScale));
$dstCellH = intval(ceil(self::$cellHeight * $outScale));
$srcW = intval(ceil(600 * $scale));
$srcH = intval(ceil(self::$cellHeight * $scale));
$fontSize = 16 * $scale;
$indent = intval(ceil(10 * $scale));
$baseline = intval(ceil((self::$cellHeight - 12) * $scale));
$dst = imagecreatetruecolor($dstW, $dstH);
$trans = imagecolorallocatealpha($dst, 255, 255, 255, 127);
imagealphablending($dst, true);
imagefill($dst, 0, 0, $trans);
foreach ($fonts as $num => $font) {
$src = imagecreatetruecolor($srcW, $srcH);
$trans = imagecolorallocatealpha($src, 255, 255, 255, 127);
$black = imagecolorallocate($src, 0, 0, 0);
imagefill($src, 0, 0, $trans);
imagealphablending($src, true);
imagettftext(
$src,
$fontSize,
0,
$indent,
$baseline,
$black,
$font['localFile'],
$font['name'],
);
$dstTop = intval(ceil($font['top'] * $outScale));
imagecopyresampled($dst, $src, 0, $dstTop, 0, 0, $dstW, $dstCellH, $srcW, $srcH);
imagedestroy($src);
}
imagesavealpha($dst, true);
imagepng($dst, $outFile . '.' . $outScale . 'x.png');
imagedestroy($dst);
}
}
private static function makeCss($fonts)
{
$css = [];
$css[] = '[class*=" font-preview-"],';
$css[] = '[class^="font-preview-"] {';
$css[] = ' background-size: 30em auto;';
$css[] = ' background-repeat: no-repeat;';
$css[] = ' height: 2em;';
$css[] = ' image-rendering: optimizequality;';
$css[] = '}';
for ($i = 0; $i < count($fonts) / self::$sliceSize; $i++) {
$slice = array_slice($fonts, $i * self::$sliceSize, self::$sliceSize);
foreach ($slice as $font) {
$css[] = '.font-preview-' . $font['sanename'] . ',';
}
$css[] = '.font-preview-on-medium-sized-screens {';
$css[] = ' background-image: url(sprite.' . ($i + 1) . '.1.5x.png);';
$css[] = '}';
}
$css[] = '@media';
$css[] = '(-webkit-max-device-pixel-ratio: 1),';
$css[] = '(max-resolution: 96dpi) {';
for ($i = 0; $i < count($fonts) / self::$sliceSize; $i++) {
$slice = array_slice($fonts, $i * self::$sliceSize, self::$sliceSize);
foreach ($slice as $font) {
$css[] = ' .font-preview-' . $font['sanename'] . ',';
}
$css[] = ' .font-preview-on-worse-than-1x {';
$css[] = ' background-image: url(sprite.' . ($i + 1) . '.1x.png);';
$css[] = ' }';
}
$css[] = '}';
$css[] = '@media';
$css[] = '(-webkit-min-device-pixel-ratio: 1.51),';
$css[] = '(min-resolution: 145dpi) {';
for ($i = 0; $i < count($fonts) / self::$sliceSize; $i++) {
$slice = array_slice($fonts, $i * self::$sliceSize, self::$sliceSize);
foreach ($slice as $font) {
$css[] = ' .font-preview-' . $font['sanename'] . ',';
}
$css[] = ' .font-preview-on-better-than-1-and-a-half-x {';
$css[] = ' background-image: url(sprite.' . ($i + 1) . '.2x.png);';
$css[] = ' }';
}
$css[] = '}';
foreach ($fonts as $font) {
$css[] = '.font-preview-' . $font['sanename'] . '{ background-position: 0px -' . ($font['top'] / 20) . 'em }';
}
file_put_contents(self::$outputPath . '/font-previews.css', implode("\n", $css));
}
private static function makeHtml($fonts)
{
$html = [];
$html[] = '<!DOCTYPE html>';
$html[] = '<html>';
$html[] = '<head>';
$html[] = ' <title>Font previews</title>';
$html[] = ' <meta charset="UTF-8">';
$html[] = ' <meta name="viewport" content="width=device-width, initial-scale=1.0">';
$html[] = ' <link href="font-previews.css" rel="stylesheet" />';
$html[] = '</head>';
$html[] = '<body>';
foreach ($fonts as $font) {
$html[] = ' <div class="font-preview-' . $font['sanename'] . '" title="' . $font['name'] . '"></div>';
}
$html[] = '</body>';
$html[] = '</html>';
file_put_contents(self::$outputPath . '/font-previews.html', implode("\n", $html));
}
}