4 # run show-thing without args
6 # one line which is a Tcl list for unk_{l,r} unk_contexts 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 ----------
30 puts stderr "SHOW-THING $m"
33 proc init_widgets {} {
35 global csrh gotsh ctxh
37 if {[winfo exists .d]} return
41 image create bitmap image/main
42 label .d.mi -image image/main -borderwidth 0
44 frame .d.csr -bg black -height $csrh
45 frame .d.got -bg black -height $gotsh
46 frame .d.ctx -bg black
48 image create bitmap image/cursor -data \
51 static unsigned char csr_bits[] = {
52 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x21, 0x04, 0x22, 0x02, 0x25, 0x05,
53 0xaa, 0x02, 0x74, 0x01, 0xa8, 0x00, 0x70, 0x00, 0x20, 0x00};
57 label .d.csr.csr.l -image image/cursor -compound left
58 entry .d.csr.csr.e -bd 0
59 pack .d.csr.csr.l -side left
61 frame .d.mi.csr_0 -bg white -width 1
62 frame .d.mi.csr_1 -bg white -width 1
64 pack .d.csr .d.mi .d.got .d.ctx -side top
71 proc show_context {maxhv x ctxs} {
75 if {[llength $ctxs]==1} { set fg blue } { set fg yellow }
76 label $w -bg black -fg $fg -text [join $ctxs "/\n"] -justify left
77 place $w -x [expr {($x-1)*$mul}] -y 0
78 set wh [winfo reqheight $w]
79 if {$wh > $maxh} { set maxh $wh }
82 proc resize_widgets {} {
83 global mulcols mulrows csrh gotsh ctxh glyphsdone
84 global unk_l unk_contexts
86 foreach w {.d.csr .d.got .d.ctx} {
87 $w configure -width $mulcols
89 #.d configure -height [expr {$csrh+$mulrows+$gotsh+$ctxh}]
91 .d.mi.csr_$w configure -height $mulrows
94 eval destroy [winfo children .d.ctx]
97 foreach {min max contexts got} $glyphsdone {
98 show_context maxh $min $contexts
100 show_context maxh $unk_l $unk_contexts
101 .d.ctx configure -height $maxh
105 #---------- xpm input processor ----------
108 global glyphsdone mul inter rhsmost_max unk_l unk_r mulcols mulrows
109 global cols rows wordmap
114 if {[gets $f l] < 0} { error "huh? "}
115 if {![regexp {^"(.*)",$} $l dummy l]} {
117 if {[regexp {^\}\;$} $l]} break
121 manyset $l cols rows colours cpp
122 if {$colours!=2 || $cpp!=1} { error "$l ?" }
124 set chop_l [expr {$unk_l - 80}]
125 set chop_r [expr {$cols - $unk_l - 100}]
126 if {$chop_l<0} { set chop_l 0 }
128 set unk_l [expr {$unk_l - $chop_l}]
129 set unk_r [expr {$unk_r - $chop_l}]
131 foreach {min max contexts got} $glyphsdone {
133 [expr {$min-$chop_l}] \
134 [expr {$max-$chop_l}] \
140 set cols [expr {$cols - $chop_l - $chop_r}]
141 debug "NOW cols=$cols chop_l,r=$chop_l,$chop_r rows=$rows\
144 set mulcols [expr {$cols*$mul+$inter}]
145 set mulrows [expr {$rows*$mul+$inter}]
146 append o "\"$mulcols $mulrows 9 1\",\n"
147 for {set x 0} {$x<$cols} {incr x} { set wordmap($x) 0 }
148 } elseif {$y==-2} { # first pixel
159 } elseif {$y==-1} { # 2nd pixel but we've already printed ours
161 set ybit [expr {1<<$y}]
166 set l [string range $l $chop_l end-$chop_r]
168 set l [string range $l $chop_l end]
169 append l [string repeat " " [expr -$chop_r]]
171 foreach c [split $l ""] {
173 if {$x >= $unk_l && $x <= $unk_r} {
177 foreach {min max contexts got} $glyphsdone {
179 if {$x >= $min && $x <= $max} {
180 set how [lindex {a b} $ab]
189 set p [string toupper $how]
190 incr wordmap($x) $ybit
192 default { error "$c ?" }
194 append ol "[string repeat $p [expr {$mul-$inter}]][
195 string repeat + $inter]"
196 append olh [string repeat + $mul]
202 set olhn [string repeat $olh $inter]
203 if {!$y} { append o $olhn }
204 append o [string repeat $ol [expr {$mul-1}]]
209 set data [exec xpmtoppm << $o]
210 image create photo image/main -data $data
214 #---------- per-invocation display ----------
216 proc draw_glyphsdone {} {
217 global glyphsdone mul inter
218 eval destroy [winfo children .d.got]
219 foreach {min max contexts got} $glyphsdone {
220 frame .d.got.m$min -bd 0 -background \#888
221 label .d.got.m$min.l -text "$got" -fg white -bg black -bd 0
222 pack .d.got.m$min.l -padx 1 -pady 1
223 place .d.got.m$min -x [expr {$min*$mul+$inter}] -y 0
227 proc startup_cursor {} {
228 global cur_already cur_mode cur_0 cur_1 last_ht
229 global glyphsdone unk_l unk_r
231 set cur_already [expr {[llength $glyphsdone]/4-1}]
232 set cur_mode 1 ;# one of: 0 1 already text
235 set cur_1 [expr {$unk_r+1}]
242 #---------- runtime display and keystroke handling ----------
246 if {![string compare $t $last_ht]} return
247 eval destroy [grid slaves .help]
248 set y 0; foreach l $t {
249 set x 0; foreach c $l {
250 set w .help.at${x}x${y}
252 grid $w -row $y -column $x -padx 5
260 proc recursor/0 {} { recursor//01 0 }
261 proc recursor/1 {} { recursor//01 1 }
262 proc recursor//01 {z1} {
263 global mul rhsmost_max cols glyphsdone
265 .d.csr.csr.l configure -text {adjust}
266 place .d.csr.csr -x [expr {$cur*$mul - 7}]
267 bind_key space { othercursor }
268 bind_leftright cur_$z1 0 [expr {$cols-1}]
269 if {[llength $glyphsdone]} {
270 bind_key Tab { set cur_mode already; recursor }
275 if {$cur_0 != $cur_1} {
276 .d.csr.csr.e delete 0 end
282 {{<- ->} {move cursor, adjusting area to define}}
283 {Space {switch to moving other cursor}}
284 {Return {confirm location, enter letter(s)}}
285 {Tab {switch to correcting earlier ocr}}
288 proc othercursor {} {
290 set cur_mode [expr {!$cur_mode}]
294 proc recursor/text {} {
296 {Return {confirm entry of new glyph}}
297 {Escape {abandon entry}}
300 .d.csr.csr.l configure -text {define:}
301 pack .d.csr.csr.e -side left
304 set strq [.d.csr.csr.e get]
305 if {[regexp {^(?:[!-[]|[]-~]|\\\\|\\x[0-9a-f]{2})+} $strq]} {
306 RETURN_RESULT DEFINE "$cur_0 $cur_1 $strq"
311 pack forget .d.csr.csr.e
317 proc recursor/already {} {
320 global cur_already mul
321 global glyphsdone cur_already mul
322 .d.csr.csr.l configure -text {correct}
323 set rmax [lindex $glyphsdone [expr {$cur_already*4}]]
324 place .d.csr.csr -x [expr {$rmax*$mul-3}]
327 bind_leftright cur_already 0 [expr {[llength $glyphsdone]/4-1}]
328 bind_key Tab { bind_key Delete {}; set cur_mode 1; recursor }
330 RETURN_RESULT DELETE [lrange $glyphsdone \
331 [expr $cur_already*4] \
332 [expr $cur_already*4+2]]
335 {{<- ->} {move cursor, selecting glyph to correct}}
336 {Del {clear this glyph from the recognition database}}
337 {Tab {switch to selecting area to define as new glyph}}
341 proc bind_key {k proc} {
343 bind . <Key-$k> $proc
344 set keybindings($k) [expr {!![string length $proc]}]
346 proc unbind_all_keys {} {
348 foreach k [array names keybindings] { bind_key $k {} }
351 proc bind_leftright {var min max} {
352 bind_key Left [list leftright $var $min $max -1]
353 bind_key Right [list leftright $var $min $max +1]
355 proc leftright {var min max inc} {
359 if {$vnew < $min || $vnew > $max} return
365 global csrh cur_mode cur_0 cur_1 mul
367 place .d.mi.csr_$z1 -y 0 -x [expr {[set cur_$z1] * $mul}]
373 #---------- database read and write ----------
377 # <context> <ncharacters> <hex>...
381 # $database($context 0x<bits> 0x<bits>...) = $hex
383 set database_header {# ypp-sc-tools pctb font v1}
386 if {[gets $f l] < 0} { error "unexpected db eof" }
390 proc read_database {} {
391 global database database_header rows database_fn
392 catch { unset database }
393 set database_fn ./charset-$rows.txt
394 if {![file exists $database_fn]} return
395 set f [open $database_fn r]
396 if {[string compare [db_getsl $f] $database_header]} { error "$l ?" }
397 if {([db_getsl $f])+0 != $rows} { error "wrong h ?" }
399 set context [db_getsl $f]
400 if {![string length $context]} continue
401 if {[regexp {^\#} $context]} continue
402 if {![string compare . $context]} break
405 set strq [db_getsl $f]
408 if {![string length $l]} break
409 lappend bm [format %x 0x$l]
411 set database($bm) $strq
416 proc write_database {} {
417 global database rows database_fn database_header
419 foreach bm [array names database] {
420 set strq $database($bm)
421 set o "[lindex $bm 0]\n$strq\n"
422 foreach x [lrange $bm 1 end] { append o "$x\n" }
426 set f [open $database_fn.new w]
427 puts $f "$database_header\n$rows\n"
428 foreach o [lsort $ol] {
433 file rename -force $database_fn.new $database_fn
436 proc dbkey {ctx l r} {
439 for {set x $l} {$x <= $r} {incr x} {
440 lappend bm [format %x $wordmap($x)]
445 proc update_database/DEFINE {c0 c1 strq} {
446 global glyphsdone unk_l unk_contexts wordmap database
447 if {$c0 > $c1} { manyset [list $c0 $c1] c1 c0 }
449 set ncontexts $unk_contexts
451 foreach {l r contexts got} $glyphsdone {
452 if {$l==$c0} { set ncontexts $contexts; break }
454 if {![info exists ncontexts]} {
455 puts stderr "must start at letter LHS!"
460 foreach c $ncontexts {
461 set bm [dbkey $c $c0 $c1]
462 set database($bm) $strq
467 proc update_database/DELETE {l r ctxs} {
470 set bm [dbkey $ctx $l $r]
471 catch { unset database($bm) }
477 proc RETURN_RESULT {how what} {
479 place forget .d.csr.csr
480 pack forget .d.csr.csr.e
481 helptext {{{ Processing }}}
485 eval update_database/$how $what
489 #---------- main progrm ----------
492 global glyphsdone unk_l unk_r unk_contexts
501 set unk_contexts Test
503 set f [open text.xpm]
516 global glyphsdone unk_l unk_r unk_contexts
518 if {[gets stdin l]<0} {
519 if {[eof stdin]} { fconfigure stdin -blocking yes; exit 0 }
523 manyset [lrange $l 0 3] unk_l unk_r unk_contexts
524 set glyphsdone [lrange $l 3 end]
527 fileevent stdin readable {}
536 proc main/automatic {} {
537 fconfigure stdin -blocking no
538 fileevent stdin readable required
540 proc done/automatic {} {
541 exec sh -c {printf \\0 >&4}
542 fileevent stdin readable required
545 switch -exact -- $argv {
546 {} { set mainkind test }
547 {--automatic 1} { set mainkind automatic }
548 {--automatic*} { error "incompatible versions - install problem" }
549 default { error "huh $argv ?" }