forked from alfredxing/brick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrick.php
94 lines (77 loc) · 2.58 KB
/
brick.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
---
layout: null
---
<?php
/*
* Brick. Webfonts that actually look good
*/
// Catalogue array
{% assign fonts = site.fonts %}
$catalogue = array(
{% for font in fonts %}
{% if font.layout == "font" %}
"{{ font.family }}" => array(
{% for style in font.styles %}
"{{ style[0] }}" => "{{ style[1] }}"{% unless forloop.last %},{% endunless %}
{% endfor %}
){% unless forloop.last %},{% endunless %}
{% endif %}
{% endfor %}
);
// Headers
header('Content-type: text/css');
header('X-Content-Type-Options: nosniff');
header('Cache-Control: public, max-age=2628000');
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 2628000));
header('Last-Modified: ' . gmdate('D, d M Y H:i:s \G\M\T', filemtime(__FILE__)));
header('Pragma: Public');
// Template
$BASE = "@font-face{font-family:'%s';font-style:%s;font-weight:%s;src:%s}";
$SVG = "@media screen and (-webkit-min-device-pixel-ratio:0){@font-face{font-family:'%s';font-style:%s;font-weight:%s;src:%s}}";
$query = explode("/", preg_replace("/\/$|^\//", "", urldecode($_SERVER['REQUEST_URI'])));
if ($query[0] == "") {
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
exit();
}
foreach ($query as $key=>$val) {
// Query sections
$val = explode(":", $val);
$family = str_replace("+", "", $val[0]);
$weights = explode(",", $val[1]);
$flags = isset($val[2]) ? $val[2] : '';
foreach ($weights as $weight) {
// Build font URL
$woff = "//brick.a.ssl.fastly.net/fonts/"
. strtolower(str_replace(" ", '', $family))
. "/"
. $weight
. ".woff";
$svg = "//brick.a.ssl.fastly.net/fonts/"
. strtolower(str_replace(" ", '', $family))
. "/"
. $weight
. ".svg"
. "#"
. strtolower(str_replace(" ", '', $family));
// Start with no URI's
$uri = '';
// Process flags
if (strpos($flags, 'f') === false) {
$local = $catalogue[$family][$weight];
$uri .= "local('" . $local . "'),";
}
// Add font URL
$uri .= "url(" . $woff . ") format('woff')";
if (substr($weight, -1) == "i") {
$style = 'italic';
$weight = substr($weight, 0, -1);
} else {
$style = 'normal';
}
echo sprintf($BASE, $family, $style, $weight, $uri);
if (strpos($flags, 's') !== false) {
echo sprintf($SVG, $family, $style, $weight, "url(" . $svg . ") format('svg')");
}
}
}
?>