tesseract  4.1.0
pdfrenderer.cpp
Go to the documentation of this file.
1 // File: pdfrenderer.cpp
3 // Description: PDF rendering interface to inject into TessBaseAPI
4 //
5 // (C) Copyright 2011, Google Inc.
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
17 
18 // Include automatically generated configuration file if running autoconf.
19 #ifdef HAVE_CONFIG_H
20 #include "config_auto.h"
21 #endif
22 
23 #include <locale> // for std::locale::classic
24 #include <memory> // std::unique_ptr
25 #include <sstream> // for std::stringstream
26 #include "allheaders.h"
27 #include "baseapi.h"
28 #include <cmath>
29 #include "renderer.h"
30 #include <cstring>
31 #include "tprintf.h"
32 
33 /*
34 
35 Design notes from Ken Sharp, with light editing.
36 
37 We think one solution is a font with a single glyph (.notdef) and a
38 CIDToGIDMap which maps all the CIDs to 0. That map would then be
39 stored as a stream in the PDF file, and when flat compressed should
40 be pretty small. The font, of course, will be approximately the same
41 size as the one you currently use.
42 
43 I'm working on such a font now, the CIDToGIDMap is trivial, you just
44 create a stream object which contains 128k bytes (2 bytes per possible
45 CID and your CIDs range from 0 to 65535) and where you currently have
46 "/CIDToGIDMap /Identity" you would have "/CIDToGIDMap <object> 0 R".
47 
48 Note that if, in future, you were to use a different (ie not 2 byte)
49 CMap for character codes you could trivially extend the CIDToGIDMap.
50 
51 The following is an explanation of how some of the font stuff works,
52 this may be too simple for you in which case please accept my
53 apologies, its hard to know how much knowledge someone has. You can
54 skip all this anyway, its just for information.
55 
56 The font embedded in a PDF file is usually intended just to be
57 rendered, but extensions allow for at least some ability to locate (or
58 copy) text from a document. This isn't something which was an original
59 goal of the PDF format, but its been retro-fitted, presumably due to
60 popular demand.
61 
62 To do this reliably the PDF file must contain a ToUnicode CMap, a
63 device for mapping character codes to Unicode code points. If one of
64 these is present, then this will be used to convert the character
65 codes into Unicode values. If its not present then the reader will
66 fall back through a series of heuristics to try and guess the
67 result. This is, as you would expect, prone to failure.
68 
69 This doesn't concern you of course, since you always write a ToUnicode
70 CMap, so because you are writing the text in text rendering mode 3 it
71 would seem that you don't really need to worry about this, but in the
72 PDF spec you cannot have an isolated ToUnicode CMap, it has to be
73 attached to a font, so in order to get even copy/paste to work you
74 need to define a font.
75 
76 This is what leads to problems, tools like pdfwrite assume that they
77 are going to be able to (or even have to) modify the font entries, so
78 they require that the font being embedded be valid, and to be honest
79 the font Tesseract embeds isn't valid (for this purpose).
80 
81 
82 To see why lets look at how text is specified in a PDF file:
83 
84 (Test) Tj
85 
86 Now that looks like text but actually it isn't. Each of those bytes is
87 a 'character code'. When it comes to rendering the text a complex
88 sequence of events takes place, which converts the character code into
89 'something' which the font understands. Its entirely possible via
90 character mappings to have that text render as 'Sftu'
91 
92 For simple fonts (PostScript type 1), we use the character code as the
93 index into an Encoding array (256 elements), each element of which is
94 a glyph name, so this gives us a glyph name. We then consult the
95 CharStrings dictionary in the font, that's a complex object which
96 contains pairs of keys and values, you can use the key to retrieve a
97 given value. So we have a glyph name, we then use that as the key to
98 the dictionary and retrieve the associated value. For a type 1 font,
99 the value is a glyph program that describes how to draw the glyph.
100 
101 For CIDFonts, its a little more complicated. Because CIDFonts can be
102 large, using a glyph name as the key is unreasonable (it would also
103 lead to unfeasibly large Encoding arrays), so instead we use a 'CID'
104 as the key. CIDs are just numbers.
105 
106 But.... We don't use the character code as the CID. What we do is use
107 a CMap to convert the character code into a CID. We then use the CID
108 to key the CharStrings dictionary and proceed as before. So the 'CMap'
109 is the equivalent of the Encoding array, but its a more compact and
110 flexible representation.
111 
112 Note that you have to use the CMap just to find out how many bytes
113 constitute a character code, and it can be variable. For example you
114 can say if the first byte is 0x00->0x7f then its just one byte, if its
115 0x80->0xf0 then its 2 bytes and if its 0xf0->0xff then its 3 bytes. I
116 have seen CMaps defining character codes up to 5 bytes wide.
117 
118 Now that's fine for 'PostScript' CIDFonts, but its not sufficient for
119 TrueType CIDFonts. The thing is that TrueType fonts are accessed using
120 a Glyph ID (GID) (and the LOCA table) which may well not be anything
121 like the CID. So for this case PDF includes a CIDToGIDMap. That maps
122 the CIDs to GIDs, and we can then use the GID to get the glyph
123 description from the GLYF table of the font.
124 
125 So for a TrueType CIDFont, character-code->CID->GID->glyf-program.
126 
127 Looking at the PDF file I was supplied with we see that it contains
128 text like :
129 
130 <0x0075> Tj
131 
132 So we start by taking the character code (117) and look it up in the
133 CMap. Well you don't supply a CMap, you just use the Identity-H one
134 which is predefined. So character code 117 maps to CID 117. Then we
135 use the CIDToGIDMap, again you don't supply one, you just use the
136 predefined 'Identity' map. So CID 117 maps to GID 117. But the font we
137 were supplied with only contains 116 glyphs.
138 
139 Now for Latin that's not a huge problem, you can just supply a bigger
140 font. But for more complex languages that *is* going to be more of a
141 problem. Either you need to supply a font which contains glyphs for
142 all the possible CID->GID mappings, or we need to think laterally.
143 
144 Our solution using a TrueType CIDFont is to intervene at the
145 CIDToGIDMap stage and convert all the CIDs to GID 0. Then we have a
146 font with just one glyph, the .notdef glyph at GID 0. This is what I'm
147 looking into now.
148 
149 It would also be possible to have a 'PostScript' (ie type 1 outlines)
150 CIDFont which contained 1 glyph, and a CMap which mapped all character
151 codes to CID 0. The effect would be the same.
152 
153 Its possible (I haven't checked) that the PostScript CIDFont and
154 associated CMap would be smaller than the TrueType font and associated
155 CIDToGIDMap.
156 
157 --- in a followup ---
158 
159 OK there is a small problem there, if I use GID 0 then Acrobat gets
160 upset about it and complains it cannot extract the font. If I set the
161 CIDToGIDMap so that all the entries are 1 instead, it's happy. Totally
162 mad......
163 
164 */
165 
166 namespace tesseract {
167 
168 // If the font is 10 pts, nominal character width is 5 pts
169 static const int kCharWidth = 2;
170 
171 // Used for memory allocation. A codepoint must take no more than this
172 // many bytes, when written in the PDF way. e.g. "<0063>" for the
173 // letter 'c'
174 static const int kMaxBytesPerCodepoint = 20;
175 
176 /**********************************************************************
177  * PDF Renderer interface implementation
178  **********************************************************************/
179 TessPDFRenderer::TessPDFRenderer(const char *outputbase, const char *datadir,
180  bool textonly)
181  : TessResultRenderer(outputbase, "pdf"),
182  datadir_(datadir) {
183  obj_ = 0;
184  textonly_ = textonly;
185  offsets_.push_back(0);
186 }
187 
188 void TessPDFRenderer::AppendPDFObjectDIY(size_t objectsize) {
189  offsets_.push_back(objectsize + offsets_.back());
190  obj_++;
191 }
192 
193 void TessPDFRenderer::AppendPDFObject(const char *data) {
194  AppendPDFObjectDIY(strlen(data));
195  AppendString(data);
196 }
197 
198 // Helper function to prevent us from accidentally writing
199 // scientific notation to an HOCR or PDF file. Besides, three
200 // decimal points are all you really need.
201 static double prec(double x) {
202  double kPrecision = 1000.0;
203  double a = round(x * kPrecision) / kPrecision;
204  if (a == -0)
205  return 0;
206  return a;
207 }
208 
209 static long dist2(int x1, int y1, int x2, int y2) {
210  return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
211 }
212 
213 // Viewers like evince can get really confused during copy-paste when
214 // the baseline wanders around. So I've decided to project every word
215 // onto the (straight) line baseline. All numbers are in the native
216 // PDF coordinate system, which has the origin in the bottom left and
217 // the unit is points, which is 1/72 inch. Tesseract reports baselines
218 // left-to-right no matter what the reading order is. We need the
219 // word baseline in reading order, so we do that conversion here. Returns
220 // the word's baseline origin and length.
221 static void GetWordBaseline(int writing_direction, int ppi, int height,
222  int word_x1, int word_y1, int word_x2, int word_y2,
223  int line_x1, int line_y1, int line_x2, int line_y2,
224  double *x0, double *y0, double *length) {
225  if (writing_direction == WRITING_DIRECTION_RIGHT_TO_LEFT) {
226  Swap(&word_x1, &word_x2);
227  Swap(&word_y1, &word_y2);
228  }
229  double word_length;
230  double x, y;
231  {
232  int px = word_x1;
233  int py = word_y1;
234  double l2 = dist2(line_x1, line_y1, line_x2, line_y2);
235  if (l2 == 0) {
236  x = line_x1;
237  y = line_y1;
238  } else {
239  double t = ((px - line_x2) * (line_x2 - line_x1) +
240  (py - line_y2) * (line_y2 - line_y1)) / l2;
241  x = line_x2 + t * (line_x2 - line_x1);
242  y = line_y2 + t * (line_y2 - line_y1);
243  }
244  word_length = sqrt(static_cast<double>(dist2(word_x1, word_y1,
245  word_x2, word_y2)));
246  word_length = word_length * 72.0 / ppi;
247  x = x * 72 / ppi;
248  y = height - (y * 72.0 / ppi);
249  }
250  *x0 = x;
251  *y0 = y;
252  *length = word_length;
253 }
254 
255 // Compute coefficients for an affine matrix describing the rotation
256 // of the text. If the text is right-to-left such as Arabic or Hebrew,
257 // we reflect over the Y-axis. This matrix will set the coordinate
258 // system for placing text in the PDF file.
259 //
260 // RTL
261 // [ x' ] = [ a b ][ x ] = [-1 0 ] [ cos sin ][ x ]
262 // [ y' ] [ c d ][ y ] [ 0 1 ] [-sin cos ][ y ]
263 static void AffineMatrix(int writing_direction,
264  int line_x1, int line_y1, int line_x2, int line_y2,
265  double *a, double *b, double *c, double *d) {
266  double theta = atan2(static_cast<double>(line_y1 - line_y2),
267  static_cast<double>(line_x2 - line_x1));
268  *a = cos(theta);
269  *b = sin(theta);
270  *c = -sin(theta);
271  *d = cos(theta);
272  switch(writing_direction) {
274  *a = -*a;
275  *b = -*b;
276  break;
278  // TODO(jbreiden) Consider using the vertical PDF writing mode.
279  break;
280  default:
281  break;
282  }
283 }
284 
285 // There are some really awkward PDF viewers in the wild, such as
286 // 'Preview' which ships with the Mac. They do a better job with text
287 // selection and highlighting when given perfectly flat baseline
288 // instead of very slightly tilted. We clip small tilts to appease
289 // these viewers. I chose this threshold large enough to absorb noise,
290 // but small enough that lines probably won't cross each other if the
291 // whole page is tilted at almost exactly the clipping threshold.
292 static void ClipBaseline(int ppi, int x1, int y1, int x2, int y2,
293  int *line_x1, int *line_y1,
294  int *line_x2, int *line_y2) {
295  *line_x1 = x1;
296  *line_y1 = y1;
297  *line_x2 = x2;
298  *line_y2 = y2;
299  int rise = abs(y2 - y1) * 72;
300  int run = abs(x2 - x1) * 72;
301  if (rise < 2 * ppi && 2 * ppi < run)
302  *line_y1 = *line_y2 = (y1 + y2) / 2;
303 }
304 
305 static bool CodepointToUtf16be(int code, char utf16[kMaxBytesPerCodepoint]) {
306  if ((code > 0xD7FF && code < 0xE000) || code > 0x10FFFF) {
307  tprintf("Dropping invalid codepoint %d\n", code);
308  return false;
309  }
310  if (code < 0x10000) {
311  snprintf(utf16, kMaxBytesPerCodepoint, "%04X", code);
312  } else {
313  int a = code - 0x010000;
314  int high_surrogate = (0x03FF & (a >> 10)) + 0xD800;
315  int low_surrogate = (0x03FF & a) + 0xDC00;
316  snprintf(utf16, kMaxBytesPerCodepoint,
317  "%04X%04X", high_surrogate, low_surrogate);
318  }
319  return true;
320 }
321 
322 char* TessPDFRenderer::GetPDFTextObjects(TessBaseAPI* api,
323  double width, double height) {
324  double ppi = api->GetSourceYResolution();
325 
326  // These initial conditions are all arbitrary and will be overwritten
327  double old_x = 0.0, old_y = 0.0;
328  int old_fontsize = 0;
329  tesseract::WritingDirection old_writing_direction =
331  bool new_block = true;
332  int fontsize = 0;
333  double a = 1;
334  double b = 0;
335  double c = 0;
336  double d = 1;
337 
338  std::stringstream pdf_str;
339  // Use "C" locale (needed for double values prec()).
340  pdf_str.imbue(std::locale::classic());
341  // Use 8 digits for double values.
342  pdf_str.precision(8);
343 
344  // TODO(jbreiden) This marries the text and image together.
345  // Slightly cleaner from an abstraction standpoint if this were to
346  // live inside a separate text object.
347  pdf_str << "q " << prec(width) << " 0 0 " << prec(height) << " 0 0 cm";
348  if (!textonly_) {
349  pdf_str << " /Im1 Do";
350  }
351  pdf_str << " Q\n";
352 
353  int line_x1 = 0;
354  int line_y1 = 0;
355  int line_x2 = 0;
356  int line_y2 = 0;
357 
358  ResultIterator *res_it = api->GetIterator();
359  while (!res_it->Empty(RIL_BLOCK)) {
360  if (res_it->IsAtBeginningOf(RIL_BLOCK)) {
361  pdf_str << "BT\n3 Tr"; // Begin text object, use invisible ink
362  old_fontsize = 0; // Every block will declare its fontsize
363  new_block = true; // Every block will declare its affine matrix
364  }
365 
366  if (res_it->IsAtBeginningOf(RIL_TEXTLINE)) {
367  int x1, y1, x2, y2;
368  res_it->Baseline(RIL_TEXTLINE, &x1, &y1, &x2, &y2);
369  ClipBaseline(ppi, x1, y1, x2, y2, &line_x1, &line_y1, &line_x2, &line_y2);
370  }
371 
372  if (res_it->Empty(RIL_WORD)) {
373  res_it->Next(RIL_WORD);
374  continue;
375  }
376 
377  // Writing direction changes at a per-word granularity
378  tesseract::WritingDirection writing_direction;
379  {
380  tesseract::Orientation orientation;
381  tesseract::TextlineOrder textline_order;
382  float deskew_angle;
383  res_it->Orientation(&orientation, &writing_direction,
384  &textline_order, &deskew_angle);
385  if (writing_direction != WRITING_DIRECTION_TOP_TO_BOTTOM) {
386  switch (res_it->WordDirection()) {
387  case DIR_LEFT_TO_RIGHT:
388  writing_direction = WRITING_DIRECTION_LEFT_TO_RIGHT;
389  break;
390  case DIR_RIGHT_TO_LEFT:
391  writing_direction = WRITING_DIRECTION_RIGHT_TO_LEFT;
392  break;
393  default:
394  writing_direction = old_writing_direction;
395  }
396  }
397  }
398 
399  // Where is word origin and how long is it?
400  double x, y, word_length;
401  {
402  int word_x1, word_y1, word_x2, word_y2;
403  res_it->Baseline(RIL_WORD, &word_x1, &word_y1, &word_x2, &word_y2);
404  GetWordBaseline(writing_direction, ppi, height,
405  word_x1, word_y1, word_x2, word_y2,
406  line_x1, line_y1, line_x2, line_y2,
407  &x, &y, &word_length);
408  }
409 
410  if (writing_direction != old_writing_direction || new_block) {
411  AffineMatrix(writing_direction,
412  line_x1, line_y1, line_x2, line_y2, &a, &b, &c, &d);
413  pdf_str << " " << prec(a) // . This affine matrix
414  << " " << prec(b) // . sets the coordinate
415  << " " << prec(c) // . system for all
416  << " " << prec(d) // . text that follows.
417  << " " << prec(x) // .
418  << " " << prec(y) // .
419  << (" Tm "); // Place cursor absolutely
420  new_block = false;
421  } else {
422  double dx = x - old_x;
423  double dy = y - old_y;
424  pdf_str << " " << prec(dx * a + dy * b)
425  << " " << prec(dx * c + dy * d)
426  << (" Td "); // Relative moveto
427  }
428  old_x = x;
429  old_y = y;
430  old_writing_direction = writing_direction;
431 
432  // Adjust font size on a per word granularity. Pay attention to
433  // fontsize, old_fontsize, and pdf_str. We've found that for
434  // in Arabic, Tesseract will happily return a fontsize of zero,
435  // so we make up a default number to protect ourselves.
436  {
437  bool bold, italic, underlined, monospace, serif, smallcaps;
438  int font_id;
439  res_it->WordFontAttributes(&bold, &italic, &underlined, &monospace,
440  &serif, &smallcaps, &fontsize, &font_id);
441  const int kDefaultFontsize = 8;
442  if (fontsize <= 0)
443  fontsize = kDefaultFontsize;
444  if (fontsize != old_fontsize) {
445  pdf_str << "/f-0-0 " << fontsize << " Tf ";
446  old_fontsize = fontsize;
447  }
448  }
449 
450  bool last_word_in_line = res_it->IsAtFinalElement(RIL_TEXTLINE, RIL_WORD);
451  bool last_word_in_block = res_it->IsAtFinalElement(RIL_BLOCK, RIL_WORD);
452  std::string pdf_word;
453  int pdf_word_len = 0;
454  do {
455  const std::unique_ptr<const char[]> grapheme(
456  res_it->GetUTF8Text(RIL_SYMBOL));
457  if (grapheme && grapheme[0] != '\0') {
458  std::vector<char32> unicodes = UNICHAR::UTF8ToUTF32(grapheme.get());
459  char utf16[kMaxBytesPerCodepoint];
460  for (char32 code : unicodes) {
461  if (CodepointToUtf16be(code, utf16)) {
462  pdf_word += utf16;
463  pdf_word_len++;
464  }
465  }
466  }
467  res_it->Next(RIL_SYMBOL);
468  } while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_WORD));
469  if (res_it->IsAtBeginningOf(RIL_WORD)) {
470  pdf_word += "0020";
471  pdf_word_len++;
472  }
473  if (word_length > 0 && pdf_word_len > 0) {
474  double h_stretch =
475  kCharWidth * prec(100.0 * word_length / (fontsize * pdf_word_len));
476  pdf_str << h_stretch << " Tz" // horizontal stretch
477  << " [ <" << pdf_word // UTF-16BE representation
478  << "> ] TJ"; // show the text
479  }
480  if (last_word_in_line) {
481  pdf_str << " \n";
482  }
483  if (last_word_in_block) {
484  pdf_str << "ET\n"; // end the text object
485  }
486  }
487  const std::string& text = pdf_str.str();
488  char* result = new char[text.length() + 1];
489  strcpy(result, text.c_str());
490  delete res_it;
491  return result;
492 }
493 
495  AppendPDFObject("%PDF-1.5\n%\xDE\xAD\xBE\xEB\n");
496 
497  // CATALOG
498  AppendPDFObject("1 0 obj\n"
499  "<<\n"
500  " /Type /Catalog\n"
501  " /Pages 2 0 R\n"
502  ">>\nendobj\n");
503 
504  // We are reserving object #2 for the /Pages
505  // object, which I am going to create and write
506  // at the end of the PDF file.
507  AppendPDFObject("");
508 
509  // TYPE0 FONT
510  AppendPDFObject("3 0 obj\n"
511  "<<\n"
512  " /BaseFont /GlyphLessFont\n"
513  " /DescendantFonts [ 4 0 R ]\n" // CIDFontType2 font
514  " /Encoding /Identity-H\n"
515  " /Subtype /Type0\n"
516  " /ToUnicode 6 0 R\n" // ToUnicode
517  " /Type /Font\n"
518  ">>\n"
519  "endobj\n");
520 
521  // CIDFONTTYPE2
522  std::stringstream stream;
523  stream <<
524  "4 0 obj\n"
525  "<<\n"
526  " /BaseFont /GlyphLessFont\n"
527  " /CIDToGIDMap 5 0 R\n" // CIDToGIDMap
528  " /CIDSystemInfo\n"
529  " <<\n"
530  " /Ordering (Identity)\n"
531  " /Registry (Adobe)\n"
532  " /Supplement 0\n"
533  " >>\n"
534  " /FontDescriptor 7 0 R\n" // Font descriptor
535  " /Subtype /CIDFontType2\n"
536  " /Type /Font\n"
537  " /DW " << (1000 / kCharWidth) << "\n"
538  ">>\n"
539  "endobj\n";
540  AppendPDFObject(stream.str().c_str());
541 
542  // CIDTOGIDMAP
543  const int kCIDToGIDMapSize = 2 * (1 << 16);
544  const std::unique_ptr<unsigned char[]> cidtogidmap(
545  new unsigned char[kCIDToGIDMapSize]);
546  for (int i = 0; i < kCIDToGIDMapSize; i++) {
547  cidtogidmap[i] = (i % 2) ? 1 : 0;
548  }
549  size_t len;
550  unsigned char *comp = zlibCompress(cidtogidmap.get(), kCIDToGIDMapSize, &len);
551  stream.str("");
552  stream <<
553  "5 0 obj\n"
554  "<<\n"
555  " /Length " << len << " /Filter /FlateDecode\n"
556  ">>\n"
557  "stream\n";
558  AppendString(stream.str().c_str());
559  long objsize = stream.str().size();
560  AppendData(reinterpret_cast<char *>(comp), len);
561  objsize += len;
562  lept_free(comp);
563  const char *endstream_endobj =
564  "endstream\n"
565  "endobj\n";
566  AppendString(endstream_endobj);
567  objsize += strlen(endstream_endobj);
568  AppendPDFObjectDIY(objsize);
569 
570  const char stream2[] =
571  "/CIDInit /ProcSet findresource begin\n"
572  "12 dict begin\n"
573  "begincmap\n"
574  "/CIDSystemInfo\n"
575  "<<\n"
576  " /Registry (Adobe)\n"
577  " /Ordering (UCS)\n"
578  " /Supplement 0\n"
579  ">> def\n"
580  "/CMapName /Adobe-Identify-UCS def\n"
581  "/CMapType 2 def\n"
582  "1 begincodespacerange\n"
583  "<0000> <FFFF>\n"
584  "endcodespacerange\n"
585  "1 beginbfrange\n"
586  "<0000> <FFFF> <0000>\n"
587  "endbfrange\n"
588  "endcmap\n"
589  "CMapName currentdict /CMap defineresource pop\n"
590  "end\n"
591  "end\n";
592 
593  // TOUNICODE
594  stream.str("");
595  stream <<
596  "6 0 obj\n"
597  "<< /Length " << (sizeof(stream2) - 1) << " >>\n"
598  "stream\n" << stream2 <<
599  "endstream\n"
600  "endobj\n";
601  AppendPDFObject(stream.str().c_str());
602 
603  // FONT DESCRIPTOR
604  stream.str("");
605  stream <<
606  "7 0 obj\n"
607  "<<\n"
608  " /Ascent 1000\n"
609  " /CapHeight 1000\n"
610  " /Descent -1\n" // Spec says must be negative
611  " /Flags 5\n" // FixedPitch + Symbolic
612  " /FontBBox [ 0 0 " << (1000 / kCharWidth) << " 1000 ]\n"
613  " /FontFile2 8 0 R\n"
614  " /FontName /GlyphLessFont\n"
615  " /ItalicAngle 0\n"
616  " /StemV 80\n"
617  " /Type /FontDescriptor\n"
618  ">>\n"
619  "endobj\n";
620  AppendPDFObject(stream.str().c_str());
621 
622  stream.str("");
623  stream << datadir_.c_str() << "/pdf.ttf";
624  FILE *fp = fopen(stream.str().c_str(), "rb");
625  if (!fp) {
626  tprintf("Cannot open file \"%s\"!\n", stream.str().c_str());
627  return false;
628  }
629  fseek(fp, 0, SEEK_END);
630  long int size = ftell(fp);
631  if (size < 0) {
632  fclose(fp);
633  return false;
634  }
635  fseek(fp, 0, SEEK_SET);
636  const std::unique_ptr<char[]> buffer(new char[size]);
637  if (!tesseract::DeSerialize(fp, buffer.get(), size)) {
638  fclose(fp);
639  return false;
640  }
641  fclose(fp);
642  // FONTFILE2
643  stream.str("");
644  stream <<
645  "8 0 obj\n"
646  "<<\n"
647  " /Length " << size << "\n"
648  " /Length1 " << size << "\n"
649  ">>\n"
650  "stream\n";
651  AppendString(stream.str().c_str());
652  objsize = stream.str().size();
653  AppendData(buffer.get(), size);
654  objsize += size;
655  AppendString(endstream_endobj);
656  objsize += strlen(endstream_endobj);
657  AppendPDFObjectDIY(objsize);
658  return true;
659 }
660 
661 bool TessPDFRenderer::imageToPDFObj(Pix *pix,
662  const char* filename,
663  long int objnum,
664  char **pdf_object,
665  long int* pdf_object_size,
666  const int jpg_quality) {
667  if (!pdf_object_size || !pdf_object)
668  return false;
669  *pdf_object = nullptr;
670  *pdf_object_size = 0;
671  if (!filename && !pix)
672  return false;
673 
674  L_Compressed_Data *cid = nullptr;
675 
676  int sad = 0;
677  if (pixGetInputFormat(pix) == IFF_PNG)
678  sad = pixGenerateCIData(pix, L_FLATE_ENCODE, 0, 0, &cid);
679  if (!cid) {
680  sad = l_generateCIDataForPdf(filename, pix, jpg_quality, &cid);
681  }
682 
683  if (sad || !cid) {
684  l_CIDataDestroy(&cid);
685  return false;
686  }
687 
688  const char *group4 = "";
689  const char *filter;
690  switch(cid->type) {
691  case L_FLATE_ENCODE:
692  filter = "/FlateDecode";
693  break;
694  case L_JPEG_ENCODE:
695  filter = "/DCTDecode";
696  break;
697  case L_G4_ENCODE:
698  filter = "/CCITTFaxDecode";
699  group4 = " /K -1\n";
700  break;
701  case L_JP2K_ENCODE:
702  filter = "/JPXDecode";
703  break;
704  default:
705  l_CIDataDestroy(&cid);
706  return false;
707  }
708 
709  // Maybe someday we will accept RGBA but today is not that day.
710  // It requires creating an /SMask for the alpha channel.
711  // http://stackoverflow.com/questions/14220221
712  std::stringstream colorspace;
713  if (cid->ncolors > 0) {
714  colorspace
715  << " /ColorSpace [ /Indexed /DeviceRGB " << (cid->ncolors - 1)
716  << " " << cid->cmapdatahex << " ]\n";
717  } else {
718  switch (cid->spp) {
719  case 1:
720  colorspace.str(" /ColorSpace /DeviceGray\n");
721  break;
722  case 3:
723  colorspace.str(" /ColorSpace /DeviceRGB\n");
724  break;
725  default:
726  l_CIDataDestroy(&cid);
727  return false;
728  }
729  }
730 
731  int predictor = (cid->predictor) ? 14 : 1;
732 
733  // IMAGE
734  std::stringstream b1;
735  b1 <<
736  objnum << " 0 obj\n"
737  "<<\n"
738  " /Length " << cid->nbytescomp << "\n"
739  " /Subtype /Image\n";
740 
741  std::stringstream b2;
742  b2 <<
743  " /Width " << cid->w << "\n"
744  " /Height " << cid->h << "\n"
745  " /BitsPerComponent " << cid->bps << "\n"
746  " /Filter " << filter << "\n"
747  " /DecodeParms\n"
748  " <<\n"
749  " /Predictor " << predictor << "\n"
750  " /Colors " << cid->spp << "\n" << group4 <<
751  " /Columns " << cid->w << "\n"
752  " /BitsPerComponent " << cid->bps << "\n"
753  " >>\n"
754  ">>\n"
755  "stream\n";
756 
757  const char *b3 =
758  "endstream\n"
759  "endobj\n";
760 
761  size_t b1_len = b1.str().size();
762  size_t b2_len = b2.str().size();
763  size_t b3_len = strlen(b3);
764  size_t colorspace_len = colorspace.str().size();
765 
766  *pdf_object_size =
767  b1_len + colorspace_len + b2_len + cid->nbytescomp + b3_len;
768  *pdf_object = new char[*pdf_object_size];
769 
770  char *p = *pdf_object;
771  memcpy(p, b1.str().c_str(), b1_len);
772  p += b1_len;
773  memcpy(p, colorspace.str().c_str(), colorspace_len);
774  p += colorspace_len;
775  memcpy(p, b2.str().c_str(), b2_len);
776  p += b2_len;
777  memcpy(p, cid->datacomp, cid->nbytescomp);
778  p += cid->nbytescomp;
779  memcpy(p, b3, b3_len);
780  l_CIDataDestroy(&cid);
781  return true;
782 }
783 
785  Pix *pix = api->GetInputImage();
786  const char* filename = api->GetInputName();
787  int ppi = api->GetSourceYResolution();
788  if (!pix || ppi <= 0)
789  return false;
790  double width = pixGetWidth(pix) * 72.0 / ppi;
791  double height = pixGetHeight(pix) * 72.0 / ppi;
792 
793  std::stringstream xobject;
794  if (!textonly_) {
795  xobject << "/XObject << /Im1 " << (obj_ + 2) << " 0 R >>\n";
796  }
797 
798  // PAGE
799  std::stringstream stream;
800  // Use "C" locale (needed for double values width and height).
801  stream.imbue(std::locale::classic());
802  stream.precision(2);
803  stream << std::fixed <<
804  obj_ << " 0 obj\n"
805  "<<\n"
806  " /Type /Page\n"
807  " /Parent 2 0 R\n" // Pages object
808  " /MediaBox [0 0 " << width << " " << height << "]\n"
809  " /Contents " << (obj_ + 1) << " 0 R\n" // Contents object
810  " /Resources\n"
811  " <<\n"
812  " " << xobject.str() << // Image object
813  " /ProcSet [ /PDF /Text /ImageB /ImageI /ImageC ]\n"
814  " /Font << /f-0-0 3 0 R >>\n" // Type0 Font
815  " >>\n"
816  ">>\n"
817  "endobj\n";
818  pages_.push_back(obj_);
819  AppendPDFObject(stream.str().c_str());
820 
821  // CONTENTS
822  const std::unique_ptr<char[]> pdftext(GetPDFTextObjects(api, width, height));
823  const size_t pdftext_len = strlen(pdftext.get());
824  size_t len;
825  unsigned char *comp_pdftext = zlibCompress(
826  reinterpret_cast<unsigned char *>(pdftext.get()), pdftext_len, &len);
827  long comp_pdftext_len = len;
828  stream.str("");
829  stream <<
830  obj_ << " 0 obj\n"
831  "<<\n"
832  " /Length " << comp_pdftext_len << " /Filter /FlateDecode\n"
833  ">>\n"
834  "stream\n";
835  AppendString(stream.str().c_str());
836  long objsize = stream.str().size();
837  AppendData(reinterpret_cast<char *>(comp_pdftext), comp_pdftext_len);
838  objsize += comp_pdftext_len;
839  lept_free(comp_pdftext);
840  const char *b2 =
841  "endstream\n"
842  "endobj\n";
843  AppendString(b2);
844  objsize += strlen(b2);
845  AppendPDFObjectDIY(objsize);
846 
847  if (!textonly_) {
848  char *pdf_object = nullptr;
849  int jpg_quality;
850  api->GetIntVariable("jpg_quality", &jpg_quality);
851  if (!imageToPDFObj(pix, filename, obj_, &pdf_object, &objsize,
852  jpg_quality)) {
853  return false;
854  }
855  AppendData(pdf_object, objsize);
856  AppendPDFObjectDIY(objsize);
857  delete[] pdf_object;
858  }
859  return true;
860 }
861 
862 
864  // We reserved the /Pages object number early, so that the /Page
865  // objects could refer to their parent. We finally have enough
866  // information to go fill it in. Using lower level calls to manipulate
867  // the offset record in two spots, because we are placing objects
868  // out of order in the file.
869 
870  // PAGES
871  const long int kPagesObjectNumber = 2;
872  offsets_[kPagesObjectNumber] = offsets_.back(); // manipulation #1
873  std::stringstream stream;
874  stream << kPagesObjectNumber << " 0 obj\n<<\n /Type /Pages\n /Kids [ ";
875  AppendString(stream.str().c_str());
876  size_t pages_objsize = stream.str().size();
877  for (size_t i = 0; i < pages_.unsigned_size(); i++) {
878  stream.str("");
879  stream << pages_[i] << " 0 R ";
880  AppendString(stream.str().c_str());
881  pages_objsize += stream.str().size();
882  }
883  stream.str("");
884  stream << "]\n /Count " << pages_.size() << "\n>>\nendobj\n";
885  AppendString(stream.str().c_str());
886  pages_objsize += stream.str().size();
887  offsets_.back() += pages_objsize; // manipulation #2
888 
889  // INFO
890  STRING utf16_title = "FEFF"; // byte_order_marker
891  std::vector<char32> unicodes = UNICHAR::UTF8ToUTF32(title());
892  char utf16[kMaxBytesPerCodepoint];
893  for (char32 code : unicodes) {
894  if (CodepointToUtf16be(code, utf16)) {
895  utf16_title += utf16;
896  }
897  }
898 
899  char* datestr = l_getFormattedDate();
900  stream.str("");
901  stream
902  << obj_ << " 0 obj\n"
903  "<<\n"
904  " /Producer (Tesseract " << tesseract::TessBaseAPI::Version() << ")\n"
905  " /CreationDate (D:" << datestr << ")\n"
906  " /Title <" << utf16_title.c_str() << ">\n"
907  ">>\n"
908  "endobj\n";
909  lept_free(datestr);
910  AppendPDFObject(stream.str().c_str());
911  stream.str("");
912  stream << "xref\n0 " << obj_ << "\n0000000000 65535 f \n";
913  AppendString(stream.str().c_str());
914  for (int i = 1; i < obj_; i++) {
915  stream.str("");
916  stream.width(10);
917  stream.fill('0');
918  stream << offsets_[i] << " 00000 n \n";
919  AppendString(stream.str().c_str());
920  }
921  stream.str("");
922  stream
923  << "trailer\n<<\n /Size " << obj_ << "\n"
924  " /Root 1 0 R\n" // catalog
925  " /Info " << (obj_ - 1) << " 0 R\n" // info
926  ">>\nstartxref\n" << offsets_.back() << "\n%%EOF\n";
927  AppendString(stream.str().c_str());
928  return true;
929 }
930 } // namespace tesseract
bool IsAtBeginningOf(PageIteratorLevel level) const override
virtual char * GetUTF8Text(PageIteratorLevel level) const
bool GetIntVariable(const char *name, int *value) const
Definition: baseapi.cpp:292
bool EndDocumentHandler() override
void Swap(T *p1, T *p2)
Definition: helpers.h:95
Definition: strngs.h:45
bool Baseline(PageIteratorLevel level, int *x1, int *y1, int *x2, int *y2) const
ResultIterator * GetIterator()
Definition: baseapi.cpp:1282
StrongScriptDirection WordDirection() const
void AppendString(const char *s)
Definition: renderer.cpp:102
static const char * Version()
Definition: baseapi.cpp:227
bool AddImageHandler(TessBaseAPI *api) override
bool IsAtFinalElement(PageIteratorLevel level, PageIteratorLevel element) const override
signed int char32
Definition: unichar.h:51
TessPDFRenderer(const char *outputbase, const char *datadir, bool textonly=false)
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:36
int push_back(T object)
size_t unsigned_size() const
Definition: genericvector.h:74
bool Empty(PageIteratorLevel level) const
const char * title() const
Definition: renderer.h:87
static std::vector< char32 > UTF8ToUTF32(const char *utf8_str)
Definition: unichar.cpp:215
const char * WordFontAttributes(bool *is_bold, bool *is_italic, bool *is_underlined, bool *is_monospace, bool *is_serif, bool *is_smallcaps, int *pointsize, int *font_id) const
bool Next(PageIteratorLevel level) override
void Orientation(tesseract::Orientation *orientation, tesseract::WritingDirection *writing_direction, tesseract::TextlineOrder *textline_order, float *deskew_angle) const
bool BeginDocumentHandler() override
int size() const
Definition: genericvector.h:70
void AppendData(const char *s, int len)
Definition: renderer.cpp:106
bool DeSerialize(FILE *fp, char *data, size_t n)
Definition: serialis.cpp:27
const char * GetInputName()
Definition: baseapi.cpp:952
T & back() const
const char * c_str() const
Definition: strngs.cpp:205