Tobias Simetsreiter преди 4 години
родител
ревизия
120eee5a89
променени са 2 файла, в които са добавени 28 реда и са изтрити 14 реда
  1. 15 12
      simpilib/__init__.py
  2. 13 2
      simpilib/eink.py

+ 15 - 12
simpilib/__init__.py

@@ -9,26 +9,29 @@ def loadFontSize(text, imagesize, fontpath):
     while True:
         fontsize += 1
         font = ImageFont.truetype(fontpath, fontsize)
-        fsize = [0,0]
-        for l in splittext:
-            fs = font.getsize(l)
-            if fsize[0] < fs[0]:
-                fsize[0] = fs[0]
-            fsize[1] += fs[1]
-            fsize[1] += 2
-            print("Fs:", fsize,fs)
         # fsize = draw.textsize(text, font=font)
-
-        if ((fsize[0] > imagesize[0]) or (fsize[1] > imagesize[1])):
-            break
+        fsize = getFontSize(font, splittext)
         print("Fontsize:", font.size, fsize, imagesize)
+        if ((fsize[0] >= imagesize[0]) or (fsize[1] >= imagesize[1])):
+            break
 
     fontsize -= 1
     font = ImageFont.truetype(fontpath, fontsize)
+    fsize = getFontSize(font, splittext)
+    print("Fontsize:", font.size, fsize, imagesize)
     return font
 
+def getFontSize(font,splittext):
+    fsize = [0,0]
+    for l in splittext:
+        fs = font.getsize(l)
+        if fsize[0] < fs[0]:
+            fsize[0] = fs[0]
+        fsize[1] += fs[1]
+        print("Fs:", fsize,fs)
+    return fsize
 
-def wrapText(text, aspect_ratio, font_factor=3.2):
+def wrapText(text, aspect_ratio, font_factor=3):
     import textwrap
     text = " ".join(text.split())
     iter_width = len(text)

+ 13 - 2
simpilib/eink.py

@@ -52,6 +52,7 @@ def eink_quote_qr(args):
                 text = groups[1]
                 author = groups[2]
 
+    text = re.sub(r"[^a-zA-Z0-9, ]","", text).strip()
     if author:
         author = re.sub(r"[^a-zA-Z0-9 ]","", author).strip()
         from urllib.parse import urlencode
@@ -93,12 +94,22 @@ def eink_text_qr(text, qrdata, smalltext=None):
 
     text, font = simpilib.fitText(text_image, fontpath, text)
 
-    draw.text((0,0), text, align="center", font = font, fill = 255, spacing=2)
+    w, h = draw.textsize(text, font=font)
+    center = [
+        int((text_image[0] - w)/2),
+        int((text_image[1]-h)/2) - 3,
+    ]
+    draw.text(center, text, align="center", font = font, fill = 255, spacing=2)
 
     if smalltext != None and len(smalltext) > 2:
         smalltext, smallfont = simpilib.fitText(small_image, fontpath, smalltext)
 
-        draw.text((base_image.width-pic.width, pic.height), smalltext, align="center", font = font, fill = 255, spacing=2)
+        w, h = draw.textsize(smalltext, font=font)
+        center = [
+            base_image.width-pic.width+int((small_image[0] - w)/2),
+            pic.height + int((small_image[1] - h)/2),
+        ]
+        draw.text(center, smalltext, align="center", font = font, fill = 255, spacing=2)
 
     epd.displayPartial(epd.getbuffer(base_image))
     epd.sleep()