Coverage Report

Created: 2025-01-11 06:55

/src/mupdf/source/fitz/output-pdfocr.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright (C) 2004-2024 Artifex Software, Inc.
2
//
3
// This file is part of MuPDF.
4
//
5
// MuPDF is free software: you can redistribute it and/or modify it under the
6
// terms of the GNU Affero General Public License as published by the Free
7
// Software Foundation, either version 3 of the License, or (at your option)
8
// any later version.
9
//
10
// MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
11
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
13
// details.
14
//
15
// You should have received a copy of the GNU Affero General Public License
16
// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
17
//
18
// Alternative licensing terms are available from the licensor.
19
// For commercial licensing, see <https://www.artifex.com/> or contact
20
// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
21
// CA 94129, USA, for further information.
22
23
#include "mupdf/fitz.h"
24
25
#include <assert.h>
26
#include <string.h>
27
#include <limits.h>
28
29
#ifdef OCR_DISABLED
30
31
/* In non-OCR builds, we need to define this otherwise SWIG Python gets SEGV
32
when it attempts to import mupdf.py and _mupdf.py. */
33
const char *fz_pdfocr_write_options_usage = "";
34
35
#else
36
37
#include "tessocr.h"
38
39
const char *fz_pdfocr_write_options_usage =
40
  "PDFOCR output options:\n"
41
  "\tcompression=none: No compression (default)\n"
42
  "\tcompression=flate: Flate compression\n"
43
  "\tstrip-height=N: Strip height (default 0=fullpage)\n"
44
  "\tocr-language=<lang>: OCR language (default=eng)\n"
45
  "\tocr-datadir=<datadir>: OCR data path (default=rely on TESSDATA_PREFIX)\n"
46
  "\tskew=none,auto,<angle>: Whether to skew correct (default=none).\n"
47
  "\tskew-border=increase,maintain,decrease: Size change for border pixels (default=increase).\n"
48
  "\n";
49
50
static const char funky_font[] =
51
"3 0 obj\n<</BaseFont/GlyphLessFont/DescendantFonts[4 0 R]"
52
"/Encoding/Identity-H/Subtype/Type0/ToUnicode 6 0 R/Type/Font"
53
">>\nendobj\n";
54
55
static const char funky_font2[] =
56
"4 0 obj\n"
57
"<</BaseFont/GlyphLessFont/CIDToGIDMap 5 0 R"
58
"/CIDSystemInfo<</Ordering (Identity)/Registry (Adobe)/Supplement 0>>"
59
"/FontDescriptor 7 0 R/Subtype/CIDFontType2/Type/Font/DW 500>>"
60
"\nendobj\n";
61
62
static const char funky_font3[] =
63
"5 0 obj\n<</Length 210/Filter/FlateDecode>>\nstream\n"
64
"\x78\x9c\xec\xc2\x01\x09\x00\x00\x00\x02\xa0\xfa\x7f\xba\x21\x89"
65
"\xa6\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
66
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
67
"\x80\x7b\x03\x00\x00\xff\xff\xec\xc2\x01\x0d\x00\x00\x00\xc2\x20"
68
"\xdf\xbf\xb4\x45\x18\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
69
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
70
"\x00\x00\x00\x00\x00\xeb\x00\x00\x00\xff\xff\xec\xc2\x01\x0d\x00"
71
"\x00\x00\xc2\x20\xdf\xbf\xb4\x45\x18\x00\x00\x00\x00\x00\x00\x00"
72
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
73
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\xeb\x00\x00\x00\xff\xff\xed"
74
"\xc2\x01\x0d\x00\x00\x00\xc2\x20\xdf\xbf\xb4\x45\x18\x00\x00\x00"
75
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
76
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xeb\x00\xff"
77
"\x00\x10"
78
"\nendstream\nendobj\n";
79
80
static const char funky_font4[] =
81
"6 0 obj\n<</Length 353>>\nstream\n"
82
"/CIDInit /ProcSet findresource begin\n"
83
"12 dict begin\n"
84
"begincmap\n"
85
"/CIDSystemInfo\n"
86
"<<\n"
87
"  /Registry (Adobe)\n"
88
"  /Ordering (UCS)\n"
89
"  /Supplement 0\n"
90
">> def\n"
91
"/CMapName /Adobe-Identity-UCS def\n"
92
"/CMapType 2 def\n"
93
"1 begincodespacerange\n"
94
"<0000> <FFFF>\n"
95
"endcodespacerange\n"
96
"1 beginbfrange\n"
97
"<0000> <FFFF> <0000>\n"
98
"endbfrange\n"
99
"endcmap\n"
100
"CMapName currentdict /CMap defineresource pop\n"
101
"end\n"
102
"end\n"
103
"endstream\n"
104
"endobj\n";
105
106
static const char funky_font5[] =
107
"7 0 obj\n"
108
"<</Ascent 1000/CapHeight 1000/Descent -1/Flags 5"
109
"/FontBBox[0 0 500 1000]/FontFile2 8 0 R/FontName/GlyphLessFont"
110
"/ItalicAngle 0/StemV 80/Type/FontDescriptor>>\nendobj\n";
111
112
static const char funky_font6[] =
113
"8 0 obj\n<</Length 572/Length1 572>>\nstream\n"
114
"\x00\x01\x00\x00\x00\x0a\x00\x80\x00\x03\x00\x20\x4f\x53\x2f\x32"
115
"\x56\xde\xc8\x94\x00\x00\x01\x28\x00\x00\x00\x60\x63\x6d\x61\x70"
116
"\x00\x0a\x00\x34\x00\x00\x01\x90\x00\x00\x00\x1e\x67\x6c\x79\x66"
117
"\x15\x22\x41\x24\x00\x00\x01\xb8\x00\x00\x00\x18\x68\x65\x61\x64"
118
"\x0b\x78\xf1\x65\x00\x00\x00\xac\x00\x00\x00\x36\x68\x68\x65\x61"
119
"\x0c\x02\x04\x02\x00\x00\x00\xe4\x00\x00\x00\x24\x68\x6d\x74\x78"
120
"\x04\x00\x00\x00\x00\x00\x01\x88\x00\x00\x00\x08\x6c\x6f\x63\x61"
121
"\x00\x0c\x00\x00\x00\x00\x01\xb0\x00\x00\x00\x06\x6d\x61\x78\x70"
122
"\x00\x04\x00\x05\x00\x00\x01\x08\x00\x00\x00\x20\x6e\x61\x6d\x65"
123
"\xf2\xeb\x16\xda\x00\x00\x01\xd0\x00\x00\x00\x4b\x70\x6f\x73\x74"
124
"\x00\x01\x00\x01\x00\x00\x02\x1c\x00\x00\x00\x20\x00\x01\x00\x00"
125
"\x00\x01\x00\x00\xb0\x94\x71\x10\x5f\x0f\x3c\xf5\x04\x07\x08\x00"
126
"\x00\x00\x00\x00\xcf\x9a\xfc\x6e\x00\x00\x00\x00\xd4\xc3\xa7\xf2"
127
"\x00\x00\x00\x00\x04\x00\x08\x00\x00\x00\x00\x10\x00\x02\x00\x00"
128
"\x00\x00\x00\x00\x00\x01\x00\x00\x08\x00\xff\xff\x00\x00\x04\x00"
129
"\x00\x00\x00\x00\x04\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
130
"\x00\x00\x00\x00\x00\x00\x00\x02\x00\x01\x00\x00\x00\x02\x00\x04"
131
"\x00\x01\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
132
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x01\x90\x00\x05"
133
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
134
"\x00\x00\x00\x00\x00\x00\x00\x00\x05\x00\x01\x00\x01\x00\x00\x00"
135
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
136
"\x00\x00\x47\x4f\x4f\x47\x00\x40\x00\x00\x00\x00\x00\x01\xff\xff"
137
"\x00\x00\x00\x01\x00\x01\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00"
138
"\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00"
139
"\x00\x00\x00\x02\x00\x01\x00\x00\x00\x00\x00\x14\x00\x03\x00\x00"
140
"\x00\x00\x00\x14\x00\x06\x00\x0a\x00\x00\x00\x00\x00\x00\x00\x00"
141
"\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x01\x00\x00\x00\x00\x04\x00"
142
"\x08\x00\x00\x03\x00\x00\x31\x21\x11\x21\x04\x00\xfc\x00\x08\x00"
143
"\x00\x00\x00\x03\x00\x2a\x00\x00\x00\x03\x00\x00\x00\x05\x00\x16"
144
"\x00\x00\x00\x01\x00\x00\x00\x00\x00\x05\x00\x0b\x00\x16\x00\x03"
145
"\x00\x01\x04\x09\x00\x05\x00\x16\x00\x00\x00\x56\x00\x65\x00\x72"
146
"\x00\x73\x00\x69\x00\x6f\x00\x6e\x00\x20\x00\x31\x00\x2e\x00\x30"
147
"\x56\x65\x72\x73\x69\x6f\x6e\x20\x31\x2e\x30\x00\x00\x01\x00\x00"
148
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00"
149
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
150
"\nendstream\nendobj\n";
151
152
#endif
153
154
fz_pdfocr_options *
155
fz_parse_pdfocr_options(fz_context *ctx, fz_pdfocr_options *opts, const char *args)
156
0
{
157
0
#ifdef OCR_DISABLED
158
0
  fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "No OCR support in this build");
