Skip to content

Commit

Permalink
Move InfoCompiler class to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
khaledhosny committed Feb 1, 2024
1 parent f8a40e6 commit c20b61d
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 153 deletions.
153 changes: 153 additions & 0 deletions Lib/ufo2ft/infoCompiler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
from ufo2ft.outlineCompiler import BaseOutlineCompiler


class InfoCompiler(BaseOutlineCompiler):
info_tables = frozenset(
[
"head",
"hhea",
"name",
"OS/2",
"post",
"vhea",
"gasp",
]
)

def __init__(self, ufo, otf):
self.orig_otf = otf
tables = self.info_tables & set(otf.keys())
super().__init__(ufo, tables=tables, glyphSet={}, glyphOrder=[])
if "gasp" in tables:
self.setupTable_gasp()

@staticmethod
def makeMissingRequiredGlyphs(*args, **kwargs):
return

def makeFontBoundingBox(self):
from ufo2ft.outlineCompiler import EMPTY_BOUNDING_BOX

return EMPTY_BOUNDING_BOX

def _set_attrs(self, tag, attrs):
temp = self.otf[tag]
orig = self.orig_otf[tag]
for attr in attrs:
if value := getattr(temp, attr, None):
setattr(orig, attr, value)

def setupTable_head(self):
super().setupTable_head()
self._set_attrs(
"head",
{
"fontRevision",
"unitsPerEm",
"created",
"macStyle",
"flags",
"lowestRecPPEM",
},
)

def setupTable_hhea(self):
super().setupTable_hhea()
self._set_attrs(
"hhea",
{
"ascent",
"descent",
"lineGap",
"caretSlopeRise",
"caretSlopeRun",
"caretOffset",
},
)

def setupTable_vhea(self):
super().setupTable_vhea()
self._set_attrs(
"vhea",
{
"ascent",
"descent",
"lineGap",
"caretSlopeRise",
"caretSlopeRun",
"caretOffset",
},
)

def setupTable_OS2(self):
super().setupTable_OS2()
self._set_attrs(
"OS/2",
{
"usWeightClass",
"usWidthClass",
"fsType",
"ySubscriptXSize",
"ySubscriptYSize",
"ySubscriptYOffset",
"ySubscriptXOffset",
"ySuperscriptXSize",
"ySuperscriptYSize",
"ySuperscriptYOffset",
"ySuperscriptXOffset",
"yStrikeoutSize",
"yStrikeoutPosition",
"sFamilyClass",
"panose",
"ulUnicodeRange1",
"ulUnicodeRange2",
"ulUnicodeRange3",
"ulUnicodeRange4",
"achVendID",
"fsSelection",
"sTypoAscender",
"sTypoDescender",
"sTypoLineGap",
"usWinAscent",
"usWinDescent",
"ulCodePageRange1",
"ulCodePageRange2",
"sxHeight",
"sCapHeight",
},
)

def setupTable_post(self):
super().setupTable_post()
self._set_attrs(
"post",
{
"italicAngle",
"underlinePosition",
"underlineThickness",
"isFixedPitch",
},
)

def setupTable_name(self):
super().setupTable_name()
temp = self.otf["name"]
orig = self.orig_otf["name"]
temp_names = {
(n.nameID, n.platformID, n.platEncID, n.langID): n for n in temp.names
}
orig_names = {
(n.nameID, n.platformID, n.platEncID, n.langID): n for n in orig.names
}
orig_names.update(temp_names)
orig.names = list(orig_names.values())

def setupTable_gasp(self):
from ufo2ft.instructionCompiler import InstructionCompiler

instructionCompiler = InstructionCompiler(self.ufo, self.otf)
instructionCompiler.setupTable_gasp()
self._set_attrs("gasp", {"gaspRange"})

def setupTable_maxp(self):
return
154 changes: 1 addition & 153 deletions Lib/ufo2ft/postProcessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
KEEP_GLYPH_NAMES,
USE_PRODUCTION_NAMES,
)
from ufo2ft.outlineCompiler import BaseOutlineCompiler

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -365,6 +364,7 @@ def apply_fontinfo(self):
"""Apply the fontinfo data from the DesignSpace variable-font's lib to
the compiled font."""
import copy
from ufo2ft.infoCompiler import InfoCompiler

