Skip to content

Commit

Permalink
Style condition will now work on VFs
Browse files Browse the repository at this point in the history
  • Loading branch information
m4rc1e committed Aug 24, 2020
1 parent 9bbf2da commit 0bc258b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
32 changes: 25 additions & 7 deletions Lib/fontbakery/profiles/googlefonts_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,38 @@
import re

from fontbakery.callable import condition
from fontbakery.constants import NameID
from fontbakery.constants import (NameID,
PlatformID,
WindowsEncodingID,
WindowsLanguageID)
from fontbakery.profiles.shared_conditions import is_variable_font

# -------------------------------------------------------------------
# FIXME! Redundant with @condition canonical_stylename(font)?
@condition
def style(font):
"""Determine font style from canonical filename."""
def style(ttFont):
"""Determine font style from canonical filename for static fonts.
For variable fonts, use either the Typographic Subfamilyname or
the Subfamily name."""
from fontbakery.constants import STATIC_STYLE_NAMES
filename = os.path.basename(font)
filename = os.path.basename(ttFont.reader.file.name)
if '-' in filename:
stylename = os.path.splitext(filename)[0].split('-')[1]
if stylename in [name.replace(' ', '') for name in STATIC_STYLE_NAMES]:
return stylename
elif is_variable_font(ttFont):
typo_stylename = ttFont['name'].getName(NameID.TYPOGRAPHIC_SUBFAMILY_NAME,
PlatformID.WINDOWS,
WindowsEncodingID.UNICODE_BMP,
WindowsLanguageID.ENGLISH_USA)
stylename = ttFont['name'].getName(NameID.FONT_SUBFAMILY_NAME,
PlatformID.WINDOWS,
WindowsEncodingID.UNICODE_BMP,
WindowsLanguageID.ENGLISH_USA)
# if font has typographic subfamily is should take presedence over the
# subfamily name.
stylename = typo_stylename or stylename
stylename = stylename.toUnicode()
if stylename in [name.replace(' ', '') for name in STATIC_STYLE_NAMES]:
return stylename
return None


Expand Down Expand Up @@ -102,7 +121,6 @@ def canonical_stylename(font):
from fontbakery.utils import suffix
from fontbakery.constants import (STATIC_STYLE_NAMES,
VARFONT_SUFFIXES)
from .shared_conditions import is_variable_font
from fontTools.ttLib import TTFont

# remove spaces in style names
Expand Down
26 changes: 13 additions & 13 deletions tests/profiles/googlefonts_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,7 @@ def test_check_metadata_nameid_font_name():
family_directory = os.path.dirname(font)
family_meta = family_metadata(family_directory)
font_meta = font_metadata(family_meta, font)
font_style = style(font)
font_style = style(ttFont)
assert_PASS(check(ttFont, font_style, font_meta),
'with a good font...')

Expand Down Expand Up @@ -1590,7 +1590,7 @@ def test_check_metadata_valid_name_values():
# Our reference Montserrat family is a good 18-styles family:
for fontfile in MONTSERRAT_RIBBI:
ttFont = TTFont(fontfile)
font_style = style(fontfile)
font_style = style(ttFont)
family_directory = os.path.dirname(fontfile)
family_meta = family_metadata(family_directory)
font_meta = font_metadata(family_meta, fontfile)
Expand All @@ -1610,7 +1610,7 @@ def test_check_metadata_valid_name_values():
#we do the same for NON-RIBBI styles:
for fontfile in MONTSERRAT_NON_RIBBI:
ttFont = TTFont(fontfile)
font_style = style(fontfile)
font_style = style(ttFont)
family_directory = os.path.dirname(fontfile)
family_meta = family_metadata(family_directory)
font_meta = font_metadata(family_meta, fontfile)
Expand Down Expand Up @@ -1644,7 +1644,7 @@ def test_check_metadata_valid_full_name_values():
# Our reference Montserrat family is a good 18-styles family:
for fontfile in MONTSERRAT_RIBBI:
ttFont = TTFont(fontfile)
font_style = style(fontfile)
font_style = style(ttFont)
family_directory = os.path.dirname(fontfile)
family_meta = family_metadata(family_directory)
font_meta = font_metadata(family_meta, fontfile)
Expand All @@ -1664,7 +1664,7 @@ def test_check_metadata_valid_full_name_values():
#we do the same for NON-RIBBI styles:
for fontfile in MONTSERRAT_NON_RIBBI:
ttFont = TTFont(fontfile)
font_style = style(fontfile)
font_style = style(ttFont)
family_directory = os.path.dirname(fontfile)
family_meta = family_metadata(family_directory)
font_meta = font_metadata(family_meta, fontfile)
Expand Down Expand Up @@ -2363,7 +2363,7 @@ def DISABLED_test_check_production_encoded_glyphs(cabin_ttFonts):
if remote:
for font in cabin_fonts:
ttFont = TTFont(font)
gfont = api_gfonts_ttFont(style(font), remote)
gfont = api_gfonts_ttFont(style(ttFont), remote)