159
#else
160
  const char *val;
161
162
  memset(opts, 0, sizeof *opts);
163
164
  if (fz_has_option(ctx, args, "compression", &val))
165
  {
166
    if (fz_option_eq(val, "none"))
167
      opts->compress = 0;
168
    else if (fz_option_eq(val, "flate"))
169
      opts->compress = 1;
170
    else
171
      fz_throw(ctx, FZ_ERROR_ARGUMENT, "Unsupported PDFOCR compression %s (none, or flate only)", val);
172
  }
173
  if (fz_has_option(ctx, args, "strip-height", &val))
174
  {
175
    int i = fz_atoi(val);
176
    if (i <= 0)
177
      fz_throw(ctx, FZ_ERROR_ARGUMENT, "Unsupported PDFOCR strip height %d (suggest 0)", i);
178
    opts->strip_height = i;
179
  }
180
  if (fz_has_option(ctx, args, "ocr-language", &val))
181
  {
182
    fz_copy_option(ctx, val, opts->language, nelem(opts->language));
183
  }
184
  if (fz_has_option(ctx, args, "ocr-datadir", &val))
185
  {
186
    fz_copy_option(ctx, val, opts->datadir, nelem(opts->datadir));
187
  }
188
  if (fz_has_option(ctx, args, "skew", &val))
189
  {
190
    if (fz_option_eq(val, "auto"))
191
      opts->skew_correct = 1;
192
    else
193
    {
194
      opts->skew_correct = 2;
195
      opts->skew_angle = fz_atof(val);
196
    }
197
  }
198
  if (fz_has_option(ctx, args, "skew-border", &val))
199
  {
200
    if (fz_option_eq(val, "increase"))
201
      opts->skew_border = 0;
202
    else if (fz_option_eq(val, "maintain"))
203
      opts->skew_border = 1;
204
    else if (fz_option_eq(val, "decrease"))
205
      opts->skew_border = 2;
206
    else
207
      fz_throw(ctx, FZ_ERROR_ARGUMENT, "Unsupported skew-border option");
208
  }
209
210
  return opts;
211
#endif
212
0
}
213
214
void
215
fz_write_pixmap_as_pdfocr(fz_context *ctx, fz_output *out, const fz_pixmap *pixmap, const fz_pdfocr_options *pdfocr)
216
0
{
217
0
#ifdef OCR_DISABLED
218
0
  fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "No OCR support in this build");
219
#else
220
  fz_band_writer *writer;
221
222
  if (!pixmap || !out)
223
    return;
224
225
  writer = fz_new_pdfocr_band_writer(ctx, out, pdfocr);
226
  fz_try(ctx)
227
  {
228
    fz_write_header(ctx, writer, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, pixmap->xres, pixmap->yres, 0, pixmap->colorspace, pixmap->seps);
229
    fz_write_band(ctx, writer, pixmap->stride, pixmap->h, pixmap->samples);
230
    fz_close_band_writer(ctx, writer);
231
  }
232
  fz_always(ctx)
233
    fz_drop_band_writer(ctx, writer);
234
  fz_catch(ctx)
235
    fz_rethrow(ctx);
