Skip to content

Commit 3b595be

Browse files
davidgodzsakDávid GodzsákDávid Godzsák
authored
Adjust SVG viewBox to enclose the font's bounding box (#23)
* Adjust Viewbox to contain whole letter * Help typo fix * ViewBox fallback to default values * Use head table for Viewbox calc. * Remove code duplicate: only return viewbox params * Test -av option and viewbox param generation * Head table can be reused in viewbox_settings * Remove unnecessary test param * Fix width, height calculation add tests Co-authored-by: Dávid Godzsák <godzsakdavid�@gmail.com> Co-authored-by: Dávid Godzsák <dg@shape.dk>
1 parent 038bb25 commit 3b595be

File tree

15 files changed

+86
-12
lines changed

15 files changed

+86
-12
lines changed

lib/opentypesvg/fonts2svg.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,18 @@ def processFonts(font_paths_list, hex_colors_list, outputFolderPath, options):
126126
glyphNamesToSaveInNestedFolder = get_gnames_to_save_in_nested_folder(
127127
glyphNamesList)
128128

129-
# Gather the fonts' UPM. For simplicity, it's assumed that all fonts have
130-
# the same UPM value. If fetching the UPM value fails, default to 1000.
131-
try:
132-
upm = ttLib.TTFont(font_paths_list[0])['head'].unitsPerEm
133-
except KeyError:
134-
upm = 1000
135-
136129
nestedFolderPath = None
137130
filesSaved = 0
138131

132+
viewbox = viewbox_settings(
133+
font_paths_list[0],
134+
options.adjust_view_box_to_glyph
135+
)
136+
139137
# Generate the SVGs
140138
for gName in glyphNamesList:
141139
svgStr = (u"""<svg xmlns="http://www.w3.org/2000/svg" """
142-
u"""viewBox="0 -{} {} {}">\n""".format(upm, upm, upm))
140+
u"""viewBox="{}">\n""".format(viewbox))
143141

144142
for index, gSet in enumerate(glyphSetsList):
145143
# Skip glyphs that don't exist in the current font,
@@ -193,6 +191,28 @@ def processFonts(font_paths_list, hex_colors_list, outputFolderPath, options):
193191
final_message(filesSaved)
194192

195193

194+
def viewbox_settings(font_path, adjust_view_box_to_glyph):
195+
try:
196+
head = ttLib.TTFont(font_path)["head"]
197+
if adjust_view_box_to_glyph:
198+
# it looks like compared to viewbox in the head table
199+
# the yMin and yMax are inverted
200+
x = head.xMin
201+
y = -head.yMax
202+
width = head.xMax - head.xMin
203+
height = head.yMax - head.yMin
204+
return """{} {} {} {}""".format(x, y, width, height)
205+
else:
206+
# Gather the fonts' UPM. For simplicity,
207+
# it's assumed that all fonts have the same UPM value.
208+
# If fetching the UPM value fails, default to 1000.
209+
upm = head.unitsPerEm
210+
return """0 -{} {} {}""".format(upm, upm, upm)
211+
except KeyError:
212+
upm = 1000
213+
return """0 -{} {} {}""".format(upm, upm, upm)
214+
215+
196216
RE_HEXCOLOR = re.compile(r"^(?=[a-fA-F0-9]*$)(?:.{6}|.{8})$")
197217

198218

@@ -260,6 +280,12 @@ def get_options(args):
260280
dest='glyphsets_union',
261281
help="do union (instead of intersection) of the fonts' glyph sets."
262282
)
283+
parser.add_argument(
284+
'-av', '--adjust-viewbox',
285+
action='store_true',
286+
dest='adjust_view_box_to_glyph',
287+
help="vertically center the viewBox on the bounding box of all glyphs."
288+
)
263289
parser.add_argument(
264290
'input_paths',
265291
metavar='FONT',

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ def fonts_dir():
1313
@pytest.fixture
1414
def base_font_path(fonts_dir):
1515
yield os.path.join(fonts_dir, 'Zebrawood.otf')
16+
17+
18+
@pytest.fixture
19+
def fixtures_dir():
20+
yield os.path.join(os.path.dirname(__file__), 'fixtures')

tests/fixtures/1.ttf

412 Bytes
Binary file not shown.

tests/fixtures/12.ttf

488 Bytes
Binary file not shown.

tests/fixtures/1234.ttf

660 Bytes
Binary file not shown.

tests/fixtures/13.ttf

528 Bytes
Binary file not shown.

tests/fixtures/14.ttf

476 Bytes
Binary file not shown.

tests/fixtures/2.ttf

452 Bytes
Binary file not shown.

tests/fixtures/23.ttf

564 Bytes
Binary file not shown.

tests/fixtures/24.ttf

512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)