chiark / gitweb /
Rename yppsc-ocr-resolver to dictionary-manager, and database and charset to dictiona...
[ypp-sc-tools.main.git] / pctb / dictionary-manager
1 #!/usr/bin/wish
2
3 # helper program for OCR in PCTB upload client
4
5 # This is part of ypp-sc-tools, a set of third-party tools for assisting
6 # players of Yohoho Puzzle Pirates.
7 #
8 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
9 #
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.
14 #
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.
19 #
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/>.
22 #
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.
26
27
28 # invocation:
29 # OUT OF DATE
30 #  run this without args
31 #  then on stdin write
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
36
37
38 #---------- library routines ----------
39
40 proc manyset {list args} {
41     foreach val $list var $args {
42         upvar 1 $var my
43         set my $val
44     }
45 }
46
47 proc must_gets {f lvar} {
48     upvar 1 $lvar l
49     if {[gets $f l] < 0} { error "huh?" }
50 }
51
52 #---------- display core ----------
53
54 set mul 6
55 set inter 1
56
57 set gotsh 20
58 set csrh 20
59 set ctxh 20
60
61 proc init_widgets {} {
62     # idempotent
63     global csrh gotsh ctxh
64
65     if {[winfo exists .d]} return
66     
67     frame .d
68
69     image create bitmap image/main
70     label .d.mi -image image/main -borderwidth 0
71
72     frame .d.csr -bg black -height $csrh
73     frame .d.got -bg black -height $gotsh
74     frame .d.ctx -bg black
75
76     image create bitmap image/cursor -data \
77 {#define csr_width 11
78 #define csr_height 11
79 static unsigned char csr_bits[] = {
80    0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x21, 0x04, 0x22, 0x02, 0x25, 0x05,
81    0xaa, 0x02, 0x74, 0x01, 0xa8, 0x00, 0x70, 0x00, 0x20, 0x00};
82 }
83
84     frame .d.csr.csr
85     label .d.csr.csr.l -image image/cursor -compound left
86     entry .d.csr.csr.e -bd 0
87     pack .d.csr.csr.l -side left
88
89     frame .d.mi.csr_0 -bg white -width 1
90     frame .d.mi.csr_1 -bg white -width 1
91     frame .d.pe
92     frame .d.pe.grid
93     button .d.pe.ok -text OK
94     pack .d.pe.grid .d.pe.ok -side left
95
96     pack .d.mi .d.ctx -side top
97     pack .d
98
99     frame .help
100     pack .help
101 }
102
103 proc resize_widgets_core {} {
104     global mulcols mulrows csrh gotsh ctxh glyphsdone
105     global unk_l unk_contexts
106     
107     foreach w {.d.csr .d.got .d.ctx} {
108         $w configure -width $mulcols
109     }
110
111     eval destroy [winfo children .d.ctx]
112 }
113
114 set last_ht {}
115
116 proc helptext {t} {
117     global last_ht
118     if {![string compare $t $last_ht]} return
119     eval destroy [grid slaves .help]
120     set y 0; foreach l $t {
121         set x 0; foreach c $l {
122             set w .help.at${x}x${y}
123             label $w -text $c
124             grid $w -row $y -column $x -padx 5
125             incr x
126         }
127         incr y
128     }
129     set last_ht $t
130 }
131
132 proc bind_key {k proc} {
133     global keybindings
134     bind . <Key-$k> $proc
135     set keybindings($k) [expr {!![string length $proc]}]
136 }
137 proc unbind_all_keys {} {
138     global keybindings
139     foreach k [array names keybindings] { bind_key $k {} }
140 }
141
142 #---------- database read and write common wrapper ----------
143
144 proc db_getsl {f} {
145     if {[gets $f l] < 0} { error "unexpected db eof" }
146     return $l
147 }
148
149 proc read_database {fn} {
150     global reqkind database database_fn
151     upvar #0 database_magic/$reqkind magic
152     catch { unset database }
153
154     set database_fn $fn
155     if {![file exists $database_fn]} return
156     set f [open $database_fn r]
157     if {[string compare [db_getsl $f] $magic]} { error "$l $reqkind ?" }
158
159     read_database_header/$reqkind $f
160     while 1 {
161         set l1 [db_getsl $f]
162     
163         if {![string length $l1]} continue
164         if {[regexp {^\#} $l1]} continue
165         if {![string compare . $l1]} break
166
167         read_database_entry/$reqkind $f $l1
168     }
169     close $f
170 }
171
172 proc write_database {} {
173     global reqkind database_fn database
174     upvar #0 database_magic/$reqkind magic
175     
176     set f [open $database_fn.new w]
177     puts $f $magic
178
179     write_database_header/$reqkind $f
180
181     set ol {}
182     foreach bm [array names database] {
183         lappend ol [format_database_entry/$reqkind $bm $database($bm)]
184     }
185     foreach o [lsort $ol] {
186         puts $f $o
187     }
188     puts $f "."
189     close $f
190     file rename -force $database_fn.new $database_fn
191 }
192
193 proc required/char {} {
194     global mulrows glyphsdone unk_l unk_r unk_contexts rows
195     
196     must_gets stdin l
197
198     manyset [lrange $l 0 3] unk_l unk_r unk_contexts
199     set glyphsdone [lrange $l 3 end]
200     debug "GOT $l"
201
202     char_read_xpm stdin
203
204     resize_widgets_core
205     foreach w {0 1} {
206         .d.mi.csr_$w configure -height $mulrows
207     }
208     set maxh 0
209     foreach {min max contexts got} $glyphsdone {
210         show_context maxh $min $contexts
211     }
212     show_context maxh $unk_l $unk_contexts
213     .d.ctx configure -height $maxh
214     pack forget .d.pe
215     pack .d.csr -side top -before .d.mi
216     pack .d.got .d.ctx -side top -after .d.mi
217
218     read_database ./charset-$rows.txt
219     draw_glyphsdone
220     startup_cursor
221 }
222
223 #========== PIXMAPS ==========
224
225 #---------- pixmap database read and write ----------
226
227 set database_magic/pixmap {# ypp-sc-tools pctb pixmaps v1}
228
229 proc read_database_header/pixmap {f} { }
230 proc read_database_entry/pixmap {f def} {
231     global database
232
233     set im ""
234     
235     set p3 [db_getsl $f];       append im $p3    "\n"
236     if {[string compare $p3 P3]} { error "$p3 ?" }
237
238     set wh [db_getsl $f];       append im $wh    "\n";   manyset $wh w h
239     set depth [db_getsl $f];    append im $depth "\n"
240
241     for {set y 0} {$y < $h} {incr y} {
242         set line [db_getsl $f]; append im $line  "\n"
243     }
244     set database($im) $def
245 }
246 proc write_database_header/pixmap {f} { puts $f "" }
247 proc format_database_entry/pixmap {im def} {
248     return "$def\n$im"
249 }
250
251 #---------- pixmap display and input handling ----------
252
253 proc foreach_pixmap_col {var body} {
254     global alloptions
255     upvar 1 $var col
256     for {set col 0} {$col < [llength $alloptions]/3} {incr col} {
257         uplevel 1 $body
258     }
259 }
260
261 proc pixmap_select {ncol} {
262     global alloptions
263     debug "PIX SELECT $ncol [llength $alloptions]"
264     foreach_pixmap_col col {
265         if {$col==$ncol} continue
266         .d.pe.grid.l$col selection clear 0 end
267     }
268     pixmap_maybe_ok
269 }
270 proc pixmap_maybe_ok {} {
271     global alloptions pixmap_selcol pixmap_selrow
272     set nsel 0
273     foreach_pixmap_col col {
274         set cs [.d.pe.grid.l$col curselection]
275         incr nsel [llength $cs]
276         set pixmap_selcol $col
277         set pixmap_selrow [lindex $cs 0]
278     }
279     if {$nsel==1} {
280         .d.pe.ok configure -state normal -command pixmap_ok
281     } else {
282         .d.pe.ok configure -state disabled -command {}
283     }
284 }
285 proc pixmap_ok {} {
286     global database ppm pixmap_selcol pixmap_selrow mainkind alloptions
287     foreach_pixmap_col col {
288         .d.pe.grid.l$col configure -state disabled
289     }
290     .d.pe.ok configure -state disabled
291     helptext {{{ Processing }}}
292     manyset [lrange $alloptions [expr {$pixmap_selcol*3}] end] \
293         colname coldesc rows
294     manyset [lrange $rows [expr {$pixmap_selrow*2}] end] \
295         rowname rowdesc
296     set result "$colname - $rowname"
297     debug "UPDATE PIXMAP AS >$result<"
298     set database($ppm) $result
299     write_database
300     done/$mainkind
301 }
302
303 proc required/pixmap {} {
304     global unk_what ppm mulcols alloptions
305     must_gets stdin unk_what
306     debug "GOT pixmap $unk_what"
307     set ppm {}
308     while 1 {
309         must_gets stdin ppml
310         if {![string length $ppml]} break
311         append ppm $ppml "\n"
312     }
313     set data [exec pnmscale 2 << $ppm]
314     image create photo image/main -data $data
315
316     set alloptions [exec ./yppsc-resolver-pixoptions $unk_what]
317
318     read_database ./pixmaps.txt
319
320     set mulcols [image width image/main]
321     set mulrows [image height image/main]
322     resize_widgets_core
323     place forget .d.mi.csr_0
324     place forget .d.mi.csr_1
325
326     pack forget .d.csr .d.got
327     pack .d.pe -side top -before .d.mi -pady 10
328
329     eval destroy [winfo children .d.pe.grid]
330     set col 0; foreach {colname coldesc rows} $alloptions {
331         debug "INIT $col $colname \"$coldesc\""
332         label .d.pe.grid.t$col -text $colname
333         listbox .d.pe.grid.l$col
334         foreach {rowname rowdesc} $rows {
335             debug "INIT $col $colname \"$coldesc\" $rowname \"$rowdesc\""
336             .d.pe.grid.l$col insert end $rowdesc
337         }
338         bind .d.pe.grid.l$col <<ListboxSelect>> [list pixmap_select $col]
339         grid .d.pe.grid.t$col -column $col -row 0
340         grid .d.pe.grid.l$col -column $col -row 1
341         incr col
342     }
343     pixmap_maybe_ok
344     
345     helptext {
346         {{Indicate the correct parse of this image, and click OK.}}
347     }
348 }
349
350 #========== CHARACTER SET ==========
351
352 #---------- xpm input processor ----------
353
354 proc char_read_xpm {f} {
355     global glyphsdone mul inter rhsmost_max unk_l unk_r mulcols mulrows
356     global cols rows wordmap
357     
358     set o {}
359     set y -3
360     while 1 {
361         must_gets $f l
362         if {![regexp {^"(.*)",$} $l dummy l]} {
363             append o "$l\n"
364             if {[regexp {^\}\;$} $l]} break
365             continue
366         }
367         if {$y==-3} {
368             manyset $l cols rows colours cpp
369             if {$colours!=2 || $cpp!=1} { error "$l ?" }
370
371             set chop_l [expr {$unk_l - 80}]
372             set chop_r [expr {$cols - $unk_l - 100}]
373             if {$chop_l<0} { set chop_l 0 }
374
375             set unk_l [expr {$unk_l - $chop_l}]
376             set unk_r [expr {$unk_r - $chop_l}]
377             set ngd {}
378             foreach {min max contexts got} $glyphsdone {
379                 lappend ngd \
380                     [expr {$min-$chop_l}] \
381                     [expr {$max-$chop_l}] \
382                     $contexts $got
383             }
384             set glyphsdone $ngd
385
386             set realcols $cols
387             set cols [expr {$cols - $chop_l - $chop_r}]
388             debug "NOW cols=$cols chop_l,r=$chop_l,$chop_r rows=$rows\
389                 $unk_l $unk_r $ngd"
390             
391             set mulcols [expr {$cols*$mul+$inter}]
392             set mulrows [expr {$rows*$mul+$inter}]
393             append o "\"$mulcols $mulrows 9 1\",\n"
394             for {set x 0} {$x<$cols} {incr x} { set wordmap($x) 0 }
395         } elseif {$y==-2} { # first pixel
396             append o \
397 "\"+ c #111\",
398 \"a c #800\",
399 \"A c #fcc\",
400 \"b c #00c\",
401 \"B c #fff\",
402 \"u c #000\",
403 \"U c #ff0\",
404 \"q c #000\",
405 \"Q c #ff0\",\n"
406         } elseif {$y==-1} { # 2nd pixel but we've already printed ours
407         } else {
408             set ybit [expr {1<<$y}]
409             set x 0
410             set ol "\"+"
411             set olh $ol
412             if {$chop_r>=0} {
413                 set l [string range $l $chop_l end-$chop_r]
414             } else {
415                 set l [string range $l $chop_l end]
416                 append l [string repeat " " [expr -$chop_r]]
417             }
418             foreach c [split $l ""] {
419                 set how "u"
420                 if {$x >= $unk_l && $x <= $unk_r} {
421                     set how q
422                 } else {
423                     set ab 0
424                     foreach {min max contexts got} $glyphsdone {
425                         set rhsmost_max $max
426                         if {$x >= $min && $x <= $max} {
427                             set how [lindex {a b} $ab]
428                             break
429                         }
430                         set ab [expr {!$ab}]
431                     }
432                 }
433                 switch -exact $c {
434                     " " { set p $how }
435                     "o" {
436                         set p [string toupper $how]
437                         incr wordmap($x) $ybit
438                     }
439                     default { error "$c ?" }
440                 }
441                 append ol "[string repeat $p [expr {$mul-$inter}]][
442                          string repeat + $inter]"
443                 append olh [string repeat + $mul]
444                 incr x
445             }
446             set ole "\",\n"
447             append ol $ole
448             append olh $ole
449             set olhn [string repeat $olh $inter]
450             if {!$y} { append o $olhn }
451             append o [string repeat $ol [expr {$mul-1}]]
452             append o $olhn
453         }
454         incr y
455     }
456     set data [exec xpmtoppm << $o]
457     image create photo image/main -data $data
458 }
459
460 #---------- character set editor display ----------
461
462 proc show_context {maxhv x ctxs} {
463     global mul
464     upvar 1 $maxhv maxh
465     set w .d.ctx.at$x
466     if {[llength $ctxs]==1} { set fg blue } { set fg yellow }
467     label $w -bg black -fg $fg -text [join $ctxs "/\n"] -justify left
468     place $w -x [expr {($x-1)*$mul}] -y 0
469     set wh [winfo reqheight $w]
470     if {$wh > $maxh} { set maxh $wh }
471 }
472
473 proc draw_glyphsdone {} {
474     global glyphsdone mul inter
475     eval destroy [winfo children .d.got]
476     foreach {min max contexts got} $glyphsdone {
477         frame .d.got.m$min -bd 0 -background \#888
478         label .d.got.m$min.l -text "$got" -fg white -bg black -bd 0
479         pack .d.got.m$min.l -padx 1 -pady 1
480         place .d.got.m$min -x [expr {$min*$mul+$inter}] -y 0
481     }
482 }
483
484 proc startup_cursor {} {
485     global cur_already cur_mode cur_0 cur_1 last_ht
486     global glyphsdone unk_l unk_r
487     
488     set cur_already [expr {[llength $glyphsdone]/4-1}]
489     set cur_mode 1 ;# one of:   0 1 already text
490
491     set cur_0 $unk_l
492     set cur_1 [expr {$unk_r+1}]
493
494     recursor
495 }
496
497 #---------- character set runtime display and keystroke handling ----------
498
499 proc recursor/0 {} { recursor//01 0 }
500 proc recursor/1 {} { recursor//01 1 }
501 proc recursor//01 {z1} {
502     global mul rhsmost_max cols glyphsdone
503     upvar #0 cur_$z1 cur
504     .d.csr.csr.l configure -text {adjust}
505     place .d.csr.csr -x [expr {$cur*$mul - 7}]
506     bind_key space { othercursor }
507     bind_leftright_q cur_$z1 0 [expr {$cols-1}]
508     if {[llength $glyphsdone]} {
509         bind_key Tab { set cur_mode already; recursor }
510     } else {
511         bind_key Tab {}
512     }
513     bind_key Return {
514         if {$cur_0 != $cur_1} {
515             .d.csr.csr.e delete 0 end
516             set cur_mode text
517             recursor
518         }
519     }
520     helptext {
521         {{<- ->}   {move cursor, adjusting area to define}}
522         {Space     {switch to moving other cursor}}
523         {Return    {confirm location, enter letter(s)}}
524         {Tab       {switch to correcting earlier ocr}}
525         {Q         {quit and abandon OCR run}}
526     }
527 }
528 proc othercursor {} {
529     global cur_mode
530     set cur_mode [expr {!$cur_mode}]
531     recursor
532 }
533
534 proc recursor/text {} {
535     helptext {
536         {Return   {confirm entry of new glyph}}
537         {Escape   {abandon entry}}
538     }
539     unbind_all_keys
540     .d.csr.csr.l configure -text {define:}
541     pack .d.csr.csr.e -side left
542     focus .d.csr.csr.e
543     bind_key Return {
544         set strq [.d.csr.csr.e get]
545         if {[regexp {^(?:[!-[]|[]-~]|\\\\|\\x[0-9a-f]{2})+} $strq]} {
546             RETURN_RESULT DEFINE "$cur_0 $cur_1 $strq"
547         }
548     }
549     bind_key Escape {
550         bind_key Escape {}
551         pack forget .d.csr.csr.e
552         set cur_mode 1
553         recursor
554     }
555 }
556
557 proc recursor/already {} {
558     global mul
559     global glyphsdone
560     global cur_already mul
561     global glyphsdone cur_already mul
562     .d.csr.csr.l configure -text {correct}
563     set rmax [lindex $glyphsdone [expr {$cur_already*4}]]
564     place .d.csr.csr -x [expr {$rmax*$mul-3}]
565     bind_key Return {}
566     bind_key space {}
567     bind_leftright_q cur_already 0 [expr {[llength $glyphsdone]/4-1}]
568     bind_key Tab { bind_key Delete {}; set cur_mode 1; recursor }
569     bind_key Delete {
570         RETURN_RESULT DELETE [lrange $glyphsdone \
571                                   [expr $cur_already*4] \
572                                   [expr $cur_already*4+2]]
573     }
574     helptext {
575         {{<- ->}   {move cursor, selecting glyph to correct}}
576         {Del       {clear this glyph from the recognition database}}
577         {Tab       {switch to selecting area to define as new glyph}}
578         {Q         {quit and abandon OCR run}}
579     }
580 }
581
582 proc bind_leftright_q {var min max} {
583     bind_key Left  [list leftright $var $min $max -1]
584     bind_key Right [list leftright $var $min $max +1]
585     bind_key q     {
586         puts stderr "\nCharacter resolver quitting as you requested."
587         exit 1
588     }
589 }
590 proc leftright {var min max inc} {
591     upvar #0 $var v
592     set vnew $v
593     incr vnew $inc
594     if {$vnew < $min || $vnew > $max} return
595     set v $vnew
596     recursor
597 }
598
599 proc recursor {} {
600     global csrh cur_mode cur_0 cur_1 mul
601     foreach z1 {0 1} {
602         place .d.mi.csr_$z1 -y 0 -x [expr {[set cur_$z1] * $mul}]
603     }
604     recursor/$cur_mode
605 }
606
607 #---------- character database read and write ----------
608
609 # OUT OF DATE
610 # database format:
611 # series of glyphs:
612 #   <context> <ncharacters> <hex>...
613 #   width
614 #   <hex-bitmap>
615
616 # $database($context 0x<bits> 0x<bits>...) = $hex
617
618 set database_magic/char {# ypp-sc-tools pctb font v1}
619     
620 proc read_database_header/char {f} {
621     global rows
622     if {([db_getsl $f])+0 != $rows} { error "wrong h ?" }
623 }
624 proc read_database_entry/char {f context} {
625     global database
626     set bm $context
627     set strq [db_getsl $f]
628     while 1 {
629         set l [db_getsl $f]
630         if {![string length $l]} break
631         lappend bm [format %x 0x$l]
632     }
633     set database($bm) $strq
634 }
635
636 proc write_database_header/char {f} {
637     puts $f "$rows\n"
638 }
639 proc format_database_entry/char {bm strq} {
640     global database rows
641     set o "[lindex $bm 0]\n$strq\n"
642     foreach x [lrange $bm 1 end] { append o "$x\n" }
643     return $o
644 }
645
646 proc dbkey {ctx l r} {
647     global wordmap
648     set bm $ctx
649     for {set x $l} {$x <= $r} {incr x} {
650         lappend bm [format %x $wordmap($x)]
651     }
652     return $bm
653 }
654
655 proc update_database/DEFINE {c0 c1 strq} {
656     global glyphsdone unk_l unk_contexts wordmap database
657     if {$c0 > $c1} { manyset [list $c0 $c1] c1 c0 }
658     if {$c0 == $unk_l} {
659         set ncontexts $unk_contexts
660     } else {
661         foreach {l r contexts got} $glyphsdone {
662             if {$l==$c0} { set ncontexts $contexts; break }
663         }
664         if {![info exists ncontexts]} {
665             puts stderr "must start at letter LHS!"
666             return
667         }
668     }
669     incr c1 -1
670     foreach c $ncontexts {
671         set bm [dbkey $c $c0 $c1]
672         set database($bm) $strq
673     }
674     write_database
675 }
676
677 proc update_database/DELETE {l r ctxs} {
678     global database
679     foreach ctx $ctxs {
680         set bm [dbkey $ctx $l $r]
681         catch { unset database($bm) }
682     }
683     write_database
684 }
685     
686 proc RETURN_RESULT {how what} {
687     global mainkind
688     place forget .d.csr.csr
689     pack forget .d.csr.csr.e
690     helptext {{{ Processing }}}
691     unbind_all_keys
692     update idletasks
693     debug "$how $what"
694     eval update_database/$how $what
695     done/$mainkind
696 }
697
698 #========== main program ==========
699
700 proc main/default {} {
701     puts stderr "Do not run this program directly."
702     exit 12
703 }
704 proc done/default {} {
705 }
706
707 proc required {} {
708     global reqkind
709
710     fileevent stdin readable {}
711     fconfigure stdin -blocking yes
712     
713     if {[gets stdin reqkind]<0} {
714         if {[eof stdin]} { fconfigure stdin -blocking yes; exit 0 }
715         return
716     }
717     init_widgets
718
719     required/$reqkind
720 }
721
722 proc main/automatic {} {
723     fconfigure stdin -blocking no
724     fileevent stdin readable required
725 }
726 proc done/automatic {} {
727     exec sh -c {printf \\0 >&4}
728     main/automatic
729 }
730
731 proc debug {m} { }
732
733 set mainkind default
734 foreach arg $argv {
735     switch -exact -- $arg {
736         {--debug}        { proc debug {m} { puts stderr "SHOW-THING $m" } }
737         {--noop-arg}     { }
738         {--automatic-1}  { set mainkind automatic }
739         {--automatic*}   { error "incompatible versions - install problem" }
740         default          { error "huh $argv ?" }
741     }
742 }
743
744 main/$mainkind