236
#endif
237
0
}
238
239
#ifndef OCR_DISABLED
240
typedef struct pdfocr_band_writer_s
241
{
242
  fz_band_writer super;
243
  fz_pdfocr_options options;
244
245
  /* The actual output size */
246
  int deskewed_w;
247
  int deskewed_h;
248
249
  int obj_num;
250
  int xref_max;
251
  int64_t *xref;
252
  int pages;
253
  int page_max;
254
  int *page_obj;
255
  unsigned char *stripbuf;
256
  unsigned char *compbuf;
257
  size_t complen;
258
259
  fz_pixmap *skew_bitmap;
260
261
  void *tessapi;
262
  fz_pixmap *ocrbitmap;
263
264
  fz_pdfocr_progress_fn *progress;
265
  void *progress_arg;
266
} pdfocr_band_writer;
267
268
static int
269
new_obj(fz_context *ctx, pdfocr_band_writer *writer)
270
{
271
  int64_t pos = fz_tell_output(ctx, writer->super.out);
272
273
  if (writer->obj_num >= writer->xref_max)
274
  {
275
    int new_max = writer->xref_max * 2;
276
    if (new_max < writer->obj_num + 8)
277
      new_max = writer->obj_num + 8;
278
    writer->xref = fz_realloc_array(ctx, writer->xref, new_max, int64_t);
279
    writer->xref_max = new_max;
280
  }
281
282
  writer->xref[writer->obj_num] = pos;
283
284
  return writer->obj_num++;
285
}
286
287
static void
288
post_skew_write_header(fz_context *ctx, pdfocr_band_writer *writer, int w, int h)
289
{
290
  fz_output *out = writer->super.out;
291
  int xres = writer->super.xres;
292
  int yres = writer->super.yres;
293
  int sh = writer->options.strip_height;
294
  int n = writer->super.n;
295
  int strips;
296
  int i;
297
298
  if (sh == 0)
299
    sh = h;
300
  assert(sh != 0 && "pdfocr_write_header() should not be given zero height input.");
301
  strips = (h + sh-1)/sh;
302
303
  writer->deskewed_w = w;
304
  writer->deskewed_h = h;
305
306
  writer->stripbuf = Memento_label(fz_malloc(ctx, (size_t)w * sh * n), "pdfocr_stripbuf");
307
  writer->complen = fz_deflate_bound(ctx, (size_t)w * sh * n);
308
  writer->compbuf = Memento_label(fz_malloc(ctx, writer->complen), "pdfocr_compbuf");
309
310
  /* Always round the width of ocrbitmap up to a multiple of 4. */
311
  writer->ocrbitmap = fz_new_pixmap(ctx, NULL, (w+3)&~3, h, NULL, 0);
312
  fz_set_pixmap_resolution(ctx, writer->ocrbitmap, xres, yres);
313
314
  /* Send the Page Object */
315
  fz_write_printf(ctx, out, "%d 0 obj\n<</Type/Page/Parent 2 0 R/Resources<</XObject<<", new_obj(ctx, writer));
316
  for (i = 0; i < strips; i++)
317
    fz_write_printf(ctx, out, "/I%d %d 0 R", i, writer->obj_num + i);
318
  fz_write_printf(ctx, out, ">>/Font<</F0 3 0 R>>>>/MediaBox[0 0 %g %g]/Contents %d 0 R>>\nendobj\n",
319
    w * 72.0f / xres, h * 72.0f / yres, writer->obj_num + strips);
320
}
321
322
static void
323
pdfocr_write_header(fz_context *ctx, fz_band_writer *writer_, fz_colorspace *cs)
324
{
325
  pdfocr_band_writer *writer = (pdfocr_band_writer *)writer_;
326
  fz_output *out = writer->super.out;
327
  int w = writer->super.w;
328
  int h = writer->super.h;
329
  int n = writer->super.n;
330
  int s = writer->super.s;
331
  int a = writer->super.alpha;
332
  int sh = writer->options.strip_height;
333
  int strips;
334
335
  if (sh == 0)
336
    sh = h;
337
  assert(sh != 0 && "pdfocr_write_header() should not be given zero height input.");
338
  strips = (h + sh-1)/sh;
339
340
  if (a != 0)
341
    fz_throw(ctx, FZ_ERROR_ARGUMENT, "PDFOCR cannot write alpha channel");
342
  if (s != 0)
343
    fz_throw(ctx, FZ_ERROR_ARGUMENT, "PDFOCR cannot write spot colors");
344
  if (n != 3 && n != 1)
345
    fz_throw(ctx, FZ_ERROR_ARGUMENT, "PDFOCR expected to be Grayscale or RGB");
346
347
  fz_free(ctx, writer->stripbuf);
348
  writer->stripbuf = NULL;
349
  fz_free(ctx, writer->compbuf);
350
  writer->compbuf = NULL;
351
  fz_drop_pixmap(ctx, writer->ocrbitmap);
352
  writer->ocrbitmap = NULL;
353
354
  /* Send the file header on the first page */
355
  if (writer->pages == 0)
356
  {
357
    fz_write_string(ctx, out, "%PDF-1.4\n%PDFOCR-1.0\n");
358
359
    if (writer->xref_max < 9)
360
    {
361
      int new_max = 9;
362
      writer->xref = fz_realloc_array(ctx, writer->xref, new_max, int64_t);
363
      writer->xref_max = new_max;
364
    }
365
    writer->xref[3] = fz_tell_output(ctx, out);
366
    fz_write_data(ctx, out, funky_font,  sizeof(funky_font)-1);
367
    writer->xref[4] = fz_tell_output(ctx, out);
368
    fz_write_data(ctx, out, funky_font2, sizeof(funky_font2)-1);
369
    writer->xref[5] = fz_tell_output(ctx, out);
370
    fz_write_data(ctx, out, funky_font3, sizeof(funky_font3)-1);
371
    writer->xref[6] = fz_tell_output(ctx, out);
372
    fz_write_data(ctx, out, funky_font4, sizeof(funky_font4)-1);
373
    writer->xref[7] = fz_tell_output(ctx, out);
374
    fz_write_data(ctx, out, funky_font5, sizeof(funky_font5)-1);
375
    writer->xref[8] = fz_tell_output(ctx, out);
376
    fz_write_data(ctx, out, funky_font6, sizeof(funky_font6)-1);
377
  }
378
379
  if (writer->page_max <= writer->pages)
380
  {
381
    int new_max = writer->page_max * 2;
382
    if (new_max == 0)
383
      new_max = writer->pages + 8;
384
    writer->page_obj = fz_realloc_array(ctx, writer->page_obj, new_max, int);
385
    writer->page_max = new_max;
386
  }
387
  writer->page_obj[writer->pages] = writer->obj_num;
388
  writer->pages++;
389
390
  if (writer->options.skew_correct)
391
    writer->skew_bitmap = fz_new_pixmap(ctx, n == 3 ? fz_device_rgb(ctx) : fz_device_gray(ctx), w, h, NULL, 0);
392
  else
393
    post_skew_write_header(ctx, writer, w, h);
394
}
395
396
static void
397
flush_strip(fz_context *ctx, pdfocr_band_writer *writer, int fill)
398
{
399
  unsigned char *data = writer->stripbuf;
400
  fz_output *out = writer->super.out;
401
  int w = writer->deskewed_w;
402
  int n = writer->super.n;
403
  size_t len = (size_t)w*n*fill;
404
405
  /* Buffer is full, compress it and write it. */
406
  if (writer->options.compress)
407
  {
408
    size_t destLen = writer->complen;
409
    fz_deflate(ctx, writer->compbuf, &destLen, data, len, FZ_DEFLATE_DEFAULT);
410
    len = destLen;
411
    data = writer->compbuf;
412
  }
413
  fz_write_printf(ctx, out, "%d 0 obj\n<</Width %d/ColorSpace/Device%s/Height %d%s/Subtype/Image",
414
    new_obj(ctx, writer), w, n == 1 ? "Gray" : "RGB", fill, writer->options.compress ? "/Filter/FlateDecode" : "");
415
  fz_write_printf(ctx, out, "/Length %zd/Type/XObject/BitsPerComponent 8>>\nstream\n", len);
416
  fz_write_data(ctx, out, data, len);
417
  fz_write_string(ctx, out, "\nendstream\nendobj\n");
418
}
419
420
static void
421
post_skew_write_band(fz_context *ctx, pdfocr_band_writer *writer, int stride, int band_start, int band_height, const unsigned char *sp)
422
{
423
  fz_output *out = writer->super.out;
424
  int w = writer->deskewed_w;
425
  int h = writer->deskewed_h;
426
  int n = writer->super.n;
427
  int x, y;
428
  int sh = writer->options.strip_height;
429
  int line;
430
  unsigned char *d;
431
432
  if (sh == 0)
433
    sh = h;
434
435
  for (line = 0; line < band_height; line++)
436
  {
437
    int dstline = (band_start+line) % sh;
438
    memcpy(writer->stripbuf + (size_t)w*n*dstline,
439
      sp + (size_t)line * w * n,
440
      (size_t)w * n);
441
    if (dstline+1 == sh)
442
      flush_strip(ctx, writer, dstline+1);
443
  }
444
  if (band_start + band_height == h && h % sh != 0)
445
    flush_strip(ctx, writer, h % sh);
446
447
  /* Copy strip to ocrbitmap, converting if required. */
448
  d = writer->ocrbitmap->samples;
449
  d += band_start*w;
450
  if (n == 1)
451
  {
452
    for (y = band_height; y > 0; y--)
453
    {
454
      memcpy(d, sp, w);
455
      if (writer->ocrbitmap->w - w)
456
        memset(d + w, 0, writer->ocrbitmap->w - w);
457
      d += writer->ocrbitmap->w;
458
    }
459
  }
460
  else
461
  {
462
    for (y = band_height; y > 0; y--)
463
    {
464
      for (x = w; x > 0; x--)
465
      {
466
        *d++ = (sp[0] + 2*sp[1] + sp[2] + 2)>>2;
467
        sp += 3;
468
      }
469
      for (x = writer->ocrbitmap->w - w; x > 0; x--)
470
        *d++ = 0;
471
    }
472
  }
473
}
474
475
static void
476
pdfocr_write_band(fz_context *ctx, fz_band_writer *writer_, int stride, int band_start, int band_height, const unsigned char *sp)
477
{
478
  pdfocr_band_writer *writer = (pdfocr_band_writer *)writer_;
479
  fz_output *out = writer->super.out;
480
  int w = writer->super.w;
481
  int h = writer->super.h;
482
  int n = writer->super.n;
483
  int sh = writer->options.strip_height;
484
  unsigned char *d;
485
486
  if (!out)
487
    return;
488
489
  if (writer->skew_bitmap)
490
  {
491
    d = writer->skew_bitmap->samples;
492
    d += band_start*w*n;
493
    memcpy(d, sp, w*n*band_height);
494
  }
495
  else
496
    post_skew_write_band(ctx, writer, stride, band_start, band_height, sp);
497
}
498
499
enum
500
{
501
  WORD_CONTAINS_L2R = 1,
502
  WORD_CONTAINS_R2L = 2,
503
  WORD_CONTAINS_T2B = 4,
504
  WORD_CONTAINS_B2T = 8
505
};
506
507
typedef struct word_t
508
{
509
  struct word_t *next;
510
  float bbox[4];
511
  int dirn;
512
  int len;
513
  int chars[1];
514
} word_t;
515
516
typedef struct
517
{
518
  fz_buffer *buf;
519
  pdfocr_band_writer *writer;
520
521
  /* We collate the current word into the following fields: */
522
  int word_max;
523
  int word_len;
524
  int *word_chars;
525
  float word_bbox[4];
526
  int word_dirn;
527
  int word_prev_char_bbox[4];
528
529
  /* When we finish a word, we try to add it to the line. If the
530
   * word fits onto the end of the existing line, great. If not,
531
   * we flush the entire line, and start a new one just with the
532
   * new word. This enables us to output a whole line at once,
533
   * which is beneficial to avoid jittering the font sizes
534
   * up/down, which looks bad when we try to select text in the
535
   * produced PDF. */
536
  word_t *line;
537
  word_t **line_tail;
538
  float line_bbox[4];
539
  int line_dirn;
540
541
  float cur_size;
542
  float cur_scale;
543
  float tx, ty;
544
} char_callback_data_t;
545
546
static void
547
flush_words(fz_context *ctx, char_callback_data_t *cb)
548
{
549
  float size;
550
551
  if (cb->line == NULL)
552
    return;
553
554
  if ((cb->line_dirn & (WORD_CONTAINS_T2B | WORD_CONTAINS_B2T)) != 0)
555
  {
556
    /* Vertical line */
557
  }
558
  else
559
  {
560
    /* Horizontal line */
561
    size = cb->line_bbox[3] - cb->line_bbox[1];
562
563
    if (size != 0 && size != cb->cur_size)
564
    {
565
      fz_append_printf(ctx, cb->buf, "/F0 %g Tf\n", size);
566
      cb->cur_size = size;
567
    }
568
    /* Guard against division by 0. This makes no difference to the
569
     * actual calculation as if size is 0, word->bbox[2] == word->bbox[0]
570
     * too. */
571
    if (size == 0)
572
      size = 1;
573
  }
574
575
  while (cb->line)
576
  {
577
    word_t *word = cb->line;
578
    float x, y;
579
    int i, len = word->len;
580
    float scale;
581
582
    if ((cb->line_dirn & (WORD_CONTAINS_T2B | WORD_CONTAINS_B2T)) != 0)
583
    {
584
      /* Contains vertical text. */
585
      size = (word->bbox[3] - word->bbox[1]) / len;
586
      if (size == 0)
587
        size = 1;
588
      if (size != cb->cur_size)
589
      {
590
        fz_append_printf(ctx, cb->buf, "/F0 %g Tf\n", size);
591
        cb->cur_size = size;
592
      }
593
594
      /* Set the scale so that our glyphs fill the line bbox. */
595
      scale = (cb->line_bbox[2] - cb->line_bbox[0]) / size * 200;
596
      if (scale != 0)
597
      {
598
        float letter_height = (word->bbox[3] - word->bbox[1]) / len;
599
600
        if (scale != cb->cur_scale)
601
        {
602
          fz_append_printf(ctx, cb->buf, "%d Tz\n", (int)scale);
603
          cb->cur_scale = scale;
604
        }
605
606
        for (i = 0; i < len; i++)
607
        {
608
          x = word->bbox[0];
609
          y = word->bbox[1] + letter_height * i;
610
          fz_append_printf(ctx, cb->buf, "%g %g Td\n", x-cb->tx, y-cb->ty);
611
          cb->tx = x;
612
          cb->ty = y;
613
614
          fz_append_printf(ctx, cb->buf, "<%04x>Tj\n", word->chars[i]);
615
        }
616
      }
617
    }
618
    else
619
    {
620
      scale = (word->bbox[2] - word->bbox[0]) / size / len * 200;
621
      if (scale != 0)
622
      {
623
        if (scale != cb->cur_scale)
624
        {
625
          fz_append_printf(ctx, cb->buf, "%d Tz\n", (int)scale);
626
          cb->cur_scale = scale;
627
        }
628
629
        if ((word->dirn & (WORD_CONTAINS_R2L | WORD_CONTAINS_L2R)) == WORD_CONTAINS_R2L)
630
        {
631
          /* Purely R2L text */
632
          x = word->bbox[0];
633
          y = cb->line_bbox[1];
634
          fz_append_printf(ctx, cb->buf, "%g %g Td\n", x-cb->tx, y-cb->ty);
635
          cb->tx = x;
636
          cb->ty = y;
637
638
          /* Tesseract has sent us R2L text in R2L order (i.e. in Logical order).
639
           * We want to output it in that same logical order, but PDF operators
640
           * all move the point as if outputting L2R. We can either reverse the
641
           * order of chars (bad, because of cut/paste) or we can perform
642
           * gymnastics with the position. We opt for the latter. */
643
          fz_append_printf(ctx, cb->buf, "[");
644
          for (i = 0; i < len; i++)
645
          {
646
            if (i == 0)
647
            {
648
              if (len > 1)
649
                fz_append_printf(ctx, cb->buf, "%d", -500*(len-1));
650
            }
651
            else
652
              fz_append_printf(ctx, cb->buf, "%d", 1000);
653
            fz_append_printf(ctx, cb->buf, "<%04x>", word->chars[i]);
654
          }
655
          fz_append_printf(ctx, cb->buf, "]TJ\n");
656
        }
657
        else
658
        {
659
          /* L2R (or mixed) text */
660
          x = word->bbox[0];
661
          y = cb->line_bbox[1];
662
          fz_append_printf(ctx, cb->buf, "%g %g Td\n", x-cb->tx, y-cb->ty);
663
          cb->tx = x;
664
          cb->ty = y;
665
666
          fz_append_printf(ctx, cb->buf, "<");
667
          for (i = 0; i < len; i++)
668
            fz_append_printf(ctx, cb->buf, "%04x", word->chars[i]);
669
          fz_append_printf(ctx, cb->buf, ">Tj\n");
670
        }
671
      }
672
    }
673
674
    cb->line = word->next;
675
    fz_free(ctx, word);
676
  }
677
678
  cb->line_tail = &cb->line;
679
  cb->line = NULL;
680
  cb->line_dirn = 0;
681
}
682
683
static void
684
queue_word(fz_context *ctx, char_callback_data_t *cb)
685
{
686
  word_t *word;
687
  int line_is_v, line_is_h, word_is_v, word_is_h;
688
689
  if (cb->word_len == 0)
690
    return;
691
692
  word = fz_malloc(ctx, sizeof(*word) + (cb->word_len-1)*sizeof(int));
693
  word->next = NULL;
694
  word->len = cb->word_len;
695
  memcpy(word->bbox, cb->word_bbox, 4*sizeof(float));
696
  memcpy(word->chars, cb->word_chars, cb->word_len * sizeof(int));
697
  cb->word_len = 0;
698
699
  line_is_v = !!(cb->line_dirn & (WORD_CONTAINS_B2T | WORD_CONTAINS_T2B));
700
  word_is_v = !!(cb->word_dirn & (WORD_CONTAINS_B2T | WORD_CONTAINS_T2B));
701
  line_is_h = !!(cb->line_dirn & (WORD_CONTAINS_L2R | WORD_CONTAINS_R2L));
702
  word_is_h = !!(cb->word_dirn & (WORD_CONTAINS_L2R | WORD_CONTAINS_R2L));
703
704
  word->dirn = cb->word_dirn;
705
  cb->word_dirn = 0;
706
707
  /* Can we put the new word onto the end of the existing line? */
708
  if (cb->line != NULL &&
709
    !line_is_v && !word_is_v &&
710
    word->bbox[1] <= cb->line_bbox[3] &&
711
    word->bbox[3] >= cb->line_bbox[1] &&
712
    (word->bbox[0] >= cb->line_bbox[2] || word->bbox[2] <= cb->line_bbox[0]))
713
  {
714
    /* Can append (horizontal motion). */
715
    if (word->bbox[0] < cb->line_bbox[0])
716
      cb->line_bbox[0] = word->bbox[0];
717
    if (word->bbox[1] < cb->line_bbox[1])
718
      cb->line_bbox[1] = word->bbox[1];
719
    if (word->bbox[2] > cb->line_bbox[2])
720
      cb->line_bbox[2] = word->bbox[2];
721
    if (word->bbox[3] > cb->line_bbox[3])
722
      cb->line_bbox[3] = word->bbox[3];
723
  }
724
  else if (cb->line != NULL &&
725
    !line_is_h && !word_is_h &&
726
    word->bbox[0] <= cb->line_bbox[2] &&
727
    word->bbox[2] >= cb->line_bbox[0] &&
728
    (word->bbox[1] >= cb->line_bbox[3] || word->bbox[3] <= cb->line_bbox[1]))
729
  {
730
    /* Can append (vertical motion). */
731
    if (!word_is_v)
732
      word->dirn |= WORD_CONTAINS_T2B;
733
    if (word->bbox[0] < cb->line_bbox[0])
734
      cb->line_bbox[0] = word->bbox[0];
735
    if (word->bbox[1] < cb->line_bbox[1])
736
      cb->line_bbox[1] = word->bbox[1];
737
    if (word->bbox[2] > cb->line_bbox[2])
738
      cb->line_bbox[2] = word->bbox[2];
739
    if (word->bbox[3] > cb->line_bbox[3])
740
      cb->line_bbox[3] = word->bbox[3];
741
  }
742
  else
743
  {
744
    fz_try(ctx)
745
      flush_words(ctx, cb);
746
    fz_catch(ctx)
747
    {
748
      fz_free(ctx, word);
749
      fz_rethrow(ctx);
750
    }
751
    memcpy(cb->line_bbox, word->bbox, 4*sizeof(float));
752
  }
753
754
  *cb->line_tail = word;
755
  cb->line_tail = &word->next;
756
  cb->line_dirn |= word->dirn;
757
}
758
759
static void
760
char_callback(fz_context *ctx, void *arg, int unicode,
761
    const char *font_name,
762
    const int *line_bbox, const int *word_bbox,
763
    const int *char_bbox, int pointsize)
