chiark / gitweb /
New --show-charset mode
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 2 Jul 2009 15:39:18 +0000 (16:39 +0100)
committerIan Jackson <Ian.Jackson@eu.citrix.com>
Thu, 2 Jul 2009 15:39:18 +0000 (16:39 +0100)
pctb/Makefile
pctb/README
pctb/common.h
pctb/convert.c
pctb/ocr.c
pctb/ocr.h

index 046493b03284b16cd492221d9dff4fec0adde0a7..85cd5792cdaac9bbd695c8482acdd1db270c869f 100644 (file)
@@ -37,7 +37,7 @@ all: $(TARGETS)
 
 CONVERT_OBJS= convert.o ocr.o pages.o structure.o common.o rgbimage.o resolve.o
 
-ypp-commodities: $(CONVERT_OBJS) -lnetpbm -lXtst -lX11
+ypp-commodities: $(CONVERT_OBJS) -lnetpbm -lXtst -lX11 -lpcre
        $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
 
 $(CONVERT_OBJS): ocr.h convert.h structure.h common.h
index e2452ddec32dd8a85a93f04a0390588c2e301ebf..240ff7267400228cbb0916802eca123e862099f3 100644 (file)
@@ -118,6 +118,7 @@ This program has quite a few dependencies:
  - pnm command line utilities for image manipulation   netpbm
  - X11 libraries, including dev files for building     libx11-dev
  - XTEST library, including dev files for building     libxtst-dev
+ - Perl-compatible regexp library, including dev files  libpcre3-dev
  - Tk interpreter /usr/bin/wish                                tk8.4
  - Perl module XML::Parser                             libxml-parser-perl
  - Perl module JSON::Parser                            libjson-perl
index 48483e238b58d82e9ede0eaedf89600301804f46..e4100f9d461bcfe216c061970016b4daa759936b 100644 (file)
@@ -39,6 +39,8 @@
 #include <stdint.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <dirent.h>
+#include <pcre.h>
 #include <inttypes.h>
 
 #include <sys/wait.h>
@@ -158,4 +160,8 @@ char *masprintf(const char *fmt, ...) FMT(1,2);
          "(Are you in the correct directory?)", helper);       \
   }while(0)
 
+
+#define ARRAYSIZE(a) ((sizeof((a)) / sizeof((a)[0])))
+
+
 #endif /*COMMON_H*/
