forked from vzubcu/gFontDownloader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.php
59 lines (52 loc) · 2.03 KB
/
example.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
<?php
/*
* usage example
*/
require('src/gFontDownloader.php');
$host = $_SERVER['SERVER_NAME'] ? $_SERVER['SERVER_NAME'] : $_SERVER['SERVER_ADDR'];
$dl = new \smtws\Tools\Fonts\Google\gFontDownloader();
$dl->setConfig();
//$dl->setConfig(['output'=>getcwd(),'formats'=>['woff','woff2','svg']]);
//$dl->setLogger(new \PSRLogger());
$dl->addFont('Roboto', 'italic', ['400', 500]);
$dl->addFont('Roboto', 'normal', ['400', 500, 700]);
//$dl->addFontByUrl('https://fonts.google.com/?selection.family=Open+Sans:400,400i,600,600i,700i');
//$dl->addFontByUrl("https://fonts.googleapis.com/css?family=Gelasio:500i,700|Open+Sans|Roboto&display=swap");
try {
$result = $dl->download(function($cb) {
echo '<pre>' . print_r($cb, true) . '</pre>';
});
} catch (\Exception $e) {
echo $e->getMessage();
$dl->createFamilyCssFiles();
}
echo '<pre>' . json_encode(formatOutput($result, $host), true) . '</pre>';
function formatOutput($fonts, $host = './') {
$formated = array();
if (is_array($fonts)) {
foreach ($fonts as $fontFamily => $weights) {
$key = count($formated);
$formated[$key]['title'] = $fontFamily;
$formated[$key]['path'] = '//' . $host . '/fonts/' . str_replace(' ', '_', $fontFamily) . '/font.css';
$formated[$key]['bold'] = false;
$formated[$key]['italic'] = false;
$formated[$key]['bolditalic'] = false;
foreach ($weights as $weight => $styles) {
foreach (array_keys($styles) as $style) {
if ($style == 'italic') {
if ($weight == 700) {
$formated[$key]['bolditalic'] = true;
} else {
$formated[$key]['italic'] = true;
}
} else {
if ($weight == 700) {
$formated[$key]['bold'] = true;
}
}
}
}
}
}
return $formated;
}