]> gitweb.factorcode.org Git - factor.git/blob - basis/unicode/collation/collation.factor
unicode.collation: Add Nushu block, it's like Tangut
[factor.git] / basis / unicode / collation / collation.factor
1 ! Copyright (C) 2008 Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators
4 combinators.short-circuit combinators.smart fry kernel locals make
5 math math.order math.parser namespaces sequences
6 simple-flat-file splitting strings unicode.data ;
7 IN: unicode.collation
8
9 <PRIVATE
10
11 SYMBOL: ducet
12
13 TUPLE: weight-levels primary secondary tertiary ignorable? ;
14 : <weight-levels> ( primary secondary tertiary -- weight-levels> )
15     weight-levels new
16         swap >>tertiary
17         swap >>secondary
18         swap >>primary ; inline
19
20 : parse-weight ( string -- weight )
21     "]" split but-last [
22         weight-levels new swap rest unclip CHAR: * = swapd >>ignorable?
23         swap "." split first3 [ hex> ] tri@
24         [ >>primary ] [ >>secondary ] [ >>tertiary ] tri*
25     ] map ;
26
27 : parse-keys ( string -- chars )
28     " " split [ hex> ] "" map-as ;
29
30 : parse-ducet ( file -- ducet )
31     load-data-file [ [ parse-keys ] [ parse-weight ] bi* ] H{ } assoc-map-as ;
32
33 "vocab:unicode/UCA/allkeys.txt" parse-ducet ducet set-global
34
35 ! Fix up table for long contractions
36 : help-one ( assoc key -- )
37     ! Need to be more general? Not for DUCET, apparently
38     2 head 2dup swap key? [ 2drop ] [
39         [ [ 1string of ] with { } map-as concat ]
40         [ swap set-at ] 2bi
41     ] if ;
42
43 : fixup-ducet ( -- )
44     {
45         {
46             { 0x0FB2 0x0F71 } ! CE(0FB2) CE(0F71)
47             {
48                 T{ weight-levels
49                     { primary 12719 }
50                     { secondary 32 }
51                     { tertiary 2 }
52                 }
53                 T{ weight-levels
54                     { primary 12741 }
55                     { secondary 32 }
56                     { tertiary 2 }
57                 }
58             }
59         }
60         {
61             { 0x0FB3 0x0F71 } ! CE(0FB3) CE(0F71)
62             {
63                 T{ weight-levels
64                     { primary 12720 }
65                     { secondary 32 }
66                     { tertiary 2 }
67                 }
68                 T{ weight-levels
69                     { primary 12741 }
70                     { secondary 32 }
71                     { tertiary 2 }
72                 }
73             }
74         }
75     } ducet get-global '[ swap >string _ set-at ] assoc-each ;
76
77 ! Add a few missing ducet values
78 fixup-ducet
79
80 : tangut-block? ( char -- ? )
81     ! Tangut Block, Tangut Components Block
82     { [ 0x17000 0x187FF between? ] [ 0x18800 0x18AFF between? ] } 1|| ; inline
83
84 : nushu-block? ( char -- ? )
85     0x1b170 0x1B2FB between? ; inline
86
87 ! https://wiki.computercraft.cc/Module:Unicode_data
88 ! Unicode TR10 - Computing Implicit Weights
89 : base ( char -- base )
90     {
91         { [ dup 0x03400 0x04DB5 between? ] [ drop 0xFB80 ] } ! Extension A
92         { [ dup 0x20000 0x2A6D6 between? ] [ drop 0xFB80 ] } ! Extension B
93         { [ dup 0x2A700 0x2B734 between? ] [ drop 0xFB80 ] } ! Extension C
94         { [ dup 0x2B740 0x2B81D between? ] [ drop 0xFB80 ] } ! Extension D
95         { [ dup 0x2B820 0x2CEA1 between? ] [ drop 0xFB80 ] } ! Extension E
96         { [ dup 0x2CEB0 0x2EBE0 between? ] [ drop 0xFB80 ] } ! Extension F
97         { [ dup 0x04E00 0x09FEF between? ] [ drop 0xFB40 ] } ! CJK
98         [ drop 0xFBC0 ] ! Other
99     } cond ;
100
101 : tangut-AAAA ( char -- weight-levels )
102     drop 0xfb00 0x0020 0x0002 <weight-levels> ; inline
103
104 : tangut-BBBB ( char -- weight-levels )
105     0x17000 - 0x8000 bitor 0 0 <weight-levels> ; inline
106
107 : nushu-AAAA ( char -- weight-levels )
108     drop 0xfb01 0x0020 0x0002 <weight-levels> ; inline
109
110 : nushu-BBBB ( char -- weight-levels )
111     0x1B170 - 0x8000 bitor 0 0 <weight-levels> ; inline
112
113 : AAAA ( char -- weight-levels )
114     [ base ] [ -15 shift ] bi + 0x0020 0x0002 <weight-levels> ; inline
115
116 : BBBB ( char -- weight-levels )
117     0x7FFF bitand 0x8000 bitor 0 0 <weight-levels> ; inline
118
119 : derive-weight ( 1string -- weight-levels-pair )
120     first
121     {
122         { [ dup tangut-block? ] [ [ tangut-AAAA ] [ tangut-BBBB ] bi 2array ] }
123         { [ dup nushu-block? ] [ [ nushu-AAAA ] [ nushu-BBBB ] bi 2array ] }
124         [ [ AAAA ] [ BBBB ] bi 2array ]
125     } cond ;
126
127 : building-last ( -- char )
128     building get [ 0 ] [ last last ] if-empty ;
129
130 : blocked? ( char -- ? )
131     combining-class dup { 0 f } member?
132     [ drop building-last non-starter? ]
133     [ building-last combining-class = ] if ;
134
135 : possible-bases ( -- slice-of-building )
136     building get dup [ first non-starter? not ] find-last
137     drop [ 0 ] unless* tail-slice ;
138
139 :: ?combine ( char slice i -- ? )
140     i slice nth char suffix :> str
141     str ducet get-global key? dup
142     [ str i slice set-nth ] when ;
143
144 : add ( char -- )
145     dup blocked? [ 1string , ] [
146         dup possible-bases dup length <iota>
147         [ ?combine ] 2with any?
148         [ drop ] [ 1string , ] if
149     ] if ;
150
151 : string>graphemes ( string -- graphemes )
152     [ [ add ] each ] { } make ;
153
154 : char>weight-levels ( 1string -- weight-levels )
155     ducet get-global ?at [ derive-weight ] unless ; inline
156
157 : graphemes>weights ( graphemes -- weights )
158     [
159         dup weight-levels?
160         [ 1array ] ! From tailoring
161         [ char>weight-levels ] if
162     ] { } map-as concat ;
163
164 : append-weights ( weight-levels quot -- seq )
165     [ [ ignorable?>> ] reject ] dip
166     map [ zero? ] reject ; inline
167
168 : variable-weight ( weight-levels -- obj )
169     dup ignorable?>> [ primary>> ] [ drop 0xFFFF ] if ;
170
171 : weights>bytes ( weights -- array )
172     [
173         {
174             [ [ primary>> ] append-weights { 0 } ]
175             [ [ secondary>> ] append-weights { 0 } ]
176             [ [ tertiary>> ] append-weights { 0 } ]
177             [ [ [ secondary>> ] [ tertiary>> ] bi [ zero? ] bi@ and not ] filter [ variable-weight ] map ]
178         } cleave
179     ] { } append-outputs-as ;
180
181 PRIVATE>
182
183 : completely-ignorable? ( weight -- ? )
184     {
185         [ primary>> zero? ]
186         [ secondary>> zero? ]
187         [ tertiary>> zero? ]
188     } 1&& ;
189
190 : filter-ignorable ( weights -- weights' )
191     f swap [
192         [ nip ] [ primary>> zero? and ] 2bi
193         [ swap ignorable?>> or ]
194         [ swap completely-ignorable? or not ] 2bi
195     ] filter nip ;