chiark / gitweb /
Rearrange and tidy up innards of yppsc-ocr-resolver
[ypp-sc-tools.db-live.git] / pctb / yppsc-parsedb-updatereceiver
index 17ba86888c6f83ad782f99f1759adff37f2d1cd2..9132c4e9d22036ab35a249ffbe8e54d388666662 100755 (executable)
@@ -22,14 +22,15 @@ $CGI::POST_MAX= 65536;
 $CGI::DISABLE_UPLOADS= 1;
 
 use CGI qw/:standard -private_tempfiles/;
-
+use IO::Pipe;
+use IO::Handle;
 
 #---------- pixmaps ----------
 
-sub parseentryin_pixmap ($) {
+sub parseentryin__pixmap ($) {
     my ($entry_in) = @_;
     $entry_in =~
-       m/^(\w+ \- \w[-+'"#! 0-9a-z]*\w)\nP3\n([1-9]\d{1,3}) ([1-9]\d{1,3})\n255\n/s or die; # ']);
+       m/^(\w+ \- \w[-+'"#! 0-9a-z]*)\nP3\n([1-9]\d{1,3}) ([1-9]\d{1,3})\n255\n/s or die; # ']);
     my ($def,$w,$h)= ($1, $2+0, $3+0);
     my @d= grep { m/./ } split /\s+/, $';
     @d == $w*$h*3 or die "$d[0]|$d[1]|...|$d[$#d-1]|$d[$#d] ?";
@@ -49,16 +50,107 @@ sub parseentryin_pixmap ($) {
        $ppm .= "\n";
     }
 
-    ppmtopgm | pnmscale -width 79 | pnmnorm -bpercent 40 -wpercent 10 | pgmtopbm -threshold
-
- <ship-ahoy.ppm ppmtopgm | pnmscale -width 79 | pnmnorm -bpercent 40 -wpercent 10 | pgmtopbm -threshold | pbmtoascii | cut -c1-79
-       
+    my $icon= pipeval($ppm,
+                        'ppmtopgm',
+                        'pnmscale -xysize 156 80',
+                        'pnmnorm -bpercent 40 -wpercent 20',
+                        'pgmtopbm -threshold',
+                        'pnminvert',
+                        'pbmtoascii -2x4');
+
+    my $whole= pipeval($ppm,
+                      'ppmtopgm',
+                      'pnmnorm -bpercent 40 -wpercent 20',
+                      'pgmtopbm -threshold',
+                      'pnminvert',
+                      'pbmtoascii');
+    
     my $entry= "$def\n$ppm";
-    return ('',$def,$entry);
+    return ('',$def,$entry,$icon,$w,$whole);
 }
 
 #---------- characters ----------
 
+sub parseentryin__char ($) {
+    my ($ei) = @_;
+    $ei =~ m/^([1-9]\d{0,2})\n(Digit|Upper|Lower)\n((?:[-&\'A-F0-9a-f ]|\x20)+)\n/s or die;
+    my ($h,$ctx,$str)= ($1+0,$2,$3);
+#print STDERR ">$'<\n";
+    my @d= grep { m/./ } split /\n/, $';
+#print STDERR ">@d<\n";
+    die if $h>31;
+    die if @d>400;
+    my $maxval= (1<<$h)-1;
+    map {
+       m/^[0-9a-f]{1,8}$/ or die;
+       $_= hex $_;
+       die "$_ ?" if $_ > $maxval;
+    } @d;
+    my $w= @d;
+    my $ppm= "P2\n$w $h\n1\n";
+    for (my $y=0; $y<$h; $y++) {
+       for (my $x=0; $x<$w; $x++) {
+           $ppm .= sprintf " %d", !!($d[$x] & (1<<$y));
+       }
+       $ppm .= "\n";
+    }
+    my $entry= sprintf "%d\n%s\n%s\n", $h,$ctx,$str;
+    map { $entry .= sprintf "%x\n", $_; } @d;
+    
+#print STDERR "[[[[\n$ppm\n]]]]";
+
+    my $icon= pipeval($ppm,
+#                    "pnmscale -xysize 78 $h",
+                     'pgmtopbm -threshold',
+                     'pnminvert',
+                     'pbmtoascii');
+
+    return ("$ctx",$str,$entry, '',$w,$icon);
+}
+
+#---------- useful stuff ----------
+
+sub pipeval ($@) {
+    my ($val, @cmds) = @_;
+    my (@pids);
+
+    my $lastpipe;
+    
+    foreach my $cmd ('',@cmds) {
+       my $pipe= new IO::Pipe or die $!;
+       my $pid= fork();  defined $pid or die $!;
+
+       if (!$pid) {
+           $pipe->writer();
+           if (!$lastpipe) {
+                print $pipe $val or die $!;
+                exit 0;
+            } else {
+                open STDIN, '<&', $lastpipe or die $!;
+                open STDOUT, '>&', $pipe or die $!;
+                close $lastpipe or die $!;
+                close $pipe or die $!;
+                exec $cmd; die $!;
+            }
+       }
+       $pipe->reader();
+       if ($lastpipe) { close $lastpipe or die $!; }
+       $lastpipe= $pipe;
+       push @pids, $pid;
+    }
+
+    $!=0; { local ($/)=undef; $val= <$lastpipe>; }
+    defined $val or die $!;
+    $lastpipe->error and die $!;  close $lastpipe or die $!;
+
+    foreach my $cmd ('(paste)', @cmds) {
+       my $pid= shift @pids;
+       waitpid($pid,0) == $pid or die "$pid $? $!";
+       $?==0 or $?==13 or die "$cmd $?";
+    }
+    return $val;
+}
+
 #---------- main program ----------
 
 my $path= path_info();
@@ -66,6 +158,7 @@ my $entry_in= param('entry');
 defined $entry_in or die;
 
 my $owner= `whoami`; $? and die $?;
+chomp $owner;
 
 my $kind;
 
@@ -75,22 +168,42 @@ if ($path =~ /(pixmap|char)/) {
     die "$path ?";
 }
 
-my ($ctx,$def,$entry)= ${"parseentryin__$kind"}($entry_in);
+my ($ctx,$def,$entry,$icon,$width,$whole)= &{"parseentryin__$kind"}($entry_in);
 
-my $summary= <<END
+$icon =~ s/^/ /mg;
+
+my $email= <<END
 To: $owner
 Subject: yppsc dictionary update
 
 Context:    $kind $ctx
 Definition: $def
 
+$icon
+
 END
     ;
 
+$whole =~ s/(.*)\n/ sprintf "%-${width}s\n", $1 /mge;
+$whole =~ s/^/|/mg;
+$whole =~ s/\n/|\n/mg;
+$whole =~ s/^(.*)/ ",".('_' x $width).".\n".$1 /e;
+$whole =~ s/(.*)$/ $1."\n\`".('~' x $width)."'\n" /e;
 
-    
-    print $entry or die $!;
-} else {
-    die "$path ?";
+my $lw= 79;
+
+while ($whole =~ m/../) {
+    my $lhs= $whole;
+    $lhs =~ s/^(.{0,$lw}).*$/$1/mg;
+    $whole =~ s/^.{1,$lw}//mg;
+#print STDERR "[[[[[$lhs########$whole]]]]]\n";
+    $email .= $lhs;
 }
 
+END
+    ;
+
+my $cutline= "-8<-\n";
+$email .= $cutline.$entry.$cutline;
+
+print $email or die $!;