michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: # You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: import os.path michael@0: import re michael@0: import sys michael@0: michael@0: f = open(sys.argv[1] if len(sys.argv) > 1 else 'StandardizedVariants.txt') michael@0: michael@0: line = f.readline() michael@0: m = re.compile('^# (StandardizedVariants(-\d+(\.\d+)*)?\.txt)').search(line) michael@0: fileversion = m.group(1) michael@0: vsdict = {} michael@0: r = re.compile('^([0-9A-F]{4,6}) (FE0[0-9A-F]); CJK COMPATIBILITY IDEOGRAPH-([0-9A-F]{4,6});') michael@0: while True: michael@0: line = f.readline() michael@0: if not line: michael@0: break michael@0: if not 'CJK COMPATIBILITY IDEOGRAPH-' in line: michael@0: continue michael@0: michael@0: m = r.search(line) michael@0: unified = int(m.group(1), 16) michael@0: vs = int(m.group(2), 16) michael@0: compat = int(m.group(3), 16) michael@0: michael@0: if not vs in vsdict: michael@0: vsdict[vs] = {} michael@0: vsdict[vs][unified] = compat michael@0: michael@0: f.close michael@0: michael@0: offsets = [] michael@0: length = 10 + 11 * len(vsdict) michael@0: for (k, mappings) in sorted(vsdict.items()): michael@0: offsets.append(length) michael@0: length += 4 + 5 * len(mappings) michael@0: michael@0: f = open(sys.argv[2] if len(sys.argv) > 2 else 'CJKCompatSVS.cpp', 'wb') michael@0: f.write("""// Generated by %s. Do not edit. michael@0: michael@0: #include michael@0: michael@0: #define U16(v) (((v) >> 8) & 0xFF), ((v) & 0xFF) michael@0: #define U24(v) (((v) >> 16) & 0xFF), (((v) >> 8) & 0xFF), ((v) & 0xFF) michael@0: #define U32(v) (((v) >> 24) & 0xFF), (((v) >> 16) & 0xFF), (((v) >> 8) & 0xFF), ((v) & 0xFF) michael@0: #define GLYPH(v) U16(v >= 0x2F800 ? (v) - (0x2F800 - 0xFB00) : (v)) michael@0: michael@0: // Fallback mappings for CJK Compatibility Ideographs Standardized Variants michael@0: // taken from %s. michael@0: // Using OpenType format 14 cmap subtable structure to reuse the lookup code michael@0: // for fonts. The glyphID field is used to store the corresponding codepoints michael@0: // CJK Compatibility Ideographs. To fit codepoints into the 16-bit glyphID michael@0: // field, CJK Compatibility Ideographs Supplement (U+2F800..U+2FA1F) will be michael@0: // mapped to 0xFB00..0xFD1F. michael@0: extern const uint8_t sCJKCompatSVSTable[] = { michael@0: """ % (os.path.basename(sys.argv[0]), fileversion)) michael@0: f.write(' U16(14), // format\n') michael@0: f.write(' U32(%d), // length\n' % length) michael@0: f.write(' U32(%d), // numVarSelectorRecords\n' % len(vsdict)) michael@0: for i, k in enumerate(sorted(vsdict.keys())): michael@0: f.write(' U24(0x%04X), U32(0), U32(%d), // varSelectorRecord[%d]\n' % (k, offsets[i], i)) michael@0: for (k, mappings) in sorted(vsdict.items()): michael@0: f.write(' // 0x%04X\n' % k) michael@0: f.write(' U32(%d), // numUVSMappings\n' % len(mappings)) michael@0: for (unified, compat) in sorted(mappings.items()): michael@0: f.write(' U24(0x%04X), GLYPH(0x%04X),\n' % (unified, compat)) michael@0: f.write("""}; michael@0: michael@0: #undef U16 michael@0: #undef U24 michael@0: #undef U32 michael@0: #undef GLYPH michael@0: michael@0: static_assert(sizeof sCJKCompatSVSTable == %d, "Table generator has a bug."); michael@0: """ % length)