Merge pull request #1438 from anthrotype/enable-varLib-labelNames

[varLib] use addMultilingualName method to support localized axis and instance names
This commit is contained in:
Cosimo Lupo 2019-01-13 17:07:31 +00:00 committed by GitHub
commit fe19178ff0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 50 additions and 21 deletions

View File

@ -277,7 +277,8 @@ AxisDescriptor object
dicts. MutatorMath + Varlib. dicts. MutatorMath + Varlib.
- ``labelNames``: dict. When defining a non-registered axis, it will be - ``labelNames``: dict. When defining a non-registered axis, it will be
necessary to define user-facing readable names for the axis. Keyed by necessary to define user-facing readable names for the axis. Keyed by
xml:lang code. Varlib. xml:lang code. Values are required to be ``unicode`` strings, even if
they only contain ASCII characters.
- ``minimum``: number. The minimum value for this axis in user space. - ``minimum``: number. The minimum value for this axis in user space.
MutatorMath + Varlib. MutatorMath + Varlib.
- ``maximum``: number. The maximum value for this axis in user space. - ``maximum``: number. The maximum value for this axis in user space.

View File

@ -70,7 +70,7 @@ readable names for this axis if this is not an axis that is registered
by OpenType. Think "The label next to the slider". The attribute is a by OpenType. Think "The label next to the slider". The attribute is a
dictionary. The key is the `xml language dictionary. The key is the `xml language
tag <https://www.w3.org/International/articles/language-tags/>`__, the tag <https://www.w3.org/International/articles/language-tags/>`__, the
value is a utf-8 string with the name. Whether or not this attribute is value is a ``unicode`` string with the name. Whether or not this attribute is
used depends on the font building tool, the operating system and the used depends on the font building tool, the operating system and the
authoring software. This, at least, is the place to record it. authoring software. This, at least, is the place to record it.

View File

@ -246,25 +246,25 @@ class InstanceDescriptor(SimpleDescriptor):
filename = posixpath_property("_filename") filename = posixpath_property("_filename")
def setStyleName(self, styleName, languageCode="en"): def setStyleName(self, styleName, languageCode="en"):
self.localisedStyleName[languageCode] = styleName self.localisedStyleName[languageCode] = tounicode(styleName)
def getStyleName(self, languageCode="en"): def getStyleName(self, languageCode="en"):
return self.localisedStyleName.get(languageCode) return self.localisedStyleName.get(languageCode)
def setFamilyName(self, familyName, languageCode="en"): def setFamilyName(self, familyName, languageCode="en"):
self.localisedFamilyName[languageCode] = familyName self.localisedFamilyName[languageCode] = tounicode(familyName)
def getFamilyName(self, languageCode="en"): def getFamilyName(self, languageCode="en"):
return self.localisedFamilyName.get(languageCode) return self.localisedFamilyName.get(languageCode)
def setStyleMapStyleName(self, styleMapStyleName, languageCode="en"): def setStyleMapStyleName(self, styleMapStyleName, languageCode="en"):
self.localisedStyleMapStyleName[languageCode] = styleMapStyleName self.localisedStyleMapStyleName[languageCode] = tounicode(styleMapStyleName)
def getStyleMapStyleName(self, languageCode="en"): def getStyleMapStyleName(self, languageCode="en"):
return self.localisedStyleMapStyleName.get(languageCode) return self.localisedStyleMapStyleName.get(languageCode)
def setStyleMapFamilyName(self, styleMapFamilyName, languageCode="en"): def setStyleMapFamilyName(self, styleMapFamilyName, languageCode="en"):
self.localisedStyleMapFamilyName[languageCode] = styleMapFamilyName self.localisedStyleMapFamilyName[languageCode] = tounicode(styleMapFamilyName)
def getStyleMapFamilyName(self, languageCode="en"): def getStyleMapFamilyName(self, languageCode="en"):
return self.localisedStyleMapFamilyName.get(languageCode) return self.localisedStyleMapFamilyName.get(languageCode)
@ -753,8 +753,7 @@ class BaseDocReader(LogMixin):
# '{http://www.w3.org/XML/1998/namespace}lang' # '{http://www.w3.org/XML/1998/namespace}lang'
for key, lang in labelNameElement.items(): for key, lang in labelNameElement.items():
if key == XML_LANG: if key == XML_LANG:
labelName = labelNameElement.text axisObject.labelNames[lang] = tounicode(labelNameElement.text)
axisObject.labelNames[lang] = labelName
self.documentObject.axes.append(axisObject) self.documentObject.axes.append(axisObject)
self.axisDefaults[axisObject.name] = axisObject.default self.axisDefaults[axisObject.name] = axisObject.default
self.documentObject.defaultLoc = self.axisDefaults self.documentObject.defaultLoc = self.axisDefaults

View File

