From c2c039f07de178037bfbf12e4ee23c06f546de32 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 2 Jul 2009 16:39:18 +0100 Subject: [PATCH] New --show-charset mode --- pctb/Makefile | 2 +- pctb/README | 1 + pctb/common.h | 6 +++ pctb/convert.c | 27 +++++++---- pctb/ocr.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++-- pctb/ocr.h | 1 + 6 files changed, 149 insertions(+), 15 deletions(-) diff --git a/pctb/Makefile b/pctb/Makefile index 046493b..85cd579 100644 --- a/pctb/Makefile +++ b/pctb/Makefile @@ -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 diff --git a/pctb/README b/pctb/README index e2452dd..240ff72 100644 --- a/pctb/README +++ b/pctb/README @@ -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 diff --git a/pctb/common.h b/pctb/common.h index 48483e2..e4100f9 100644 --- a/pctb/common.h +++ b/pctb/common.h @@ -39,6 +39,8 @@ #include #include #include +#include +#include #include #include @@ -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*/ diff --git a/pctb/convert.c b/pctb/convert.c index 3ef5018..61901a5 100644 --- a/pctb/convert.c +++ b/pctb/convert.c @@ -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(); diff --git a/pctb/ocr.c b/pctb/ocr.c index 6448ee4..f174c68 100644 --- a/pctb/ocr.c +++ b/pctb/ocr.c @@ -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; ctxicontexts[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; lnlinks; 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; hcontexts[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"; + + 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); + } + } +} diff --git a/pctb/ocr.h b/pctb/ocr.h index 6473906..9a2b2c7 100644 --- a/pctb/ocr.h +++ b/pctb/ocr.h @@ -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} -- 2.30.2