3 # helper program for OCR in PCTB upload client
5 # This is part of ypp-sc-tools, a set of third-party tools for assisting
6 # players of Yohoho Puzzle Pirates.
8 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
24 # are used without permission. This program is not endorsed or
25 # sponsored by Three Rings.
30 # run this without args
32 # one line which is a Tcl list for unk_{l,r} unk_contexts glyphsdone etc.
33 # the xpm in the format expected
34 # then expect child to exit 0, or write a single 0 byte to fd 4
35 # if it wrote a byte to fd 4, it can take another question
38 proc manyset {list args} {
39 foreach val $list var $args {
46 #---------- display core ----------
55 proc init_widgets {} {
57 global csrh gotsh ctxh
59 if {[winfo exists .d]} return
63 image create bitmap image/main
64 label .d.mi -image image/main -borderwidth 0
66 frame .d.csr -bg black -height $csrh
67 frame .d.got -bg black -height $gotsh
68 frame .d.ctx -bg black
70 image create bitmap image/cursor -data \
73 static unsigned char csr_bits[] = {
74 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x21, 0x04, 0x22, 0x02, 0x25, 0x05,
75 0xaa, 0x02, 0x74, 0x01, 0xa8, 0x00, 0x70, 0x00, 0x20, 0x00};
79 label .d.csr.csr.l -image image/cursor -compound left
80 entry .d.csr.csr.e -bd 0
81 pack .d.csr.csr.l -side left
83 frame .d.mi.csr_0 -bg white -width 1
84 frame .d.mi.csr_1 -bg white -width 1
87 button .d.pe.ok -text OK
88 pack .d.pe.grid .d.pe.ok -side left
90 pack .d.mi .d.ctx -side top
97 proc show_context {maxhv x ctxs} {
101 if {[llength $ctxs]==1} { set fg blue } { set fg yellow }
102 label $w -bg black -fg $fg -text [join $ctxs "/\n"] -justify left
103 place $w -x [expr {($x-1)*$mul}] -y 0
104 set wh [winfo reqheight $w]
105 if {$wh > $maxh} { set maxh $wh }
108 proc resize_widgets_core {} {
109 global mulcols mulrows csrh gotsh ctxh glyphsdone
110 global unk_l unk_contexts
112 foreach w {.d.csr .d.got .d.ctx} {
113 $w configure -width $mulcols
116 eval destroy [winfo children .d.ctx]
120 #---------- xpm input processor ----------
122 proc must_gets {f lvar} {
124 if {[gets $f l] < 0} { error "huh?" }
128 global glyphsdone mul inter rhsmost_max unk_l unk_r mulcols mulrows
129 global cols rows wordmap
135 if {![regexp {^"(.*)",$} $l dummy l]} {
137 if {[regexp {^\}\;$} $l]} break
141 manyset $l cols rows colours cpp
142 if {$colours!=2 || $cpp!=1} { error "$l ?" }
144 set chop_l [expr {$unk_l - 80}]
145 set chop_r [expr {$cols - $unk_l - 100}]
146 if {$chop_l<0} { set chop_l 0 }
148 set unk_l [expr {$unk_l - $chop_l}]
149 set unk_r [expr {$unk_r - $chop_l}]
151 foreach {min max contexts got} $glyphsdone {
153 [expr {$min-$chop_l}] \
154 [expr {$max-$chop_l}] \
160 set cols [expr {$cols - $chop_l - $chop_r}]
161 debug "NOW cols=$cols chop_l,r=$chop_l,$chop_r rows=$rows\
164 set mulcols [expr {$cols*$mul+$inter}]
165 set mulrows [expr {$rows*$mul+$inter}]
166 append o "\"$mulcols $mulrows 9 1\",\n"
167 for {set x 0} {$x<$cols} {incr x} { set wordmap($x) 0 }
168 } elseif {$y==-2} { # first pixel
179 } elseif {$y==-1} { # 2nd pixel but we've already printed ours
181 set ybit [expr {1<<$y}]
186 set l [string range $l $chop_l end-$chop_r]
188 set l [string range $l $chop_l end]
189 append l [string repeat " " [expr -$chop_r]]
191 foreach c [split $l ""] {
193 if {$x >= $unk_l && $x <= $unk_r} {
197 foreach {min max contexts got} $glyphsdone {
199 if {$x >= $min && $x <= $max} {
200 set how [lindex {a b} $ab]
209 set p [string toupper $how]
210 incr wordmap($x) $ybit
212 default { error "$c ?" }
214 append ol "[string repeat $p [expr {$mul-$inter}]][
215 string repeat + $inter]"
216 append olh [string repeat + $mul]
222 set olhn [string repeat $olh $inter]
223 if {!$y} { append o $olhn }
224 append o [string repeat $ol [expr {$mul-1}]]
229 set data [exec xpmtoppm << $o]
230 image create photo image/main -data $data
234 #---------- per-invocation display ----------
236 proc draw_glyphsdone {} {
237 global glyphsdone mul inter
238 eval destroy [winfo children .d.got]
239 foreach {min max contexts got} $glyphsdone {
240 frame .d.got.m$min -bd 0 -background \#888
241 label .d.got.m$min.l -text "$got" -fg white -bg black -bd 0
242 pack .d.got.m$min.l -padx 1 -pady 1
243 place .d.got.m$min -x [expr {$min*$mul+$inter}] -y 0
247 proc startup_cursor {} {
248 global cur_already cur_mode cur_0 cur_1 last_ht
249 global glyphsdone unk_l unk_r
251 set cur_already [expr {[llength $glyphsdone]/4-1}]
252 set cur_mode 1 ;# one of: 0 1 already text
255 set cur_1 [expr {$unk_r+1}]
261 #---------- runtime display and keystroke handling ----------
267 if {![string compare $t $last_ht]} return
268 eval destroy [grid slaves .help]
269 set y 0; foreach l $t {
270 set x 0; foreach c $l {
271 set w .help.at${x}x${y}
273 grid $w -row $y -column $x -padx 5
281 proc recursor/0 {} { recursor//01 0 }
282 proc recursor/1 {} { recursor//01 1 }
283 proc recursor//01 {z1} {
284 global mul rhsmost_max cols glyphsdone
286 .d.csr.csr.l configure -text {adjust}
287 place .d.csr.csr -x [expr {$cur*$mul - 7}]
288 bind_key space { othercursor }
289 bind_leftright_q cur_$z1 0 [expr {$cols-1}]
290 if {[llength $glyphsdone]} {
291 bind_key Tab { set cur_mode already; recursor }
296 if {$cur_0 != $cur_1} {
297 .d.csr.csr.e delete 0 end
303 {{<- ->} {move cursor, adjusting area to define}}
304 {Space {switch to moving other cursor}}
305 {Return {confirm location, enter letter(s)}}
306 {Tab {switch to correcting earlier ocr}}
307 {Q {quit and abandon OCR run}}
310 proc othercursor {} {
312 set cur_mode [expr {!$cur_mode}]
316 proc recursor/text {} {
318 {Return {confirm entry of new glyph}}
319 {Escape {abandon entry}}
322 .d.csr.csr.l configure -text {define:}
323 pack .d.csr.csr.e -side left
326 set strq [.d.csr.csr.e get]
327 if {[regexp {^(?:[!-[]|[]-~]|\\\\|\\x[0-9a-f]{2})+} $strq]} {
328 RETURN_RESULT DEFINE "$cur_0 $cur_1 $strq"
333 pack forget .d.csr.csr.e
339 proc recursor/already {} {
342 global cur_already mul
343 global glyphsdone cur_already mul
344 .d.csr.csr.l configure -text {correct}
345 set rmax [lindex $glyphsdone [expr {$cur_already*4}]]
346 place .d.csr.csr -x [expr {$rmax*$mul-3}]
349 bind_leftright_q cur_already 0 [expr {[llength $glyphsdone]/4-1}]
350 bind_key Tab { bind_key Delete {}; set cur_mode 1; recursor }
352 RETURN_RESULT DELETE [lrange $glyphsdone \
353 [expr $cur_already*4] \
354 [expr $cur_already*4+2]]
357 {{<- ->} {move cursor, selecting glyph to correct}}
358 {Del {clear this glyph from the recognition database}}
359 {Tab {switch to selecting area to define as new glyph}}
360 {Q {quit and abandon OCR run}}
364 proc bind_key {k proc} {
366 bind . <Key-$k> $proc
367 set keybindings($k) [expr {!![string length $proc]}]
369 proc unbind_all_keys {} {
371 foreach k [array names keybindings] { bind_key $k {} }
374 proc bind_leftright_q {var min max} {
375 bind_key Left [list leftright $var $min $max -1]
376 bind_key Right [list leftright $var $min $max +1]
378 puts stderr "\nCharacter resolver quitting as you requested."
382 proc leftright {var min max inc} {
386 if {$vnew < $min || $vnew > $max} return
392 global csrh cur_mode cur_0 cur_1 mul
394 place .d.mi.csr_$z1 -y 0 -x [expr {[set cur_$z1] * $mul}]
399 #---------- database read and write common wrapper ----------
402 if {[gets $f l] < 0} { error "unexpected db eof" }
406 proc read_database {fn} {
407 global reqkind database database_fn
408 upvar #0 database_magic/$reqkind magic
409 catch { unset database }
412 if {![file exists $database_fn]} return
413 set f [open $database_fn r]
414 if {[string compare [db_getsl $f] $magic]} { error "$l $reqkind ?" }
416 read_database_header/$reqkind $f
420 if {![string length $l1]} continue
421 if {[regexp {^\#} $l1]} continue
422 if {![string compare . $l1]} break
424 read_database_entry/$reqkind $f $l1
429 proc write_database {} {
430 global reqkind database_fn database
431 upvar #0 database_magic/$reqkind magic
433 set f [open $database_fn.new w]
436 write_database_header/$reqkind $f
439 foreach bm [array names database] {
440 append ol [format_database_entry/$reqkind $bm $database($bm)]
442 foreach o [lsort $ol] {
447 file rename -force $database_fn.new $database_fn
450 #---------- pixmap database read and write ----------
452 set database_magic/pixmap {# ypp-sc-tools pctb pixmaps v1}
454 proc read_database_header/pixmap {f} { }
455 proc read_database_entry/pixmap {f def} {
460 set p3 [db_getsl $f]; append im $p3 "\n"
461 if {[string compare $p3 P3]} { error "$p3 ?" }
463 set wh [db_getsl $f]; append im $wh "\n"; manyset $wh w h
464 set depth [db_getsl $f]; append im $depth "\n"
466 for {set y 0} {$y < $h} {incr y} {
467 set line [db_getsl $f]; append im $line "\n"
469 set database($im) $def
471 proc write_database_header/pixmap {f} { }
472 proc format_database_entry/pixmap {im def} {
476 #---------- character database read and write ----------
481 # <context> <ncharacters> <hex>...
485 # $database($context 0x<bits> 0x<bits>...) = $hex
487 set database_magic/char {# ypp-sc-tools pctb font v1}
489 proc read_database_header/char {f} {
491 if {([db_getsl $f])+0 != $rows} { error "wrong h ?" }
493 proc read_database_entry/char {f context} {
496 set strq [db_getsl $f]
499 if {![string length $l]} break
500 lappend bm [format %x 0x$l]
502 set database($bm) $strq
505 proc write_database_header/char {f} {
508 proc format_database_entry/char {bm strq} {
510 set o "[lindex $bm 0]\n$strq\n"
511 foreach x [lrange $bm 1 end] { append o "$x\n" }
515 proc dbkey {ctx l r} {
518 for {set x $l} {$x <= $r} {incr x} {
519 lappend bm [format %x $wordmap($x)]
524 proc update_database/DEFINE {c0 c1 strq} {
525 global glyphsdone unk_l unk_contexts wordmap database
526 if {$c0 > $c1} { manyset [list $c0 $c1] c1 c0 }
528 set ncontexts $unk_contexts
530 foreach {l r contexts got} $glyphsdone {
531 if {$l==$c0} { set ncontexts $contexts; break }
533 if {![info exists ncontexts]} {
534 puts stderr "must start at letter LHS!"
539 foreach c $ncontexts {
540 set bm [dbkey $c $c0 $c1]
541 set database($bm) $strq
546 proc update_database/DELETE {l r ctxs} {
549 set bm [dbkey $ctx $l $r]
550 catch { unset database($bm) }
556 proc RETURN_RESULT {how what} {
558 place forget .d.csr.csr
559 pack forget .d.csr.csr.e
560 helptext {{{ Processing }}}
564 eval update_database/$how $what
568 #---------- main program ----------
570 proc main/default {} {
571 puts stderr "Do not run this program directly."
574 proc done/default {} {
580 fileevent stdin readable {}
581 fconfigure stdin -blocking yes
583 if {[gets stdin reqkind]<0} {
584 if {[eof stdin]} { fconfigure stdin -blocking yes; exit 0 }
593 proc foreach_pixmap_col {var body} {
596 for {set col 0} {$col < [llength $alloptions]/3} {incr col} {
601 proc pixmap_select {ncol} {
603 debug "PIX SELECT $ncol [llength $alloptions]"
604 foreach_pixmap_col col {
605 if {$col==$ncol} continue
606 .d.pe.grid.l$col selection clear 0 end
610 proc pixmap_maybe_ok {} {
611 global alloptions pixmap_selcol pixmap_selrow
613 foreach_pixmap_col col {
614 set cs [.d.pe.grid.l$col curselection]
615 incr nsel [llength $cs]
616 set pixmap_selcol $col
617 set pixmap_selrow [lindex $cs 0]
620 .d.pe.ok configure -state normal -command pixmap_ok
622 .d.pe.ok configure -state disabled -command {}
626 global database ppm pixmap_selcol pixmap_selrow mainkind alloptions
627 foreach_pixmap_col col {
628 .d.pe.grid.l$col configure -state disabled
630 .d.pe.ok configure -state disabled
631 helptext {{{ Processing }}}
632 manyset [lrange $alloptions [expr {$pixmap_selcol*3}] end] \
634 manyset [lrange $rows [expr {$pixmap_selrow*2}] end] \
636 set database($ppm) "$colname - $coldesc"
641 proc required/pixmap {} {
642 global unk_what ppm mulcols alloptions
643 must_gets stdin unk_what
644 debug "GOT pixmap $unk_what"
648 if {![string length $ppml]} break
649 append ppm $ppml "\n"
651 set data [exec pnmscale 2 << $ppm]
652 image create photo image/main -data $data
654 set alloptions [exec ./yppsc-resolver-pixoptions $unk_what]
656 read_database ./pixmaps.txt
658 set mulcols [image width image/main]
659 set mulrows [image height image/main]
661 place forget .d.mi.csr_0
662 place forget .d.mi.csr_1
664 pack forget .d.csr .d.got
665 pack .d.pe -side top -before .d.mi -pady 10
667 eval destroy [winfo children .d.pe.grid]
668 set col 0; foreach {colname coldesc rows} $alloptions {
669 debug "INIT $col $colname \"$coldesc\""
670 label .d.pe.grid.t$col -text $colname
671 listbox .d.pe.grid.l$col
672 foreach {rowname rowdesc} $rows {
673 debug "INIT $col $colname \"$coldesc\" $rowname \"$rowdesc\""
674 .d.pe.grid.l$col insert end $rowdesc
676 bind .d.pe.grid.l$col <<ListboxSelect>> [list pixmap_select $col]
677 grid .d.pe.grid.t$col -column $col -row 0
678 grid .d.pe.grid.l$col -column $col -row 1
684 {{Indicate the correct parse of this image, and click OK.}}
688 proc required/char {} {
689 global mulrows glyphsdone unk_l unk_r unk_contexts rows
693 manyset [lrange $l 0 3] unk_l unk_r unk_contexts
694 set glyphsdone [lrange $l 3 end]
701 .d.mi.csr_$w configure -height $mulrows
704 foreach {min max contexts got} $glyphsdone {
705 show_context maxh $min $contexts
707 show_context maxh $unk_l $unk_contexts
708 .d.ctx configure -height $maxh
710 pack .d.csr -side top -before .d.mi
711 pack .d.got .d.ctx -side top -after .d.mi
713 read_database ./charset-$rows.txt
718 proc main/automatic {} {
719 fconfigure stdin -blocking no
720 fileevent stdin readable required
722 proc done/automatic {} {
723 exec sh -c {printf \\0 >&4}
731 switch -exact -- $arg {
732 {--debug} { proc debug {m} { puts stderr "SHOW-THING $m" } }
734 {--automatic-1} { set mainkind automatic }
735 {--automatic*} { error "incompatible versions - install problem" }
736 default { error "huh $argv ?" }