764
{
765
  char_callback_data_t *cb = (char_callback_data_t *)arg;
766
  pdfocr_band_writer *writer = cb->writer;
767
  float bbox[4];
768
769
  bbox[0] = word_bbox[0] * 72.0f / cb->writer->ocrbitmap->xres;
770
  bbox[3] = (writer->ocrbitmap->h - 1 - word_bbox[1]) * 72.0f / cb->writer->ocrbitmap->yres;
771
  bbox[2] = word_bbox[2] * 72.0f / cb->writer->ocrbitmap->yres;
772
  bbox[1] = (writer->ocrbitmap->h - 1 - word_bbox[3]) * 72.0f / cb->writer->ocrbitmap->yres;
773
774
  if (bbox[0] != cb->word_bbox[0] ||
775
    bbox[1] != cb->word_bbox[1] ||
776
    bbox[2] != cb->word_bbox[2] ||
777
    bbox[3] != cb->word_bbox[3])
778
  {
779
    queue_word(ctx, cb);
780
    memcpy(cb->word_bbox, bbox, 4 * sizeof(float));
781
  }
782
783
  if (cb->word_len == 0)
784
  {
785
    cb->word_dirn = 0;
786
    memcpy(cb->word_prev_char_bbox, char_bbox, 4 * sizeof(int));
787
  }
788
  else
789
  {
790
    int ox = cb->word_prev_char_bbox[0] + cb->word_prev_char_bbox[2];
791
    int oy = cb->word_prev_char_bbox[1] + cb->word_prev_char_bbox[3];
792
    int x = char_bbox[0] + char_bbox[2] - ox;
793
    int y = char_bbox[1] + char_bbox[3] - oy;
794
    int ax = x < 0 ? -x : x;
795
    int ay = y < 0 ? -y : y;
796
    if (ax > ay)
797
    {
798
      if (x > 0)
799
        cb->word_dirn |= WORD_CONTAINS_L2R;
800
      else if (x < 0)
801
        cb->word_dirn |= WORD_CONTAINS_R2L;
802
    }
803
    else if (ay < ax)
804
    {
805
      if (y > 0)
806
        cb->word_dirn |= WORD_CONTAINS_T2B;
807
      else if (y < 0)
808
        cb->word_dirn |= WORD_CONTAINS_B2T;
809
    }
810
  }
811
812
  if (cb->word_max == cb->word_len)
813
  {
814
    int newmax = cb->word_max * 2;
815
    if (newmax == 0)
816
      newmax = 16;
817
    cb->word_chars = fz_realloc_array(ctx, cb->word_chars, newmax, int);
818
    cb->word_max = newmax;
819
  }
820
821
  cb->word_chars[cb->word_len++] = unicode;
822
}
823
824
static int
825
pdfocr_progress(fz_context *ctx, void *arg, int prog)
826
{
827
  char_callback_data_t *cb = (char_callback_data_t *)arg;
828
  pdfocr_band_writer *writer = cb->writer;
829
830
  if (writer->progress == NULL)
831
    return 0;
832
833
  return writer->progress(ctx, writer->progress_arg, writer->pages - 1, prog);
834
}
835
836
static void
837
do_skew_correct(fz_context *ctx, pdfocr_band_writer *writer)
838
{
839
  fz_pixmap *deskewed;
840
841
  if (writer->options.skew_correct == 1)
842
    writer->options.skew_angle = fz_skew_detect(ctx, writer->skew_bitmap);
843
844
  deskewed = fz_deskew_pixmap(ctx, writer->skew_bitmap, writer->options.skew_angle, writer->options.skew_border);
845
846
  fz_try(ctx)
847
  {
848
    post_skew_write_header(ctx, writer, deskewed->w, deskewed->h);
849
    post_skew_write_band(ctx, writer, deskewed->stride, 0, deskewed->h, deskewed->samples);
850
  }
851
  fz_always(ctx)
852
    fz_drop_pixmap(ctx, deskewed);
853
  fz_catch(ctx)
854
    fz_rethrow(ctx);
855
}
856
857
static void
858
pdfocr_write_trailer(fz_context *ctx, fz_band_writer *writer_)
859
{
860
  pdfocr_band_writer *writer = (pdfocr_band_writer *)writer_;
861
  fz_output *out = writer->super.out;
862
  int xres = writer->super.xres;
863
  int yres = writer->super.yres;
864
  int sh = writer->options.strip_height;
865
  int strips;
866
  int w, h, i;
867
  size_t len;
868
  unsigned char *data;
869
  fz_buffer *buf = NULL;
870
  char_callback_data_t cb = { NULL };
871
872
  if (writer->options.skew_correct)
873
    do_skew_correct(ctx, writer);
874
875
  w = writer->deskewed_w;
876
  h = writer->deskewed_h;
877
  if (sh == 0)
878
    sh = h;
879
  strips = (h + sh-1)/sh;
880
881
  /* Send the Page contents */
882
  /* We need the length to this, so write to a buffer first */
883
  fz_var(buf);
884
  fz_var(cb);
885
  fz_try(ctx)
886
  {
887
    cb.writer = writer;
888
    cb.buf = buf = fz_new_buffer(ctx, 0);
889
    cb.line_tail = &cb.line;
890
    cb.word_dirn = 0;
891
    cb.line_dirn = 0;
892
    fz_append_printf(ctx, buf, "q\n%g 0 0 %g 0 0 cm\n", 72.0f/xres, 72.0f/yres);
893
    for (i = 0; i < strips; i++)
894
    {
895
      int at = h - (i+1)*sh;
896
      int this_sh = sh;
897
      if (at < 0)
898
      {
899
        this_sh += at;
900
        at = 0;
901
      }
902
      fz_append_printf(ctx, buf, "/P <</MCID 0>> BDC\nq\n%d 0 0 %d 0 %d cm\n/I%d Do\nQ\n",
903
        w, this_sh, at, i);
904
    }
905
906
    fz_append_printf(ctx, buf, "Q\nBT\n3 Tr\n");
907
908
    ocr_recognise(ctx, writer->tessapi, writer->ocrbitmap, char_callback, pdfocr_progress, &cb);
909
    queue_word(ctx, &cb);
910
    flush_words(ctx, &cb);
911
    fz_append_printf(ctx, buf, "ET\n");
912
913
    len = fz_buffer_storage(ctx, buf, &data);
914
    fz_write_printf(ctx, out, "%d 0 obj\n<</Length %zd>>\nstream\n", new_obj(ctx, writer), len);
915
    fz_write_data(ctx, out, data, len);
916
    fz_drop_buffer(ctx, buf);
917
    buf = NULL;
918
    fz_write_string(ctx, out, "\nendstream\nendobj\n");
919
  }
920
  fz_always(ctx)
921
  {
922
    fz_free(ctx, cb.word_chars);
923
  }
924
  fz_catch(ctx)
925
  {
926
    fz_drop_buffer(ctx, buf);
927
    fz_rethrow(ctx);
928
  }
929
}
930
931
static void
932
pdfocr_close_band_writer(fz_context *ctx, fz_band_writer *writer_)
933
{
934
  pdfocr_band_writer *writer = (pdfocr_band_writer *)writer_;
935
  fz_output *out = writer->super.out;
936
  int i;
937
938
  /* We actually do the trailer writing in the close */
939
  if (writer->xref_max > 2)
940
  {
941
    int64_t t_pos;
942
943
    /* Catalog */
944
    writer->xref[1] = fz_tell_output(ctx, out);
945
    fz_write_printf(ctx, out, "1 0 obj\n<</Type/Catalog/Pages 2 0 R>>\nendobj\n");
946
947
    /* Page table */
948
    writer->xref[2] = fz_tell_output(ctx, out);
949
    fz_write_printf(ctx, out, "2 0 obj\n<</Count %d/Kids[", writer->pages);
950
951
    for (i = 0; i < writer->pages; i++)
952
    {
953
      if (i > 0)
954
        fz_write_byte(ctx, out, ' ');
955
      fz_write_printf(ctx, out, "%d 0 R", writer->page_obj[i]);
956
    }
957
    fz_write_string(ctx, out, "]/Type/Pages>>\nendobj\n");
958
959
    /* Xref */
960
    t_pos = fz_tell_output(ctx, out);
961
    fz_write_printf(ctx, out, "xref\n0 %d\n0000000000 65535 f \n", writer->obj_num);
962
    for (i = 1; i < writer->obj_num; i++)
963
      fz_write_printf(ctx, out, "%010ld 00000 n \n", writer->xref[i]);
964
    fz_write_printf(ctx, out, "trailer\n<</Size %d/Root 1 0 R>>\nstartxref\n%ld\n%%%%EOF\n", writer->obj_num, t_pos);
965
  }
966
}
967
968
static void
969
pdfocr_drop_band_writer(fz_context *ctx, fz_band_writer *writer_)
970
{
971
  pdfocr_band_writer *writer = (pdfocr_band_writer *)writer_;
972
  fz_free(ctx, writer->stripbuf);
973
  fz_free(ctx, writer->compbuf);
974
  fz_free(ctx, writer->page_obj);
975
  fz_free(ctx, writer->xref);
976
  fz_drop_pixmap(ctx, writer->ocrbitmap);
977
  ocr_fin(ctx, writer->tessapi);
978
}
979
#endif
980
981
fz_band_writer *fz_new_pdfocr_band_writer(fz_context *ctx, fz_output *out, const fz_pdfocr_options *options)
982
0
{
983
0
#ifdef OCR_DISABLED
984
0
  fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "No OCR support in this build");
