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

Accept .glyphspackage in GSFont constructor #932

Merged
merged 1 commit into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions Lib/glyphsLib/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
from enum import IntEnum
from io import StringIO

import openstep_plist

# renamed to avoid shadowing glyphsLib.types.Transform imported further below
from fontTools.misc.transform import Identity, Transform as Affine
from fontTools.pens.basePen import AbstractPen
Expand All @@ -34,7 +32,7 @@
SegmentToPointPen,
)

from glyphsLib.parser import Parser
from glyphsLib.parser import load, Parser
from glyphsLib.pens import LayerPointPen
from glyphsLib.types import (
IndexPath,
Expand Down Expand Up @@ -4518,14 +4516,7 @@ def __init__(self, path=None):

if path:
path = os.fsdecode(os.fspath(path))
assert os.path.splitext(path)[-1] == ".glyphs", (
"Please supply a file path to a .glyphs file",
)

with open(path, "r", encoding="utf-8") as fp:
logger.info('Parsing "%s" file into <GSFont>', path)
p = Parser()
p.parse_into_object(self, openstep_plist.load(fp, use_numbers=True))
load(path, self)
self.filepath = path
for master in self.masters:
master.font = self
Expand Down
15 changes: 8 additions & 7 deletions Lib/glyphsLib/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,25 @@ def sort_key(glyph):
return data


def load(file_or_path):
def load(file_or_path, font=None):
"""Read a .glyphs file. 'file_or_path' should be a (readable) file
object, a file name, or in the case of a .glyphspackage file, a
directory name.
Return a GSFont object.
directory name. 'font' is an existing object to parse into, or None.
Return a 'font' or a GSFont object.
"""
p = Parser(current_type=glyphsLib.classes.GSFont)
logger.info("Parsing .glyphs file")
res = glyphsLib.classes.GSFont()
if font is None:
font = glyphsLib.classes.GSFont()
p = Parser(current_type=font.__class__)
if hasattr(file_or_path, "read"):
data = openstep_plist.load(file_or_path, use_numbers=True)
elif os.path.isdir(file_or_path):
data = load_glyphspackage(file_or_path)
else:
fp = open(file_or_path, "r", encoding="utf-8")
data = openstep_plist.load(fp, use_numbers=True)
p.parse_into_object(res, data)
return res
p.parse_into_object(font, data)
return font


def loads(s):
Expand Down
16 changes: 11 additions & 5 deletions tests/glyphs3_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ def test_glyphs3_italic_angle(datadir):


def test_glyphspackage_load(datadir):
font1 = glyphsLib.load(str(datadir.join("GlyphsUnitTestSans3.glyphs")))
font1.DisplayStrings = "" # glyphspackages, rather sensibly, don't store user state
font2 = glyphsLib.load(str(datadir.join("GlyphsUnitTestSans3.glyphspackage")))
names = [glyph.name for glyph in font2.glyphs]
assert names == [
expected = [
"A",
"Adieresis",
"a",
Expand All @@ -36,6 +32,16 @@ def test_glyphspackage_load(datadir):
"_part.shoulder",
"_part.stem", # Deliberately removed from glyph order file
]
font1 = glyphsLib.load(str(datadir.join("GlyphsUnitTestSans3.glyphs")))
font1.DisplayStrings = "" # glyphspackages, rather sensibly, don't store user state
font2 = glyphsLib.load(str(datadir.join("GlyphsUnitTestSans3.glyphspackage")))
assert [glyph.name for glyph in font2.glyphs] == expected
assert glyphsLib.dumps(font1) == glyphsLib.dumps(font2)

font1 = glyphsLib.load(str(datadir.join("GlyphsUnitTestSans3.glyphs")))
font1.DisplayStrings = "" # glyphspackages, rather sensibly, don't store user state
font2 = GSFont(str(datadir.join("GlyphsUnitTestSans3.glyphspackage")))
assert [glyph.name for glyph in font2.glyphs] == expected
assert glyphsLib.dumps(font1) == glyphsLib.dumps(font2)


Expand Down
Loading