Skip to content

Commit

Permalink
Merge pull request #8 from tobyxdd/feat/svg
Browse files Browse the repository at this point in the history
feat/svg
  • Loading branch information
eligao authored Aug 1, 2023
2 parents d3a13c8 + 1884209 commit 51a3c72
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true
}
]
}
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
fonttools==4.41.1
Pillow==10.0.0
svgwrite==1.4.3
59 changes: 47 additions & 12 deletions src/generate.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from PIL import Image, ImageDraw, ImageFont
import os
import svgwrite
from fontTools.ttLib import TTFont
from fontTools.pens.svgPathPen import SVGPathPen
from fontTools.pens.transformPen import TransformPen
from fontTools.misc import transform


from LETTERS import LETTERS

FONT = ImageFont.truetype('assets/三极隶书简体.ttf', 650)
FONT_FILE = 'assets/三极隶书简体.ttf'

OUTPUT_DIR = 'dist'
SMALL_SUBDIR = f'{OUTPUT_DIR}/small'
Expand All @@ -12,21 +17,51 @@
os.makedirs(SMALL_SUBDIR, exist_ok=True)
os.makedirs(LARGE_SUBDIR, exist_ok=True)


def main() -> None:
i = 0
font = TTFont(FONT_FILE)
# transform fonts, fixing size and upside-down
# see: https://github.com/fonttools/fonttools/issues/2388
unitsPerEm = font['head'].unitsPerEm
ttf2em = transform.Identity.scale(1/unitsPerEm, 1/unitsPerEm)
svgPerEm = 700
xform = transform.Identity.translate(
(1000-svgPerEm)/2, svgPerEm).scale(svgPerEm, -svgPerEm).transform(ttf2em)
glyphSet = font.getGlyphSet()

for letter in LETTERS:
i += 1
image = Image.new('RGBA', (1000, 1000), (0, 0, 0, 0))
pen = SVGPathPen(glyphSet)
tpen = TransformPen(pen, xform)
uni_hex = hex(ord(letter))[2:].upper()
uni_key = f"uni{uni_hex}"
glyph = glyphSet[uni_key]
glyph.draw(tpen)
pen.closePath()
path = pen.getCommands()

# TODO: extract to function
dwg = svgwrite.Drawing(
f'{SMALL_SUBDIR}/{i:03d}_{letter}.svg', size=(100, 100))
dwg.viewbox(0, 0, 1000, 1000)
dwg.add(dwg.ellipse(center=(500, 500), r=(490, 490),
stroke='black', fill='white', stroke_width=30))
dwg.add(dwg.ellipse(center=(500, 500), r=(420, 420),
stroke='black', fill='white', stroke_width=60))
dwg.add(dwg.path(d=path, fill='black'))
dwg.save()

draw = ImageDraw.Draw(image)
draw.ellipse((10, 10, 990, 990), outline='black', fill='white', width=30)
draw.ellipse((80, 80, 920, 920), outline='black', fill='white', width=60)
draw.text((500, 410), letter, fill='black', anchor='mm', font=FONT)
dwg = svgwrite.Drawing(
f'{LARGE_SUBDIR}/{i:03d}_{letter}.svg', size=(512, 512))
dwg.viewbox(0, 0, 1000, 1000)
dwg.add(dwg.ellipse(center=(500, 500), r=(490, 490),
stroke='black', fill='white', stroke_width=30))
dwg.add(dwg.ellipse(center=(500, 500), r=(420, 420),
stroke='black', fill='white', stroke_width=60))
dwg.add(dwg.path(d=path, fill='black'))
dwg.save()

image.resize((100, 100), Image.LANCZOS).save(
f'{SMALL_SUBDIR}/{i:03d}_{letter}.png') # for emoji
image.resize((512, 512), Image.LANCZOS).save(
f'{LARGE_SUBDIR}/{i:03d}_{letter}.png') # for sticker

if __name__ == '__main__':
main()
main()

0 comments on commit 51a3c72

Please sign in to comment.