Update app.py
Browse files
app.py
CHANGED
|
@@ -552,10 +552,22 @@ def _render_preview(otf_path, font_name):
|
|
| 552 |
try:
|
| 553 |
from PIL import Image, ImageDraw, ImageFont
|
| 554 |
sample = f"{font_name}\nAa Bb Cc 123\nThe quick brown fox jumps over the lazy dog."
|
| 555 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 556 |
draw = ImageDraw.Draw(img)
|
| 557 |
-
|
| 558 |
-
draw.multiline_text((
|
|
|
|
| 559 |
return img
|
| 560 |
except Exception as e:
|
| 561 |
print(f"⚠️ Preview failed: {e}")
|
|
|
|
| 552 |
try:
|
| 553 |
from PIL import Image, ImageDraw, ImageFont
|
| 554 |
sample = f"{font_name}\nAa Bb Cc 123\nThe quick brown fox jumps over the lazy dog."
|
| 555 |
+
font_size = 180
|
| 556 |
+
spacing = 36
|
| 557 |
+
pad = 70
|
| 558 |
+
font = ImageFont.truetype(otf_path, font_size)
|
| 559 |
+
|
| 560 |
+
# measure first on a throwaway canvas so the text never clips
|
| 561 |
+
meas = ImageDraw.Draw(Image.new("RGB", (10, 10)))
|
| 562 |
+
bbox = meas.multiline_textbbox((0, 0), sample, font=font, spacing=spacing)
|
| 563 |
+
w = (bbox[2] - bbox[0]) + pad * 2
|
| 564 |
+
h = (bbox[3] - bbox[1]) + pad * 2
|
| 565 |
+
|
| 566 |
+
img = Image.new("RGB", (w, h), "white")
|
| 567 |
draw = ImageDraw.Draw(img)
|
| 568 |
+
# offset by bbox origin (handles negative side-bearings / descenders)
|
| 569 |
+
draw.multiline_text((pad - bbox[0], pad - bbox[1]),
|
| 570 |
+
sample, font=font, fill="black", spacing=spacing)
|
| 571 |
return img
|
| 572 |
except Exception as e:
|
| 573 |
print(f"⚠️ Preview failed: {e}")
|