Skip to content

Commit

Permalink
Script to derive somewhat more correct COLRv1 font from a good CBDT one
Browse files Browse the repository at this point in the history
  • Loading branch information
rsheeter committed Dec 22, 2021
1 parent 8130ead commit f46188e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
14 changes: 9 additions & 5 deletions colrv1/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# COLRv1 Build

Assumptions:

* bitmap font already exists.
* a font that contains complete emojicompat metadata exists
We assume the bitmap version with equivalent coverage exists and
contains emojicompat metadata.

## Build Steps

Expand All @@ -14,4 +12,10 @@ Assumptions:
```

1. Post-process COLRv1 font for Android
* At time of writing only the noflags version is for Android
* At time of writing only the noflags version is for Android

```shell
# Assumed to be in a python3 environment with requirements.txt fulfilled
python postproc.py build/NotoColorEmoji-noflags.ttf \
PATH_TO/NotoColorEmojiCompat.ttf
```
70 changes: 70 additions & 0 deletions colrv1/postproc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""
Post-nanoemoji processing of the Noto COLRv1 Emoji file.
For now substantially based on copying from a correct bitmap build.
"""
from absl import app
from fontTools import ttLib


def _is_colrv1(font):
return (
"COLR" in font
and font["COLR"].version == 1
)


def _is_cbdt(font):
return "CBDT" in font


def _is_compat_font(font):
return (
"meta" in font
and "Emji" in font["meta"].data
)


def _copy_emojicompat_data(colr_font, cbdt_font):
colr_font["meta"] = cbdt_font["meta"]


def _set_name(name_table, nameID):
name_table.getName(value, nameID, 3, 1, 0x409)


def _set_name(name_table, nameID, value):
name_table.setName(value, nameID, 3, 1, 0x409)


def _copy_names(colr_font, cbdt_font):
colr_font["name"] = cbdt_font["name"]
name_table = colr_font["name"]
assert all((n.platformID, n.platEncID, n.langID) == (3, 1, 0x409)
for n in name_table.names), "Should only have names Android uses"

# Amendments
_set_name(name_table, 10, "Color emoji font using COLRv1.")
_set_name(name_table, 11, "https://github.com/googlefonts/noto-emoji")


def main(argv):
if len(argv) != 3:
raise ValueError("Must have two args, a COLRv1 font and a CBDT emojicompat font")

colr_font = ttLib.TTFont(argv[1])
if not _is_colrv1(colr_font):
raise ValueError("First arg must be a COLRv1 font")

cbdt_font = ttLib.TTFont(argv[2])
if not _is_cbdt(cbdt_font) or not _is_compat_font(cbdt_font):
raise ValueError("Second arg must be a CBDT emojicompat font")

_copy_emojicompat_data(colr_font, cbdt_font)
_copy_names(colr_font, cbdt_font)

colr_font.save('../fonts/Noto-COLRv1-noflags.ttf')


if __name__ == "__main__":
app.run(main)
Binary file added fonts/Noto-COLRv1-noflags.ttf
Binary file not shown.

0 comments on commit f46188e

Please sign in to comment.