Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[designspaceLib] Provide fallback for elidedFallbackName #2903

Open
wants to merge 1 commit into
base: dslib-stat-instances-clarify
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 19 additions & 5 deletions Lib/fontTools/designspaceLib/statNames.py
Expand Up @@ -49,10 +49,13 @@ class StatNames:


def getStatNames(
doc: DesignSpaceDocument, userLocation: SimpleLocationDict
doc: DesignSpaceDocument,
userLocation: SimpleLocationDict,
defaultElidedFallbackName: Dict[str, str] | None = None,
) -> StatNames:
"""Compute the family, style, PostScript names of the given ``userLocation``
using the document's STAT information.
using the document's STAT information, falling back to the names in
``defaultElidedFallbackName`` if all names are elided.

Also computes localizations.

Expand All @@ -66,6 +69,9 @@ def getStatNames(

.. versionadded:: 5.0
"""
if defaultElidedFallbackName is None:
defaultElidedFallbackName = {"en": "Regular"}

familyNames: Dict[str, str] = {}
defaultSource: Optional[SourceDescriptor] = doc.findDefault()
if defaultSource is None:
Expand Down Expand Up @@ -102,8 +108,13 @@ def getStatNames(
for label in labels
if not label.elidable
)
if not styleName and doc.elidedFallbackName is not None:
styleName = doc.elidedFallbackName
if not styleName:
if doc.elidedFallbackName is not None:
styleName = doc.elidedFallbackName
else:
styleName = defaultElidedFallbackName.get(
language, defaultElidedFallbackName["en"]
)
styleNames[language] = styleName

