Dusted off - made to work to some extent
This commit is contained in:
@@ -1,34 +1,31 @@
|
||||
import os
|
||||
|
||||
import svgwrite
|
||||
from svgwrite.shapes import Polyline
|
||||
from svgwrite.path import Path
|
||||
|
||||
from dataclasses import Glyph, Metadata
|
||||
from local_dataclasses import Glyph, Metadata
|
||||
|
||||
base = 16
|
||||
cap = -16
|
||||
svg_size = 1024
|
||||
guard = 12
|
||||
line_style = {'fill': 'none', 'stroke': 'black', 'stroke-width': 0, 'stroke-linecap': 'round',
|
||||
'stroke-linejoin': 'round'}
|
||||
|
||||
|
||||
def generate(glyph: Glyph, metadata: Metadata):
|
||||
"""Uses the glyph and the metadata to create the SVGs.
|
||||
print(f"Generating character '{metadata.character}' for font {metadata.font_name}")
|
||||
|
||||
The subfolder is the font name.
|
||||
The SVG name contains the metadata necessary to import the glyph to the font.
|
||||
"""
|
||||
print(f'Generating character \'{metadata.character}\' for font {metadata.font_name}')
|
||||
|
||||
# Assumes font folder already exists
|
||||
font_path = os.path.join(os.getcwd(), 'svgs', metadata.font_name)
|
||||
|
||||
left_side_bearing, right_side_bearing = _calculate_bearings(glyph)
|
||||
svg_filename = f'ascii{str(metadata.ascii_value)}_l{left_side_bearing}_r{right_side_bearing}.svg'
|
||||
dwg = svgwrite.Drawing(os.path.join(font_path, svg_filename), viewBox=f"0 0 {svg_size} {svg_size}")
|
||||
|
||||
dwg = svgwrite.Drawing(
|
||||
os.path.join(font_path, svg_filename),
|
||||
viewBox=f"0 0 {svg_size} {svg_size}"
|
||||
)
|
||||
|
||||
points = []
|
||||
|
||||
for coordinate in glyph.coordinates:
|
||||
if coordinate:
|
||||
x, y = coordinate
|
||||
@@ -36,20 +33,27 @@ def generate(glyph: Glyph, metadata: Metadata):
|
||||
y = _map_to_svg(y)
|
||||
points.append((x, y))
|
||||
else:
|
||||
# Pen up
|
||||
# Always check if line is empty (i.e. points array is empty).
|
||||
# Empty lines can cause problems.
|
||||
if points:
|
||||
dwg.add(Polyline(points, **line_style))
|
||||
_add_path(dwg, points)
|
||||
points = []
|
||||
|
||||
# Finalize the drawing
|
||||
if points:
|
||||
dwg.add(Polyline(points, **line_style))
|
||||
_add_path(dwg, points)
|
||||
|
||||
dwg.save()
|
||||
|
||||
|
||||
def _add_path(dwg, points):
|
||||
if not points:
|
||||
return
|
||||
|
||||
p = Path()
|
||||
p.push(f"M {points[0][0]} {points[0][1]}")
|
||||
for x, y in points[1:]:
|
||||
p.push(f"L {x} {y}")
|
||||
dwg.add(p)
|
||||
|
||||
|
||||
def _calculate_bearings(glyph: Glyph):
|
||||
if glyph.coordinates:
|
||||
leftmost_point = _map_to_svg(min(coordinate[0] for coordinate in glyph.coordinates if coordinate))
|
||||
@@ -63,7 +67,4 @@ def _calculate_bearings(glyph: Glyph):
|
||||
|
||||
|
||||
def _map_to_svg(x):
|
||||
# Scale proportionally (* svg_size / (base - cap))
|
||||
# Guard to avoid going out of bounds after stroking the character
|
||||
# Offset, since the SVG starts at zero (+ svg_size / 2)
|
||||
return x * (svg_size - 2 * guard) / (base - cap) + svg_size / 2
|
||||
|
||||
Reference in New Issue
Block a user