# Cabin font hosted on fonts.google.com contains
# all the glyphs for the font in data/test/cabin
Expand Down Expand Up @@ -2542,11 +2542,11 @@ def test_check_name_familyname():
if name.nameID == NameID.FONT_FAMILY_NAME:
ttFont['name'].names[i].string = value.encode(name.getEncoding())
assert_results_contain(check(ttFont,
style(filename),
style(ttFont),
familyname_with_spaces(familyname(filename))),
expected, keyword,
f'with filename="{filename}",'
f' value="{value}", style="{style(filename)}"...')
f' value="{value}", style="{style(ttFont)}"...')


def test_check_name_subfamilyname():
Expand Down Expand Up @@ -2690,14 +2690,14 @@ def test_check_name_typographicfamilyname():
font = TEST_FILE("montserrat/Montserrat-BoldItalic.ttf")
ttFont = TTFont(font)
assert_PASS(check(ttFont,
style(font),
style(ttFont),
familyname_with_spaces(familyname(font))),
f"with a RIBBI without nameid={NameID.TYPOGRAPHIC_FAMILY_NAME} entry...")

# so we add one and make sure is emits a FAIL:
ttFont['name'].names[5].nameID = NameID.TYPOGRAPHIC_FAMILY_NAME # 5 is arbitrary here
assert_results_contain(check(ttFont,
style(font),
style(ttFont),
familyname_with_spaces(familyname(font))),
FAIL, 'ribbi',
f'with a RIBBI that has got a nameid={NameID.TYPOGRAPHIC_FAMILY_NAME} entry...')
Expand All @@ -2706,7 +2706,7 @@ def test_check_name_typographicfamilyname():
font = TEST_FILE("montserrat/Montserrat-ExtraLight.ttf")
ttFont = TTFont(font)
assert_PASS(check(ttFont,
style(font),
style(ttFont),
familyname_with_spaces(familyname(font))),
f"with a non-RIBBI containing a nameid={NameID.TYPOGRAPHIC_FAMILY_NAME} entry...")

Expand All @@ -2716,7 +2716,7 @@ def test_check_name_typographicfamilyname():
ttFont['name'].names[i].string = "foo".encode(name.getEncoding())

assert_results_contain(check(ttFont,
style(font),
style(ttFont),
familyname_with_spaces(familyname(font))),
FAIL, 'non-ribbi-bad-value',
'with a non-RIBBI with bad nameid={NameID.TYPOGRAPHIC_FAMILY_NAME} entries...')
Expand All @@ -2728,7 +2728,7 @@ def test_check_name_typographicfamilyname():
ttFont['name'].names[i].nameID = 255 # blah! :-)

assert_results_contain(check(ttFont,
style(font),
style(ttFont),
familyname_with_spaces(familyname(font))),
FAIL, 'non-ribbi-lacks-entry',
f'with a non-RIBBI lacking a nameid={NameID.TYPOGRAPHIC_FAMILY_NAME} entry...')
Expand Down

0 comments on commit 0bc258b

Please sign in to comment.