3 Part of the swftools package.
5 Copyright (c) 2007 Matthias Kramm <kramm@quiss.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
28 #include "../gfxdevice.h"
29 #include "../gfxtools.h"
32 #include "../gocr/pnm.h"
33 #include "../gocr/pgm2asc.h"
34 #include "../gocr/ocr0.h"
36 typedef struct _textpage {
39 struct _textpage*next;
42 typedef struct _internal {
46 textpage_t*first_page;
47 textpage_t*current_page;
50 int ocr_setparameter(gfxdevice_t*dev, const char*key, const char*value)
52 internal_t*i = (internal_t*)dev->internal;
53 return i->render->setparameter(i->render,key,value);
56 void ocr_startpage(gfxdevice_t*dev, int width, int height)
58 internal_t*i = (internal_t*)dev->internal;
60 fprintf(stderr, "Call endpage() before calling startpage()\n");
63 i->render = malloc(sizeof(gfxdevice_t));
64 gfxdevice_render_init(i->render);
65 i->render->startpage(i->render,width,height);
69 void ocr_startclip(gfxdevice_t*dev, gfxline_t*line) { ((internal_t*)dev->internal)->render->startclip(((internal_t*)dev->internal)->render,line); }
70 void ocr_endclip(gfxdevice_t*dev) { ((internal_t*)dev->internal)->render->endclip(((internal_t*)dev->internal)->render); }
71 void ocr_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit) { ((internal_t*)dev->internal)->render->stroke(((internal_t*)dev->internal)->render, line, width, color, cap_style, joint_style, miterLimit); }
72 void ocr_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color) { ((internal_t*)dev->internal)->render->fill(((internal_t*)dev->internal)->render, line, color); }
73 void ocr_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform) { ((internal_t*)dev->internal)->render->fillbitmap(((internal_t*)dev->internal)->render, line, img, matrix, cxform); }
74 void ocr_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix) { ((internal_t*)dev->internal)->render->fillgradient(((internal_t*)dev->internal)->render, line, gradient, type, matrix); }
75 void ocr_addfont(gfxdevice_t*dev, gfxfont_t*font) { ((internal_t*)dev->internal)->render->addfont(((internal_t*)dev->internal)->render, font); }
76 void ocr_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix) { ((internal_t*)dev->internal)->render->drawchar(((internal_t*)dev->internal)->render, font, glyphnr, color, matrix); }
77 void ocr_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action) { ((internal_t*)dev->internal)->render->drawlink(((internal_t*)dev->internal)->render, line, action); }
79 void ocr_result_write(gfxresult_t*r, int filedesc)
81 textpage_t*i= (textpage_t*)r->internal;
83 int ocr_result_save(gfxresult_t*r, const char*filename)
85 textpage_t*i= (textpage_t*)r->internal;
87 return 0; // no pages drawn
89 FILE*fi = fopen(filename, "wb");
93 fwrite(i->text, i->textpos, 1, fi);
100 void*ocr_result_get(gfxresult_t*r, const char*name)
102 textpage_t*i= (textpage_t*)r->internal;
103 if(!strcmp(name,"text")) {
110 char*text = (char*)malloc(len);
114 memcpy(&text[pos], i->text, i->textpos);
120 } else if(!strncmp(name,"page",4)) {
121 int pagenr = atoi(&name[4]);
130 i->text[i->textpos] = 0;
131 return strdup(i->text);
135 void ocr_result_destroy(gfxresult_t*r)
137 textpage_t*i= (textpage_t*)r->internal;
141 textpage_t*next = i->next;
142 free(i->text);i->text = 0;
151 void ocr_endpage(gfxdevice_t*dev)
153 internal_t*i = (internal_t*)dev->internal;
154 i->render->endpage(i->render);
156 gfxdevice_t*out = i->render;
157 gfxresult_t* r = out->finish(out);
158 free(i->render);i->render = 0;
160 gfximage_t*img = (gfximage_t*)r->get(r, "page");
166 job.cfg.out_format=UTF8;
168 job.src.fname = "<none>";
169 job.src.p.p = malloc(img->width*img->height);
171 job.src.p.x = img->width;
172 job.src.p.y = img->height;
173 int size=img->width*img->height;
175 for(t=0;t<size;t++) {
176 job.src.p.p[t] = (img->data[t].r+img->data[t].g+img->data[t].b)/3;
182 const char *line = 0;
185 line = getTextLine(linecounter++);
187 len += strlen(line)+1;
188 line = getTextLine(linecounter++);
191 textpage_t*page = malloc(sizeof(textpage_t));
193 page->text = malloc(len+1);
196 i->first_page = i->current_page = page;
198 i->current_page->next = page;
199 i->current_page = page;
203 line = getTextLine(linecounter++);
205 int l = strlen(line);
206 memcpy(&page->text[page->textpos], line, l);
208 page->text[page->textpos++] = '\n';
210 line = getTextLine(linecounter++);
212 page->text[page->textpos++] = 0;
216 job_free(&job);JOB=0;
221 gfxresult_t* ocr_finish(gfxdevice_t*dev)
223 internal_t*i = (internal_t*)dev->internal;
225 gfxresult_t*r = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
227 r->internal = i->first_page;
228 r->write = ocr_result_write;
229 r->save = ocr_result_save;
230 r->get = ocr_result_get;
231 r->destroy = ocr_result_destroy;
233 free(dev->internal); dev->internal = 0; i = 0;
238 void gfxdevice_ocr_init(gfxdevice_t*dev, gfxdevice_t*out)
240 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
241 memset(dev, 0, sizeof(gfxdevice_t));
247 dev->setparameter = ocr_setparameter;
248 dev->startpage = ocr_startpage;
249 dev->startclip = ocr_startclip;
250 dev->endclip = ocr_endclip;
251 dev->stroke = ocr_stroke;
252 dev->fill = ocr_fill;
253 dev->fillbitmap = ocr_fillbitmap;
254 dev->fillgradient = ocr_fillgradient;
255 dev->addfont = ocr_addfont;
256 dev->drawchar = ocr_drawchar;
257 dev->drawlink = ocr_drawlink;
258 dev->endpage = ocr_endpage;
259 dev->finish = ocr_finish;