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