/
gen.go
291 lines (260 loc) · 8.68 KB
/
gen.go
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Command gen.go generates decomposition mappings from Unicode data.
//
// Unlike the package /x/text/unicode/norm, this also includes the
// [compatibility mapping tags] extracted from the
// [XML decomposition properties].
//
// [compatibility mapping tags]: https://unicode.org/reports/tr44/#Formatting_Tags_Table
// [XML decomposition properties]: https://www.unicode.org/reports/tr42/#d1e2932
package main
import (
"archive/zip"
"bufio"
"compress/gzip"
"encoding/xml"
"fmt"
"io"
"os"
"sort"
"strconv"
"strings"
"github.com/tawesoft/golib/v2/internal/legacy"
"github.com/tawesoft/golib/v2/must"
"github.com/tawesoft/golib/v2/operator"
)
type Char struct {
codepoint rune
coderange [2]rune // First and Last codepoint
decompositionType string
decompositionMapping string
Offset int // set later, index into table
}
func (c Char) Codepoint() rune {
return c.codepoint
}
// Pack encodes a unicode character's codepoint (21 bits), decomposition
// type (dt) (5 bits), length of the decomposition mapping (dml) (5 bits),
// an index dmi that points into table decomposition table dms (16 bits),
// for a total of 47 bits, which fits in 6 bytes with 1 bit of padding.
func (c Char) Pack() uint64 {
cp := c.codepoint
dt := c.DecompositionType()
dmi := c.Offset
dml := len(c.DecompositionMappings())
must.True(dt <= 0x1F)
must.True(dml <= 0x1F)
must.True(dmi <= 0xFFFF)
must.True(cp <= 0x1FFFFF)
var x uint64
x |= (uint64(cp) & 0x1FFFFF)
x |= (uint64(dt) & 0x1F) << 21
x |= (uint64(dml) & 0x1F) << (21 + 5)
x |= (uint64(dmi) & 0xFFFF) << (21 + 5 + 5)
return x
}
func ParseCodepoint(x string) rune {
return rune(must.Result(strconv.ParseInt(x, 16, 32)))
}
func (c Char) IsRange() bool {
return (c.codepoint == 0) && ((c.coderange[0] != 0) || (c.coderange[1] != 0))
}
func CharFromAttrs(attr []xml.Attr, parent Char) Char {
c := parent
for _, a := range attr {
switch a.Name.Local {
case "cp":
c.codepoint = ParseCodepoint(a.Value)
case "first-cp":
c.coderange[0] = ParseCodepoint(a.Value)
case "last-cp":
c.coderange[1] = ParseCodepoint(a.Value)
case "dt":
c.decompositionType = a.Value
case "dm":
c.decompositionMapping = a.Value
}
}
return c
}
// DecompositionMappings returns the code points, excluding itself, of a
// decomposition mapping
func (c Char) DecompositionMappings() []rune {
var xs []rune
for _, d := range strings.Split(c.decompositionMapping, " ") {
if d == "#" { continue }
xs = append(xs, ParseCodepoint(d))
}
return xs
}
// DecompositionMappingsLiteral formats DecompositionMappings so that
// the output looks like "0x1, 0x2, 0x3"
func (c Char) DecompositionMappingsLiteral() string {
var sb strings.Builder
rs := c.DecompositionMappings()
for i, r := range rs {
sb.WriteString(fmt.Sprintf("0x%X", r))
if i + 1 < len(rs) {
sb.WriteRune(',')
}
}
return sb.String()
}
type DT int8
const (
DTNone DT = 0
DTCan DT = 1 // Canonical
DTCom DT = 2 // Otherwise unspecified compatibility character
DTEnc DT = 3 // Encircled form
DTFin DT = 4 // Final presentation form (Arabic)
DTFont DT = 5 // Font variant (for example, a blackletter form)
DTFra DT = 6 // Vulgar fraction form
DTInit DT = 7 // Initial presentation form (Arabic)
DTIso DT = 8 // Isolated presentation form (Arabic)
DTMed DT = 9 // Medial presentation form (Arabic)
DTNar DT = 10 // Narrow (or hankaku) compatibility character
DTNb DT = 11 // No-break version of a space or hyphen
DTSml DT = 12 // Small variant form (CNS compatibility)
DTSqr DT = 13 // CJK squared font variant
DTSub DT = 14 // Subscript form
DTSup DT = 15 // Superscript form
DTVert DT = 16 // Vertical layout presentation form
DTWide DT = 17 // Wide (or zenkaku) compatibility character
) // fits in a 5 bit number
func (c Char) DecompositionType() DT {
/*
XML stores this as
attribute dt { "can" | "com" | "enc" | "fin" | "font" | "fra"
| "init" | "iso" | "med" | "nar" | "nb" | "sml"
| "sqr" | "sub" | "sup" | "vert" | "wide" | "none"}?
*/
switch c.decompositionType {
case "none": return DTNone
case "can": return DTCan
case "com": return DTCom
case "enc": return DTEnc
case "fin": return DTFin
case "font": return DTFont
case "fra": return DTFra
case "init": return DTInit
case "iso": return DTIso
case "med": return DTMed
case "nar": return DTNar
case "nb": return DTNb
case "sml": return DTSml
case "sqr": return DTSqr
case "sup": return DTSup
case "sub": return DTSub
case "vert": return DTVert
case "wide": return DTWide
default:
fmt.Println(c.decompositionType)
must.Never()
}
return DTNone
}
func main() {
zr := must.Result(zip.OpenReader("../../DATA/ucd.nounihan.grouped.13.0.0.zip"))
opener := func(name string) func() (io.ReadCloser, error) {
return func() (io.ReadCloser, error) {
return zr.Open(name)
}
}
chars := make([]Char, 0)
must.Check(legacy.WithCloser(opener("ucd.nounihan.grouped.xml"), func(f io.ReadCloser) error {
d := xml.NewDecoder(bufio.NewReaderSize(f, 64 * 1024))
var group Char
var inRepertoire, inGroup bool
for {
tok, err := d.Token()
if err == io.EOF {
break
} else if err != nil {
panic(fmt.Errorf("xml decode error: %w", err))
}
switch ty := tok.(type) {
case xml.StartElement:
switch ty.Name.Local {
case "repertoire":
if inRepertoire { must.Never() }
inRepertoire = true
case "group":
if !inRepertoire { break }
if inGroup { must.Never() }
inGroup = true
group = CharFromAttrs(ty.Attr, operator.Zero[Char]())
case "char":
if !inRepertoire { break }
if !inGroup { must.Never() }
c := CharFromAttrs(ty.Attr, group)
if c.DecompositionType() == 0 { break }
if c.IsRange() { must.Never() }
chars = append(chars, c)
}
case xml.EndElement:
switch ty.Name.Local {
case "repertoire":
inRepertoire = false
case "group":
inGroup = false
case "char":
break
}
}
}
return nil
}))
must.True(sort.SliceIsSorted(chars, func(i int, j int) bool {
return chars[i].codepoint < chars[j].codepoint
}))
offset := 0
for i := 0; i < len(chars); i++ {
chars[i].Offset = offset
offset += len(chars[i].DecompositionMappings())
}
{
dest := must.Result(os.Create("../../../../text/dm/dti.bin"))
defer dest.Close()
dtiEncode(dest, chars)
}
{
dest := must.Result(os.Create("../../../../text/dm/dms.bin.gz"))
defer dest.Close()
zdest := gzip.NewWriter(dest)
defer zdest.Close()
dmsEncode(zdest, chars)
}
}
func dtiEncode(dest io.Writer, chars []Char) {
for _, c := range chars {
p := c.Pack() // 48 bit
b0 := (p) & 0xFF
b1 := (p >> 8) & 0xFF
b2 := (p >> 16) & 0xFF
b3 := (p >> 24) & 0xFF
b4 := (p >> 32) & 0xFF
b5 := (p >> 40) & 0xFF
must.Result(dest.Write([]byte{
uint8(b0),
uint8(b1),
uint8(b2),
uint8(b3),
uint8(b4),
uint8(b5),
}))
}
}
func dmsEncode(dest io.Writer, chars []Char) {
for _, c := range chars {
for _, m := range c.DecompositionMappings() {
x := uint32(m)
b0 := (x) & 0xFF
b1 := (x >> 8) & 0xFF
b2 := (x >> 16) & 0xFF
must.Result(dest.Write([]byte{
uint8(b0),
uint8(b1),
uint8(b2),
}))
}
}
}