gfx/thebes/gencjkcisvs.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/thebes/gencjkcisvs.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,77 @@
     1.4 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.5 +# License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 +# You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 +
     1.8 +import os.path
     1.9 +import re
    1.10 +import sys
    1.11 +
    1.12 +f = open(sys.argv[1] if len(sys.argv) > 1 else 'StandardizedVariants.txt')
    1.13 +
    1.14 +line = f.readline()
    1.15 +m = re.compile('^# (StandardizedVariants(-\d+(\.\d+)*)?\.txt)').search(line)
    1.16 +fileversion = m.group(1)
    1.17 +vsdict = {}
    1.18 +r = re.compile('^([0-9A-F]{4,6}) (FE0[0-9A-F]); CJK COMPATIBILITY IDEOGRAPH-([0-9A-F]{4,6});')
    1.19 +while True:
    1.20 +    line = f.readline()
    1.21 +    if not line:
    1.22 +        break
    1.23 +    if not 'CJK COMPATIBILITY IDEOGRAPH-' in line:
    1.24 +        continue
    1.25 +
    1.26 +    m = r.search(line)
    1.27 +    unified = int(m.group(1), 16)
    1.28 +    vs = int(m.group(2), 16)
    1.29 +    compat = int(m.group(3), 16)
    1.30 +
    1.31 +    if not vs in vsdict:
    1.32 +        vsdict[vs] = {}
    1.33 +    vsdict[vs][unified] = compat
    1.34 +
    1.35 +f.close
    1.36 +
    1.37 +offsets = []
    1.38 +length = 10 + 11 * len(vsdict)
    1.39 +for (k, mappings) in sorted(vsdict.items()):
    1.40 +    offsets.append(length)
    1.41 +    length += 4 + 5 * len(mappings)
    1.42 +
    1.43 +f = open(sys.argv[2] if len(sys.argv) > 2 else 'CJKCompatSVS.cpp', 'wb')
    1.44 +f.write("""// Generated by %s. Do not edit.
    1.45 +
    1.46 +#include <stdint.h>
    1.47 +
    1.48 +#define U16(v) (((v) >> 8) & 0xFF), ((v) & 0xFF)
    1.49 +#define U24(v) (((v) >> 16) & 0xFF), (((v) >> 8) & 0xFF), ((v) & 0xFF)
    1.50 +#define U32(v) (((v) >> 24) & 0xFF), (((v) >> 16) & 0xFF), (((v) >> 8) & 0xFF), ((v) & 0xFF)
    1.51 +#define GLYPH(v) U16(v >= 0x2F800 ? (v) - (0x2F800 - 0xFB00) : (v))
    1.52 +
    1.53 +// Fallback mappings for CJK Compatibility Ideographs Standardized Variants
    1.54 +// taken from %s.
    1.55 +// Using OpenType format 14 cmap subtable structure to reuse the lookup code
    1.56 +// for fonts. The glyphID field is used to store the corresponding codepoints
    1.57 +// CJK Compatibility Ideographs. To fit codepoints into the 16-bit glyphID
    1.58 +// field, CJK Compatibility Ideographs Supplement (U+2F800..U+2FA1F) will be
    1.59 +// mapped to 0xFB00..0xFD1F.
    1.60 +extern const uint8_t sCJKCompatSVSTable[] = {
    1.61 +""" % (os.path.basename(sys.argv[0]), fileversion))
    1.62 +f.write('  U16(14), // format\n')
    1.63 +f.write('  U32(%d), // length\n' % length)
    1.64 +f.write('  U32(%d), // numVarSelectorRecords\n' % len(vsdict))
    1.65 +for i, k in enumerate(sorted(vsdict.keys())):
    1.66 +    f.write('    U24(0x%04X), U32(0), U32(%d), // varSelectorRecord[%d]\n' % (k, offsets[i], i))
    1.67 +for (k, mappings) in sorted(vsdict.items()):
    1.68 +    f.write('  // 0x%04X\n' % k)
    1.69 +    f.write('  U32(%d), // numUVSMappings\n' % len(mappings))
    1.70 +    for (unified, compat) in sorted(mappings.items()):
    1.71 +        f.write('    U24(0x%04X), GLYPH(0x%04X),\n' % (unified, compat))
    1.72 +f.write("""};
    1.73 +
    1.74 +#undef U16
    1.75 +#undef U24
    1.76 +#undef U32
    1.77 +#undef GLYPH
    1.78 +
    1.79 +static_assert(sizeof sCJKCompatSVSTable == %d, "Table generator has a bug.");
    1.80 +""" % length)

mercurial