985
#else
986
  pdfocr_band_writer *writer = fz_new_band_writer(ctx, pdfocr_band_writer, out);
987
988
  writer->super.header = pdfocr_write_header;
989
  writer->super.band = pdfocr_write_band;
990
  writer->super.trailer = pdfocr_write_trailer;
991
  writer->super.close = pdfocr_close_band_writer;
992
  writer->super.drop = pdfocr_drop_band_writer;
993
994
  if (options)
995
    writer->options = *options;
996
  else
997
    memset(&writer->options, 0, sizeof(writer->options));
998
999
  /* Objects:
1000
   *  1 reserved for catalog
1001
   *  2 for pages tree
1002
   *  3 font
1003
   *  4 cidfont
1004
   *  5 cid to gid map
1005
   *  6 tounicode
1006
   *  7 font descriptor
1007
   *  8 font file
1008
   */
1009
  writer->obj_num = 9;
1010
1011
  fz_try(ctx)
1012
  {
1013
    writer->tessapi = ocr_init(ctx, writer->options.language, writer->options.datadir);
1014
  }
1015
  fz_catch(ctx)
1016
  {
1017
    fz_drop_band_writer(ctx, &writer->super);
1018
    fz_rethrow(ctx);
1019
  }
1020
1021
  return &writer->super;
