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 kernel locals make math math.order
5 math.parser namespaces sequences simple-flat-file splitting
13 TUPLE: weight primary secondary tertiary ignorable? ;
15 : parse-weight ( string -- weight )
17 weight new swap rest unclip CHAR: * = swapd >>ignorable?
18 swap "." split first3 [ hex> ] tri@
19 [ >>primary ] [ >>secondary ] [ >>tertiary ] tri*
22 : parse-keys ( string -- chars )
23 " " split [ hex> ] "" map-as ;
25 : parse-ducet ( file -- ducet )
27 [ [ parse-keys ] [ parse-weight ] bi* ] H{ } assoc-map-as ;
29 "vocab:unicode/collation/allkeys.txt" parse-ducet ducet set-global
31 ! Fix up table for long contractions
32 : help-one ( assoc key -- )
33 ! Need to be more general? Not for DUCET, apparently
34 2 head 2dup swap key? [ 2drop ] [
35 [ [ 1string of ] with { } map-as concat ]
39 : insert-helpers ( assoc -- )
40 dup keys [ length 3 >= ] filter
41 [ help-one ] with each ;
43 ducet get-global insert-helpers
45 : base ( char -- base )
47 { [ dup 0x3400 0x4DB5 between? ] [ drop 0xFB80 ] } ! Extension A
48 { [ dup 0x20000 0x2A6D6 between? ] [ drop 0xFB80 ] } ! Extension B
49 { [ dup 0x4E00 0x9FC3 between? ] [ drop 0xFB40 ] } ! CJK
50 [ drop 0xFBC0 ] ! Other
53 : AAAA ( char -- weight )
54 [ base ] [ -15 shift ] bi + 0x20 2 f weight boa ;
56 : BBBB ( char -- weight )
57 0x7FFF bitand 0x8000 bitor 0 0 f weight boa ;
59 : illegal? ( char -- ? )
60 { [ "Noncharacter_Code_Point" property? ] [ category "Cs" = ] } 1|| ;
62 : derive-weight ( char -- weights )
65 [ [ AAAA ] [ BBBB ] bi 2array ] if ;
67 : building-last ( -- char )
68 building get empty? [ 0 ] [ building get last last ] if ;
70 : blocked? ( char -- ? )
71 combining-class dup { 0 f } member?
72 [ drop building-last non-starter? ]
73 [ building-last combining-class = ] if ;
75 : possible-bases ( -- slice-of-building )
76 building get dup [ first non-starter? not ] find-last
77 drop [ 0 ] unless* tail-slice ;
79 :: ?combine ( char slice i -- ? )
80 i slice nth char suffix :> str
81 str ducet get-global key? dup
82 [ str i slice set-nth ] when ;
85 dup blocked? [ 1string , ] [
86 dup possible-bases dup length iota
87 [ ?combine ] 2with any?
88 [ drop ] [ 1string , ] if
91 : string>graphemes ( string -- graphemes )
92 [ [ add ] each ] { } make ;
94 : graphemes>weights ( graphemes -- weights )
96 dup weight? [ 1array ] ! From tailoring
97 [ dup ducet get-global at [ ] [ derive-weight ] ?if ] if
100 : append-weights ( weights quot -- )
101 [ [ ignorable?>> ] reject ] dip
102 map [ zero? ] reject % 0 , ; inline
104 : variable-weight ( weight -- )
105 dup ignorable?>> [ primary>> ] [ drop 0xFFFF ] if , ;
107 : weights>bytes ( weights -- byte-array )
110 [ [ primary>> ] append-weights ]
111 [ [ secondary>> ] append-weights ]
112 [ [ tertiary>> ] append-weights ]
113 [ [ variable-weight ] each ]
119 : completely-ignorable? ( weight -- ? )
120 [ primary>> ] [ secondary>> ] [ tertiary>> ] tri
121 [ zero? ] tri@ and and ;
123 : filter-ignorable ( weights -- weights' )
125 [ nip ] [ primary>> zero? and ] 2bi
126 [ swap ignorable?>> or ]
127 [ swap completely-ignorable? or not ] 2bi