-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Space filling #4
Changes from 7 commits
bbd0b22
5b54413
7147cf6
9eafff9
5d7ae94
7bcb6d4
b8a9118
7ba4a53
0edaeea
3653253
a7f34b8
ac5f98c
e716ef5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from src.rectangle import Rectangle | ||
from wrdcld.rectangle import Rectangle | ||
from unittest import TestCase | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
from pathlib import Path | ||
from PIL import Image, ImageDraw, ImageFont | ||
from wrdcld.util import get_repo_root | ||
|
||
|
||
FONT_PATH = Path(r"C:\\Windows\\Fonts\\Verdanab.ttf") | ||
# FONT_PATH = Path("/usr/share/fonts/truetype/liberation/LiberationSerif-Bold.ttf") | ||
def get_default_font_path(): | ||
return get_repo_root() / "fonts" / "OpenSans-Regular.ttf" | ||
|
||
|
||
def find_fontsize_for_width(width, word): | ||
fontsize = width / 2 | ||
step = width / 2 | ||
|
||
font_path = get_default_font_path() | ||
while step > 0.5: | ||
step /= 2 | ||
font = ImageFont.truetype(FONT_PATH, fontsize) | ||
|
||
font = ImageFont.truetype(font_path, fontsize) | ||
length = font.getlength(word) | ||
|
||
if length < width: | ||
|
@@ -24,16 +26,23 @@ def find_fontsize_for_width(width, word): | |
|
||
|
||
def draw_text(canvas, img, rectangle, word, font, rotate=False): | ||
""" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good talk |
||
""" | ||
FONT_COLOR = (255, 255, 0) | ||
|
||
# text can sometimes have a negative bounding box, so we need to account for that | ||
text_bbox = font.getbbox(word) | ||
|
||
if rotate: | ||
text_image = Image.new("RGB", rectangle.rotated_ccw.wh, (73, 109, 137)) | ||
text_draw = ImageDraw.Draw(text_image) | ||
text_draw.text((0, 0), word, font=font, fill=FONT_COLOR) | ||
|
||
text_draw.text((-text_bbox[0], -text_bbox[1]), word, font=font, fill=FONT_COLOR) | ||
rotated_text_image = text_image.rotate(90, expand=True) | ||
img.paste(rotated_text_image, rectangle.xy) | ||
# canvas.rectangle(rectangle.xyrb) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
EDIT: I take that back, I think this just shows up because it's not rebased, so nothing to be doen here. |
||
|
||
else: | ||
canvas.text(rectangle.xy, word, font=font, fill=FONT_COLOR) | ||
canvas.text((rectangle.x-text_bbox[0],rectangle.y-text_bbox[1]), word, font=font, fill=FONT_COLOR) | ||
# canvas.rectangle(rectangle.xyrb) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from pathlib import Path | ||
|
||
def get_repo_root(): | ||
return Path(__file__).parent.parent |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we talked about already - I think this is too heavyweight :)