1#!/usr/bin/python
2# coding=UTF-8
3#
4# Copyright 2016 Google Inc. All rights reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18"""Create a curated subset of Noto CJK for Android."""
19
20import os
21
22from fontTools import ttLib
23from nototools import font_data
24from nototools import ttc_utils
25
26# Characters supported in Noto CJK fonts that UTR #51 recommends default to
27# emoji-style.
28EMOJI_IN_CJK = {
29    0x26BD, # ⚽ SOCCER BALL
30    0x26BE, # ⚾ BASEBALL
31    0x1F18E, # �� NEGATIVE SQUARED AB
32    0x1F191, # �� SQUARED CL
33    0x1F192, # �� SQUARED COOL
34    0x1F193, # �� SQUARED FREE
35    0x1F194, # �� SQUARED ID
36    0x1F195, # �� SQUARED NEW
37    0x1F196, # �� SQUARED NG
38    0x1F197, # �� SQUARED OK
39    0x1F198, # �� SQUARED SOS
40    0x1F199, # �� SQUARED UP WITH EXCLAMATION MARK
41    0x1F19A, # �� SQUARED VS
42    0x1F201, # �� SQUARED KATAKANA KOKO
43    0x1F21A, # �� SQUARED CJK UNIFIED IDEOGRAPH-7121
44    0x1F22F, # �� SQUARED CJK UNIFIED IDEOGRAPH-6307
45    0x1F232, # �� SQUARED CJK UNIFIED IDEOGRAPH-7981
46    0x1F233, # �� SQUARED CJK UNIFIED IDEOGRAPH-7A7A
47    0x1F234, # �� SQUARED CJK UNIFIED IDEOGRAPH-5408
48    0x1F235, # �� SQUARED CJK UNIFIED IDEOGRAPH-6E80
49    0x1F236, # �� SQUARED CJK UNIFIED IDEOGRAPH-6709
50    0x1F238, # �� SQUARED CJK UNIFIED IDEOGRAPH-7533
51    0x1F239, # �� SQUARED CJK UNIFIED IDEOGRAPH-5272
52    0x1F23A, # �� SQUARED CJK UNIFIED IDEOGRAPH-55B6
53    0x1F250, # �� CIRCLED IDEOGRAPH ADVANTAGE
54    0x1F251, # �� CIRCLED IDEOGRAPH ACCEPT
55}
56
57# Characters we have decided we are doing as emoji-style in Android,
58# despite UTR #51's recommendation
59ANDROID_EMOJI = {
60    0x2600, # ☀ BLACK SUN WITH RAYS
61    0x2601, # ☁ CLOUD
62    0X260E, # ☎ BLACK TELEPHONE
63    0x261D, # ☝ WHITE UP POINTING INDEX
64    0x263A, # ☺ WHITE SMILING FACE
65    0x2660, # ♠ BLACK SPADE SUIT
66    0x2663, # ♣ BLACK CLUB SUIT
67    0x2665, # ♥ BLACK HEART SUIT
68    0x2666, # ♦ BLACK DIAMOND SUIT
69    0x270C, # ✌ VICTORY HAND
70    0x2744, # ❄ SNOWFLAKE
71    0x2764, # ❤ HEAVY BLACK HEART
72}
73
74
75def remove_from_cmap(infile, outfile, exclude=frozenset()):
76    """Removes a set of characters from a font file's cmap table."""
77    font = ttLib.TTFont(infile)
78    font_data.delete_from_cmap(font, exclude)
79    font.save(outfile)
80
81
82TTC_NAME = 'NotoSansCJK-Regular.ttc'
83OTF_NAMES = ['NotoSans%sCJK%s-Regular.otf' % (mono, variant)
84             for mono in ['', 'Mono']
85             for variant in ['jp', 'kr', 'sc', 'tc']]
86TEMP_DIR = 'no-emoji'
87
88
89if not os.path.exists(TEMP_DIR):
90    os.mkdir(TEMP_DIR)
91
92for index, otf_name in enumerate(OTF_NAMES):
93    print 'Subsetting %s...' % otf_name
94    font = ttLib.TTFont(TTC_NAME, fontNumber=index)
95    font.save(otf_name)
96    remove_from_cmap(
97        otf_name,
98        os.path.join(TEMP_DIR, otf_name),
99        exclude=sorted(EMOJI_IN_CJK | ANDROID_EMOJI))
100
101os.chdir(TEMP_DIR)
102ttc_utils.build_ttc(TTC_NAME, OTF_NAMES)
103