1022
#endif
1023
0
}
1024
1025
void
1026
fz_pdfocr_band_writer_set_progress(fz_context *ctx, fz_band_writer *writer_, fz_pdfocr_progress_fn *progress, void *progress_arg)
1027
0
{
1028
0
#ifdef OCR_DISABLED
1029
0
  fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "No OCR support in this build");
1030
#else
1031
  pdfocr_band_writer *writer = (pdfocr_band_writer *)writer_;
1032
  if (writer == NULL)
1033
    return;
1034
  if (writer->super.header != pdfocr_write_header)
1035
    fz_throw(ctx, FZ_ERROR_ARGUMENT, "Not a pdfocr band writer!");
1036
1037
  writer->progress = progress;
1038
  writer->progress_arg = progress_arg;
1039
#endif
1040
0
}
1041
1042
void
1043
fz_save_pixmap_as_pdfocr(fz_context *ctx, fz_pixmap *pixmap, char *filename, int append, const fz_pdfocr_options *pdfocr)
1044
0
{
1045
0
#ifdef OCR_DISABLED
1046
0
  fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "No OCR support in this build");
1047
#else
1048
  fz_output *out = fz_new_output_with_path(ctx, filename, append);
1049
  fz_try(ctx)
1050
  {
1051
    fz_write_pixmap_as_pdfocr(ctx, out, pixmap, pdfocr);
1052
    fz_close_output(ctx, out);
1053
  }
