Skip to content

Commit

Permalink
Use a microfont for the watermark
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Nov 26, 2020
1 parent a028ee7 commit 49755c3
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ promote: install .env .envrc
echo WEB_CONCURRENCY=2 >> $@
echo MAX_REQUESTS=0 >> $@
echo MAX_REQUESTS_JITTER=0 >> $@
echo WATERMARK_DEFAULT=memegen.link >> $@

.envrc:
echo dotenv >> $@
Expand Down
3 changes: 2 additions & 1 deletion app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
# Fonts

FONTS_DIRECTORY = ROOT / "fonts"
FONT_THIN = FONTS_DIRECTORY / "TitilliumWeb-SemiBold.ttf"
FONT_THICK = FONTS_DIRECTORY / "TitilliumWeb-Black.ttf"
FONT_THIN = FONTS_DIRECTORY / "TitilliumWeb-SemiBold.ttf"
FONT_TINY = FONTS_DIRECTORY / "MicroFLF-Bold.ttf"

# Image rendering

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion app/tests/test_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import pytest

from .. import settings


def describe_template_list():
def describe_GET():
Expand Down Expand Up @@ -187,7 +189,8 @@ def it_returns_a_unique_image(expect, client):

expect(len(response.content)) != len(response2.content)

def it_allows_disabling(expect, client):
def it_allows_disabling(expect, client, monkeypatch):
monkeypatch.setattr(settings, "DEFAULT_WATERMARK", "")
request, response = client.get("/images/fry/test.png")
expect(response.status) == 200

Expand Down
8 changes: 5 additions & 3 deletions app/utils/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def add_blurred_background(

def add_watermark(image: Image, text: str) -> Image:
size = (image.size[0], 14)
font = get_font(text, 0.0, size, 99, thin=True)
font = get_font(text, 0.0, size, 99, tiny=True)
offset = get_text_offset(text, font, size)

draw = ImageDraw.Draw(image)
Expand Down Expand Up @@ -261,14 +261,16 @@ def get_font(
max_text_size: Dimensions,
max_font_size: int,
*,
thin: bool = False,
tiny: bool = False,
) -> ImageFont:
max_text_width = max_text_size[0] - max_text_size[0] / 35
max_text_height = max_text_size[1] - max_text_size[1] / 10

for size in range(max(7, max_font_size), 6, -1):

if angle or thin:
if tiny:
font = ImageFont.truetype(str(settings.FONT_TINY), size=size)
elif angle:
font = ImageFont.truetype(str(settings.FONT_THIN), size=size)
else:
font = ImageFont.truetype(str(settings.FONT_THICK), size=size)
Expand Down
Binary file added fonts/MicroFLF-Bold.ttf
Binary file not shown.

0 comments on commit 49755c3

Please sign in to comment.