summaryrefslogtreecommitdiff
path: root/tools/unicode_dbl_width.py
blob: 2457834a134d3549d3c441f5661587ce4a27d698 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python3

"""
Generate src/bin/termptydbl.{c,h} from unicode files
used with ucd.all.flat.xml from
https://www.unicode.org/Public/UCD/latest/ucdxml/ucd.all.flat.zip
"""

import argparse
from collections import namedtuple
import xml.etree.ElementTree as ET

Range = namedtuple('range', ['width', 'start', 'end'])

def get_ranges(xmlfile, emoji_as_wide):
    tree = ET.parse(xmlfile)
    root = tree.getroot()
    repertoire = root.find("{http://www.unicode.org/ns/2003/ucd/1.0}repertoire")
    chars = repertoire.findall("{http://www.unicode.org/ns/2003/ucd/1.0}char")

    ranges = []
    range = Range('N', 0, 0)
    for c in chars:
        ea = c.get('ea')
        if ea in ('Na', 'H'):
            ea = 'N'
        if ea in ('F'):
            ea = 'W'
        assert ea in ('N', 'A', 'W')
        cp = c.get('cp')
        if not cp:
            continue
        if emoji_as_wide:
            emoji = c.get('ExtPict')
            if emoji == 'Y':
                ea = 'W'

        cp = int(cp, 16)
        if ea != range[0]:
            ranges.append(range)
            range = Range(ea, cp, cp)
        else:
            range = range._replace(end=cp)

    ranges.append(range)

    return ranges

def merge_ranges(ranges, is_same_width):
    res = []
    range = ranges[0]
    for r in ranges:
        if is_same_width(r, range):
            range = range._replace(end=r.end)
        else:
            res.append(range)
            range = r
    res.append(range)
    return res

def skip_ranges(ranges, width_skipped):
    res = []
    for r in ranges:
        if r.width not in width_skipped:
            res.append(r)
    return res

def gen_header(range, file_header):
    file_header.write(
"""/* XXX: Code generated by tool unicode_dbl_width.py */
#ifndef _TERMPTY_DBL_H__
#define _TERMPTY_DBL_H__ 1

Eina_Bool _termpty_is_wide(const Eina_Unicode g);
Eina_Bool _termpty_is_ambigous_wide(const Eina_Unicode g);

static inline Eina_Bool
_termpty_is_dblwidth_get(const Termpty *ty, const Eina_Unicode g)
{
   /* optimize for latin1 non-ambiguous */
""")
    file_header.write(f"   if (g <= 0x{range.end:X})")
    file_header.write(
"""
     return EINA_FALSE;
   if (!ty->termstate.cjk_ambiguous_wide)
     return _termpty_is_wide(g);
   else
     return _termpty_is_ambigous_wide(g);
}

#endif
""")

def gen_ambigous(ranges, file_source):
    file_source.write(
"""
__attribute__((const))
Eina_Bool
_termpty_is_ambigous_wide(Eina_Unicode g)
{
    switch (g)
      {
""")
    def is_same_width(r1, r2):
        if r1.width == 'N':
            return r2.width == 'N'
        else:
            return r2.width in ('A', 'W')
    ranges = merge_ranges(ranges[1:], is_same_width)
    ranges = skip_ranges(ranges, ('N',))

    fallthrough = " EINA_FALLTHROUGH;"
    for idx, r in enumerate(ranges):
        if r.width == 'N':
            continue;
        if idx == len(ranges) -1:
            fallthrough = ""
        if r.start == r.end:
            file_source.write(f"       case 0x{r.start:X}:{fallthrough}\n")
        else:
            file_source.write(f"       case 0x{r.start:X} ... 0x{r.end:X}:{fallthrough}\n")

    file_source.write(
"""
        return EINA_TRUE;
    }
   return EINA_FALSE;
}
""")

def gen_wide(ranges, file_source):
    file_source.write(
"""
__attribute__((const))
Eina_Bool
_termpty_is_wide(Eina_Unicode g)
{
    switch (g)
      {
""")
    def is_same_width(r1, r2):
        if r1.width in ('N', 'A'):
            return r2.width in ('N', 'A')
        else:
            return r2.width == 'W'
    ranges = merge_ranges(ranges[1:], is_same_width)
    ranges = skip_ranges(ranges, ('N', 'A'))
    fallthrough = " EINA_FALLTHROUGH;"
    for idx, r in enumerate(ranges):
        if r.width in ('N', 'A'):
            continue;
        if idx == len(ranges) -1:
            fallthrough = ""
        if r.start == r.end:
            file_source.write(f"       case 0x{r.start:X}:{fallthrough}\n")
        else:
            file_source.write(f"       case 0x{r.start:X} ... 0x{r.end:X}:{fallthrough}\n")

    file_source.write(
"""
        return EINA_TRUE;
    }
   return EINA_FALSE;
}
""")


def gen_c(ranges, file_header, file_source):
    gen_header(ranges[0], file_header)
    file_source.write(
"""/* XXX: Code generated by tool unicode_dbl_width.py */
#include "private.h"

#include <Elementary.h>
#include "termpty.h"
#include "termptydbl.h"
""")
    gen_ambigous(ranges, file_source)
    gen_wide(ranges, file_source)

parser = argparse.ArgumentParser(description='Generate code handling different widths of unicode codepoints.')
parser.add_argument('xml', type=argparse.FileType('r'))
parser.add_argument('header', type=argparse.FileType('w'))
parser.add_argument('source', type=argparse.FileType('w'))

args = parser.parse_args()

ranges = get_ranges(args.xml, True)
gen_c(ranges, args.header, args.source)