...
1
2
3 package width
4
5
6
7 import (
8 "flag"
9 "log"
10 "strconv"
11 "strings"
12
13 "golang.org/x/text/internal/gen"
14 "golang.org/x/text/internal/ucd"
15 )
16
17 var (
18 outputFile = flag.String("out", "tables.go", "output file")
19 )
20
21 var typeMap = map[string]elem{
22 "A": tagAmbiguous,
23 "N": tagNeutral,
24 "Na": tagNarrow,
25 "W": tagWide,
26 "F": tagFullwidth,
27 "H": tagHalfwidth,
28 }
29
30
31
32
33
34
35 func getWidthData(f func(r rune, tag elem, alt rune)) {
36
37
38 for _, b := range []struct{ lo, hi rune }{
39 {0x4E00, 0x9FFF},
40 {0x3400, 0x4DBF},
41 {0xF900, 0xFAFF},
42 {0x20000, 0x2FFFF},
43 {0x30000, 0x3FFFF},
44 } {
45 for r := b.lo; r <= b.hi; r++ {
46 f(r, tagWide, 0)
47 }
48 }
49
50 inverse := map[rune]rune{}
51 maps := map[string]bool{
52 "<wide>": true,
53 "<narrow>": true,
54 }
55
56
57
58
59 ucd.Parse(gen.OpenUCDFile("UnicodeData.txt"), func(p *ucd.Parser) {
60 r := p.Rune(0)
61 s := strings.SplitN(p.String(ucd.DecompMapping), " ", 2)
62 if !maps[s[0]] {
63 return
64 }
65 x, err := strconv.ParseUint(s[1], 16, 32)
66 if err != nil {
67 log.Fatalf("Error parsing rune %q", s[1])
68 }
69 if inverse[r] != 0 || inverse[rune(x)] != 0 {
70 log.Fatalf("Circular dependency in mapping between %U and %U", r, x)
71 }
72 inverse[r] = rune(x)
73 inverse[rune(x)] = r
74 })
75
76
77 ucd.Parse(gen.OpenUCDFile("EastAsianWidth.txt"), func(p *ucd.Parser) {
78 tag, ok := typeMap[p.String(1)]
79 if !ok {
80 log.Fatalf("Unknown width type %q", p.String(1))
81 }
82 r := p.Rune(0)
83 alt, ok := inverse[r]
84 if tag == tagFullwidth || tag == tagHalfwidth && r != wonSign {
85 tag |= tagNeedsFold
86 if !ok {
87 log.Fatalf("Narrow or wide rune %U has no decomposition", r)
88 }
89 }
90 f(r, tag, alt)
91 })
92 }
93
View as plain text