2019-07-17 08:35:02 +00:00
|
|
|
#!/usr/bin/env python3
|
2015-06-05 17:58:41 +00:00
|
|
|
#
|
|
|
|
# Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
"""Modify an emoji font to map legacy PUA characters to standard ligatures."""
|
|
|
|
|
|
|
|
__author__ = 'roozbeh@google.com (Roozbeh Pournader)'
|
|
|
|
|
|
|
|
import sys
|
2019-10-21 15:03:06 +00:00
|
|
|
import itertools
|
2015-06-05 17:58:41 +00:00
|
|
|
|
|
|
|
from fontTools import ttLib
|
|
|
|
|
|
|
|
from nototools import font_data
|
|
|
|
|
2015-06-19 16:34:08 +00:00
|
|
|
import add_emoji_gsub
|
|
|
|
|
2015-06-05 17:58:41 +00:00
|
|
|
|
|
|
|
def get_glyph_name_from_gsub(char_seq, font):
|
|
|
|
"""Find the glyph name for ligature of a given character sequence from GSUB.
|
|
|
|
"""
|
|
|
|
cmap = font_data.get_cmap(font)
|
|
|
|
# FIXME: So many assumptions are made here.
|
|
|
|
try:
|
|
|
|
first_glyph = cmap[char_seq[0]]
|
|
|
|
rest_of_glyphs = [cmap[ch] for ch in char_seq[1:]]
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
for lookup in font['GSUB'].table.LookupList.Lookup:
|
|
|
|
ligatures = lookup.SubTable[0].ligatures
|
|
|
|
try:
|
|
|
|
for ligature in ligatures[first_glyph]:
|
|
|
|
if ligature.Component == rest_of_glyphs:
|
|
|
|
return ligature.LigGlyph
|
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2021-12-24 01:44:08 +00:00
|
|
|
def add_pua_cmap_to_font(font):
|
2015-06-05 17:58:41 +00:00
|
|
|
cmap = font_data.get_cmap(font)
|
2019-10-21 15:03:06 +00:00
|
|
|
for pua, (ch1, ch2) in itertools.chain(
|
|
|
|
add_emoji_gsub.EMOJI_KEYCAPS.items(), add_emoji_gsub.EMOJI_FLAGS.items()
|
|
|
|
):
|
2015-06-05 17:58:41 +00:00
|
|
|
if pua not in cmap:
|
|
|
|
glyph_name = get_glyph_name_from_gsub([ch1, ch2], font)
|
|
|
|
if glyph_name is not None:
|
|
|
|
cmap[pua] = glyph_name
|
2021-12-24 01:44:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_pua_cmap(source_file, target_file):
|
|
|
|
"""Add PUA characters to the cmap of the first font and save as second."""
|
|
|
|
font = ttLib.TTFont(source_file)
|
|
|
|
add_pua_cmap_to_font(font)
|
2015-06-05 17:58:41 +00:00
|
|
|
font.save(target_file)
|
|
|
|
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
"""Save the first font given to the second font."""
|
|
|
|
add_pua_cmap(argv[1], argv[2])
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main(sys.argv)
|
|
|
|
|