From 6203345e8e5f73a0c9bff3bb927283b669bf096d Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 24 May 2020 23:10:14 +0100 Subject: [PATCH] Support for custom fonts --- daemon.py | 41 +++++++++++++++++++++++++++++++++++++++++ fonts/README.md | 1 + 2 files changed, 42 insertions(+) create mode 100644 fonts/README.md diff --git a/daemon.py b/daemon.py index 6ab0d405..e51ca3b3 100644 --- a/daemon.py +++ b/daemon.py @@ -1738,6 +1738,46 @@ class PubServer(BaseHTTPRequestHandler): self._benchmarkGETtimings(GETstartTime, GETtimings, 21) + # get fonts + if self.path.startswith('/fonts/'): + if self.path.endswith('.ttf') or \ + self.path.endswith('.woff') or \ + self.path.endswith('.woff2'): + if self.path.endswith('.ttf'): + fontType = 'application/x-font-ttf' + elif self.path.endswith('.woff'): + fontType = 'application/font-woff' + else: + fontType = 'application/font-woff2' + fontStr = self.path.split('/fonts/')[1] + fontFilename = \ + self.server.baseDir + '/fonts/' + fontStr + if self._etag_exists(fontFilename): + # The file has not changed + self._304() + return + if self.server.fontsCache.get(fontStr): + fontBinary = self.server.fontsCache[fontStr] + self._set_headers_etag(fontFilename, + fontType, + fontBinary, cookie, + callingDomain) + self._write(fontBinary) + return + else: + if os.path.isfile(fontFilename): + with open(fontFilename, 'rb') as avFile: + fontBinary = avFile.read() + self._set_headers_etag(fontFilename, + fontType, + fontBinary, cookie, + callingDomain) + self._write(fontBinary) + self.server.fontsCache[fontStr] = fontBinary + return + self._404() + return + # icon images # Note that this comes before the busy flag to avoid conflicts if self.path.startswith('/icons/'): @@ -7428,6 +7468,7 @@ def runDaemon(blogsInstance: bool, mediaInstance: bool, httpd.recentPostsCache = {} httpd.maxRecentPosts = maxRecentPosts httpd.iconsCache = {} + httpd.fontsCache = {} print('Creating inbox queue') httpd.thrInboxQueue = \ diff --git a/fonts/README.md b/fonts/README.md new file mode 100644 index 00000000..8d418685 --- /dev/null +++ b/fonts/README.md @@ -0,0 +1 @@ +Add any fonts that you may want to use within themes to this directory. They can be in ttf, woff or woff2 format. \ No newline at end of file