feaLib.parser: if no glyphNames, treat dash as names, not ranges

But print a warning about the possible ambiguity.

Fixes https://github.com/fonttools/fonttools/issues/1768
This commit is contained in:
Cosimo Lupo 2020-04-03 12:38:35 +01:00
parent be77f3eeab
commit a913431ecd
No known key found for this signature in database
GPG Key ID: 179A8F0895A02F4F
2 changed files with 22 additions and 3 deletions

View File

@ -299,7 +299,7 @@ class Parser(object):
if self.next_token_type_ is Lexer.NAME:
glyph = self.expect_glyph_()
location = self.cur_token_location_
if '-' in glyph and glyph not in self.glyphNames_:
if '-' in glyph and self.glyphNames_ and glyph not in self.glyphNames_:
start, limit = self.split_glyph_range_(glyph, location)
self.check_glyph_name_in_glyph_set(start, limit)
glyphs.add_range(
@ -314,6 +314,11 @@ class Parser(object):
start, limit,
self.make_glyph_range_(location, start, limit))
else:
if not self.glyphNames_:
log.warning(str(FeatureLibError(
f"Ambiguous glyph name that looks like a range: {glyph!r}",
location
)))
self.check_glyph_name_in_glyph_set(glyph)
glyphs.append(glyph)
elif self.next_token_type_ is Lexer.CID:
@ -350,8 +355,8 @@ class Parser(object):
glyphs.add_class(gc)
else:
raise FeatureLibError(
"Expected glyph name, glyph range, "
"or glyph class reference",
f"Expected glyph name, glyph range, "
f"or glyph class reference, found {self.next_token_!r}",
self.next_token_location_)
self.expect_symbol_("]")
return glyphs

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from fontTools.misc.loggingTools import CapturingLogHandler
from fontTools.feaLib.error import FeatureLibError
from fontTools.feaLib.parser import Parser, SymbolTable
from io import StringIO
@ -346,6 +347,19 @@ class ParserTest(unittest.TestCase):
[gc] = self.parse("@range = [A-foo.sc - C-foo.sc];", gn).statements
self.assertEqual(gc.glyphSet(), ("A-foo.sc", "B-foo.sc", "C-foo.sc"))
def test_glyphclass_ambiguous_dash_no_glyph_names(self):
# If Parser is initialized without a glyphNames parameter (or with empty one)
# it cannot distinguish between a glyph name containing an hyphen, or a
# range of glyph names; thus it will interpret them as literal glyph names
# while also outputting a logging warning to alert user about the ambiguity.
# https://github.com/fonttools/fonttools/issues/1768
glyphNames = ()
with CapturingLogHandler("fontTools.feaLib.parser", level="WARNING") as caplog:
[gc] = self.parse("@class = [A-foo.sc B-foo.sc];", glyphNames).statements
self.assertEqual(gc.glyphSet(), ("A-foo.sc", "B-foo.sc"))
self.assertEqual(len(caplog.records), 2)
caplog.assertRegex("Ambiguous glyph name that looks like a range:")
def test_glyphclass_glyph_name_should_win_over_range(self):
# The OpenType Feature File Specification v1.20 makes it clear
# that if a dashed name could be interpreted either as a glyph name