]> gitweb.factorcode.org Git - factor.git/blob - basis/unicode/collation/collation.factor
collation: Adding more character ranges
[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
76         ! FIXME: WRONG WEIGHTS
77         {
78             { 0x0FB2 0x0F71 0x0F72 } ! CE(0FB2) CE(0F71 0F72)
79             {
80                 T{ weight-levels
81                     { primary 12719 }
82                     { secondary 32 }
83                     { tertiary 2 }
84                 }
85                 T{ weight-levels
86                     { primary 12741 }
87                     { secondary 32 }
88                     { tertiary 2 }
89                 }
90             }
91         }
92         {
93             { 0x0FB2 0x0F73        } ! CE(0FB2) CE(0F71 0F72)
94             {
95                 T{ weight-levels
96                     { primary 12719 }
97                     { secondary 32 }
98                     { tertiary 2 }
99                 }
100                 T{ weight-levels
101                     { primary 12741 }
102                     { secondary 32 }
103                     { tertiary 2 }
104                 }
105             }
106         }
107         {
108             { 0x0FB2 0x0F71 0x0F74 } ! CE(0FB2) CE(0F71 0F74)
109             {
110                 T{ weight-levels
111                     { primary 12719 }
112                     { secondary 32 }
113                     { tertiary 2 }
114                 }
115                 T{ weight-levels
116                     { primary 12741 }
117                     { secondary 32 }
118                     { tertiary 2 }
119                 }
120             }
121         }
122         {
123             { 0x0FB2 0x0F75        } ! CE(0FB2) CE(0F71 0F74)
124             {
125                 T{ weight-levels
126                     { primary 12719 }
127                     { secondary 32 }
128                     { tertiary 2 }
129                 }
130                 T{ weight-levels
131                     { primary 12741 }
132                     { secondary 32 }
133                     { tertiary 2 }
134                 }
135             }
136         }
137         {
138             { 0x0FB3 0x0F71 0x0F72 } ! CE(0FB3) CE(0F71 0F72)
139             {
140                 T{ weight-levels
141                     { primary 12719 }
142                     { secondary 32 }
143                     { tertiary 2 }
144                 }
145                 T{ weight-levels
146                     { primary 12741 }
147                     { secondary 32 }
148                     { tertiary 2 }
149                 }
150             }
151         }
152         {
153             { 0x0FB3 0x0F73        } ! CE(0FB3) CE(0F71 0F72)
154             {
155                 T{ weight-levels
156                     { primary 12719 }
157                     { secondary 32 }
158                     { tertiary 2 }
159                 }
160                 T{ weight-levels
161                     { primary 12741 }
162                     { secondary 32 }
163                     { tertiary 2 }
164                 }
165             }
166         }
167         {
168             { 0x0FB3 0x0F71 0x0F74 } ! CE(0FB3) CE(0F71 0F74)
169             {
170                 T{ weight-levels
171                     { primary 12719 }
172                     { secondary 32 }
173                     { tertiary 2 }
174                 }
175                 T{ weight-levels
176                     { primary 12741 }
177                     { secondary 32 }
178                     { tertiary 2 }
179                 }
180             }
181         }
182         {
183             { 0x0FB3 0x0F75        } ! CE(0FB3) CE(0F71 0F74)
184             {
185                 T{ weight-levels
186                     { primary 12719 }
187                     { secondary 32 }
188                     { tertiary 2 }
189                 }
190                 T{ weight-levels
191                     { primary 12741 }
192                     { secondary 32 }
193                     { tertiary 2 }
194                 }
195             }
196         }
197     } ducet get-global '[ swap >string _ set-at ] assoc-each ;
198
199 ! Add a few missing ducet values
200 fixup-ducet
201
202 : tangut-block? ( char -- ? )
203     ! Tangut Block, Tangut Components Block
204     { [ 0x17000 0x187FF between? ] [ 0x18800 0x18AFF between? ] } 1|| ; inline
205
206 ! https://wiki.computercraft.cc/Module:Unicode_data
207 ! Unicode TR10 - Computing Implicit Weights
208 : base ( char -- base )
209     {
210         { [ dup 0x03400 0x04DB5 between? ] [ drop 0xFB80 ] } ! Extension A
211         { [ dup 0x20000 0x2A6D6 between? ] [ drop 0xFB80 ] } ! Extension B
212         { [ dup 0x2A700 0x2B734 between? ] [ drop 0xFB80 ] } ! Extension C
213         { [ dup 0x2B740 0x2B81D between? ] [ drop 0xFB80 ] } ! Extension D
214         { [ dup 0x2B820 0x2CEA1 between? ] [ drop 0xFB80 ] } ! Extension E
215         { [ dup 0x2CEB0 0x2EBE0 between? ] [ drop 0xFB80 ] } ! Extension F
216         { [ dup 0x04E00 0x09FD5 between? ] [ drop 0xFB40 ] } ! CJK
217         [ drop 0xFBC0 ] ! Other
218     } cond ;
219
220 : tangut-AAAA ( char -- weight-levels )
221     drop 0xfb00 0x0020 0x0002 <weight-levels> ; inline
222
223 : tangut-BBBB ( char -- weight-levels )
224     0x17000 - 0x8000 bitor 0 0 <weight-levels> ; inline
225
226 : AAAA ( char -- weight-levels )
227     [ base ] [ -15 shift ] bi + 0x0020 0x0002 <weight-levels> ; inline
228
229 : BBBB ( char -- weight-levels )
230     0x7FFF bitand 0x8000 bitor 0 0 <weight-levels> ; inline
231
232 : derive-weight ( 1string -- weight-levels-pair )
233     first
234     dup tangut-block? [
235         [ tangut-AAAA ] [ tangut-BBBB ] bi 2array
236     ] [
237         [ AAAA ] [ BBBB ] bi 2array
238     ] if ;
239
240 : building-last ( -- char )
241     building get [ 0 ] [ last last ] if-empty ;
242
243 : blocked? ( char -- ? )
244     combining-class dup { 0 f } member?
245     [ drop building-last non-starter? ]
246     [ building-last combining-class = ] if ;
247
248 : possible-bases ( -- slice-of-building )
249     building get dup [ first non-starter? not ] find-last
250     drop [ 0 ] unless* tail-slice ;
251
252 :: ?combine ( char slice i -- ? )
253     i slice nth char suffix :> str
254     str ducet get-global key? dup
255     [ str i slice set-nth ] when ;
256
257 : add ( char -- )
258     dup blocked? [ 1string , ] [
259         dup possible-bases dup length <iota>
260         [ ?combine ] 2with any?
261         [ drop ] [ 1string , ] if
262     ] if ;
263
264 : string>graphemes ( string -- graphemes )
265     [ [ add ] each ] { } make ;
266
267 : char>weight-levels ( 1string -- weight-levels )
268     ducet get-global ?at [ derive-weight ] unless ; inline
269
270 : graphemes>weights ( graphemes -- weights )
271     [
272         dup weight-levels?
273         [ 1array ] ! From tailoring
274         [ char>weight-levels ] if
275     ] { } map-as concat ;
276
277 : append-weights ( weight-levels quot -- seq )
278     [ [ ignorable?>> ] reject ] dip
279     map [ zero? ] reject ; inline
280
281 : variable-weight ( weight-levels -- obj )
282     dup ignorable?>> [ primary>> ] [ drop 0xFFFF ] if ;
283
284 : weights>bytes ( weights -- array )
285     [
286         {
287             [ [ primary>> ] append-weights { 0 } ]
288             [ [ secondary>> ] append-weights { 0 } ]
289             [ [ tertiary>> ] append-weights { 0 } ]
290             [ [ [ secondary>> ] [ tertiary>> ] bi [ zero? ] bi@ and not ] filter [ variable-weight ] map ]
291         } cleave
292     ] { } append-outputs-as ;
293
294 PRIVATE>
295
296 : completely-ignorable? ( weight -- ? )
297     {
298         [ primary>> zero? ]
299         [ secondary>> zero? ]
300         [ tertiary>> zero? ]
301     } 1&& ;
302
303 : filter-ignorable ( weights -- weights' )
304     f swap [
305         [ nip ] [ primary>> zero? and ] 2bi
306         [ swap ignorable?>> or ]
307         [ swap completely-ignorable? or not ] 2bi
308     ] filter nip ;