index 3ef5018fb1d3ca4167d2260113b68c993770447b..61901a5924854c967d9fc981a63406d6af89f66d 100644 (file)
@@ -37,16 +37,18 @@ const char *get_libdir(void) { return "."; }
 
 
 enum mode {
-  mf_findwindow=      0001,
-  mf_screenshot=      0010,
-  mf_readscreenshot=  0020,
-  mf_analyse=         0100,
+  mf_findwindow=      00001,
+  mf_screenshot=      00010,
+  mf_readscreenshot=  00020,
+  mf_analyse=         00100,
+  mfm_special=        07000,
   
-  mode_findwindow=    0001,
-  mode_screenshot=    0011,
-  mode_analyse=       0120,
+  mode_findwindow=    00001,
+  mode_screenshot=    00011,
+  mode_analyse=       00120,
+  mode_showcharset=   01000,
 
-  mode_all=           0111,
+  mode_all=           00111,
 };
 
 enum outmodekind {
@@ -184,6 +186,7 @@ int main(int argc, char **argv) {
   while ((arg=*++argv)) {
     if (IS("--find-window-only"))      o_mode= mode_findwindow;
     else if (IS("--screenshot-only"))  o_mode= mode_screenshot;
+    else if (IS("--show-charset"))     o_mode= mode_showcharset;
     else if (IS("--analyse-only") ||
             IS("--same"))             o_mode= mode_analyse;
     else if (IS("--everything"))       o_mode= mode_all;
@@ -272,13 +275,19 @@ int main(int argc, char **argv) {
   
   if (o_flags & ff_needisland)
     if (!ocean)
-      badusage("need --ocean option when replaying images"
+      badusage("need --ocean option when not using actual YPP client window"
               " (consider supplying --pirate too)");
   if (ocean)
     sysassert(! setenv("YPPSC_OCEAN",ocean,1) );
   if (pirate && (o_flags & ff_dict_pirate))
     sysassert(! setenv("YPPSC_PIRATE",pirate,1) );
 
+  switch (o_mode & mfm_special) {
+  case 0:                                      break;
+  case mode_showcharset:  ocr_showcharsets();  exit(0);
+  default:                                     abort();
+  }
+
   if (o_mode & mf_screenshot) {
     open_screenshot_file("w");
     if (o_flags & ff_singlepage) take_one_screenshot();
index 6448ee44d5e72bb275e17459a2863e3be68d7d2c..f174c68117c8d31e29da62fe94f153882eb8d14f 100644 (file)
@@ -36,7 +36,7 @@ typedef struct {
 typedef struct DatabaseNode {
   char *str;
   int nlinks, alinks;
-  unsigned match:1, defined:1, endsword:1;
+  unsigned match:1, defined:1, endsword:1, local:1;
   DatabaseLink *links;
 } DatabaseNode;
 
@@ -99,7 +99,7 @@ static void cleardb_node(DatabaseNode *n) {
     cleardb_node(n->links[i].then);
 }
 
-static void readdb1(OcrReader *rd, const char *which);
+static void readdb1(OcrReader *rd, const char *which, int local);
 
 static void readdb(OcrReader *rd) {
   int ctxi;
@@ -107,11 +107,11 @@ static void readdb(OcrReader *rd) {
   for (ctxi=0; ctxi<NCONTEXTS; ctxi++)
     cleardb_node(&rd->contexts[ctxi]);
 
-  readdb1(rd, "master");
-  readdb1(rd, "local");
+  readdb1(rd, "master", 0);
+  readdb1(rd, "local",  1);
 }
 
-static void readdb1(OcrReader *rd, const char *which) {
+static void readdb1(OcrReader *rd, const char *which, int local) {
   int nchrs;
   DatabaseNode *current, *additional;
   char chrs[100];
@@ -197,6 +197,7 @@ static void readdb1(OcrReader *rd, const char *which) {
       current->str= 0;
       current->defined= 1;
       current->match= 0;
+      current->local= local;
 
       if (nchrs) {
        current->str= mmalloc(nchrs+1);
@@ -483,3 +484,119 @@ OcrReader *ocr_init(int h) {
   readdb(rd);
   return rd;
 }
+
+/*---------- character set dump ----------*/
+
+static void show_recurse(const DatabaseNode *t, int *count,
+                        const DatabaseNode **store_ary) {
+  if (t->defined) {
+    if (store_ary) store_ary[*count]= t;
+    (*count)++;
+  }
+  int l;
+  for (l=0; l<t->nlinks; l++)
+    show_recurse(t->links[l].then, count,store_ary);
+}
+
+static int show_char_compar(const void *av, const void *bv) {
+  const DatabaseNode *const *ap= av;  const DatabaseNode *a= *ap;
+  const DatabaseNode *const *bp= bv;  const DatabaseNode *b= *bp;
+  return strcmp(a->str, b->str) ?:
+    ((int)a->match -    (int)b->match) ?:
+    ((int)a->endsword - (int)b->endsword) ?:
+    ((int)a->local -    (int)b->local) ?:
+    0;
+}
+
+void ocr_showcharsets(void) {
+  DIR *d;
+  struct dirent *de;
+  char found[32];
+  pcre *fnpat;
+  int matchvec[10];
+  char hbuf[10];
+  const char *pcre_err;
+  int pcre_erroffset;
+
+  memset(found,0,sizeof(found));
+  
+  fnpat= pcre_compile("\\#(?:master|local)\\-char([1-9]\\d{0,2})\\#\\.txt$",
+                     PCRE_ANCHORED|PCRE_DOLLAR_ENDONLY,
+                     &pcre_err,&pcre_erroffset, 0);
+  debugf("pcre_compile %p %s\n",fnpat,pcre_err);
+  assert(fnpat);
+
+  sysassert( d= opendir(get_vardir()) );
+  for (;;) {
+    errno=0; de= readdir(d);  if (!de) break;
+
+    int rer= pcre_exec(fnpat,0, de->d_name,strlen(de->d_name), 0,0,
+                      matchvec,ARRAYSIZE(matchvec));
+    debugf("pcre_exec `%s' => %d\n", de->d_name,rer);
+
+    if (rer==PCRE_ERROR_NOMATCH || rer==PCRE_ERROR_BADUTF8) continue;
+    assert(rer==2);
+
+    rer= pcre_copy_substring(de->d_name,matchvec,rer, 1, hbuf,sizeof(hbuf));
+    debugf("pcre_copy_substring => %d\n", rer);
+    assert(rer>0);
+
+    int h= atoi(hbuf);
+    if (h >= ARRAYSIZE(found)) continue;
+    
+    found[h]= 1;
+  }
+
+  int h;
+  for (h=0; h<ARRAYSIZE(found); h++) {
+    if (!found[h]) continue;
+
+    OcrReader *rd= ocr_init(h); /* we leak this but never mind */
+    progress("");
+
+    int ctxi;
+    for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
+      int nchars= 0;
+      show_recurse(&rd->contexts[ctxi], &nchars, 0);
+      const DatabaseNode **chars= mmalloc(sizeof(*chars) * nchars);
+      int chari= 0;
+      show_recurse(&rd->contexts[ctxi], &chari, chars);
+      assert(chari==nchars);
+      qsort(chars, nchars, sizeof(*chars), show_char_compar);
+
+      int local;
+      for (local=0; local<2; local++) {
+       printf("%2d %-6s %-6s ", h, context_names[ctxi],
+              local?"local":"master");
+       for (chari=0; chari<nchars; chari++) {
+         const DatabaseNode *t= chars[chari];
+         static const char accept[]=
+           "abcdefghijklmnopqrstuvwxyz"
+           "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+           "0123456789" "'!&-+.,>";
+
+         if (t->local != local) continue;
+
+         if (!t->match)
+           printf(" [nomatch]");
+         else if (!t->endsword && strspn(t->str, accept) == strlen(t->str))
+           printf(" %s",t->str);
+         else {
+           printf(" \"");
+           char *p= t->str;
+           int c;
+           while ((c=*p++)) {
+             if (c=='"' || c=='\\') printf("\\%c",c);
+             else if (c>=' ' && c<=126) putchar(c);
+             else printf("\\x%02x", (unsigned char)c);
+           }
+           if (t->endsword) putchar(' ');
+           putchar('"');
+         }
+       }
+       putchar('\n');
+      }
+      free(chars);
+    }
+  }
+}
index 64739068fc51b23ba06ff9395699ffab1055b982..9a2b2c7c830cb577aa04be935aff7cac7d2bbb66 100644 (file)
@@ -51,6 +51,7 @@ const char *ocr_celltype_name(OcrCellType ct);
 
 typedef struct OcrReader OcrReader;
 OcrReader *ocr_init(int h);
+void ocr_showcharsets(void);
 
 OcrResultGlyph *ocr(OcrReader *rd, OcrCellType, int w, Pixcol cols[]);
   /* return value is array terminated by {0,-1,-1}