forked from TagStudioDev/TagStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add font thumbnail preview support (TagStudioDev#307)
* Add font thumbnail preview support * Add multiple font sizes to thumbnail * Ruff reformat * Ruff reformat * Added Metadata to info * Change the way thumbnails are structured * Small performance improvement * changed Metadata display structure * added copyright notice to added file * fix(ui): dynamically scale font previews; add .woff2, .ttc --------- Co-authored-by: Travis Abendshien <[email protected]>
- Loading branch information
1 parent
aa0aad4
commit e463635
Showing
4 changed files
with
109 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright (C) 2024 Travis Abendshien (CyanVoxel). | ||
# Licensed under the GPL-3.0 License. | ||
# Created for TagStudio: https://github.com/CyanVoxel/TagStudio | ||
|
||
from PIL import Image, ImageDraw, ImageFont | ||
|
||
|
||
def wrap_line( # type: ignore | ||
text: str, | ||
font: ImageFont.ImageFont, | ||
width: int = 256, | ||
draw: ImageDraw.ImageDraw = None, | ||
) -> int: | ||
""" | ||
Takes in a single line and returns the index it should be broken up at but | ||
it only splits one Time | ||
""" | ||
if draw is None: | ||
bg = Image.new("RGB", (width, width), color="#1e1e1e") | ||
draw = ImageDraw.Draw(bg) | ||
if draw.textlength(text, font=font) > width: | ||
for i in range( | ||
int(len(text) / int(draw.textlength(text, font=font)) * width) - 2, | ||
0, | ||
-1, | ||
): | ||
if draw.textlength(text[:i], font=font) < width: | ||
return i | ||
else: | ||
return -1 | ||
|
||
|
||
def wrap_full_text( | ||
text: str, | ||
font: ImageFont.ImageFont, | ||
width: int = 256, | ||
draw: ImageDraw.ImageDraw = None, | ||
) -> str: | ||
""" | ||
Takes in a string and breaks it up to fit in the canvas given accounts for kerning and font size etc. | ||
""" | ||
lines = [] | ||
i = 0 | ||
last_i = 0 | ||
while wrap_line(text[i:], font=font, width=width, draw=draw) > 0: | ||
i = wrap_line(text[i:], font=font, width=width, draw=draw) + last_i | ||
lines.append(text[last_i:i]) | ||
last_i = i | ||
lines.append(text[last_i:]) | ||
text_wrapped = "\n".join(lines) | ||
return text_wrapped |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters