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"
35 typedef struct _textpage {
38 struct _textpage*next;
41 typedef struct _internal {
45 textpage_t*first_page;
46 textpage_t*current_page;
49 int ocr_setparameter(gfxdevice_t*dev, const char*key, const char*value)
51 internal_t*i = (internal_t*)dev->internal;
52 return i->render->setparameter(i->render,key,value);
55 void ocr_startpage(gfxdevice_t*dev, int width, int height)
57 internal_t*i = (internal_t*)dev->internal;
59 fprintf(stderr, "Call endpage() before calling startpage()\n");
62 i->render = malloc(sizeof(gfxdevice_t));
63 gfxdevice_render_init(i->render);
64 i->render->startpage(i->render,width,height);
68 void ocr_startclip(gfxdevice_t*dev, gfxline_t*line) { ((internal_t*)dev->internal)->render->startclip(((internal_t*)dev->internal)->render,line); }
69 void ocr_endclip(gfxdevice_t*dev) { ((internal_t*)dev->internal)->render->endclip(((internal_t*)dev->internal)->render); }
70 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); }
71 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); }
72 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); }
73 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); }
74 void ocr_addfont(gfxdevice_t*dev, gfxfont_t*font) { ((internal_t*)dev->internal)->render->addfont(((internal_t*)dev->internal)->render, font); }
75 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); }
76 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); }
78 void ocr_result_write(gfxresult_t*r, int filedesc)
80 textpage_t*i= (textpage_t*)r->internal;
82 int ocr_result_save(gfxresult_t*r, const char*filename)
84 textpage_t*i= (textpage_t*)r->internal;
86 return 0; // no pages drawn
88 FILE*fi = fopen(filename, "wb");
92 fwrite(i->text, i->textpos, 1, fi);
99 void*ocr_result_get(gfxresult_t*r, const char*name)
101 textpage_t*i= (textpage_t*)r->internal;
102 if(!strcmp(name,"text")) {
109 char*text = (char*)malloc(len);
113 memcpy(&text[pos], i->text, i->textpos);
119 } else if(!strncmp(name,"page",4)) {
120 int pagenr = atoi(&name[4]);
129 i->text[i->textpos] = 0;
130 return strdup(i->text);
134 void ocr_result_destroy(gfxresult_t*r)
136 textpage_t*i= (textpage_t*)r->internal;
140 textpage_t*next = i->next;
141 free(i->text);i->text = 0;
150 void ocr_endpage(gfxdevice_t*dev)
152 internal_t*i = (internal_t*)dev->internal;
153 i->render->endpage(i->render);
155 gfxdevice_t*out = i->render;
156 gfxresult_t* r = out->finish(out);
157 free(i->render);i->render = 0;
159 gfximage_t*img = (gfximage_t*)r->get(r, "page");
165 job.cfg.out_format=UTF8;
167 job.src.fname = "<none>";
168 job.src.p.p = malloc(img->width*img->height);
170 job.src.p.x = img->width;
171 job.src.p.y = img->height;
172 int size=img->width*img->height;
174 for(t=0;t<size;t++) {
175 job.src.p.p[t] = (img->data[t].r+img->data[t].g+img->data[t].b)/3;
181 const char *line = 0;
184 line = getTextLine(linecounter++);
186 len += strlen(line)+1;
187 line = getTextLine(linecounter++);
190 textpage_t*page = malloc(sizeof(textpage_t));
192 page->text = malloc(len+1);
195 i->first_page = i->current_page = page;
197 i->current_page->next = page;
198 i->current_page = page;
202 line = getTextLine(linecounter++);
204 int l = strlen(line);
205 memcpy(&page->text[page->textpos], line, l);
207 page->text[page->textpos++] = '\n';
209 line = getTextLine(linecounter++);
211 page->text[page->textpos++] = 0;
215 job_free(&job);JOB=0;
220 gfxresult_t* ocr_finish(gfxdevice_t*dev)
222 internal_t*i = (internal_t*)dev->internal;
224 gfxresult_t*r = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
226 r->internal = i->first_page;
227 r->write = ocr_result_write;
228 r->save = ocr_result_save;
229 r->get = ocr_result_get;
230 r->destroy = ocr_result_destroy;
232 free(dev->internal); dev->internal = 0; i = 0;
237 void gfxdevice_ocr_init(gfxdevice_t*dev, gfxdevice_t*out)
239 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
240 memset(dev, 0, sizeof(gfxdevice_t));
246 dev->setparameter = ocr_setparameter;
247 dev->startpage = ocr_startpage;
248 dev->startclip = ocr_startclip;
249 dev->endclip = ocr_endclip;
250 dev->stroke = ocr_stroke;
251 dev->fill = ocr_fill;
252 dev->fillbitmap = ocr_fillbitmap;
253 dev->fillgradient = ocr_fillgradient;
254 dev->addfont = ocr_addfont;
255 dev->drawchar = ocr_drawchar;
256 dev->drawlink = ocr_drawlink;
257 dev->endpage = ocr_endpage;
258 dev->finish = ocr_finish;