1054
  fz_always(ctx)
1055
    fz_drop_output(ctx, out);
1056
  fz_catch(ctx)
1057
    fz_rethrow(ctx);
1058
#endif
1059
0
}
1060
1061
/* High-level document writer interface */
1062
1063
#ifndef OCR_DISABLED
1064
typedef struct
1065
{
1066
  fz_document_writer super;
1067
  fz_draw_options draw;
1068
  fz_pdfocr_options pdfocr;
1069
  fz_pixmap *pixmap;
1070
  fz_band_writer *bander;
1071
  fz_output *out;
1072
  int pagenum;
1073
} fz_pdfocr_writer;
1074
1075
static fz_device *
1076
pdfocr_begin_page(fz_context *ctx, fz_document_writer *wri_, fz_rect mediabox)
1077
{
1078
  fz_pdfocr_writer *wri = (fz_pdfocr_writer*)wri_;
1079
  return fz_new_draw_device_with_options(ctx, &wri->draw, mediabox, &wri->pixmap);
1080
}
1081
1082
static void
1083
pdfocr_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev)
1084
{
1085
  fz_pdfocr_writer *wri = (fz_pdfocr_writer*)wri_;
1086
  fz_pixmap *pix = wri->pixmap;
1087
1088
  fz_try(ctx)
1089
  {
1090
    fz_close_device(ctx, dev);
1091
    fz_write_header(ctx, wri->bander, pix->w, pix->h, pix->n, pix->alpha, pix->xres, pix->yres, wri->pagenum++, pix->colorspace, pix->seps);
1092
    fz_write_band(ctx, wri->bander, pix->stride, pix->h, pix->samples);
1093
  }
1094
  fz_always(ctx)
1095
  {
1096
    fz_drop_device(ctx, dev);
1097
    fz_drop_pixmap(ctx, pix);
1098
    wri->pixmap = NULL;
1099
  }
1100
  fz_catch(ctx)
1101
    fz_rethrow(ctx);
1102
}
1103
1104
static void
1105
pdfocr_close_writer(fz_context *ctx, fz_document_writer *wri_)
1106
{
1107
  fz_pdfocr_writer *wri = (fz_pdfocr_writer*)wri_;
1108
1109
  fz_close_band_writer(ctx, wri->bander);
1110
  fz_close_output(ctx, wri->out);
1111
}
1112
1113
static void
1114
pdfocr_drop_writer(fz_context *ctx, fz_document_writer *wri_)
1115
{
1116
  fz_pdfocr_writer *wri = (fz_pdfocr_writer*)wri_;
1117
1118
  fz_drop_pixmap(ctx, wri->pixmap);
1119
  fz_drop_band_writer(ctx, wri->bander);
1120
  fz_drop_output(ctx, wri->out);
1121
}
1122
#endif
1123
1124
fz_document_writer *
1125
fz_new_pdfocr_writer_with_output(fz_context *ctx, fz_output *out, const char *options)
1126
0
{
1127
0
#ifdef OCR_DISABLED
1128
0
  fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "No OCR support in this build");