if "en" not in familyNames or "en" not in styleNames:
Expand All @@ -129,7 +140,10 @@ def getStatNames(
for language in set(familyNames).union(styleNames.keys()):
familyName = familyNames.get(language, familyNames["en"])
styleName = styleNamesForStyleMap.get(language, styleNamesForStyleMap["en"])
styleMapFamilyNames[language] = (familyName + " " + styleName).strip()
if styleName.lower() in BOLD_ITALIC_TO_RIBBI_STYLE.values():
styleMapFamilyNames[language] = familyName.strip()
else:
styleMapFamilyNames[language] = f"{familyName} {styleName}".strip()

return StatNames(
familyNames=familyNames,
Expand Down
Expand Up @@ -57,7 +57,7 @@
<kerning/>
<info/>
</instance>
<instance name="Aktiv Grotesk " familyname="Aktiv Grotesk" stylename="" filename="../instances/AktivGrotesk_Rg.ufo" postscriptfontname="AktivGrotesk-" stylemapfamilyname="Aktiv Grotesk" stylemapstylename="regular">
<instance name="Aktiv Grotesk Regular" familyname="Aktiv Grotesk" stylename="Regular" filename="../instances/AktivGrotesk_Rg.ufo" postscriptfontname="AktivGrotesk-Regular" stylemapfamilyname="Aktiv Grotesk" stylemapstylename="regular">
<location>
<dimension name="Weight" xvalue="84"/>
</location>
Expand Down
Expand Up @@ -167,7 +167,7 @@
<kerning/>
<info/>
</instance>
<instance name="Aktiv Grotesk " familyname="Aktiv Grotesk" stylename="" filename="../instances/AktivGrotesk_Rg.ufo" postscriptfontname="AktivGrotesk-" stylemapfamilyname="Aktiv Grotesk" stylemapstylename="regular">
<instance name="Aktiv Grotesk Regular" familyname="Aktiv Grotesk" stylename="Regular" filename="../instances/AktivGrotesk_Rg.ufo" postscriptfontname="AktivGrotesk-Regular" stylemapfamilyname="Aktiv Grotesk" stylemapstylename="regular">
<location>
<dimension name="Weight" xvalue="84"/>
<dimension name="Width" xvalue="100"/>
Expand Down
Expand Up @@ -360,7 +360,7 @@
<kerning/>
<info/>
</instance>
<instance name="Aktiv Grotesk " familyname="Aktiv Grotesk" stylename="" filename="../instances/AktivGrotesk_Rg.ufo" postscriptfontname="AktivGrotesk-" stylemapfamilyname="Aktiv Grotesk" stylemapstylename="regular">
<instance name="Aktiv Grotesk Regular" familyname="Aktiv Grotesk" stylename="Regular" filename="../instances/AktivGrotesk_Rg.ufo" postscriptfontname="AktivGrotesk-Regular" stylemapfamilyname="Aktiv Grotesk" stylemapstylename="regular">
<location>
<dimension name="Weight" xvalue="84"/>
<dimension name="Width" xvalue="100"/>
Expand Down
Expand Up @@ -180,7 +180,7 @@
<kerning/>
<info/>
</instance>
<instance name="Source Serif 4 Italic" familyname="Source Serif 4" stylename="Italic" filename="Source Serif 4-Italic.ttf" postscriptfontname="SourceSerif4Italic-Regular" stylemapfamilyname="Source Serif 4 Italic" stylemapstylename="regular">
<instance name="Source Serif 4 Italic" familyname="Source Serif 4" stylename="Italic" filename="Source Serif 4-Italic.ttf" postscriptfontname="SourceSerif4Italic-Regular" stylemapfamilyname="Source Serif 4" stylemapstylename="regular">
<location>
<dimension name="weight" xvalue="394"/>
<dimension name="optical" xvalue="20"/>
Expand All @@ -196,7 +196,7 @@
<kerning/>
<info/>
</instance>
<instance name="Source Serif 4 Bold Italic" familyname="Source Serif 4" stylename="Bold Italic" filename="Source Serif 4-Bold Italic.ttf" postscriptfontname="SourceSerif4Italic-Bold" stylemapfamilyname="Source Serif 4 Bold Italic" stylemapstylename="regular">
<instance name="Source Serif 4 Bold Italic" familyname="Source Serif 4" stylename="Bold Italic" filename="Source Serif 4-Bold Italic.ttf" postscriptfontname="SourceSerif4Italic-Bold" stylemapfamilyname="Source Serif 4" stylemapstylename="regular">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change sounds wrong, either keep the stylemapfamilyname, or change the stylemapstylename to bold italic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DS has no style linking though? How is getStatName supposed to do the right thing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No style linking = stylemapfamilyname is set to the familyname + stylename, and stylemapstylename is set to regular

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, applications that don't understand the 16/17 named ids are going to see a bunch of "Source Serif 4" fonts and won't be able to distinguish them, that's why we put the stylename into the stylemapfamilyname.

The other option, if the DS had style linking, would be to set the stylemapstylename correctly, in this case bold italic

<location>
<dimension name="weight" xvalue="823"/>
<dimension name="optical" xvalue="20"/>
Expand Down
Expand Up @@ -196,7 +196,7 @@
<kerning/>
<info/>
</instance>
<instance name="Source Serif 4 Bold" familyname="Source Serif 4" stylename="Bold" filename="Source Serif 4-Bold.ttf" postscriptfontname="SourceSerif4Roman-Bold" stylemapfamilyname="Source Serif 4 Bold" stylemapstylename="regular">
<instance name="Source Serif 4 Bold" familyname="Source Serif 4" stylename="Bold" filename="Source Serif 4-Bold.ttf" postscriptfontname="SourceSerif4Roman-Bold" stylemapfamilyname="Source Serif 4" stylemapstylename="regular">
<location>
<dimension name="weight" xvalue="823"/>
<dimension name="optical" xvalue="20"/>
Expand Down
Expand Up @@ -51,7 +51,7 @@
<dimension name="Weight" xvalue="57"/>
</location>
</instance>
<instance name="Aktiv Grotesk " familyname="Aktiv Grotesk" stylename="" filename="../instances/AktivGrotesk_Rg.ufo" postscriptfontname="AktivGrotesk-" stylemapfamilyname="Aktiv Grotesk" stylemapstylename="regular">
<instance name="Aktiv Grotesk Regular" familyname="Aktiv Grotesk" stylename="Regular" filename="../instances/AktivGrotesk_Rg.ufo" postscriptfontname="AktivGrotesk-Regular" stylemapfamilyname="Aktiv Grotesk" stylemapstylename="regular">
<location>
<dimension name="Weight" xvalue="84"/>
</location>
Expand Down
Expand Up @@ -147,7 +147,7 @@
<dimension name="Width" xvalue="75"/>
</location>
</instance>
<instance name="Aktiv Grotesk " familyname="Aktiv Grotesk" stylename="" filename="../instances/AktivGrotesk_Rg.ufo" postscriptfontname="AktivGrotesk-" stylemapfamilyname="Aktiv Grotesk" stylemapstylename="regular">
<instance name="Aktiv Grotesk Regular" familyname="Aktiv Grotesk" stylename="Regular" filename="../instances/AktivGrotesk_Rg.ufo" postscriptfontname="AktivGrotesk-Regular" stylemapfamilyname="Aktiv Grotesk" stylemapstylename="regular">
<location>
<dimension name="Weight" xvalue="84"/>
<dimension name="Width" xvalue="100"/>
Expand Down
Expand Up @@ -320,7 +320,7 @@
<dimension name="Italic" xvalue="1"/>
</location>
</instance>
<instance name="Aktiv Grotesk " familyname="Aktiv Grotesk" stylename="" filename="../instances/AktivGrotesk_Rg.ufo" postscriptfontname="AktivGrotesk-" stylemapfamilyname="Aktiv Grotesk" stylemapstylename="regular">
<instance name="Aktiv Grotesk Regular" familyname="Aktiv Grotesk" stylename="Regular" filename="../instances/AktivGrotesk_Rg.ufo" postscriptfontname="AktivGrotesk-Regular" stylemapfamilyname="Aktiv Grotesk" stylemapstylename="regular">
<location>
<dimension name="Weight" xvalue="84"/>
<dimension name="Width" xvalue="100"/>
Expand Down
Expand Up @@ -152,7 +152,7 @@
<dimension name="optical" xvalue="20"/>
</location>
</instance>
<instance name="Source Serif 4 Italic" familyname="Source Serif 4" stylename="Italic" filename="Source Serif 4-Italic.ttf" postscriptfontname="SourceSerif4Italic-Regular" stylemapfamilyname="Source Serif 4 Italic" stylemapstylename="regular">
<instance name="Source Serif 4 Italic" familyname="Source Serif 4" stylename="Italic" filename="Source Serif 4-Italic.ttf" postscriptfontname="SourceSerif4Italic-Regular" stylemapfamilyname="Source Serif 4" stylemapstylename="regular">
<location>
<dimension name="weight" xvalue="394"/>
<dimension name="optical" xvalue="20"/>
Expand All @@ -164,7 +164,7 @@
<dimension name="optical" xvalue="20"/>
</location>
</instance>
<instance name="Source Serif 4 Bold Italic" familyname="Source Serif 4" stylename="Bold Italic" filename="Source Serif 4-Bold Italic.ttf" postscriptfontname="SourceSerif4Italic-Bold" stylemapfamilyname="Source Serif 4 Bold Italic" stylemapstylename="regular">
<instance name="Source Serif 4 Bold Italic" familyname="Source Serif 4" stylename="Bold Italic" filename="Source Serif 4-Bold Italic.ttf" postscriptfontname="SourceSerif4Italic-Bold" stylemapfamilyname="Source Serif 4" stylemapstylename="regular">
<location>
<dimension name="weight" xvalue="823"/>
<dimension name="optical" xvalue="20"/>
Expand Down
Expand Up @@ -164,7 +164,7 @@
<dimension name="optical" xvalue="20"/>
</location>
</instance>
<instance name="Source Serif 4 Bold" familyname="Source Serif 4" stylename="Bold" filename="Source Serif 4-Bold.ttf" postscriptfontname="SourceSerif4Roman-Bold" stylemapfamilyname="Source Serif 4 Bold" stylemapstylename="regular">
<instance name="Source Serif 4 Bold" familyname="Source Serif 4" stylename="Bold" filename="Source Serif 4-Bold.ttf" postscriptfontname="SourceSerif4Roman-Bold" stylemapfamilyname="Source Serif 4" stylemapstylename="regular">
<location>
<dimension name="weight" xvalue="823"/>
<dimension name="optical" xvalue="20"/>
Expand Down
Expand Up @@ -353,7 +353,7 @@
<dimension name="Italic" xvalue="1"/>
</location>
</instance>
<instance name="Aktiv Grotesk " familyname="Aktiv Grotesk" stylename="" filename="../instances/AktivGrotesk_Rg.ufo" postscriptfontname="AktivGrotesk-" stylemapfamilyname="Aktiv Grotesk" stylemapstylename="regular">
<instance name="Aktiv Grotesk Regular" familyname="Aktiv Grotesk" stylename="Regular" filename="../instances/AktivGrotesk_Rg.ufo" postscriptfontname="AktivGrotesk-Regular" stylemapfamilyname="Aktiv Grotesk" stylemapstylename="regular">
<location>
<dimension name="Weight" xvalue="84"/>
<dimension name="Width" xvalue="100"/>
Expand Down
Expand Up @@ -194,7 +194,7 @@
<dimension name="optical" xvalue="20"/>
</location>
</instance>
<instance name="Source Serif 4 Bold" familyname="Source Serif 4" stylename="Bold" filename="Source Serif 4-Bold.ttf" postscriptfontname="SourceSerif4Roman-Bold" stylemapfamilyname="Source Serif 4 Bold" stylemapstylename="regular">
<instance name="Source Serif 4 Bold" familyname="Source Serif 4" stylename="Bold" filename="Source Serif 4-Bold.ttf" postscriptfontname="SourceSerif4Roman-Bold" stylemapfamilyname="Source Serif 4" stylemapstylename="regular">
<location>
<dimension name="weight" xvalue="823"/>
<dimension name="optical" xvalue="20"/>
Expand Down
Expand Up @@ -174,7 +174,7 @@
<dimension name="optical" xvalue="20"/>
</location>
</instance>
<instance name="Source Serif 4 Italic" familyname="Source Serif 4" stylename="Italic" filename="Source Serif 4-Italic.ttf" postscriptfontname="SourceSerif4Italic-Regular" stylemapfamilyname="Source Serif 4 Italic" stylemapstylename="regular">
<instance name="Source Serif 4 Italic" familyname="Source Serif 4" stylename="Italic" filename="Source Serif 4-Italic.ttf" postscriptfontname="SourceSerif4Italic-Regular" stylemapfamilyname="Source Serif 4" stylemapstylename="regular">
<location>
<dimension name="weight" xvalue="394"/>
<dimension name="optical" xvalue="20"/>
Expand All @@ -186,7 +186,7 @@
<dimension name="optical" xvalue="20"/>
</location>
</instance>
<instance name="Source Serif 4 Bold Italic" familyname="Source Serif 4" stylename="Bold Italic" filename="Source Serif 4-Bold Italic.ttf" postscriptfontname="SourceSerif4Italic-Bold" stylemapfamilyname="Source Serif 4 Bold Italic" stylemapstylename="regular">
<instance name="Source Serif 4 Bold Italic" familyname="Source Serif 4" stylename="Bold Italic" filename="Source Serif 4-Bold Italic.ttf" postscriptfontname="SourceSerif4Italic-Bold" stylemapfamilyname="Source Serif 4" stylemapstylename="regular">
<location>
<dimension name="weight" xvalue="823"/>
<dimension name="optical" xvalue="20"/>
Expand Down
31 changes: 31 additions & 0 deletions Tests/designspaceLib/statNames_test.py
Expand Up @@ -16,6 +16,19 @@ def test_instance_getStatNames(datadir):
)


def test_instance_getStatNames_no_style_links(datadir):
"""Tests that without style linking in the DS, all styleMapStyleNames are
`regular`.

This is not necessarily a feature, just the code following orders.
"""
doc = DesignSpaceDocument.fromfile(datadir / "test_v5_sourceserif.designspace")

for instance in doc.instances:
location = instance.getFullUserLocation(doc)
assert getStatNames(doc, location).styleMapStyleName == "regular"


def test_not_all_ordering_specified_and_translations(datadir):
doc = DesignSpaceDocument.fromfile(datadir / "test_v5.designspace")

Expand Down Expand Up @@ -44,6 +57,16 @@ def test_not_all_ordering_specified_and_translations(datadir):
def test_detect_ribbi_aktiv(datadir):
doc = DesignSpaceDocument.fromfile(datadir / "test_v5_aktiv.designspace")

# The default location has all names elided, so getStatNames must fall back
# to the fallback.
assert getStatNames(doc, doc.newDefaultLocation()) == StatNames(
familyNames={"en": "Aktiv Grotesk"},
styleNames={"en": "Regular"},
postScriptFontName="AktivGrotesk-Regular",
styleMapFamilyNames={"en": "Aktiv Grotesk"},
styleMapStyleName="regular",
)

assert getStatNames(doc, {"Weight": 600, "Width": 125, "Italic": 1}) == StatNames(
familyNames={"en": "Aktiv Grotesk"},
styleNames={"en": "Ex SemiBold Italic"},
Expand All @@ -52,6 +75,14 @@ def test_detect_ribbi_aktiv(datadir):
styleMapStyleName="italic",
)

assert getStatNames(doc, {"Weight": 700, "Width": 100, "Italic": 0}) == StatNames(
familyNames={"en": "Aktiv Grotesk"},
styleNames={"en": "Bold"},
postScriptFontName="AktivGrotesk-Bold",
styleMapFamilyNames={"en": "Aktiv Grotesk"},
styleMapStyleName="bold",
)

assert getStatNames(doc, {"Weight": 700, "Width": 75, "Italic": 1}) == StatNames(
familyNames={"en": "Aktiv Grotesk"},
styleNames={"en": "Cd Bold Italic"},
Expand Down