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 {} {
32 if {[winfo exists .d]} return
36 image create bitmap image/main
37 label .d.mi -image image/main -borderwidth 0
39 frame .d.csr -bg black -height $csrh
40 frame .d.got -bg black -height $gotsh
42 image create bitmap image/cursor -data \
45 static unsigned char csr_bits[] = {
46 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x21, 0x04, 0x22, 0x02, 0x25, 0x05,
47 0xaa, 0x02, 0x74, 0x01, 0xa8, 0x00, 0x70, 0x00, 0x20, 0x00};
51 label .d.csr.csr.l -image image/cursor -compound left
52 entry .d.csr.csr.e -bd 0
53 pack .d.csr.csr.l -side left
55 frame .d.csr_0 -bg white -width 1
56 frame .d.csr_1 -bg white -width 1
58 place .d.csr -x 0 -y 0
59 place .d.mi -x 0 -y $csrh
67 #---------- xpm input processor ----------
70 global glyphsdone mul inter rhsmost_max unk_l unk_r gotsh csrh
71 global cols rows wordmap
76 if {[gets $f l] < 0} { error "huh? "}
77 if {![regexp {^"(.*)",$} $l dummy l]} {
79 if {[regexp {^\}\;$} $l]} break
83 manyset $l cols rows colours cpp
86 set mulcols [expr {$cols*$mul+$inter}]
87 set mulrows [expr {$rows*$mul+$inter}]
88 append o "\"$mulcols $mulrows 9 1\",\n"
89 for {set x 0} {$x<$cols} {incr x} { set wordmap($x) 0 }
90 } elseif {$y==-2} { # first pixel
101 } elseif {$y==-1} { # 2nd pixel but we've already printed ours
103 set ybit [expr {1<<$y}]
107 foreach c [split $l ""] {
109 if {$x >= $unk_l && $x <= $unk_r} {
113 foreach {min max context got} $glyphsdone {
115 if {$x >= $min && $x <= $max} {
116 set how [lindex {a b} $ab]
125 set p [string toupper $how]
126 incr wordmap($x) $ybit
128 default { error "$c ?" }
130 append ol "[string repeat $p [expr {$mul-$inter}]][
131 string repeat + $inter]"
132 append olh [string repeat + $mul]
138 set olhn [string repeat $olh $inter]
139 if {!$y} { append o $olhn }
140 append o [string repeat $ol [expr {$mul-1}]]
145 set data [exec xpmtoppm << $o]
146 image create photo image/main -data $data
148 foreach w {.d .d.csr .d.got} {
149 $w configure -width $mulcols
151 .d configure -height [expr {$csrh+$mulrows+$gotsh}]
153 .d.csr_$w configure -height $mulrows
155 place .d.got -x 0 -y [expr {$csrh+$mulrows}]
159 #---------- per-invocation display ----------
161 proc draw_glyphsdone {} {
162 global glyphsdone mul inter
163 eval destroy [winfo children .d.got]
164 foreach {min max context got} $glyphsdone {
165 frame .d.got.m$min -bd 0 -background \#888
166 label .d.got.m$min.l -text "$got" -fg white -bg black -bd 0
167 pack .d.got.m$min.l -padx 1 -pady 1
168 place .d.got.m$min -x [expr {$min*$mul+$inter}] -y 0
172 proc startup_cursor {} {
173 global cur_already cur_mode cur_0 cur_1 last_ht
174 global glyphsdone unk_l unk_r
176 set cur_already [expr {[llength $glyphsdone]/4-1}]
177 set cur_mode 1 ;# one of: 0 1 already text
180 set cur_1 [expr {$unk_r+1}]
187 #---------- runtime display and keystroke handling ----------
191 if {![string compare $t $last_ht]} return
192 eval destroy [grid slaves .help]
193 set y 0; foreach l $t {
194 set x 0; foreach c $l {
195 set w .help.at${x}x${y}
197 grid $w -row $y -column $x -padx 5
205 proc recursor/0 {} { recursor//01 0 }
206 proc recursor/1 {} { recursor//01 1 }
207 proc recursor//01 {z1} {
208 global mul rhsmost_max cols glyphsdone
210 .d.csr.csr.l configure -text {adjust}
211 place .d.csr.csr -x [expr {$cur*$mul - 7}]
212 bind_key space { othercursor }
213 bind_leftright cur_$z1 0 [expr {$cols-1}]
214 if {[llength $glyphsdone]} {
215 bind_key Tab { set cur_mode already; recursor }
220 if {$cur_0 != $cur_1} {
226 {{<- ->} {move cursor, adjusting area to define}}
227 {Space {switch to moving other cursor}}
228 {Return {confirm location, enter letter(s)}}
229 {Tab {switch to correcting earlier ocr}}
232 proc othercursor {} {
234 set cur_mode [expr {!$cur_mode}]
238 proc recursor/text {} {
240 {Return {confirm entry of new glyph}}
241 {Escape {abandon entry}}
244 .d.csr.csr.l configure -text {define:}
245 pack .d.csr.csr.e -side left
248 binary scan [.d.csr.csr.e get] H* hex
249 if {[string length $hex]} {
250 RETURN_RESULT DEFINE "$cur_0 $cur_1 $hex"
255 pack forget .d.csr.csr.e
261 proc recursor/already {} {
264 global cur_already mul
265 global glyphsdone cur_already mul
266 .d.csr.csr.l configure -text {correct}
267 set rmax [lindex $glyphsdone [expr {$cur_already*4}]]
268 place .d.csr.csr -x [expr {$rmax*$mul-3}]
271 bind_leftright cur_already 0 [expr {[llength $glyphsdone]/4-1}]
272 bind_key Tab { bind_key Delete {}; set cur_mode 1; recursor }
274 RETURN_RESULT DELETE [lrange $glyphsdone \
275 [expr $cur_already*4] \
276 [expr $cur_already*4+2]]
279 {{<- ->} {move cursor, selecting glyph to correct}}
280 {Del {clear this glyph from the recognition database}}
281 {Tab {switch to selecting area to define as new glyph}}
285 proc bind_key {k proc} {
287 bind . <Key-$k> $proc
288 set keybindings($k) [expr {!![string length $proc]}]
290 proc unbind_all_keys {} {
292 foreach k [array names keybindings] { bind_key $k {} }
295 proc bind_leftright {var min max} {
296 bind_key Left [list leftright $var $min $max -1]
297 bind_key Right [list leftright $var $min $max +1]
299 proc leftright {var min max inc} {
303 if {$vnew < $min || $vnew > $max} return
309 global csrh cur_mode cur_0 cur_1 mul
311 place .d.csr_$z1 -y $csrh -x [expr {[set cur_$z1] * $mul}]
317 #---------- database read and write ----------
321 # <context> <ncharacters> <hex>...
325 # $database($context 0x<bits> 0x<bits>...) = $hex
327 proc read_database {} {
329 set f [open database r]
330 while {[gets $f l] >= 0} {
331 if {![regexp {^(\w+) (\d+) ((?:[0-9a-f]{2})+)$} $l \
332 dummy context strl strh]} {
335 if {[string length $strh] != $strl*2} { error "$strh $strl" }
336 gets $f l; set width [format %d $l]
338 for {set x 0} {$x < $width} {incr x} {
339 gets $f l; lappend bm [format %x 0x$l]
341 set database($bm) $strh
346 proc write_database {} {
349 foreach bm [array names database] {
350 set strh $database($bm)
351 set strs [binary format H* $strh]
352 set strdo [format "%d %s" [expr {[string length $strh]/2}] $strh]
353 set o "[lindex $bm 0] $strdo\n"
354 append o [format "%d\n" [expr {[llength $bm]-1}]]
355 foreach x [lrange $bm 1 end] { append o "$x\n" }
358 set f [open database.new w]
359 foreach o [lsort $ol] {
360 puts -nonewline $f $o
363 file rename -force database.new database
366 proc dbkey {ctx l r} {
369 for {set x $l} {$x <= $r} {incr x} {
370 lappend bm [format %x $wordmap($x)]
375 proc update_database/DEFINE {c0 c1 strh} {
376 global glyphsdone unk_l unk_context wordmap database
377 if {$c0 > $c1} { manyset [list $c0 $c1] c1 c0 }
379 set ncontext $unk_context
381 foreach {l r context got} $glyphsdone {
382 if {$l==$c0} { set ncontext $context; break }
384 if {![info exists ncontext]} {
385 puts stderr "must start at letter LHS!"
390 set bm [dbkey $ncontext $c0 $c1]
391 set database($bm) $strh
395 proc update_database/DELETE {l r ctx} {
397 set bm [dbkey $ctx $l $r]
403 proc RETURN_RESULT {how what} {
405 place forget .d.csr.csr
406 pack forget .d.csr.csr.e
407 helptext {{{ Processing }}}
410 puts stderr "$how $what"
411 eval update_database/$how $what
415 #---------- main progrm ----------
418 global glyphsdone unk_l unk_r unk_context
429 set f [open text.xpm]
440 global glyphsdone unk_l unk_r unk_context
442 if {[gets stdin l]<0} {
443 if {[eof stdin]} { fconfigure stdin -blocking yes; exit 0 }
447 manyset [lrange $l 0 3] unk_l unk_r unk_context
448 set glyphsdone [lrange $l 3 end]
449 puts stderr "SHOW-THING GOT $l"
451 fileevent stdin readable {}
458 proc main/automatic {} {
459 fconfigure stdin -blocking no
460 fileevent stdin readable required
462 proc done/automatic {} {
463 exec sh -c {printf \\0 >&4}
464 fileevent stdin readable required
467 switch -exact -- $argv {
468 {} { set mainkind test }
469 {--automatic 1} { set mainkind automatic }
470 {--automatic*} { error "incompatible versions - install problem" }
471 default { error "huh $argv ?" }