4 # run show-thing without args
6 # one line which is a Tcl list for glyphsdone
7 # the xpm in the format expected
8 # then expect child to raise SIGSTOP or exit 0 or exit nonzero
9 # if child raised SIGSTOP, check database was updated
12 proc manyset {list args} {
13 foreach val $list var $args {
20 #---------- display core ----------
28 proc init_widgets {} {
33 image create bitmap image/main
34 label .d.mi -image image/main -borderwidth 0
36 frame .d.csr -bg black -height $csrh
37 frame .d.got -bg black -height $gotsh
39 image create bitmap image/cursor -data \
42 static unsigned char csr_bits[] = {
43 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x21, 0x04, 0x22, 0x02, 0x25, 0x05,
44 0xaa, 0x02, 0x74, 0x01, 0xa8, 0x00, 0x70, 0x00, 0x20, 0x00};
48 label .d.csr.csr.l -image image/cursor -compound left
49 entry .d.csr.csr.e -bd 0
50 pack .d.csr.csr.l -side left
52 frame .d.csr_0 -bg white -width 1
53 frame .d.csr_1 -bg white -width 1
55 place .d.csr -x 0 -y 0
56 place .d.mi -x 0 -y $csrh
64 #---------- xpm input processor ----------
67 global glyphsdone mul inter rhsmost_max unk_l unk_r gotsh csrh
68 global cols rows wordmap
73 if {[gets $f l] < 0} { error "huh? "}
74 if {![regexp {^"(.*)",$} $l dummy l]} {
76 if {[regexp {^\}\;$} $l]} break
80 manyset $l cols rows colours cpp
83 set mulcols [expr {$cols*$mul+$inter}]
84 set mulrows [expr {$rows*$mul+$inter}]
85 append o "\"$mulcols $mulrows 9 1\",\n"
86 for {set x 0} {$x<$cols} {incr x} { set wordmap($x) 0 }
87 } elseif {$y==-2} { # first pixel
98 } elseif {$y==-1} { # 2nd pixel but we've already printed ours
100 set ybit [expr {1<<$y}]
104 foreach c [split $l ""] {
106 if {$x >= $unk_l && $x <= $unk_r} {
110 foreach {min max context got} $glyphsdone {
112 if {$x >= $min && $x <= $max} {
113 set how [lindex {a b} $ab]
122 set p [string toupper $how]
123 incr wordmap($x) $ybit
125 default { error "$c ?" }
127 append ol "[string repeat $p [expr {$mul-$inter}]][
128 string repeat + $inter]"
129 append olh [string repeat + $mul]
135 set olhn [string repeat $olh $inter]
136 if {!$y} { append o $olhn }
137 append o [string repeat $ol [expr {$mul-1}]]
142 set data [exec xpmtoppm << $o]
143 image create photo image/main -data $data
145 foreach w {.d .d.csr .d.got} {
146 $w configure -width $mulcols
148 .d configure -height [expr {$csrh+$mulrows+$gotsh}]
150 .d.csr_$w configure -height $mulrows
152 place .d.got -x 0 -y [expr {$csrh+$mulrows}]
156 #---------- per-invocation display ----------
158 proc draw_glyphsdone {} {
159 global glyphsdone mul inter
160 eval destroy [winfo children .d.got]
161 foreach {min max context got} $glyphsdone {
162 frame .d.got.m$min -bd 0 -background \#888
163 label .d.got.m$min.l -text "$got" -fg white -bg black -bd 0
164 pack .d.got.m$min.l -padx 1 -pady 1
165 place .d.got.m$min -x [expr {$min*$mul+$inter}] -y 0
169 proc startup_cursor {} {
170 global cur_already cur_mode cur_0 cur_1 last_ht
171 global glyphsdone unk_l unk_r
173 set cur_already [expr {[llength $glyphsdone]/4-1}]
174 set cur_mode 1 ;# one of: 0 1 already text
177 set cur_1 [expr {$unk_r+1}]
182 #---------- runtime display and keystroke handling ----------
186 if {![string compare $t $last_ht]} return
187 eval destroy [grid slaves .help]
188 set y 0; foreach l $t {
189 set x 0; foreach c $l {
190 set w .help.at${x}x${y}
192 grid $w -row $y -column $x -padx 5
200 proc recursor/0 {} { recursor//01 0 }
201 proc recursor/1 {} { recursor//01 1 }
202 proc recursor//01 {z1} {
203 global mul rhsmost_max cols glyphsdone
205 .d.csr.csr.l configure -text {adjust}
206 place .d.csr.csr -x [expr {$cur*$mul - 7}]
207 bind_key space { othercursor }
208 bind_leftright cur_$z1 0 [expr {$cols-1}]
209 if {[llength $glyphsdone]} {
210 bind_key Tab { set cur_mode already; recursor }
215 if {$cur_0 != $cur_1} {
221 {{<- ->} {move cursor, adjusting area to define}}
222 {Space {switch to moving other cursor}}
223 {Return {confirm location, enter letter(s)}}
224 {Tab {switch to correcting earlier ocr}}
227 proc othercursor {} {
229 set cur_mode [expr {!$cur_mode}]
233 proc recursor/text {} {
235 {Return {confirm entry of new glyph}}
236 {Escape {abandon entry}}
239 .d.csr.csr.l configure -text {define:}
240 pack .d.csr.csr.e -side left
243 binary scan [.d.csr.csr.e get] h* hex
244 if {[string length $hex]} {
245 RETURN_RESULT DEFINE "$cur_0 $cur_1 $hex"
250 pack forget .d.csr.csr.e
256 proc recursor/already {} {
259 global cur_already mul
260 global glyphsdone cur_already mul
261 .d.csr.csr.l configure -text {correct}
262 set rmax [lindex $glyphsdone [expr {$cur_already*4}]]
263 place .d.csr.csr -x [expr {$rmax*$mul-3}]
266 bind_leftright cur_already 0 [expr {[llength $glyphsdone]/4-1}]
267 bind_key Tab { bind_key Delete {}; set cur_mode 1; recursor }
269 RETURN_RESULT DELETE [lrange $glyphsdone \
270 [expr $cur_already*4] \
271 [expr $cur_already*4+2]]
274 {{<- ->} {move cursor, selecting glyph to correct}}
275 {Del {clear this glyph from the recognition database}}
276 {Tab {switch to selecting area to define as new glyph}}
280 proc bind_key {k proc} {
282 bind . <Key-$k> $proc
283 set keybindings($k) [expr {!![string length $proc]}]
285 proc unbind_all_keys {} {
287 foreach k [array names keybindings] { bind_key $k {} }
290 proc bind_leftright {var min max} {
291 bind_key Left [list leftright $var $min $max -1]
292 bind_key Right [list leftright $var $min $max +1]
294 proc leftright {var min max inc} {
298 if {$vnew < $min || $vnew > $max} return
304 global csrh cur_mode cur_0 cur_1 mul
306 place .d.csr_$z1 -y $csrh -x [expr {[set cur_$z1] * $mul}]
312 #---------- database read and write ----------
316 # <context> <ncharacters> <hex>...
320 # $database($context 0x<bits> 0x<bits>...) = $hex
322 proc read_database {} {
324 set f [open database r]
325 while {[gets $f l] >= 0} {
326 if {![regexp {^(\w+) (\d+) ((?:[0-9a-f]{2})+)$} $l \
327 dummy context strl strh]} {
330 if {[string length $strh] != $strl*2} { error "$strh $strl" }
331 gets $f l; set width [format %d $l]
333 for {set x 0} {$x < $width} {incr x} {
334 gets $f l; lappend bm [format %x 0x$l]
336 set database($bm) $strh
341 proc write_database {} {
344 foreach bm [array names database] {
345 set strh $database($bm)
346 set strs [binary format h* $strh]
347 set strdo [format "%d %s" [expr {[string length $strh]/2}] $strh]
348 set o "[lindex $bm 0] $strdo\n"
349 append o [format "%d\n" [expr {[llength $bm]-1}]]
350 foreach x [lrange $bm 1 end] { append o "$x\n" }
353 set f [open database.new w]
354 foreach o [lsort $ol] {
355 puts -nonewline $f $o
358 file rename -force database.new database
361 proc dbkey {ctx l r} {
364 for {set x $l} {$x <= $r} {incr x} {
365 lappend bm [format %x $wordmap($x)]
370 proc update_database/DEFINE {c0 c1 strh} {
371 global glyphsdone unk_l unk_context wordmap database
372 if {$c0 > $c1} { manyset [list $c0 $c1] c1 c0 }
374 set ncontext $unk_context
376 foreach {l r context got} $glyphsdone {
377 if {$l==$c0} { set ncontext $context; break }
379 if {![info exists ncontext]} {
380 puts stderr "must start at letter LHS!"
385 set bm [dbkey $ncontext $c0 $c1]
386 set database($bm) $strh
390 proc update_database/DELETE {l r ctx} {
392 set bm [dbkey $ctx $l $r]
398 proc RETURN_RESULT {how what} {
399 place forget .d.csr.csr
400 pack forget .d.csr.csr.e
401 helptext {{{ Processing }}}
405 eval update_database/$how $what
408 # bind . <Key-space> {}
411 global glyphsdone unk_l unk_r unk_context
422 set f [open text.xpm]