chiark / gitweb /
database updates seem to work
[ypp-sc-tools.web-live.git] / pctb / show-thing.tcl
1 #!/usr/bin/wish
2
3 # usage:
4 #  run show-thing without args
5 #  then on stdin write
6 #     one line which is a Tcl list for 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
10
11
12 proc manyset {list args} {
13     foreach val $list var $args {
14         upvar 1 $var my
15         set my $val
16     }
17 }
18
19
20 #---------- display core ----------
21
22 set mul 6
23 set inter 1
24
25 set gotsh 20
26 set csrh 20
27
28 proc init_widgets {} {
29     global csrh gotsh
30     
31     frame .d
32
33     image create bitmap image/main
34     label .d.mi -image image/main -borderwidth 0
35
36     frame .d.csr -bg black -height $csrh
37     frame .d.got -bg black -height $gotsh
38
39     image create bitmap image/cursor -data \
40 {#define csr_width 11
41 #define csr_height 11
42 static unsigned char csr_bits[] = {
43    0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x21, 0x04, 0x22, 0x02, 0x25, 0x05,
44    0xaa, 0x02, 0x74, 0x01, 0xa8, 0x00, 0x70, 0x00, 0x20, 0x00};
45 }
46
47     frame .d.csr.csr
48     label .d.csr.csr.l -image image/cursor -compound left
49     entry .d.csr.csr.e -bd 0
50     pack .d.csr.csr.l -side left
51
52     frame .d.csr_0 -bg white -width 1
53     frame .d.csr_1 -bg white -width 1
54
55     place .d.csr -x 0 -y 0
56     place .d.mi -x 0 -y $csrh
57     pack .d
58
59     frame .help
60     pack .help
61 }
62
63
64 #---------- xpm input processor ----------
65
66 proc read_xpm {f} {
67     global glyphsdone mul inter rhsmost_max unk_l unk_r gotsh csrh
68     global cols rows wordmap
69     
70     set o {}
71     set y -3
72     while 1 {
73         if {[gets $f l] < 0} { error "huh? "}
74         if {![regexp {^"(.*)",$} $l dummy l]} {
75             append o "$l\n"
76             if {[regexp {^\}\;$} $l]} break
77             continue
78         }
79         if {$y==-3} {
80             manyset $l cols rows colours cpp
81             #assert {$colours==2}
82             #assert {$cpp==1}
83             set mulcols [expr {$cols*$mul+$inter}]
84             set mulrows [expr {$rows*$mul+$inter}]
85             append o "\"$mulcols $mulrows 9 1\",\n"
86             for {set x 0} {$x<$cols} {incr x} { set wordmap($x) 0 }
87         } elseif {$y==-2} { # first pixel
88             append o \
89 "\"+ c #111\",
90 \"a c #800\",
91 \"A c #fcc\",
92 \"b c #00c\",
93 \"B c #fff\",
94 \"u c #000\",
95 \"U c #ff0\",
96 \"q c #000\",
97 \"Q c #ff0\",\n"
98         } elseif {$y==-1} { # 2nd pixel but we've already printed ours
99         } else {
100             set ybit [expr {1<<$y}]
101             set x 0
102             set ol "\"+"
103             set olh $ol
104             foreach c [split $l ""] {
105                 set how "u"
106                 if {$x >= $unk_l && $x <= $unk_r} {
107                     set how q
108                 } else {
109                     set ab 0
110                     foreach {min max context got} $glyphsdone {
111                         set rhsmost_max $max
112                         if {$x >= $min && $x <= $max} {
113                             set how [lindex {a b} $ab]
114                             break
115                         }
116                         set ab [expr {!$ab}]
117                     }
118                 }
119                 switch -exact $c {
120                     " " { set p $how }
121                     "o" {
122                         set p [string toupper $how]
123                         incr wordmap($x) $ybit
124                     }
125                     default { error "$c ?" }
126                 }
127                 append ol "[string repeat $p [expr {$mul-$inter}]][
128                          string repeat + $inter]"
129                 append olh [string repeat + $mul]
130                 incr x
131             }
132             set ole "\",\n"
133             append ol $ole
134             append olh $ole
135             set olhn [string repeat $olh $inter]
136             if {!$y} { append o $olhn }
137             append o [string repeat $ol [expr {$mul-1}]]
138             append o $olhn
139         }
140         incr y
141     }
142     set data [exec xpmtoppm << $o]
143     image create photo image/main -data $data
144     
145     foreach w {.d .d.csr .d.got} {
146         $w configure -width $mulcols
147     }
148     .d configure -height [expr {$csrh+$mulrows+$gotsh}]
149     foreach w {0 1} {
150         .d.csr_$w configure -height $mulrows
151     }
152     place .d.got -x 0 -y [expr {$csrh+$mulrows}]
153 }
154
155
156 #---------- per-invocation display ----------
157
158 proc draw_glyphsdone {} {
159     global glyphsdone mul inter
160     eval destroy [winfo children .d.got]
161     foreach {min max context got} $glyphsdone {
162         frame .d.got.m$min -bd 0 -background \#888
163         label .d.got.m$min.l -text "$got" -fg white -bg black -bd 0
164         pack .d.got.m$min.l -padx 1 -pady 1
165         place .d.got.m$min -x [expr {$min*$mul+$inter}] -y 0
166     }
167 }
168
169 proc startup_cursor {} {
170     global cur_already cur_mode cur_0 cur_1 last_ht
171     global glyphsdone unk_l unk_r
172     
173     set cur_already [expr {[llength $glyphsdone]/4-1}]
174     set cur_mode 1 ;# one of:   0 1 already text
175
176     set cur_0 $unk_l
177     set cur_1 [expr {$unk_r+1}]
178     set last_ht {}
179 }
180
181
182 #---------- runtime display and keystroke handling ----------
183
184 proc helptext {t} {
185     global last_ht
186     if {![string compare $t $last_ht]} return
187     eval destroy [grid slaves .help]
188     set y 0; foreach l $t {
189         set x 0; foreach c $l {
190             set w .help.at${x}x${y}
191             label $w -text $c
192             grid $w -row $y -column $x -padx 5
193             incr x
194         }
195         incr y
196     }
197     set last_ht $t
198 }
199
200 proc recursor/0 {} { recursor//01 0 }
201 proc recursor/1 {} { recursor//01 1 }
202 proc recursor//01 {z1} {
203     global mul rhsmost_max cols glyphsdone
204     upvar #0 cur_$z1 cur
205     .d.csr.csr.l configure -text {adjust}
206     place .d.csr.csr -x [expr {$cur*$mul - 7}]
207     bind_key space { othercursor }
208     bind_leftright cur_$z1 0 [expr {$cols-1}]
209     if {[llength $glyphsdone]} {
210         bind_key Tab { set cur_mode already; recursor }
211     } else {
212         bind_key Tab {}
213     }
214     bind_key Return {
215         if {$cur_0 != $cur_1} {
216             set cur_mode text
217             recursor
218         }
219     }
220     helptext {
221         {{<- ->}   {move cursor, adjusting area to define}}
222         {Space     {switch to moving other cursor}}
223         {Return    {confirm location, enter letter(s)}}
224         {Tab       {switch to correcting earlier ocr}}
225     }
226 }
227 proc othercursor {} {
228     global cur_mode
229     set cur_mode [expr {!$cur_mode}]
230     recursor
231 }
232
233 proc recursor/text {} {
234     helptext {
235         {Return   {confirm entry of new glyph}}
236         {Escape   {abandon entry}}
237     }
238     unbind_all_keys
239     .d.csr.csr.l configure -text {define:}
240     pack .d.csr.csr.e -side left
241     focus .d.csr.csr.e
242     bind_key Return {
243         binary scan [.d.csr.csr.e get] h* hex
244         if {[string length $hex]} {
245             RETURN_RESULT DEFINE "$cur_0 $cur_1 $hex"
246         }
247     }
248     bind_key Escape {
249         bind_key Escape {}
250         pack forget .d.csr.csr.e
251         set cur_mode 1
252         recursor
253     }
254 }
255
256 proc recursor/already {} {
257     global mul
258     global glyphsdone
259     global cur_already mul
260     global glyphsdone cur_already mul
261     .d.csr.csr.l configure -text {correct}
262     set rmax [lindex $glyphsdone [expr {$cur_already*4}]]
263     place .d.csr.csr -x [expr {$rmax*$mul-3}]
264     bind_key Return {}
265     bind_key space {}
266     bind_leftright cur_already 0 [expr {[llength $glyphsdone]/4-1}]
267     bind_key Tab { bind_key Delete {}; set cur_mode 1; recursor }
268     bind_key Delete {
269         RETURN_RESULT DELETE [lrange $glyphsdone \
270                                   [expr $cur_already*4] \
271                                   [expr $cur_already*4+2]]
272     }
273     helptext {
274         {{<- ->}   {move cursor, selecting glyph to correct}}
275         {Del       {clear this glyph from the recognition database}}
276         {Tab       {switch to selecting area to define as new glyph}}
277     }
278 }
279
280 proc bind_key {k proc} {
281     global keybindings
282     bind . <Key-$k> $proc
283     set keybindings($k) [expr {!![string length $proc]}]
284 }
285 proc unbind_all_keys {} {
286     global keybindings
287     foreach k [array names keybindings] { bind_key $k {} }
288 }
289
290 proc bind_leftright {var min max} {
291     bind_key Left  [list leftright $var $min $max -1]
292     bind_key Right [list leftright $var $min $max +1]
293 }
294 proc leftright {var min max inc} {
295     upvar #0 $var v
296     set vnew $v
297     incr vnew $inc
298     if {$vnew < $min || $vnew > $max} return
299     set v $vnew
300     recursor
301 }
302
303 proc recursor {} {
304     global csrh cur_mode cur_0 cur_1 mul
305     foreach z1 {0 1} {
306         place .d.csr_$z1 -y $csrh -x [expr {[set cur_$z1] * $mul}]
307     }
308     recursor/$cur_mode
309 }
310
311
312 #---------- database read and write ----------
313
314 # database format:
315 # series of glyphs:
316 #   <context> <ncharacters> <hex>...
317 #   width
318 #   <hex-bitmap>
319
320 # $database($context 0x<bits> 0x<bits>...) = $hex
321
322 proc read_database {} {
323     global database
324     set f [open database r]
325     while {[gets $f l] >= 0} {
326         if {![regexp {^(\w+) (\d+) ((?:[0-9a-f]{2})+)$} $l \
327                   dummy context strl strh]} {
328             error "bad syntax"
329         }
330         if {[string length $strh] != $strl*2} { error "$strh $strl" }
331         gets $f l; set width [format %d $l]
332         set bm $context
333         for {set x 0} {$x < $width} {incr x} {
334             gets $f l; lappend bm [format %x 0x$l]
335         }
336         set database($bm) $strh
337     }
338     close $f
339 }
340
341 proc write_database {} {
342     global database
343     set ol {}
344     foreach bm [array names database] {
345         set strh $database($bm)
346         set strs [binary format h* $strh]
347         set strdo [format "%d %s" [expr {[string length $strh]/2}] $strh]
348         set o "[lindex $bm 0] $strdo\n"
349         append o [format "%d\n" [expr {[llength $bm]-1}]]
350         foreach x [lrange $bm 1 end] { append o "$x\n" }
351         lappend ol $o
352     }
353     set f [open database.new w]
354     foreach o [lsort $ol] {
355         puts -nonewline $f $o
356     }
357     close $f
358     file rename -force database.new database
359 }
360
361 proc dbkey {ctx l r} {
362     global wordmap
363     set bm $ctx
364     for {set x $l} {$x <= $r} {incr x} {
365         lappend bm [format %x $wordmap($x)]
366     }
367     return $bm
368 }
369
370 proc update_database/DEFINE {c0 c1 strh} {
371     global glyphsdone unk_l unk_context wordmap database
372     if {$c0 > $c1} { manyset [list $c0 $c1] c1 c0 }
373     if {$c0 == $unk_l} {
374         set ncontext $unk_context
375     } else {
376         foreach {l r context got} $glyphsdone {
377             if {$l==$c0} { set ncontext $context; break }
378         }
379         if {![info exists ncontext]} {
380             puts stderr "must start at letter LHS!"
381             return
382         }
383     }
384     incr c1 -1
385     set bm [dbkey $ncontext $c0 $c1]
386     set database($bm) $strh
387     write_database
388 }
389
390 proc update_database/DELETE {l r ctx} {
391     global database
392     set bm [dbkey $ctx $l $r]
393     unset database($bm)
394     write_database
395 }
396     
397
398 proc RETURN_RESULT {how what} {
399     place forget .d.csr.csr
400     pack forget .d.csr.csr.e
401     helptext {{{ Processing }}}
402     unbind_all_keys
403     update idletasks
404     puts "$how $what"
405     eval update_database/$how $what
406 }
407
408 #    bind . <Key-space> {}
409
410 proc test_main {} {
411     global glyphsdone unk_l unk_r unk_context
412
413     set glyphsdone {
414         7 11 1 M
415         13 17 0 a
416         19 23 0 n
417     }
418     set unk_l 25
419     set unk_r 29
420     set unk_context 0
421
422     set f [open text.xpm]
423     read_xpm $f
424     close $f
425
426     draw_glyphsdone
427     startup_cursor
428 }
429
430 read_database
431 init_widgets
432 test_main
433 recursor