mirror of
https://github.com/googlefonts/noto-emoji.git
synced 2025-06-07 23:37:58 +00:00
Make it work both with Python2 and Python3
This commit is contained in:
parent
f09acc559b
commit
a9ca546689
4 changed files with 74 additions and 45 deletions
|
@ -66,7 +66,7 @@ def collect_seq_to_file(image_dirs, prefix, suffix):
|
||||||
|
|
||||||
|
|
||||||
def remap_values(seq_to_file, map_fn):
|
def remap_values(seq_to_file, map_fn):
|
||||||
return {k: map_fn(v) for k, v in seq_to_file.iteritems()}
|
return {k: map_fn(v) for k, v in seq_to_file.items()}
|
||||||
|
|
||||||
|
|
||||||
def get_png_file_to_advance_mapper(lineheight):
|
def get_png_file_to_advance_mapper(lineheight):
|
||||||
|
@ -228,7 +228,7 @@ def get_rtl_seq(seq):
|
||||||
|
|
||||||
rev_seq = list(seq)
|
rev_seq = list(seq)
|
||||||
rev_seq.reverse()
|
rev_seq.reverse()
|
||||||
for i in xrange(1, len(rev_seq)):
|
for i in range(1, len(rev_seq)):
|
||||||
if is_fitzpatrick(rev_seq[i-1]):
|
if is_fitzpatrick(rev_seq[i-1]):
|
||||||
tmp = rev_seq[i]
|
tmp = rev_seq[i]
|
||||||
rev_seq[i] = rev_seq[i-1]
|
rev_seq[i] = rev_seq[i-1]
|
||||||
|
@ -282,7 +282,7 @@ def add_ligature_sequences(font, seqs, aliases):
|
||||||
return
|
return
|
||||||
|
|
||||||
rtl_seq_to_target_name = {
|
rtl_seq_to_target_name = {
|
||||||
get_rtl_seq(seq): name for seq, name in seq_to_target_name.iteritems()}
|
get_rtl_seq(seq): name for seq, name in seq_to_target_name.items()}
|
||||||
seq_to_target_name.update(rtl_seq_to_target_name)
|
seq_to_target_name.update(rtl_seq_to_target_name)
|
||||||
# sequences that don't have rtl variants get mapped to the empty sequence,
|
# sequences that don't have rtl variants get mapped to the empty sequence,
|
||||||
# delete it.
|
# delete it.
|
||||||
|
@ -291,7 +291,7 @@ def add_ligature_sequences(font, seqs, aliases):
|
||||||
|
|
||||||
# organize by first codepoint in sequence
|
# organize by first codepoint in sequence
|
||||||
keyed_ligatures = collections.defaultdict(list)
|
keyed_ligatures = collections.defaultdict(list)
|
||||||
for t in seq_to_target_name.iteritems():
|
for t in seq_to_target_name.items():
|
||||||
first_cp = t[0][0]
|
first_cp = t[0][0]
|
||||||
keyed_ligatures[first_cp].append(t)
|
keyed_ligatures[first_cp].append(t)
|
||||||
|
|
||||||
|
@ -341,7 +341,7 @@ def apply_aliases(seq_dict, aliases):
|
||||||
source is a key in the dictionary, we can delete it. This updates the
|
source is a key in the dictionary, we can delete it. This updates the
|
||||||
dictionary and returns the usable aliases."""
|
dictionary and returns the usable aliases."""
|
||||||
usable_aliases = {}
|
usable_aliases = {}
|
||||||
for k, v in aliases.iteritems():
|
for k, v in aliases.items():
|
||||||
if v in seq_dict:
|
if v in seq_dict:
|
||||||
usable_aliases[k] = v
|
usable_aliases[k] = v
|
||||||
if k in seq_dict:
|
if k in seq_dict:
|
||||||
|
|
|
@ -53,8 +53,8 @@ def add_pua_cmap(source_file, target_file):
|
||||||
"""Add PUA characters to the cmap of the first font and save as second."""
|
"""Add PUA characters to the cmap of the first font and save as second."""
|
||||||
font = ttLib.TTFont(source_file)
|
font = ttLib.TTFont(source_file)
|
||||||
cmap = font_data.get_cmap(font)
|
cmap = font_data.get_cmap(font)
|
||||||
for pua, (ch1, ch2) in (add_emoji_gsub.EMOJI_KEYCAPS.items()
|
for pua, (ch1, ch2) in (list(add_emoji_gsub.EMOJI_KEYCAPS.items())
|
||||||
+ add_emoji_gsub.EMOJI_FLAGS.items()):
|
+ list(add_emoji_gsub.EMOJI_FLAGS.items())):
|
||||||
if pua not in cmap:
|
if pua not in cmap:
|
||||||
glyph_name = get_glyph_name_from_gsub([ch1, ch2], font)
|
glyph_name = get_glyph_name_from_gsub([ch1, ch2], font)
|
||||||
if glyph_name is not None:
|
if glyph_name is not None:
|
||||||
|
|
17
third_party/color_emoji/emoji_builder.py
vendored
17
third_party/color_emoji/emoji_builder.py
vendored
|
@ -19,7 +19,7 @@
|
||||||
|
|
||||||
|
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import sys, struct, StringIO
|
import sys, struct
|
||||||
from png import PNG
|
from png import PNG
|
||||||
import os
|
import os
|
||||||
from os import path
|
from os import path
|
||||||
|
@ -179,7 +179,10 @@ class CBDT:
|
||||||
self.write (pixel)
|
self.write (pixel)
|
||||||
offset += stride
|
offset += stride
|
||||||
|
|
||||||
png_allowed_chunks = ["IHDR", "PLTE", "tRNS", "sRGB", "IDAT", "IEND"]
|
png_allowed_chunks = [
|
||||||
|
"IHDR", "PLTE", "tRNS", "sRGB", "IDAT", "IEND", # Python2
|
||||||
|
b"IHDR", b"PLTE", b"tRNS", b"sRGB", b"IDAT", b"IEND", # Python3
|
||||||
|
]
|
||||||
|
|
||||||
def write_format17 (self, png):
|
def write_format17 (self, png):
|
||||||
self.write_format17or18(png, False)
|
self.write_format17or18(png, False)
|
||||||
|
@ -441,6 +444,9 @@ By default they are dropped.
|
||||||
|
|
||||||
def add_font_table (font, tag, data):
|
def add_font_table (font, tag, data):
|
||||||
tab = ttLib.tables.DefaultTable.DefaultTable (tag)
|
tab = ttLib.tables.DefaultTable.DefaultTable (tag)
|
||||||
|
if sys.version_info >= (3, 0, 0):
|
||||||
|
tab.data = data
|
||||||
|
else:
|
||||||
tab.data = str(data)
|
tab.data = str(data)
|
||||||
font[tag] = tab
|
font[tag] = tab
|
||||||
|
|
||||||
|
@ -492,12 +498,18 @@ By default they are dropped.
|
||||||
if "_" in codes:
|
if "_" in codes:
|
||||||
pieces = codes.split ("_")
|
pieces = codes.split ("_")
|
||||||
cps = [int(code, 16) for code in pieces]
|
cps = [int(code, 16) for code in pieces]
|
||||||
|
if sys.version_info >= (3, 0, 0):
|
||||||
|
uchars = "".join ([chr(cp) for cp in cps if not is_vs(cp)])
|
||||||
|
else:
|
||||||
uchars = "".join ([unichr(cp) for cp in cps if not is_vs(cp)])
|
uchars = "".join ([unichr(cp) for cp in cps if not is_vs(cp)])
|
||||||
else:
|
else:
|
||||||
cp = int(codes, 16)
|
cp = int(codes, 16)
|
||||||
if is_vs(cp):
|
if is_vs(cp):
|
||||||
print("ignoring unexpected vs input %04x" % cp)
|
print("ignoring unexpected vs input %04x" % cp)
|
||||||
continue
|
continue
|
||||||
|
if sys.version_info >= (3, 0, 0):
|
||||||
|
uchars = chr(cp)
|
||||||
|
else:
|
||||||
uchars = unichr(cp)
|
uchars = unichr(cp)
|
||||||
img_files[uchars] = img_file
|
img_files[uchars] = img_file
|
||||||
if not img_files:
|
if not img_files:
|
||||||
|
@ -562,7 +574,6 @@ By default they are dropped.
|
||||||
# remove it earlier, getGlyphID dies. Need to restructure all of this
|
# remove it earlier, getGlyphID dies. Need to restructure all of this
|
||||||
# code.
|
# code.
|
||||||
font_data.delete_from_cmap(font, [0xfe82b])
|
font_data.delete_from_cmap(font, [0xfe82b])
|
||||||
|
|
||||||
font.save (out_file)
|
font.save (out_file)
|
||||||
print("Output font '%s' generated." % out_file)
|
print("Output font '%s' generated." % out_file)
|
||||||
|
|
||||||
|
|
30
third_party/color_emoji/png.py
vendored
30
third_party/color_emoji/png.py
vendored
|
@ -17,7 +17,12 @@
|
||||||
# Google Author(s): Behdad Esfahbod
|
# Google Author(s): Behdad Esfahbod
|
||||||
#
|
#
|
||||||
|
|
||||||
import struct, StringIO
|
import struct
|
||||||
|
import sys
|
||||||
|
if sys.version_info >= (3,0,0): # Python3
|
||||||
|
from io import StringIO
|
||||||
|
else:
|
||||||
|
from StringIO import StringIO
|
||||||
|
|
||||||
|
|
||||||
class PNG:
|
class PNG:
|
||||||
|
@ -26,7 +31,7 @@ class PNG:
|
||||||
|
|
||||||
def __init__ (self, f):
|
def __init__ (self, f):
|
||||||
|
|
||||||
if isinstance(f, basestring):
|
if (isinstance(f, str) or isinstance(f, type(u''))):
|
||||||
f = open (f, 'rb')
|
f = open (f, 'rb')
|
||||||
|
|
||||||
self.f = f
|
self.f = f
|
||||||
|
@ -43,6 +48,9 @@ class PNG:
|
||||||
|
|
||||||
def data (self):
|
def data (self):
|
||||||
self.seek (0)
|
self.seek (0)
|
||||||
|
if sys.version_info >= (3,0,0): # Python3
|
||||||
|
return bytearray (self.f.read (), 'iso-8859-1')
|
||||||
|
else:
|
||||||
return bytearray (self.f.read ())
|
return bytearray (self.f.read ())
|
||||||
|
|
||||||
class BadSignature (Exception): pass
|
class BadSignature (Exception): pass
|
||||||
|
@ -55,7 +63,8 @@ class PNG:
|
||||||
return PNG.signature
|
return PNG.signature
|
||||||
|
|
||||||
def read_chunk (self):
|
def read_chunk (self):
|
||||||
length = struct.unpack (">I", self.f.read (4))[0]
|
buf = self.f.read (4)
|
||||||
|
length = struct.unpack (">I", buf)[0]
|
||||||
chunk_type = self.f.read (4)
|
chunk_type = self.f.read (4)
|
||||||
chunk_data = self.f.read (length)
|
chunk_data = self.f.read (length)
|
||||||
if len (chunk_data) != length:
|
if len (chunk_data) != length:
|
||||||
|
@ -67,7 +76,7 @@ class PNG:
|
||||||
|
|
||||||
def read_IHDR (self):
|
def read_IHDR (self):
|
||||||
(chunk_type, chunk_data, crc) = self.read_chunk ()
|
(chunk_type, chunk_data, crc) = self.read_chunk ()
|
||||||
if chunk_type != "IHDR":
|
if chunk_type not in ("IHDR", b"IHDR"):
|
||||||
raise PNG.BadChunk
|
raise PNG.BadChunk
|
||||||
# Width: 4 bytes
|
# Width: 4 bytes
|
||||||
# Height: 4 bytes
|
# Height: 4 bytes
|
||||||
|
@ -93,15 +102,24 @@ class PNG:
|
||||||
|
|
||||||
def filter_chunks (self, chunks):
|
def filter_chunks (self, chunks):
|
||||||
self.seek (0);
|
self.seek (0);
|
||||||
out = StringIO.StringIO ()
|
out = StringIO ()
|
||||||
|
if sys.version_info >= (3,0,0): # Python3
|
||||||
|
out.write (self.read_signature ().decode('iso-8859-1'))
|
||||||
|
else:
|
||||||
out.write (self.read_signature ())
|
out.write (self.read_signature ())
|
||||||
while True:
|
while True:
|
||||||
chunk_type, chunk_data, crc = self.read_chunk ()
|
chunk_type, chunk_data, crc = self.read_chunk ()
|
||||||
if chunk_type in chunks:
|
if chunk_type in chunks:
|
||||||
|
if sys.version_info >= (3,0,0): # Python3
|
||||||
|
out.write (struct.pack (">I", len (chunk_data)).decode('iso-8859-1'))
|
||||||
|
out.write (chunk_type.decode('iso-8859-1'))
|
||||||
|
out.write (chunk_data.decode('iso-8859-1'))
|
||||||
|
out.write (crc.decode('iso-8859-1'))
|
||||||
|
else:
|
||||||
out.write (struct.pack (">I", len (chunk_data)))
|
out.write (struct.pack (">I", len (chunk_data)))
|
||||||
out.write (chunk_type)
|
out.write (chunk_type)
|
||||||
out.write (chunk_data)
|
out.write (chunk_data)
|
||||||
out.write (crc)
|
out.write (crc)
|
||||||
if chunk_type == "IEND":
|
if chunk_type in ("IEND", b"IEND"):
|
||||||
break
|
break
|
||||||
return PNG (out)
|
return PNG (out)
|
||||||
|
|
Loading…
Add table
Reference in a new issue