Skip to content

Commit

Permalink
[COLRv1ToCOLRv2] Expand recursive PaintColrLayers
Browse files Browse the repository at this point in the history
Increases savings from 2.88% to 3.2% in COLR table of
NotoColorEmoji.
  • Loading branch information
behdad committed Aug 10, 2023
1 parent a851fa9 commit 56f2ff6
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion Lib/fontTools/colorLib/COLRv1ToCOLRv2.py
Expand Up @@ -144,7 +144,8 @@ def templateForObjectTuples(allTuples, arguments):
if ret is not None:
return ret

Check warning on line 145 in Lib/fontTools/colorLib/COLRv1ToCOLRv2.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/colorLib/COLRv1ToCOLRv2.py#L145

Added line #L145 was not covered by tests

if v0[0] == "Paint":
# If length is 2, this is a key,value pair, not an object.
if len(v0) > 2 and v0[0] == "Paint":
paint = (

Check warning on line 149 in Lib/fontTools/colorLib/COLRv1ToCOLRv2.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/colorLib/COLRv1ToCOLRv2.py#L149

Added line #L149 was not covered by tests
"Paint",
("Format", PaintFormat.PaintTemplateArgument),
Expand Down Expand Up @@ -296,6 +297,41 @@ def serializeObjectTuple(objTuple):
return obj

Check warning on line 297 in Lib/fontTools/colorLib/COLRv1ToCOLRv2.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/colorLib/COLRv1ToCOLRv2.py#L296-L297

Added lines #L296 - L297 were not covered by tests


def expandRecursivePaintColrLayers(paintTuple):
if not isinstance(paintTuple, tuple):
return paintTuple

Check warning on line 302 in Lib/fontTools/colorLib/COLRv1ToCOLRv2.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/colorLib/COLRv1ToCOLRv2.py#L302

Added line #L302 was not covered by tests

if paintTuple[0] == "list":
# Nothing to expand in lists at this point
return paintTuple

Check warning on line 306 in Lib/fontTools/colorLib/COLRv1ToCOLRv2.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/colorLib/COLRv1ToCOLRv2.py#L306

Added line #L306 was not covered by tests
# return ("list",) + tuple(expandRecursivePaintColrLayers(o) for o in paintTuple[1:])

if paintTuple[0] != "PaintColrLayers":
return (paintTuple[0],) + tuple(
(k, expandRecursivePaintColrLayers(v)) for k, v in paintTuple[1:]
)

paintTuples = tuple(
expandRecursivePaintColrLayers(layer) for layer in paintTuple[1:]
)

layers = []

Check warning on line 318 in Lib/fontTools/colorLib/COLRv1ToCOLRv2.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/colorLib/COLRv1ToCOLRv2.py#L318

Added line #L318 was not covered by tests
for layer in paintTuples:
if len(layers) > 255:
break

Check warning on line 321 in Lib/fontTools/colorLib/COLRv1ToCOLRv2.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/colorLib/COLRv1ToCOLRv2.py#L321

Added line #L321 was not covered by tests

if not isinstance(layer, tuple) or layer[0] != "PaintColrLayers":
layers.append(layer)
continue

Check warning on line 325 in Lib/fontTools/colorLib/COLRv1ToCOLRv2.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/colorLib/COLRv1ToCOLRv2.py#L324-L325

Added lines #L324 - L325 were not covered by tests

layers.extend(layer[1:])

Check warning on line 327 in Lib/fontTools/colorLib/COLRv1ToCOLRv2.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/colorLib/COLRv1ToCOLRv2.py#L327

Added line #L327 was not covered by tests

if len(layers) > 255:
return ("PaintColrLayers",) + paintTuples

Check warning on line 330 in Lib/fontTools/colorLib/COLRv1ToCOLRv2.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/colorLib/COLRv1ToCOLRv2.py#L330

Added line #L330 was not covered by tests

return ("PaintColrLayers",) + tuple(layers)

Check warning on line 332 in Lib/fontTools/colorLib/COLRv1ToCOLRv2.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/colorLib/COLRv1ToCOLRv2.py#L332

Added line #L332 was not covered by tests


def collectPaintColrLayers(paint, allPaintColrLayers):
if not isinstance(paint, Paint):
return

Check warning on line 337 in Lib/fontTools/colorLib/COLRv1ToCOLRv2.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/colorLib/COLRv1ToCOLRv2.py#L337

Added line #L337 was not covered by tests
Expand Down Expand Up @@ -449,6 +485,8 @@ def main(args=None):

log.info("Templatizing paints.")

Check warning on line 486 in Lib/fontTools/colorLib/COLRv1ToCOLRv2.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/colorLib/COLRv1ToCOLRv2.py#L486

Added line #L486 was not covered by tests

paintTuples = {k: expandRecursivePaintColrLayers(v) for k, v in paintTuples.items()}

genericTemplates = defaultdict(list)

Check warning on line 490 in Lib/fontTools/colorLib/COLRv1ToCOLRv2.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/colorLib/COLRv1ToCOLRv2.py#L490

Added line #L490 was not covered by tests
for glyphName, paintTuple in paintTuples.items():
paintTemplate = templateForObjectTuple(paintTuple)

Check warning on line 492 in Lib/fontTools/colorLib/COLRv1ToCOLRv2.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/colorLib/COLRv1ToCOLRv2.py#L492

Added line #L492 was not covered by tests
Expand Down Expand Up @@ -527,6 +565,7 @@ def main(args=None):
("Arguments", ("list",) + tuple(args[i] for args in arguments)),
)
paintTuples[glyphName] = paintTuple

Check warning on line 567 in Lib/fontTools/colorLib/COLRv1ToCOLRv2.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/colorLib/COLRv1ToCOLRv2.py#L567

Added line #L567 was not covered by tests

log.info("Skipped %d templates as they didn't save space", skipped)

Check warning on line 569 in Lib/fontTools/colorLib/COLRv1ToCOLRv2.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/colorLib/COLRv1ToCOLRv2.py#L569

Added line #L569 was not covered by tests

log.info("Building COLRv2 font")
Expand Down

0 comments on commit 56f2ff6

Please sign in to comment.