Download fonts at app startup instead
We can't bake the font into the Docker image as such, because we mount the volumes which blows away anything we have in the app tree beforehand. We could stash it somewhere in the image and then copy it from there on app startup or something, but at that point we might as well just download it as part of the app startup.
This commit is contained in:
parent
766a0cc652
commit
9e6390662b
4 changed files with 66 additions and 20 deletions
|
@ -4,6 +4,8 @@ import os
|
|||
import textwrap
|
||||
from io import BytesIO
|
||||
from uuid import uuid4
|
||||
import urllib
|
||||
import logging
|
||||
|
||||
import colorsys
|
||||
from colorthief import ColorThief
|
||||
|
@ -17,29 +19,37 @@ from django.db.models import Avg
|
|||
from bookwyrm import models, settings
|
||||
from bookwyrm.tasks import app
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
IMG_WIDTH = settings.PREVIEW_IMG_WIDTH
|
||||
IMG_HEIGHT = settings.PREVIEW_IMG_HEIGHT
|
||||
BG_COLOR = settings.PREVIEW_BG_COLOR
|
||||
TEXT_COLOR = settings.PREVIEW_TEXT_COLOR
|
||||
DEFAULT_COVER_COLOR = settings.PREVIEW_DEFAULT_COVER_COLOR
|
||||
DEFAULT_FONT = settings.PREVIEW_DEFAULT_FONT
|
||||
TRANSPARENT_COLOR = (0, 0, 0, 0)
|
||||
|
||||
margin = math.floor(IMG_HEIGHT / 10)
|
||||
gutter = math.floor(margin / 2)
|
||||
inner_img_height = math.floor(IMG_HEIGHT * 0.8)
|
||||
inner_img_width = math.floor(inner_img_height * 0.7)
|
||||
font_dir = os.path.join(settings.STATIC_ROOT, "fonts/source_han_sans")
|
||||
|
||||
|
||||
def get_imagefont(name, size):
|
||||
try:
|
||||
config = settings.FONTS[name]
|
||||
path = os.path.join(settings.FONT_DIR, config["directory"], config["filename"])
|
||||
return ImageFont.truetype(path, size)
|
||||
except KeyError:
|
||||
logger.error("Font %s not found in config", name)
|
||||
except OSError:
|
||||
logger.error("Could not load font %s from file", name)
|
||||
|
||||
return ImageFont.load_default()
|
||||
|
||||
|
||||
def get_font(weight, size=28):
|
||||
"""Loads custom font"""
|
||||
font_path = os.path.join(font_dir, "SourceHanSans-VF.ttf.ttc")
|
||||
|
||||
try:
|
||||
font = ImageFont.truetype(font_path, size)
|
||||
except OSError:
|
||||
font = ImageFont.load_default()
|
||||
font = get_imagefont(DEFAULT_FONT, size)
|
||||
|
||||
try:
|
||||
if weight == "light":
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue