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
86 pack .d.csr .d.mi .d.got .d.ctx -side top
93 proc show_context {maxhv x ctxs} {
97 if {[llength $ctxs]==1} { set fg blue } { set fg yellow }
98 label $w -bg black -fg $fg -text [join $ctxs "/\n"] -justify left
99 place $w -x [expr {($x-1)*$mul}] -y 0
100 set wh [winfo reqheight $w]
101 if {$wh > $maxh} { set maxh $wh }
104 proc resize_widgets {} {
105 global mulcols mulrows csrh gotsh ctxh glyphsdone
106 global unk_l unk_contexts
108 foreach w {.d.csr .d.got .d.ctx} {
109 $w configure -width $mulcols
111 #.d configure -height [expr {$csrh+$mulrows+$gotsh+$ctxh}]
113 .d.mi.csr_$w configure -height $mulrows
116 eval destroy [winfo children .d.ctx]
119 foreach {min max contexts got} $glyphsdone {
120 show_context maxh $min $contexts
122 show_context maxh $unk_l $unk_contexts
123 .d.ctx configure -height $maxh
127 #---------- xpm input processor ----------
130 global glyphsdone mul inter rhsmost_max unk_l unk_r mulcols mulrows
131 global cols rows wordmap
136 if {[gets $f l] < 0} { error "huh? "}
137 if {![regexp {^"(.*)",$} $l dummy l]} {
139 if {[regexp {^\}\;$} $l]} break
143 manyset $l cols rows colours cpp
144 if {$colours!=2 || $cpp!=1} { error "$l ?" }
146 set chop_l [expr {$unk_l - 80}]
147 set chop_r [expr {$cols - $unk_l - 100}]
148 if {$chop_l<0} { set chop_l 0 }
150 set unk_l [expr {$unk_l - $chop_l}]
151 set unk_r [expr {$unk_r - $chop_l}]
153 foreach {min max contexts got} $glyphsdone {
155 [expr {$min-$chop_l}] \
156 [expr {$max-$chop_l}] \
162 set cols [expr {$cols - $chop_l - $chop_r}]
163 debug "NOW cols=$cols chop_l,r=$chop_l,$chop_r rows=$rows\
166 set mulcols [expr {$cols*$mul+$inter}]
167 set mulrows [expr {$rows*$mul+$inter}]
168 append o "\"$mulcols $mulrows 9 1\",\n"
169 for {set x 0} {$x<$cols} {incr x} { set wordmap($x) 0 }
170 } elseif {$y==-2} { # first pixel
181 } elseif {$y==-1} { # 2nd pixel but we've already printed ours
183 set ybit [expr {1<<$y}]
188 set l [string range $l $chop_l end-$chop_r]
190 set l [string range $l $chop_l end]
191 append l [string repeat " " [expr -$chop_r]]
193 foreach c [split $l ""] {
195 if {$x >= $unk_l && $x <= $unk_r} {
199 foreach {min max contexts got} $glyphsdone {
201 if {$x >= $min && $x <= $max} {
202 set how [lindex {a b} $ab]
211 set p [string toupper $how]
212 incr wordmap($x) $ybit
214 default { error "$c ?" }
216 append ol "[string repeat $p [expr {$mul-$inter}]][
217 string repeat + $inter]"
218 append olh [string repeat + $mul]
224 set olhn [string repeat $olh $inter]
225 if {!$y} { append o $olhn }
226 append o [string repeat $ol [expr {$mul-1}]]
231 set data [exec xpmtoppm << $o]
232 image create photo image/main -data $data
236 #---------- per-invocation display ----------
238 proc draw_glyphsdone {} {
239 global glyphsdone mul inter
240 eval destroy [winfo children .d.got]
241 foreach {min max contexts got} $glyphsdone {
242 frame .d.got.m$min -bd 0 -background \#888
243 label .d.got.m$min.l -text "$got" -fg white -bg black -bd 0
244 pack .d.got.m$min.l -padx 1 -pady 1
245 place .d.got.m$min -x [expr {$min*$mul+$inter}] -y 0
249 proc startup_cursor {} {
250 global cur_already cur_mode cur_0 cur_1 last_ht
251 global glyphsdone unk_l unk_r
253 set cur_already [expr {[llength $glyphsdone]/4-1}]
254 set cur_mode 1 ;# one of: 0 1 already text
257 set cur_1 [expr {$unk_r+1}]
264 #---------- runtime display and keystroke handling ----------
268 if {![string compare $t $last_ht]} return
269 eval destroy [grid slaves .help]
270 set y 0; foreach l $t {
271 set x 0; foreach c $l {
272 set w .help.at${x}x${y}
274 grid $w -row $y -column $x -padx 5
282 proc recursor/0 {} { recursor//01 0 }
283 proc recursor/1 {} { recursor//01 1 }
284 proc recursor//01 {z1} {
285 global mul rhsmost_max cols glyphsdone
287 .d.csr.csr.l configure -text {adjust}
288 place .d.csr.csr -x [expr {$cur*$mul - 7}]
289 bind_key space { othercursor }
290 bind_leftright_q cur_$z1 0 [expr {$cols-1}]
291 if {[llength $glyphsdone]} {
292 bind_key Tab { set cur_mode already; recursor }
297 if {$cur_0 != $cur_1} {
298 .d.csr.csr.e delete 0 end
304 {{<- ->} {move cursor, adjusting area to define}}
305 {Space {switch to moving other cursor}}
306 {Return {confirm location, enter letter(s)}}
307 {Tab {switch to correcting earlier ocr}}
308 {Q {quit and abandon OCR run}}
311 proc othercursor {} {
313 set cur_mode [expr {!$cur_mode}]
317 proc recursor/text {} {
319 {Return {confirm entry of new glyph}}
320 {Escape {abandon entry}}
323 .d.csr.csr.l configure -text {define:}
324 pack .d.csr.csr.e -side left
327 set strq [.d.csr.csr.e get]
328 if {[regexp {^(?:[!-[]|[]-~]|\\\\|\\x[0-9a-f]{2})+} $strq]} {
329 RETURN_RESULT DEFINE "$cur_0 $cur_1 $strq"
334 pack forget .d.csr.csr.e
340 proc recursor/already {} {
343 global cur_already mul
344 global glyphsdone cur_already mul
345 .d.csr.csr.l configure -text {correct}
346 set rmax [lindex $glyphsdone [expr {$cur_already*4}]]
347 place .d.csr.csr -x [expr {$rmax*$mul-3}]
350 bind_leftright_q cur_already 0 [expr {[llength $glyphsdone]/4-1}]
351 bind_key Tab { bind_key Delete {}; set cur_mode 1; recursor }
353 RETURN_RESULT DELETE [lrange $glyphsdone \
354 [expr $cur_already*4] \
355 [expr $cur_already*4+2]]
358 {{<- ->} {move cursor, selecting glyph to correct}}
359 {Del {clear this glyph from the recognition database}}
360 {Tab {switch to selecting area to define as new glyph}}
361 {Q {quit and abandon OCR run}}
365 proc bind_key {k proc} {
367 bind . <Key-$k> $proc
368 set keybindings($k) [expr {!![string length $proc]}]
370 proc unbind_all_keys {} {
372 foreach k [array names keybindings] { bind_key $k {} }
375 proc bind_leftright_q {var min max} {
376 bind_key Left [list leftright $var $min $max -1]
377 bind_key Right [list leftright $var $min $max +1]
379 puts stderr "\nCharacter resolver quitting as you requested."
383 proc leftright {var min max inc} {
387 if {$vnew < $min || $vnew > $max} return
393 global csrh cur_mode cur_0 cur_1 mul
395 place .d.mi.csr_$z1 -y 0 -x [expr {[set cur_$z1] * $mul}]
401 #---------- database read and write ----------
406 # <context> <ncharacters> <hex>...
410 # $database($context 0x<bits> 0x<bits>...) = $hex
412 set database_header {# ypp-sc-tools pctb font v1}
415 if {[gets $f l] < 0} { error "unexpected db eof" }
419 proc read_database {} {
420 global database database_header rows database_fn
421 catch { unset database }
422 set database_fn ./charset-$rows.txt
423 if {![file exists $database_fn]} return
424 set f [open $database_fn r]
425 if {[string compare [db_getsl $f] $database_header]} { error "$l ?" }
426 if {([db_getsl $f])+0 != $rows} { error "wrong h ?" }
428 set context [db_getsl $f]
429 if {![string length $context]} continue
430 if {[regexp {^\#} $context]} continue
431 if {![string compare . $context]} break
434 set strq [db_getsl $f]
437 if {![string length $l]} break
438 lappend bm [format %x 0x$l]
440 set database($bm) $strq
445 proc write_database {} {
446 global database rows database_fn database_header
448 foreach bm [array names database] {
449 set strq $database($bm)
450 set o "[lindex $bm 0]\n$strq\n"
451 foreach x [lrange $bm 1 end] { append o "$x\n" }
455 set f [open $database_fn.new w]
456 puts $f "$database_header\n$rows\n"
457 foreach o [lsort $ol] {
462 file rename -force $database_fn.new $database_fn
465 proc dbkey {ctx l r} {
468 for {set x $l} {$x <= $r} {incr x} {
469 lappend bm [format %x $wordmap($x)]
474 proc update_database/DEFINE {c0 c1 strq} {
475 global glyphsdone unk_l unk_contexts wordmap database
476 if {$c0 > $c1} { manyset [list $c0 $c1] c1 c0 }
478 set ncontexts $unk_contexts
480 foreach {l r contexts got} $glyphsdone {
481 if {$l==$c0} { set ncontexts $contexts; break }
483 if {![info exists ncontexts]} {
484 puts stderr "must start at letter LHS!"
489 foreach c $ncontexts {
490 set bm [dbkey $c $c0 $c1]
491 set database($bm) $strq
496 proc update_database/DELETE {l r ctxs} {
499 set bm [dbkey $ctx $l $r]
500 catch { unset database($bm) }
506 proc RETURN_RESULT {how what} {
508 place forget .d.csr.csr
509 pack forget .d.csr.csr.e
510 helptext {{{ Processing }}}
514 eval update_database/$how $what
518 #---------- main progrm ----------
520 proc main/default {} {
521 puts stderr "Do not run this program directly."
524 global glyphsdone unk_l unk_r unk_contexts
533 set unk_contexts Test
535 set f [open text.xpm]
544 proc done/default {} {
548 global glyphsdone unk_l unk_r unk_contexts
550 if {[gets stdin l]<0} {
551 if {[eof stdin]} { fconfigure stdin -blocking yes; exit 0 }
555 manyset [lrange $l 0 3] unk_l unk_r unk_contexts
556 set glyphsdone [lrange $l 3 end]
559 fileevent stdin readable {}
560 fconfigure stdin -blocking yes
569 proc main/automatic {} {
570 fconfigure stdin -blocking no
571 fileevent stdin readable required
573 proc done/automatic {} {
574 exec sh -c {printf \\0 >&4}
582 switch -exact -- $arg {
583 {--debug} { proc debug {m} { puts stderr "SHOW-THING $m" } }
585 {--automatic-1} { set mainkind automatic }
586 {--automatic*} { error "incompatible versions - install problem" }
587 default { error "huh $argv ?" }