1129
#else
1130
  fz_pdfocr_writer *wri = NULL;
1131
1132
  fz_var(wri);
1133
1134
  fz_try(ctx)
1135
  {
1136
    wri = fz_new_derived_document_writer(ctx, fz_pdfocr_writer, pdfocr_begin_page, pdfocr_end_page, pdfocr_close_writer, pdfocr_drop_writer);
1137
    fz_parse_draw_options(ctx, &wri->draw, options);
1138
    fz_parse_pdfocr_options(ctx, &wri->pdfocr, options);
1139
    wri->out = out;
1140
    wri->bander = fz_new_pdfocr_band_writer(ctx, wri->out, &wri->pdfocr);
1141
  }
1142
  fz_catch(ctx)
1143
  {
1144
    fz_drop_output(ctx, out);
1145
    fz_free(ctx, wri);
1146
    fz_rethrow(ctx);
1147
  }
1148
1149
  return (fz_document_writer*)wri;
1150
#endif
1151
0
}
1152
1153
fz_document_writer *
1154
fz_new_pdfocr_writer(fz_context *ctx, const char *path, const char *options)
1155
0
{
1156
0
#ifdef OCR_DISABLED
1157
0
  fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "No OCR support in this build");
1158
#else
1159
  fz_output *out = fz_new_output_with_path(ctx, path ? path : "out.pdfocr", 0);
1160
  return fz_new_pdfocr_writer_with_output(ctx, out, options);
1161
#endif
1162
0
}
1163
1164
void
1165
fz_pdfocr_writer_set_progress(fz_context *ctx, fz_document_writer *writer, fz_pdfocr_progress_fn *progress, void *progress_arg)
1166
0
{
1167
0
#ifdef OCR_DISABLED
1168
0
  fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "No OCR support in this build");
1169
#else
1170
  fz_pdfocr_writer *wri = (fz_pdfocr_writer *)writer;
1171
  if (!writer)
1172
    return;
1173
  if (writer->begin_page != pdfocr_begin_page)
1174
    fz_throw(ctx, FZ_ERROR_ARGUMENT, "Not a pdfocr writer!");
1175
  fz_pdfocr_band_writer_set_progress(ctx, wri->bander, progress, progress_arg);
1176
#endif
1177
0
}