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