diff --git a/src/gen-emoji-table.py b/src/gen-emoji-table.py index a72cc25b4..09d34e528 100755 --- a/src/gen-emoji-table.py +++ b/src/gen-emoji-table.py @@ -1,16 +1,23 @@ #!/usr/bin/env python3 -"""usage: ./gen-emoji-table.py emoji-data.txt emoji-test.txt +"""usage: ./gen-emoji-table.py [--rust] emoji-data.txt emoji-test.txt Input file: * https://www.unicode.org/Public/UCD/latest/ucd/emoji/emoji-data.txt * https://www.unicode.org/Public/emoji/latest/emoji-test.txt """ +import os import sys from collections import OrderedDict import packTab +if len(sys.argv) > 1 and sys.argv[1] == "--rust": + del sys.argv[1] + language = packTab.languages["rust"] +else: + language = packTab.languages["c"] + if len (sys.argv) != 3: sys.exit (__doc__) @@ -43,7 +50,8 @@ print ("/* == Start of generated table == */") print ("/*") print (" * The following tables are generated by running:") print (" *") -print (" * ./gen-emoji-table.py emoji-data.txt") +print (" * ./gen-emoji-table.py %semoji-data.txt" % + ("--rust " if language.name == "rust" else "")) print (" *") print (" * on file with this header:") print (" *") @@ -51,11 +59,20 @@ for l in header: print (" * %s" % (l.strip())) print (" */") print () -print ("#ifndef HB_UNICODE_EMOJI_TABLE_HH") -print ("#define HB_UNICODE_EMOJI_TABLE_HH") -print () -print ('#include "hb-unicode.hh"') -print () +if language.name == "c": + print ("#ifndef HB_UNICODE_EMOJI_TABLE_HH") + print ("#define HB_UNICODE_EMOJI_TABLE_HH") + print () + print ('#include "hb-unicode.hh"') + print () +elif language.name == "rust": + print ("#![allow(unused_parens)]") + print ("#![allow(clippy::unnecessary_cast, clippy::unreadable_literal, clippy::double_parens)]") + print () + print ("use crate::hb::unicode::Codepoint;") + print () +else: + assert False, "Unknown language: %s" % language.name for typ, s in ranges.items(): if typ != "Extended_Pictographic": continue @@ -67,12 +84,26 @@ for typ, s in ranges.items(): sol = packTab.pack_table(arr, 0, compression=9) code = packTab.Code('_hb_emoji') - sol.genCode(code, 'is_'+typ) - code.print_c() - print() + if language.name == "c": + sol.genCode(code, 'is_'+typ, language=language) + code.print_code(language=language) + print() + elif language.name == "rust": + sol.genCode(code, 'is_'+typ+'_u8', language=language, private=False) + code.print_code(language=language, private=False) + print() + print ("#[inline]") + print ("pub(crate) fn is_%s (u: Codepoint) -> bool" % typ) + print ("{") + print (" _hb_emoji_is_%s_u8 (u as usize) != 0" % typ) + print ("}") + print() + else: + assert False, "Unknown language: %s" % language.name print () -print ("#endif /* HB_UNICODE_EMOJI_TABLE_HH */") +if language.name == "c": + print ("#endif /* HB_UNICODE_EMOJI_TABLE_HH */") print () print ("/* == End of generated table == */") @@ -91,8 +122,13 @@ with open(sys.argv[2]) as f: continue sequences.append(line) -with open("../test/shape/data/in-house/tests/emoji-clusters.tests", "w") as f: - for sequence in sequences: - f.write("../fonts/AdobeBlank2.ttf;--no-glyph-names --no-positions --font-funcs=ot") - f.write(";" + ",".join(sequence)) - f.write(";[" + "|".join("1=0" for c in sequence) + "]\n") +test_path = os.path.join(os.path.dirname(__file__), "..", "test", "shape", "data", "in-house", "tests", "emoji-clusters.tests") +if os.path.isdir(os.path.dirname(test_path)): + try: + with open(test_path, "w") as f: + for sequence in sequences: + f.write("../fonts/AdobeBlank2.ttf;--no-glyph-names --no-positions --font-funcs=ot") + f.write(";" + ",".join(sequence)) + f.write(";[" + "|".join("1=0" for c in sequence) + "]\n") + except OSError: + pass