logger.info("Applying variable-font info from DesignSpace lib")

Expand All @@ -381,158 +381,6 @@ def apply_fontinfo(self):
compiler.compile()


class InfoCompiler(BaseOutlineCompiler):
info_tables = frozenset(
[
"head",
"hhea",
"name",
"OS/2",
"post",
"vhea",
"gasp",
]
)

def __init__(self, ufo, otf):
self.orig_otf = otf
tables = self.info_tables & set(otf.keys())
super().__init__(ufo, tables=tables, glyphSet={}, glyphOrder=[])
if "gasp" in tables:
self.setupTable_gasp()

@staticmethod
def makeMissingRequiredGlyphs(*args, **kwargs):
return

def makeFontBoundingBox(self):
from ufo2ft.outlineCompiler import EMPTY_BOUNDING_BOX

return EMPTY_BOUNDING_BOX

def _set_attrs(self, tag, attrs):
temp = self.otf[tag]
orig = self.orig_otf[tag]
for attr in attrs:
if value := getattr(temp, attr, None):
setattr(orig, attr, value)

def setupTable_head(self):
super().setupTable_head()
self._set_attrs(
"head",
{
"fontRevision",
"unitsPerEm",
"created",
"macStyle",
"flags",
"lowestRecPPEM",
},
)

def setupTable_hhea(self):
super().setupTable_hhea()
self._set_attrs(
"hhea",
{
"ascent",
"descent",
"lineGap",
"caretSlopeRise",
"caretSlopeRun",
"caretOffset",
},
)

def setupTable_vhea(self):
super().setupTable_vhea()
self._set_attrs(
"vhea",
{
"ascent",
"descent",
"lineGap",
"caretSlopeRise",
"caretSlopeRun",
"caretOffset",
},
)

def setupTable_OS2(self):
super().setupTable_OS2()
self._set_attrs(
"OS/2",
{
"usWeightClass",
"usWidthClass",
"fsType",
"ySubscriptXSize",
"ySubscriptYSize",
"ySubscriptYOffset",
"ySubscriptXOffset",
"ySuperscriptXSize",
"ySuperscriptYSize",
"ySuperscriptYOffset",
"ySuperscriptXOffset",
"yStrikeoutSize",
"yStrikeoutPosition",
"sFamilyClass",
"panose",
"ulUnicodeRange1",
"ulUnicodeRange2",
"ulUnicodeRange3",
"ulUnicodeRange4",
"achVendID",
"fsSelection",
"sTypoAscender",
"sTypoDescender",
"sTypoLineGap",
"usWinAscent",
"usWinDescent",
"ulCodePageRange1",
"ulCodePageRange2",
"sxHeight",
"sCapHeight",
},
)

def setupTable_post(self):
super().setupTable_post()
self._set_attrs(
"post",
{
"italicAngle",
"underlinePosition",
"underlineThickness",
"isFixedPitch",
},
)

def setupTable_name(self):
super().setupTable_name()
temp = self.otf["name"]
orig = self.orig_otf["name"]
temp_names = {
(n.nameID, n.platformID, n.platEncID, n.langID): n for n in temp.names
}
orig_names = {
(n.nameID, n.platformID, n.platEncID, n.langID): n for n in orig.names
}
orig_names.update(temp_names)
orig.names = list(orig_names.values())

def setupTable_gasp(self):
from ufo2ft.instructionCompiler import InstructionCompiler

instructionCompiler = InstructionCompiler(self.ufo, self.otf)
instructionCompiler.setupTable_gasp()
self._set_attrs("gasp", {"gaspRange"})

def setupTable_maxp(self):
return


# Adapted from fontTools.cff.specializer.programToCommands
# https://github.com/fonttools/fonttools/blob/babca16
# /Lib/fontTools/cffLib/specializer.py#L40-L122
Expand Down

0 comments on commit c20b61d

Please sign in to comment.