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 ----------
29 proc init_widgets {} {
31 global csrh gotsh ctxh
33 if {[winfo exists .d]} return
37 image create bitmap image/main
38 label .d.mi -image image/main -borderwidth 0
40 frame .d.csr -bg black -height $csrh
41 frame .d.got -bg black -height $gotsh
42 frame .d.ctx -bg black
44 image create bitmap image/cursor -data \
47 static unsigned char csr_bits[] = {
48 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x21, 0x04, 0x22, 0x02, 0x25, 0x05,
49 0xaa, 0x02, 0x74, 0x01, 0xa8, 0x00, 0x70, 0x00, 0x20, 0x00};
53 label .d.csr.csr.l -image image/cursor -compound left
54 entry .d.csr.csr.e -bd 0
55 pack .d.csr.csr.l -side left
57 frame .d.mi.csr_0 -bg white -width 1
58 frame .d.mi.csr_1 -bg white -width 1
60 pack .d.csr .d.mi .d.got .d.ctx -side top
67 proc show_context {maxhv x ctxs} {
71 if {[llength $ctxs]==1} { set fg blue } { set fg yellow }
72 label $w -bg black -fg $fg -text [join $ctxs "/\n"] -justify left
73 place $w -x [expr {($x-1)*$mul}] -y 0
74 set wh [winfo reqheight $w]
75 if {$wh > $maxh} { set maxh $wh }
78 proc resize_widgets {} {
79 global mulcols mulrows csrh gotsh ctxh glyphsdone
80 global unk_l unk_contexts
82 foreach w {.d.csr .d.got .d.ctx} {
83 $w configure -width $mulcols
85 #.d configure -height [expr {$csrh+$mulrows+$gotsh+$ctxh}]
87 .d.mi.csr_$w configure -height $mulrows
90 eval destroy [winfo children .d.ctx]
93 foreach {min max contexts got} $glyphsdone {
94 show_context maxh $min $contexts
96 show_context maxh $unk_l $unk_contexts
97 .d.ctx configure -height $maxh
101 #---------- xpm input processor ----------
104 global glyphsdone mul inter rhsmost_max unk_l unk_r mulcols mulrows
105 global cols rows wordmap
110 if {[gets $f l] < 0} { error "huh? "}
111 if {![regexp {^"(.*)",$} $l dummy l]} {
113 if {[regexp {^\}\;$} $l]} break
117 manyset $l cols rows colours cpp
118 if {$colours!=2 || $cpp!=1} { error "$l ?" }
120 set chop_l [expr {$unk_l - 80}]
121 set chop_r [expr {$cols - $unk_l - 100}]
122 if {$chop_l<0} { set chop_l 0 }
124 set unk_l [expr {$unk_l - $chop_l}]
125 set unk_r [expr {$unk_r - $chop_l}]
127 foreach {min max contexts got} $glyphsdone {
129 [expr {$min-$chop_l}] \
130 [expr {$max-$chop_l}] \
136 set cols [expr {$cols - $chop_l - $chop_r}]
137 debug "NOW cols=$cols chop_l,r=$chop_l,$chop_r rows=$rows\
140 set mulcols [expr {$cols*$mul+$inter}]
141 set mulrows [expr {$rows*$mul+$inter}]
142 append o "\"$mulcols $mulrows 9 1\",\n"
143 for {set x 0} {$x<$cols} {incr x} { set wordmap($x) 0 }
144 } elseif {$y==-2} { # first pixel
155 } elseif {$y==-1} { # 2nd pixel but we've already printed ours
157 set ybit [expr {1<<$y}]
162 set l [string range $l $chop_l end-$chop_r]
164 set l [string range $l $chop_l end]
165 append l [string repeat " " [expr -$chop_r]]
167 foreach c [split $l ""] {
169 if {$x >= $unk_l && $x <= $unk_r} {
173 foreach {min max contexts got} $glyphsdone {
175 if {$x >= $min && $x <= $max} {
176 set how [lindex {a b} $ab]
185 set p [string toupper $how]
186 incr wordmap($x) $ybit
188 default { error "$c ?" }
190 append ol "[string repeat $p [expr {$mul-$inter}]][
191 string repeat + $inter]"
192 append olh [string repeat + $mul]
198 set olhn [string repeat $olh $inter]
199 if {!$y} { append o $olhn }
200 append o [string repeat $ol [expr {$mul-1}]]
205 set data [exec xpmtoppm << $o]
206 image create photo image/main -data $data
210 #---------- per-invocation display ----------
212 proc draw_glyphsdone {} {
213 global glyphsdone mul inter
214 eval destroy [winfo children .d.got]
215 foreach {min max contexts got} $glyphsdone {
216 frame .d.got.m$min -bd 0 -background \#888
217 label .d.got.m$min.l -text "$got" -fg white -bg black -bd 0
218 pack .d.got.m$min.l -padx 1 -pady 1
219 place .d.got.m$min -x [expr {$min*$mul+$inter}] -y 0
223 proc startup_cursor {} {
224 global cur_already cur_mode cur_0 cur_1 last_ht
225 global glyphsdone unk_l unk_r
227 set cur_already [expr {[llength $glyphsdone]/4-1}]
228 set cur_mode 1 ;# one of: 0 1 already text
231 set cur_1 [expr {$unk_r+1}]
238 #---------- runtime display and keystroke handling ----------
242 if {![string compare $t $last_ht]} return
243 eval destroy [grid slaves .help]
244 set y 0; foreach l $t {
245 set x 0; foreach c $l {
246 set w .help.at${x}x${y}
248 grid $w -row $y -column $x -padx 5
256 proc recursor/0 {} { recursor//01 0 }
257 proc recursor/1 {} { recursor//01 1 }
258 proc recursor//01 {z1} {
259 global mul rhsmost_max cols glyphsdone
261 .d.csr.csr.l configure -text {adjust}
262 place .d.csr.csr -x [expr {$cur*$mul - 7}]
263 bind_key space { othercursor }
264 bind_leftright cur_$z1 0 [expr {$cols-1}]
265 if {[llength $glyphsdone]} {
266 bind_key Tab { set cur_mode already; recursor }
271 if {$cur_0 != $cur_1} {
272 .d.csr.csr.e delete 0 end
278 {{<- ->} {move cursor, adjusting area to define}}
279 {Space {switch to moving other cursor}}
280 {Return {confirm location, enter letter(s)}}
281 {Tab {switch to correcting earlier ocr}}
284 proc othercursor {} {
286 set cur_mode [expr {!$cur_mode}]
290 proc recursor/text {} {
292 {Return {confirm entry of new glyph}}
293 {Escape {abandon entry}}
296 .d.csr.csr.l configure -text {define:}
297 pack .d.csr.csr.e -side left
300 set strq [.d.csr.csr.e get]
301 if {[regexp {^(?:[!-[]|[]-~]|\\\\|\\x[0-9a-f]{2})+} $strq]} {
302 RETURN_RESULT DEFINE "$cur_0 $cur_1 $strq"
307 pack forget .d.csr.csr.e
313 proc recursor/already {} {
316 global cur_already mul
317 global glyphsdone cur_already mul
318 .d.csr.csr.l configure -text {correct}
319 set rmax [lindex $glyphsdone [expr {$cur_already*4}]]
320 place .d.csr.csr -x [expr {$rmax*$mul-3}]
323 bind_leftright cur_already 0 [expr {[llength $glyphsdone]/4-1}]
324 bind_key Tab { bind_key Delete {}; set cur_mode 1; recursor }
326 RETURN_RESULT DELETE [lrange $glyphsdone \
327 [expr $cur_already*4] \
328 [expr $cur_already*4+2]]
331 {{<- ->} {move cursor, selecting glyph to correct}}
332 {Del {clear this glyph from the recognition database}}
333 {Tab {switch to selecting area to define as new glyph}}
337 proc bind_key {k proc} {
339 bind . <Key-$k> $proc
340 set keybindings($k) [expr {!![string length $proc]}]
342 proc unbind_all_keys {} {
344 foreach k [array names keybindings] { bind_key $k {} }
347 proc bind_leftright {var min max} {
348 bind_key Left [list leftright $var $min $max -1]
349 bind_key Right [list leftright $var $min $max +1]
351 proc leftright {var min max inc} {
355 if {$vnew < $min || $vnew > $max} return
361 global csrh cur_mode cur_0 cur_1 mul
363 place .d.mi.csr_$z1 -y 0 -x [expr {[set cur_$z1] * $mul}]
369 #---------- database read and write ----------
373 # <context> <ncharacters> <hex>...
377 # $database($context 0x<bits> 0x<bits>...) = $hex
379 set database_header {# ypp-sc-tools pctb font v1}
382 if {[gets $f l] < 0} { error "unexpected db eof" }
386 proc read_database {} {
387 global database database_header rows database_fn
388 catch { unset database }
389 set database_fn ./charset-$rows.txt
390 if {![file exists $database_fn]} return
391 set f [open $database_fn r]
392 if {[string compare [db_getsl $f] $database_header]} { error "$l ?" }
393 if {([db_getsl $f])+0 != $rows} { error "wrong h ?" }
395 set context [db_getsl $f]
396 if {![string length $context]} continue
397 if {[regexp {^\#} $context]} continue
398 if {![string compare . $context]} break
401 set strq [db_getsl $f]
404 if {![string length $l]} break
405 lappend bm [format %x 0x$l]
407 set database($bm) $strq
412 proc write_database {} {
413 global database rows database_fn database_header
415 foreach bm [array names database] {
416 set strq $database($bm)
417 set o "[lindex $bm 0]\n$strq\n"
418 foreach x [lrange $bm 1 end] { append o "$x\n" }
422 set f [open $database_fn.new w]
423 puts $f "$database_header\n$rows\n"
424 foreach o [lsort $ol] {
429 file rename -force $database_fn.new $database_fn
432 proc dbkey {ctx l r} {
435 for {set x $l} {$x <= $r} {incr x} {
436 lappend bm [format %x $wordmap($x)]
441 proc update_database/DEFINE {c0 c1 strq} {
442 global glyphsdone unk_l unk_contexts wordmap database
443 if {$c0 > $c1} { manyset [list $c0 $c1] c1 c0 }
445 set ncontexts $unk_contexts
447 foreach {l r contexts got} $glyphsdone {
448 if {$l==$c0} { set ncontexts $contexts; break }
450 if {![info exists ncontexts]} {
451 puts stderr "must start at letter LHS!"
456 foreach c $ncontexts {
457 set bm [dbkey $c $c0 $c1]
458 set database($bm) $strq
463 proc update_database/DELETE {l r ctxs} {
466 set bm [dbkey $ctx $l $r]
467 catch { unset database($bm) }
473 proc RETURN_RESULT {how what} {
475 place forget .d.csr.csr
476 pack forget .d.csr.csr.e
477 helptext {{{ Processing }}}
481 eval update_database/$how $what
485 #---------- main progrm ----------
488 global glyphsdone unk_l unk_r unk_contexts
497 set unk_contexts Test
499 set f [open text.xpm]
512 global glyphsdone unk_l unk_r unk_contexts
514 if {[gets stdin l]<0} {
515 if {[eof stdin]} { fconfigure stdin -blocking yes; exit 0 }
519 manyset [lrange $l 0 3] unk_l unk_r unk_contexts
520 set glyphsdone [lrange $l 3 end]
523 fileevent stdin readable {}
524 fconfigure stdin -blocking yes
533 proc main/automatic {} {
534 fconfigure stdin -blocking no
535 fileevent stdin readable required
537 proc done/automatic {} {
538 exec sh -c {printf \\0 >&4}
546 switch -exact -- $arg {
547 {--debug} { proc debug {m} { puts stderr "SHOW-THING $m" } }
549 {--automatic-1} { set mainkind automatic }
550 {--automatic*} { error "incompatible versions - install problem" }
551 default { error "huh $argv ?" }