From 6f343ad93655bce604aef511bc0edb8bcbcf104b Mon Sep 17 00:00:00 2001 From: Dheatly23 <71598333+Dheatly23@users.noreply.github.com> Date: Thu, 9 Feb 2023 11:24:37 +0700 Subject: [PATCH 1/2] Take Into Account Non-integer Offsets --- src/monocraft.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/monocraft.py b/src/monocraft.py index d2855bb..2cc61a6 100644 --- a/src/monocraft.py +++ b/src/monocraft.py @@ -16,6 +16,7 @@ import os import fontforge import json +import math from generate_diacritics import generateDiacritics from generate_examples import generateExamples from polygonizer import PixelImage, generatePolygons @@ -75,13 +76,20 @@ def generateFont(): def generateImage(character): image = PixelImage() + dx, dy = 0, 0 if "pixels" in character: arr = character["pixels"] - x = int(character["leftMargin"]) if "leftMargin" in character else 0 - y = int(-character["descent"]) if "descent" in character else 0 + leftMargin = character["leftMargin"] if "leftMargin" in character else 0 + x = math.floor(leftMargin) + dx += leftMargin - x + descent = -character["descent"] if "descent" in character else 0 + y = math.floor(descent) + dy += descent - y image = image | imageFromArray(arr, x, y) if "reference" in character: - image = image | generateImage(charactersByCodepoint[character["reference"]]) + # XXX: What to do with dx and dy? + other, (dx, dy) = generateImage(charactersByCodepoint[character["reference"]]) + image = image | other if "diacritic" in character: diacritic = diacritics[character["diacritic"]] arr = diacritic["pixels"] @@ -90,7 +98,7 @@ def generateImage(character): if "diacriticSpace" in character: y += int(character["diacriticSpace"]) image = image | imageFromArray(arr, x, y) - return image + return (image, (dx, dy)) def findHighestY(image): for y in range(image.y_end - 1, image.y, -1): @@ -108,12 +116,13 @@ def imageFromArray(arr, x=0, y=0): data=bytes(x for a in reversed(arr) for x in a), ) -def drawImage(image, pen): +def drawImage(data, pen): + image, (dx, dy) = data for polygon in generatePolygons(image): start = True for x, y in polygon: - x *= PIXEL_SIZE - y *= PIXEL_SIZE + x = (x + dx) * PIXEL_SIZE + y = (y + dy) * PIXEL_SIZE if start: pen.moveTo(x, y) start = False From 47696c0c272a000f76fdf61c33d055e805fae286 Mon Sep 17 00:00:00 2001 From: Dheatly23 <71598333+Dheatly23@users.noreply.github.com> Date: Fri, 10 Feb 2023 11:35:16 +0700 Subject: [PATCH 2/2] Use Keyword Argument Ensure future-proofing for extra data with kwarg. --- src/monocraft.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/monocraft.py b/src/monocraft.py index 2cc61a6..c1d5022 100644 --- a/src/monocraft.py +++ b/src/monocraft.py @@ -53,7 +53,8 @@ def generateFont(): top = 0 drawn = character - drawImage(generateImage(character), pen) + image, kw = generateImage(character) + drawImage(image, pen, **kw) monocraft[character["name"]].width = PIXEL_SIZE * 6 print(f"Generated {len(characters)} characters") @@ -66,7 +67,8 @@ def generateFont(): for ligature in ligatures: lig = monocraft.createChar(-1, ligature["name"]) pen = monocraft[ligature["name"]].glyphPen() - drawImage(generateImage(ligature), pen) + image, kw = generateImage(ligature) + drawImage(image, pen, **kw) monocraft[ligature["name"]].width = PIXEL_SIZE * len(ligature["sequence"]) * 6 lig.addPosSub("ligatures-subtable", tuple(map(lambda codepoint: charactersByCodepoint[codepoint]["name"], ligature["sequence"]))) print(f"Generated {len(ligatures)} ligatures") @@ -76,20 +78,20 @@ def generateFont(): def generateImage(character): image = PixelImage() - dx, dy = 0, 0 + kw = {} if "pixels" in character: arr = character["pixels"] leftMargin = character["leftMargin"] if "leftMargin" in character else 0 x = math.floor(leftMargin) - dx += leftMargin - x + kw['dx'] = leftMargin - x descent = -character["descent"] if "descent" in character else 0 y = math.floor(descent) - dy += descent - y + kw['dy'] = descent - y image = image | imageFromArray(arr, x, y) if "reference" in character: - # XXX: What to do with dx and dy? - other, (dx, dy) = generateImage(charactersByCodepoint[character["reference"]]) - image = image | other + other = generateImage(charactersByCodepoint[character["reference"]]) + kw.update(other[1]) + image = image | other[0] if "diacritic" in character: diacritic = diacritics[character["diacritic"]] arr = diacritic["pixels"] @@ -98,7 +100,7 @@ def generateImage(character): if "diacriticSpace" in character: y += int(character["diacriticSpace"]) image = image | imageFromArray(arr, x, y) - return (image, (dx, dy)) + return (image, kw) def findHighestY(image): for y in range(image.y_end - 1, image.y, -1): @@ -116,8 +118,7 @@ def imageFromArray(arr, x=0, y=0): data=bytes(x for a in reversed(arr) for x in a), ) -def drawImage(data, pen): - image, (dx, dy) = data +def drawImage(image, pen, *, dx=0, dy=0): for polygon in generatePolygons(image): start = True for x, y in polygon: