From f364c130b3b7d2e46e6cb57e4ff39a59fb54b226 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 20 Jun 2009 10:10:29 +0100 Subject: [PATCH] Rearrange and tidy up innards of yppsc-ocr-resolver --- pctb/yppsc-ocr-resolver | 510 ++++++++++++++++++++-------------------- 1 file changed, 256 insertions(+), 254 deletions(-) diff --git a/pctb/yppsc-ocr-resolver b/pctb/yppsc-ocr-resolver index 529fa4d..7aa7077 100755 --- a/pctb/yppsc-ocr-resolver +++ b/pctb/yppsc-ocr-resolver @@ -35,6 +35,8 @@ # if it wrote a byte to fd 4, it can take another question +#---------- library routines ---------- + proc manyset {list args} { foreach val $list var $args { upvar 1 $var my @@ -42,6 +44,10 @@ proc manyset {list args} { } } +proc must_gets {f lvar} { + upvar 1 $lvar l + if {[gets $f l] < 0} { error "huh?" } +} #---------- display core ---------- @@ -94,17 +100,6 @@ static unsigned char csr_bits[] = { pack .help } -proc show_context {maxhv x ctxs} { - global mul - upvar 1 $maxhv maxh - set w .d.ctx.at$x - if {[llength $ctxs]==1} { set fg blue } { set fg yellow } - label $w -bg black -fg $fg -text [join $ctxs "/\n"] -justify left - place $w -x [expr {($x-1)*$mul}] -y 0 - set wh [winfo reqheight $w] - if {$wh > $maxh} { set maxh $wh } -} - proc resize_widgets_core {} { global mulcols mulrows csrh gotsh ctxh glyphsdone global unk_l unk_contexts @@ -116,15 +111,247 @@ proc resize_widgets_core {} { eval destroy [winfo children .d.ctx] } +set last_ht {} -#---------- xpm input processor ---------- +proc helptext {t} { + global last_ht + if {![string compare $t $last_ht]} return + eval destroy [grid slaves .help] + set y 0; foreach l $t { + set x 0; foreach c $l { + set w .help.at${x}x${y} + label $w -text $c + grid $w -row $y -column $x -padx 5 + incr x + } + incr y + } + set last_ht $t +} -proc must_gets {f lvar} { - upvar 1 $lvar l - if {[gets $f l] < 0} { error "huh?" } +proc bind_key {k proc} { + global keybindings + bind . $proc + set keybindings($k) [expr {!![string length $proc]}] +} +proc unbind_all_keys {} { + global keybindings + foreach k [array names keybindings] { bind_key $k {} } +} + +#---------- database read and write common wrapper ---------- + +proc db_getsl {f} { + if {[gets $f l] < 0} { error "unexpected db eof" } + return $l +} + +proc read_database {fn} { + global reqkind database database_fn + upvar #0 database_magic/$reqkind magic + catch { unset database } + + set database_fn $fn + if {![file exists $database_fn]} return + set f [open $database_fn r] + if {[string compare [db_getsl $f] $magic]} { error "$l $reqkind ?" } + + read_database_header/$reqkind $f + while 1 { + set l1 [db_getsl $f] + + if {![string length $l1]} continue + if {[regexp {^\#} $l1]} continue + if {![string compare . $l1]} break + + read_database_entry/$reqkind $f $l1 + } + close $f +} + +proc write_database {} { + global reqkind database_fn database + upvar #0 database_magic/$reqkind magic + + set f [open $database_fn.new w] + puts $f $magic + + write_database_header/$reqkind $f + + set ol {} + foreach bm [array names database] { + lappend ol [format_database_entry/$reqkind $bm $database($bm)] + } + foreach o [lsort $ol] { + puts $f $o + } + puts $f "." + close $f + file rename -force $database_fn.new $database_fn +} + +proc required/char {} { + global mulrows glyphsdone unk_l unk_r unk_contexts rows + + must_gets stdin l + + manyset [lrange $l 0 3] unk_l unk_r unk_contexts + set glyphsdone [lrange $l 3 end] + debug "GOT $l" + + char_read_xpm stdin + + resize_widgets_core + foreach w {0 1} { + .d.mi.csr_$w configure -height $mulrows + } + set maxh 0 + foreach {min max contexts got} $glyphsdone { + show_context maxh $min $contexts + } + show_context maxh $unk_l $unk_contexts + .d.ctx configure -height $maxh + pack forget .d.pe + pack .d.csr -side top -before .d.mi + pack .d.got .d.ctx -side top -after .d.mi + + read_database ./charset-$rows.txt + draw_glyphsdone + startup_cursor +} + +#========== PIXMAPS ========== + +#---------- pixmap database read and write ---------- + +set database_magic/pixmap {# ypp-sc-tools pctb pixmaps v1} + +proc read_database_header/pixmap {f} { } +proc read_database_entry/pixmap {f def} { + global database + + set im "" + + set p3 [db_getsl $f]; append im $p3 "\n" + if {[string compare $p3 P3]} { error "$p3 ?" } + + set wh [db_getsl $f]; append im $wh "\n"; manyset $wh w h + set depth [db_getsl $f]; append im $depth "\n" + + for {set y 0} {$y < $h} {incr y} { + set line [db_getsl $f]; append im $line "\n" + } + set database($im) $def +} +proc write_database_header/pixmap {f} { puts $f "" } +proc format_database_entry/pixmap {im def} { + return "$def\n$im" +} + +#---------- pixmap display and input handling ---------- + +proc foreach_pixmap_col {var body} { + global alloptions + upvar 1 $var col + for {set col 0} {$col < [llength $alloptions]/3} {incr col} { + uplevel 1 $body + } +} + +proc pixmap_select {ncol} { + global alloptions + debug "PIX SELECT $ncol [llength $alloptions]" + foreach_pixmap_col col { + if {$col==$ncol} continue + .d.pe.grid.l$col selection clear 0 end + } + pixmap_maybe_ok +} +proc pixmap_maybe_ok {} { + global alloptions pixmap_selcol pixmap_selrow + set nsel 0 + foreach_pixmap_col col { + set cs [.d.pe.grid.l$col curselection] + incr nsel [llength $cs] + set pixmap_selcol $col + set pixmap_selrow [lindex $cs 0] + } + if {$nsel==1} { + .d.pe.ok configure -state normal -command pixmap_ok + } else { + .d.pe.ok configure -state disabled -command {} + } +} +proc pixmap_ok {} { + global database ppm pixmap_selcol pixmap_selrow mainkind alloptions + foreach_pixmap_col col { + .d.pe.grid.l$col configure -state disabled + } + .d.pe.ok configure -state disabled + helptext {{{ Processing }}} + manyset [lrange $alloptions [expr {$pixmap_selcol*3}] end] \ + colname coldesc rows + manyset [lrange $rows [expr {$pixmap_selrow*2}] end] \ + rowname rowdesc + set result "$colname - $rowname" + debug "UPDATE PIXMAP AS >$result<" + set database($ppm) $result + write_database + done/$mainkind +} + +proc required/pixmap {} { + global unk_what ppm mulcols alloptions + must_gets stdin unk_what + debug "GOT pixmap $unk_what" + set ppm {} + while 1 { + must_gets stdin ppml + if {![string length $ppml]} break + append ppm $ppml "\n" + } + set data [exec pnmscale 2 << $ppm] + image create photo image/main -data $data + + set alloptions [exec ./yppsc-resolver-pixoptions $unk_what] + + read_database ./pixmaps.txt + + set mulcols [image width image/main] + set mulrows [image height image/main] + resize_widgets_core + place forget .d.mi.csr_0 + place forget .d.mi.csr_1 + + pack forget .d.csr .d.got + pack .d.pe -side top -before .d.mi -pady 10 + + eval destroy [winfo children .d.pe.grid] + set col 0; foreach {colname coldesc rows} $alloptions { + debug "INIT $col $colname \"$coldesc\"" + label .d.pe.grid.t$col -text $colname + listbox .d.pe.grid.l$col + foreach {rowname rowdesc} $rows { + debug "INIT $col $colname \"$coldesc\" $rowname \"$rowdesc\"" + .d.pe.grid.l$col insert end $rowdesc + } + bind .d.pe.grid.l$col <> [list pixmap_select $col] + grid .d.pe.grid.t$col -column $col -row 0 + grid .d.pe.grid.l$col -column $col -row 1 + incr col + } + pixmap_maybe_ok + + helptext { + {{Indicate the correct parse of this image, and click OK.}} + } } -proc read_xpm {f} { +#========== CHARACTER SET ========== + +#---------- xpm input processor ---------- + +proc char_read_xpm {f} { global glyphsdone mul inter rhsmost_max unk_l unk_r mulcols mulrows global cols rows wordmap @@ -230,8 +457,18 @@ proc read_xpm {f} { image create photo image/main -data $data } +#---------- character set editor display ---------- -#---------- per-invocation display ---------- +proc show_context {maxhv x ctxs} { + global mul + upvar 1 $maxhv maxh + set w .d.ctx.at$x + if {[llength $ctxs]==1} { set fg blue } { set fg yellow } + label $w -bg black -fg $fg -text [join $ctxs "/\n"] -justify left + place $w -x [expr {($x-1)*$mul}] -y 0 + set wh [winfo reqheight $w] + if {$wh > $maxh} { set maxh $wh } +} proc draw_glyphsdone {} { global glyphsdone mul inter @@ -257,26 +494,7 @@ proc startup_cursor {} { recursor } - -#---------- runtime display and keystroke handling ---------- - -set last_ht {} - -proc helptext {t} { - global last_ht - if {![string compare $t $last_ht]} return - eval destroy [grid slaves .help] - set y 0; foreach l $t { - set x 0; foreach c $l { - set w .help.at${x}x${y} - label $w -text $c - grid $w -row $y -column $x -padx 5 - incr x - } - incr y - } - set last_ht $t -} +#---------- character set runtime display and keystroke handling ---------- proc recursor/0 {} { recursor//01 0 } proc recursor/1 {} { recursor//01 1 } @@ -361,16 +579,6 @@ proc recursor/already {} { } } -proc bind_key {k proc} { - global keybindings - bind . $proc - set keybindings($k) [expr {!![string length $proc]}] -} -proc unbind_all_keys {} { - global keybindings - foreach k [array names keybindings] { bind_key $k {} } -} - proc bind_leftright_q {var min max} { bind_key Left [list leftright $var $min $max -1] bind_key Right [list leftright $var $min $max +1] @@ -396,83 +604,6 @@ proc recursor {} { recursor/$cur_mode } -#---------- database read and write common wrapper ---------- - -proc db_getsl {f} { - if {[gets $f l] < 0} { error "unexpected db eof" } - return $l -} - -proc read_database {fn} { - global reqkind database database_fn - upvar #0 database_magic/$reqkind magic - catch { unset database } - - set database_fn $fn - if {![file exists $database_fn]} return - set f [open $database_fn r] - if {[string compare [db_getsl $f] $magic]} { error "$l $reqkind ?" } - - read_database_header/$reqkind $f - while 1 { - set l1 [db_getsl $f] - - if {![string length $l1]} continue - if {[regexp {^\#} $l1]} continue - if {![string compare . $l1]} break - - read_database_entry/$reqkind $f $l1 - } - close $f -} - -proc write_database {} { - global reqkind database_fn database - upvar #0 database_magic/$reqkind magic - - set f [open $database_fn.new w] - puts $f $magic - - write_database_header/$reqkind $f - - set ol {} - foreach bm [array names database] { - lappend ol [format_database_entry/$reqkind $bm $database($bm)] - } - foreach o [lsort $ol] { - puts $f $o - } - puts $f "." - close $f - file rename -force $database_fn.new $database_fn -} - -#---------- pixmap database read and write ---------- - -set database_magic/pixmap {# ypp-sc-tools pctb pixmaps v1} - -proc read_database_header/pixmap {f} { } -proc read_database_entry/pixmap {f def} { - global database - - set im "" - - set p3 [db_getsl $f]; append im $p3 "\n" - if {[string compare $p3 P3]} { error "$p3 ?" } - - set wh [db_getsl $f]; append im $wh "\n"; manyset $wh w h - set depth [db_getsl $f]; append im $depth "\n" - - for {set y 0} {$y < $h} {incr y} { - set line [db_getsl $f]; append im $line "\n" - } - set database($im) $def -} -proc write_database_header/pixmap {f} { puts $f "" } -proc format_database_entry/pixmap {im def} { - return "$def\n$im" -} - #---------- character database read and write ---------- # OUT OF DATE @@ -552,7 +683,6 @@ proc update_database/DELETE {l r ctxs} { write_database } - proc RETURN_RESULT {how what} { global mainkind place forget .d.csr.csr @@ -565,7 +695,7 @@ proc RETURN_RESULT {how what} { done/$mainkind } -#---------- main program ---------- +#========== main program ========== proc main/default {} { puts stderr "Do not run this program directly." @@ -589,134 +719,6 @@ proc required {} { required/$reqkind } - -proc foreach_pixmap_col {var body} { - global alloptions - upvar 1 $var col - for {set col 0} {$col < [llength $alloptions]/3} {incr col} { - uplevel 1 $body - } -} - -proc pixmap_select {ncol} { - global alloptions - debug "PIX SELECT $ncol [llength $alloptions]" - foreach_pixmap_col col { - if {$col==$ncol} continue - .d.pe.grid.l$col selection clear 0 end - } - pixmap_maybe_ok -} -proc pixmap_maybe_ok {} { - global alloptions pixmap_selcol pixmap_selrow - set nsel 0 - foreach_pixmap_col col { - set cs [.d.pe.grid.l$col curselection] - incr nsel [llength $cs] - set pixmap_selcol $col - set pixmap_selrow [lindex $cs 0] - } - if {$nsel==1} { - .d.pe.ok configure -state normal -command pixmap_ok - } else { - .d.pe.ok configure -state disabled -command {} - } -} -proc pixmap_ok {} { - global database ppm pixmap_selcol pixmap_selrow mainkind alloptions - foreach_pixmap_col col { - .d.pe.grid.l$col configure -state disabled - } - .d.pe.ok configure -state disabled - helptext {{{ Processing }}} - manyset [lrange $alloptions [expr {$pixmap_selcol*3}] end] \ - colname coldesc rows - manyset [lrange $rows [expr {$pixmap_selrow*2}] end] \ - rowname rowdesc - set result "$colname - $rowname" - debug "UPDATE PIXMAP AS >$result<" - set database($ppm) $result - write_database - done/$mainkind -} - -proc required/pixmap {} { - global unk_what ppm mulcols alloptions - must_gets stdin unk_what - debug "GOT pixmap $unk_what" - set ppm {} - while 1 { - must_gets stdin ppml - if {![string length $ppml]} break - append ppm $ppml "\n" - } - set data [exec pnmscale 2 << $ppm] - image create photo image/main -data $data - - set alloptions [exec ./yppsc-resolver-pixoptions $unk_what] - - read_database ./pixmaps.txt - - set mulcols [image width image/main] - set mulrows [image height image/main] - resize_widgets_core - place forget .d.mi.csr_0 - place forget .d.mi.csr_1 - - pack forget .d.csr .d.got - pack .d.pe -side top -before .d.mi -pady 10 - - eval destroy [winfo children .d.pe.grid] - set col 0; foreach {colname coldesc rows} $alloptions { - debug "INIT $col $colname \"$coldesc\"" - label .d.pe.grid.t$col -text $colname - listbox .d.pe.grid.l$col - foreach {rowname rowdesc} $rows { - debug "INIT $col $colname \"$coldesc\" $rowname \"$rowdesc\"" - .d.pe.grid.l$col insert end $rowdesc - } - bind .d.pe.grid.l$col <> [list pixmap_select $col] - grid .d.pe.grid.t$col -column $col -row 0 - grid .d.pe.grid.l$col -column $col -row 1 - incr col - } - pixmap_maybe_ok - - helptext { - {{Indicate the correct parse of this image, and click OK.}} - } -} - -proc required/char {} { - global mulrows glyphsdone unk_l unk_r unk_contexts rows - - must_gets stdin l - - manyset [lrange $l 0 3] unk_l unk_r unk_contexts - set glyphsdone [lrange $l 3 end] - debug "GOT $l" - - read_xpm stdin - - resize_widgets_core - foreach w {0 1} { - .d.mi.csr_$w configure -height $mulrows - } - set maxh 0 - foreach {min max contexts got} $glyphsdone { - show_context maxh $min $contexts - } - show_context maxh $unk_l $unk_contexts - .d.ctx configure -height $maxh - pack forget .d.pe - pack .d.csr -side top -before .d.mi - pack .d.got .d.ctx -side top -after .d.mi - - read_database ./charset-$rows.txt - draw_glyphsdone - startup_cursor -} - proc main/automatic {} { fconfigure stdin -blocking no fileevent stdin readable required -- 2.30.2