@ -76,21 +76,23 @@ def _add_fvar(font, axes, instances):
axis.axisTag = Tag(a.tag) axis.axisTag = Tag(a.tag)
# TODO Skip axes that have no variation. # TODO Skip axes that have no variation.
axis.minValue, axis.defaultValue, axis.maxValue = a.minimum, a.default, a.maximum axis.minValue, axis.defaultValue, axis.maxValue = a.minimum, a.default, a.maximum
axis.axisNameID = nameTable.addName(tounicode(a.labelNames['en'])) axis.axisNameID = nameTable.addMultilingualName(a.labelNames, font)
# TODO:
# Replace previous line with the following when the following issues are resolved:
# https://github.com/fonttools/fonttools/issues/930
# https://github.com/fonttools/fonttools/issues/931
# axis.axisNameID = nameTable.addMultilingualName(a.labelname, font)
fvar.axes.append(axis) fvar.axes.append(axis)
for instance in instances: for instance in instances:
coordinates = instance.location coordinates = instance.location
name = tounicode(instance.styleName)
if "en" not in instance.localisedStyleName:
assert instance.styleName
localisedStyleName = dict(instance.localisedStyleName)
localisedStyleName["en"] = tounicode(instance.styleName)
else:
localisedStyleName = instance.localisedStyleName
psname = instance.postScriptFontName psname = instance.postScriptFontName
inst = NamedInstance() inst = NamedInstance()
inst.subfamilyNameID = nameTable.addName(name) inst.subfamilyNameID = nameTable.addMultilingualName(localisedStyleName)
if psname is not None: if psname is not None:
psname = tounicode(psname) psname = tounicode(psname)
inst.postscriptNameID = nameTable.addName(psname) inst.postscriptNameID = nameTable.addName(psname)
@ -662,10 +664,10 @@ def load_designspace(designspace):
instances = ds.instances instances = ds.instances
standard_axis_map = OrderedDict([ standard_axis_map = OrderedDict([
('weight', ('wght', {'en':'Weight'})), ('weight', ('wght', {'en': u'Weight'})),
('width', ('wdth', {'en':'Width'})), ('width', ('wdth', {'en': u'Width'})),
('slant', ('slnt', {'en':'Slant'})), ('slant', ('slnt', {'en': u'Slant'})),
('optical', ('opsz', {'en':'Optical Size'})), ('optical', ('opsz', {'en': u'Optical Size'})),
]) ])
# Setup axes # Setup axes
@ -684,7 +686,7 @@ def load_designspace(designspace):
else: else:
assert axis.tag is not None assert axis.tag is not None
if not axis.labelNames: if not axis.labelNames:
axis.labelNames["en"] = axis_name axis.labelNames["en"] = tounicode(axis_name)
axes[axis_name] = axis axes[axis_name] = axis
log.info("Axes:\n%s", pformat([axis.asdict() for axis in axes.values()])) log.info("Axes:\n%s", pformat([axis.asdict() for axis in axes.values()]))

View File

@ -345,6 +345,16 @@ def instantiateVariableFont(varfont, location, inplace=False):
for i in fvar.instances: for i in fvar.instances:
exclude.add(i.subfamilyNameID) exclude.add(i.subfamilyNameID)
exclude.add(i.postscriptNameID) exclude.add(i.postscriptNameID)
if 'ltag' in varfont:
# Drop the whole 'ltag' table if all its language tags are referenced by
# name records to be pruned.
# TODO: prune unused ltag tags and re-enumerate langIDs accordingly
excludedUnicodeLangIDs = [
n.langID for n in varfont['name'].names
if n.nameID in exclude and n.platformID == 0 and n.langID != 0xFFFF
]
if set(excludedUnicodeLangIDs) == set(range(len((varfont['ltag'].tags)))):
del varfont['ltag']
varfont['name'].names[:] = [ varfont['name'].names[:] = [
n for n in varfont['name'].names n for n in varfont['name'].names
if n.nameID not in exclude if n.nameID not in exclude

View File

@ -4,6 +4,8 @@
<axis default="368.0" maximum="1000.0" minimum="0.0" name="weight" tag="wght" /> <axis default="368.0" maximum="1000.0" minimum="0.0" name="weight" tag="wght" />
<axis default="0.0" maximum="100.0" minimum="0.0" name="contrast" tag="cntr"> <axis default="0.0" maximum="100.0" minimum="0.0" name="contrast" tag="cntr">
<labelname xml:lang="en">Contrast</labelname> <labelname xml:lang="en">Contrast</labelname>
<labelname xml:lang="de">Kontrast</labelname>
<labelname xml:lang="fa">کنتراست</labelname>
</axis> </axis>
</axes> </axes>
<sources> <sources>

View File

@ -440,6 +440,9 @@
</glyf> </glyf>
<name> <name>
<namerecord nameID="257" platformID="0" platEncID="4" langID="0x0">
کنتراست
</namerecord>
<namerecord nameID="256" platformID="1" platEncID="0" langID="0x0" unicode="True"> <namerecord nameID="256" platformID="1" platEncID="0" langID="0x0" unicode="True">
Weight Weight
</namerecord> </namerecord>
@ -494,6 +497,12 @@
<namerecord nameID="273" platformID="1" platEncID="0" langID="0x0" unicode="True"> <namerecord nameID="273" platformID="1" platEncID="0" langID="0x0" unicode="True">
TestFamily-BlackHighContrast TestFamily-BlackHighContrast
</namerecord> </namerecord>
<namerecord nameID="257" platformID="1" platEncID="0" langID="0x2" unicode="True">
Kontrast
</namerecord>
<namerecord nameID="257" platformID="3" platEncID="1" langID="0x407">
Kontrast
</namerecord>
<namerecord nameID="1" platformID="3" platEncID="1" langID="0x409"> <namerecord nameID="1" platformID="3" platEncID="1" langID="0x409">
Test Family Test Family
</namerecord> </namerecord>
@ -2235,4 +2244,10 @@
</glyphVariations> </glyphVariations>
</gvar> </gvar>
<ltag>
<version value="1"/>
<flags value="0"/>
<LanguageTag tag="fa"/>
</ltag>
</ttFont> </ttFont>