Serving fonts through getFont.php
authorAlexander Ebert <ebert@woltlab.com>
Tue, 29 Oct 2013 13:39:57 +0000 (14:39 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Tue, 29 Oct 2013 13:39:57 +0000 (14:39 +0100)
wcfsetup/install/files/font/getFont.php [new file with mode: 0644]
wcfsetup/install/files/style/icon.less

diff --git a/wcfsetup/install/files/font/getFont.php b/wcfsetup/install/files/font/getFont.php
new file mode 100644 (file)
index 0000000..9961622
--- /dev/null
@@ -0,0 +1,48 @@
+<?php
+/**
+ * Serves fonts to enforce caching and to properly allow cross-domain (CORS) fetching.
+ * 
+ * This script solves the following issues:
+ *  - Firefox and Internet Explorer refuse to load fonts from different domains unless allowed by 'Access-Control-Allow-Origin'
+ *  - Chrome sometimes does not properly cache fonts, resulting in strange rendering bugs
+ * 
+ * @author     Alexander Ebert
+ * @copyright  2001-2013 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ */
+
+// allow font fetching from all domains (CORS)
+header('Access-Control-Allow-Origin: *');
+
+// ignore request if client seems to already have fetched this file
+if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) || !empty($_SERVER['HTTP_IF_NONE_MATCH'])) {
+       header("HTTP/1.1 304 Not Modified");
+       exit;
+}
+
+// list of known font types
+$types = array(
+       'eot' => 'application/vnd.ms-fontobject',
+       'woff' => 'application/x-woff', // best supported, but this is not the right one according to http://www.w3.org/TR/WOFF/#appendix-b
+       'ttf' => 'application/octet-stream'
+);
+
+if (!empty($_GET['type'])) {
+       if (isset($types[$_GET['type']])) {
+               $filename = 'fontawesome-webfont.' . $_GET['type'];
+               $data = file_get_contents($filename);
+               
+               // send cache and type headers
+               header('Content-Type: ' . $types[$_GET['type']]);
+               header('Cache-control: max-age=31536000, private');
+               header('Expires: ' . gmdate("D, d M Y H:i:s", time() + 31536000) . ' GMT');
+               header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($filename)) . ' GMT');
+               header('Content-Length: ' . strlen($data));
+               
+               die($data);
+       }
+       
+       die("Invalid type '" . $_GET['type'] . "' given");
+}
+
+die("Missing type parameter");
index 74587d182fdbe1bb84665f2ec2e72d77444d5750..2f7d58e27d0860cced0fc5def307effadbc1403c 100644 (file)
@@ -1,9 +1,10 @@
+/* do NOT reference fonts directly, always make use of 'getFont.php' */
 @font-face {
        font-family: 'FontAwesome';
-       src: url('../font/fontawesome-webfont.eot?v=3.2.1');
-       src: url('../font/fontawesome-webfont.eot?#iefix&v=3.2.1') format('embedded-opentype'),
-               url('../font/fontawesome-webfont.woff?v=3.2.1') format('woff'),
-               url('../font/fontawesome-webfont.ttf?v=3.2.1') format('truetype');
+       src: url('../font/getFont.php?type=eot&v=3.2.1');
+       src: url('../font/getFont.php?type=eot&v=3.2.1#iefix') format('embedded-opentype'),
+               url('../font/getFont.php?type=woff&v=3.2.1') format('woff'),
+               url('../font/getFont.php?type=ttf&v=3.2.1') format('truetype');
        font-weight: normal;
        font-style: normal;
 }