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 ;
13 TUPLE: weight-levels primary secondary tertiary ignorable? ;
14 : <weight-levels> ( primary secondary tertiary -- weight-levels> )
18 swap >>primary ; inline
20 : parse-weight ( string -- weight )
22 weight-levels new swap rest unclip CHAR: * = swapd >>ignorable?
23 swap "." split first3 [ hex> ] tri@
24 [ >>primary ] [ >>secondary ] [ >>tertiary ] tri*
27 : parse-keys ( string -- chars )
28 " " split [ hex> ] "" map-as ;
30 : parse-ducet ( file -- ducet )
31 load-data-file [ [ parse-keys ] [ parse-weight ] bi* ] H{ } assoc-map-as ;
33 "vocab:unicode/UCA/allkeys.txt" parse-ducet ducet set-global
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 ]
46 { 0x0FB2 0x0F71 } ! CE(0FB2) CE(0F71)
61 { 0x0FB3 0x0F71 } ! CE(0FB3) CE(0F71)
75 } ducet get-global '[ swap >string _ set-at ] assoc-each ;
77 ! Add a few missing ducet values
80 : tangut-block? ( char -- ? )
81 ! Tangut Block, Tangut Components Block
82 { [ 0x17000 0x187FF between? ] [ 0x18800 0x18AFF between? ] } 1|| ; inline
84 : nushu-block? ( char -- ? )
85 0x1b170 0x1B2FB between? ; inline
87 ! https://wiki.computercraft.cc/Module:Unicode_data
88 ! Unicode TR10 - Computing Implicit Weights
89 : base ( char -- base )
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
101 : tangut-AAAA ( char -- weight-levels )
102 drop 0xfb00 0x0020 0x0002 <weight-levels> ; inline
104 : tangut-BBBB ( char -- weight-levels )
105 0x17000 - 0x8000 bitor 0 0 <weight-levels> ; inline
107 : nushu-AAAA ( char -- weight-levels )
108 drop 0xfb01 0x0020 0x0002 <weight-levels> ; inline
110 : nushu-BBBB ( char -- weight-levels )
111 0x1B170 - 0x8000 bitor 0 0 <weight-levels> ; inline
113 : AAAA ( char -- weight-levels )
114 [ base ] [ -15 shift ] bi + 0x0020 0x0002 <weight-levels> ; inline
116 : BBBB ( char -- weight-levels )
117 0x7FFF bitand 0x8000 bitor 0 0 <weight-levels> ; inline
119 : derive-weight ( 1string -- weight-levels-pair )
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 ]
127 : building-last ( -- char )
128 building get [ 0 ] [ last last ] if-empty ;
130 : blocked? ( char -- ? )
131 combining-class dup { 0 f } member?
132 [ drop building-last non-starter? ]
133 [ building-last combining-class = ] if ;
135 : possible-bases ( -- slice-of-building )
136 building get dup [ first non-starter? not ] find-last
137 drop [ 0 ] unless* tail-slice ;
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 ;
145 dup blocked? [ 1string , ] [
146 dup possible-bases dup length <iota>
147 [ ?combine ] 2with any?
148 [ drop ] [ 1string , ] if
151 : string>graphemes ( string -- graphemes )
152 [ [ add ] each ] { } make ;
154 : char>weight-levels ( 1string -- weight-levels )
155 ducet get-global ?at [ derive-weight ] unless ; inline
157 : graphemes>weights ( graphemes -- weights )
160 [ 1array ] ! From tailoring
161 [ char>weight-levels ] if
162 ] { } map-as concat ;
164 : append-weights ( weight-levels quot -- seq )
165 [ [ ignorable?>> ] reject ] dip
166 map [ zero? ] reject ; inline
168 : variable-weight ( weight-levels -- obj )
169 dup ignorable?>> [ primary>> ] [ drop 0xFFFF ] if ;
171 : weights>bytes ( weights -- array )
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 ]
179 ] { } append-outputs-as ;
183 : completely-ignorable? ( weight -- ? )
186 [ secondary>> zero? ]
190 : filter-ignorable ( weights -- weights' )
192 [ nip ] [ primary>> zero? and ] 2bi
193 [ swap ignorable?>> or ]
194 [ swap completely-ignorable? or not ] 2bi