From 3a57df2ac61b58882c07d57c80f8bdb457863718 Mon Sep 17 00:00:00 2001 From: Doug Felt Date: Fri, 11 Mar 2016 19:02:15 -0800 Subject: [PATCH] Improve flag support. This adds some additional flags to the default set. In addition, it contains code that creates ligatures for some flag sequences to others, for a few cases where we want different regions to share the same flag. Finally, it adds default ligatures so that pairs of regional indicator characters for which there's no predefined glyph get a 'missing flag' glyph. This avoids cases where sequences of regional indicator sequences accidentally match at odd locations because of a previous mismatch. There is no actual 'missing flag' glyph yet. The code uses an existing emoji as a placeholder. --- Makefile | 8 ++-- third_party/color_emoji/add_glyphs.py | 57 ++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 64974a823..dc450eb53 100644 --- a/Makefile +++ b/Makefile @@ -44,7 +44,7 @@ QUANTIZED_DIR := $(BUILD_DIR)/quantized_pngs COMPRESSED_DIR := $(BUILD_DIR)/compressed_pngs LIMITED_FLAGS = CN DE ES FR GB IT JP KR RU US -SELECTED_FLAGS = AD AE AF AG AI AL AM AO AR AS AT AU AW AX AZ \ +SELECTED_FLAGS = AC AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ \ BA BB BD BE BF BG BH BI BJ BM BN BO BR BS BT BW BY BZ \ CA CC CD CF CG CH CI CK CL CM CN CO CR CU CV CW CX CY CZ \ DE DJ DK DM DO DZ \ @@ -52,7 +52,7 @@ SELECTED_FLAGS = AD AE AF AG AI AL AM AO AR AS AT AU AW AX AZ \ FI FJ FM FO FR \ GA GB GD GE GG GH GI GL GM GN GQ GR GT GU GW GY \ HK HN HR HT HU \ - ID IE IL IM IN IO IQ IR IS IT \ + IC ID IE IL IM IN IO IQ IR IS IT \ JE JM JO JP \ KE KG KH KI KM KN KP KR KW KY KZ \ LA LB LC LI LK LR LS LT LU LV LY \ @@ -62,8 +62,8 @@ SELECTED_FLAGS = AD AE AF AG AI AL AM AO AR AS AT AU AW AX AZ \ PA PE PF PG PH PK PL PN PR PS PT PW PY \ QA \ RO RS RU RW \ - SA SB SC SD SE SG SI SK SL SM SN SO SR SS ST SV SX SY SZ \ - TC TD TG TH TJ TK TL TM TN TO TR TT TV TW TZ \ + SA SB SC SD SE SG SH SI SK SL SM SN SO SR SS ST SV SX SY SZ \ + TA TC TD TG TH TJ TK TL TM TN TO TR TT TV TW TZ \ UA UG US UY UZ \ VA VC VE VG VI VN VU \ WS \ diff --git a/third_party/color_emoji/add_glyphs.py b/third_party/color_emoji/add_glyphs.py index 0d78a7d2b..2cba6a809 100644 --- a/third_party/color_emoji/add_glyphs.py +++ b/third_party/color_emoji/add_glyphs.py @@ -1,10 +1,13 @@ #!/usr/bin/env python -import collections, glob, os, sys +import collections, glob, os, re, sys from fontTools import ttx from fontTools.ttLib.tables import otTables from png import PNG +# TODO: replace with actual name once we have a glyph. +MISSING_FLAG_GLYPH_NAME = "u2764" + sys.path.append( os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) import add_emoji_gsub @@ -78,6 +81,15 @@ EXTRA_SEQUENCES = { 'u1F48F': '1F469_200D_2764_FE0F_200D_1F48B_200D_1F468', # WHKM } +# Flag aliases - from: to +FLAG_ALIASES = { + 'BV': 'NO', + 'SJ': 'NO', + 'UM': 'FR', + 'HM': 'AU', + 'UM': 'US', +} + if len (sys.argv) < 4: print >>sys.stderr, """ Usage: @@ -180,6 +192,49 @@ for n in EXTRA_SEQUENCES: else: print 'extras: no glyph for %s' % n +# Add missing regional indicator sequences and flag aliases +# if we support any. +regional_names = frozenset('u%X' % cp for cp in range(0x1F1E6, 0x1F200)) + +def _is_flag_sequence(t): + return len(t) == 2 and t[0] in regional_names and t[1] in regional_names + +have_flags = False +for k in ligatures: + if _is_flag_sequence(k): + have_flags = True + break + +# sigh, too many separate files with the same code. +# copied from add_emoji_gsub. +def _reg_indicator(letter): + assert 'A' <= letter <= 'Z' + return 0x1F1E6 + ord(letter) - ord('A') + +def _reg_lig_sequence(flag_name): + """Returns a tuple of strings naming the codepoints that form the ligature.""" + assert len(flag_name) == 2 + return tuple('u%X' % _reg_indicator(cp) for cp in flag_name) + +def _reg_lig_name(flag_name): + """Returns a glyph name for the flag name.""" + return '_'.join(_reg_lig_sequence(flag_name)) + +if have_flags: + print 'Adding flag aliases.' + for flag_from, flag_to in FLAG_ALIASES.iteritems(): + seq = _reg_lig_sequence(flag_from) + name = _reg_lig_name(flag_to) + add_lig_sequence(ligatures, seq, name) + + print 'Adding unused flag sequences' + # every flag sequence we don't have gets the missing flag glyph + for first in regional_names: + for second in regional_names: + seq = (first, second) + if seq not in ligatures: + add_lig_sequence(ligatures, seq, MISSING_FLAG_GLYPH_NAME) + keyed_ligatures = collections.defaultdict(list) for k, v in ligatures.iteritems():