-
-
Notifications
You must be signed in to change notification settings - Fork 465
Add font thumbnail preview support #307
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
Changes from 9 commits
02b6f36
59476d8
81d44f6
da7983b
5b11f56
5e02fe6
f3b17ad
9eaa9fc
9d20c4c
2e0a400
678dc6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,11 @@ | |||||
| COLLAGE_FOLDER_NAME: str = "collages" | ||||||
| LIBRARY_FILENAME: str = "ts_library.json" | ||||||
|
|
||||||
| FONT_SAMPLE_TEXT: str = ( | ||||||
| """ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!?@$%(){}[]""" | ||||||
| ) | ||||||
| FONT_SAMPLE_SIZES: list[int] = [12, 15, 20] | ||||||
|
|
||||||
| # TODO: Turn this whitelist into a user-configurable blacklist. | ||||||
| IMAGE_TYPES: list[str] = [ | ||||||
| ".png", | ||||||
|
|
@@ -107,6 +112,7 @@ | |||||
| ] | ||||||
| PROGRAM_TYPES: list[str] = [".exe", ".app"] | ||||||
| SHORTCUT_TYPES: list[str] = [".lnk", ".desktop", ".url"] | ||||||
| FONT_TYPES: list[str] = [".ttf", ".otf", ".woff"] | ||||||
|
Member
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. Add
Suggested change
|
||||||
|
|
||||||
| ALL_FILE_TYPES: list[str] = ( | ||||||
| IMAGE_TYPES | ||||||
|
|
@@ -118,6 +124,7 @@ | |||||
| + ARCHIVE_TYPES | ||||||
| + PROGRAM_TYPES | ||||||
| + SHORTCUT_TYPES | ||||||
| + FONT_TYPES | ||||||
| ) | ||||||
|
|
||||||
| BOX_FIELDS = ["tag_box", "text_box"] | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,54 @@ | ||||||||||||||||||||||||||||||||||||||
| # 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) > 256: | ||||||||||||||||||||||||||||||||||||||
| # print(draw.textlength(text, font=font)) | ||||||||||||||||||||||||||||||||||||||
| for i in range( | ||||||||||||||||||||||||||||||||||||||
| int(len(text) / int(draw.textlength(text, font=font)) * 256) - 2, | ||||||||||||||||||||||||||||||||||||||
| 0, | ||||||||||||||||||||||||||||||||||||||
| -1, | ||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||
| if draw.textlength(text[:i], font=font) < 256: | ||||||||||||||||||||||||||||||||||||||
| # print(len(text)) | ||||||||||||||||||||||||||||||||||||||
| # print(i) | ||||||||||||||||||||||||||||||||||||||
|
Member
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. Change to use
Suggested change
|
||||||||||||||||||||||||||||||||||||||
| 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, draw=draw) + last_i | ||||||||||||||||||||||||||||||||||||||
|
Member
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. Pass width variable to
Suggested change
|
||||||||||||||||||||||||||||||||||||||
| lines.append(text[last_i:i]) | ||||||||||||||||||||||||||||||||||||||
| last_i = i | ||||||||||||||||||||||||||||||||||||||
| lines.append(text[last_i:]) | ||||||||||||||||||||||||||||||||||||||
| text_wrapped = "\n".join(lines) | ||||||||||||||||||||||||||||||||||||||
| return text_wrapped | ||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,11 +23,15 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from PySide6.QtCore import QObject, Signal, QSize | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from PySide6.QtGui import QPixmap | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from src.qt.helpers.gradient import four_corner_gradient_background | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from src.qt.helpers.text_wrapper import wrap_full_text | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from src.core.constants import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PLAINTEXT_TYPES, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FONT_TYPES, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| VIDEO_TYPES, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| IMAGE_TYPES, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RAW_IMAGE_TYPES, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FONT_SAMPLE_TEXT, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FONT_SAMPLE_SIZES, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from src.core.utils.encoding import detect_char_encoding | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -183,7 +187,33 @@ def render( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| text = text_file.read(256) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bg = Image.new("RGB", (256, 256), color="#1e1e1e") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| draw = ImageDraw.Draw(bg) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| draw.text((16, 16), text, file=(255, 255, 255)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| draw.text((16, 16), text, fill=(255, 255, 255)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| image = bg | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Fonts ======================================================== | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elif _filepath.suffix.lower() in FONT_TYPES: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if gradient: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # handles small thumbnails | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bg = Image.new("RGB", (256, 256), color="#1e1e1e") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| draw = ImageDraw.Draw(bg) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| font = ImageFont.truetype(_filepath, size=170) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| draw.text((10, 0), "Aa", font=font) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # handles big thumbnails and renders a sample text in multiple font sizes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bg = Image.new("RGB", (256, 256), color="#1e1e1e") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| draw = ImageDraw.Draw(bg) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lines_of_padding = 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| y_offset = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for font_size in FONT_SAMPLE_SIZES: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| font = ImageFont.truetype(_filepath, size=font_size) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| text_wrapped: str = wrap_full_text( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FONT_SAMPLE_TEXT, font=font, draw=draw | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
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. Use native preview sizes with dynamic scaling
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| draw.multiline_text((0, y_offset), text_wrapped, font=font) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| y_offset += ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| len(text_wrapped.split("\n")) + lines_of_padding | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) * draw.textbbox((0, 0), "A", font=font)[-1] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| image = bg | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # 3D =========================================================== | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # elif extension == 'stl': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Slight adjustment to font sample sizes to show more difference between them