tesseract  4.00.00dev
tesseract::TessPDFRenderer Class Reference

#include <renderer.h>

Inheritance diagram for tesseract::TessPDFRenderer:
tesseract::TessResultRenderer

Public Member Functions

 TessPDFRenderer (const char *outputbase, const char *datadir, bool textonly)
 
- Public Member Functions inherited from tesseract::TessResultRenderer
virtual ~TessResultRenderer ()
 
void insert (TessResultRenderer *next)
 
TessResultRenderernext ()
 
bool BeginDocument (const char *title)
 
bool AddImage (TessBaseAPI *api)
 
bool EndDocument ()
 
const char * file_extension () const
 
const char * title () const
 
int imagenum () const
 

Protected Member Functions

virtual bool BeginDocumentHandler ()
 
virtual bool AddImageHandler (TessBaseAPI *api)
 
virtual bool EndDocumentHandler ()
 
- Protected Member Functions inherited from tesseract::TessResultRenderer
 TessResultRenderer (const char *outputbase, const char *extension)
 
void AppendString (const char *s)
 
void AppendData (const char *s, int len)
 

Detailed Description

Renders tesseract output into searchable PDF

Definition at line 186 of file renderer.h.

Constructor & Destructor Documentation

◆ TessPDFRenderer()

tesseract::TessPDFRenderer::TessPDFRenderer ( const char *  outputbase,
const char *  datadir,
bool  textonly 
)

Definition at line 183 of file pdfrenderer.cpp.

185  : TessResultRenderer(outputbase, "pdf") {
186  obj_ = 0;
187  datadir_ = datadir;
188  textonly_ = textonly;
189  offsets_.push_back(0);
190 }
int push_back(T object)
TessResultRenderer(const char *outputbase, const char *extension)
Definition: renderer.cpp:33

Member Function Documentation

◆ AddImageHandler()

bool tesseract::TessPDFRenderer::AddImageHandler ( TessBaseAPI api)
protectedvirtual

Implements tesseract::TessResultRenderer.

Definition at line 839 of file pdfrenderer.cpp.

839  {
840  size_t n;
841  char buf[kBasicBufSize];
842  char buf2[kBasicBufSize];
843  Pix *pix = api->GetInputImage();
844  char *filename = (char *)api->GetInputName();
845  int ppi = api->GetSourceYResolution();
846  if (!pix || ppi <= 0)
847  return false;
848  double width = pixGetWidth(pix) * 72.0 / ppi;
849  double height = pixGetHeight(pix) * 72.0 / ppi;
850 
851  snprintf(buf2, sizeof(buf2), "/XObject << /Im1 %ld 0 R >>\n", obj_ + 2);
852  const char *xobject = (textonly_) ? "" : buf2;
853 
854  // PAGE
855  n = snprintf(buf, sizeof(buf),
856  "%ld 0 obj\n"
857  "<<\n"
858  " /Type /Page\n"
859  " /Parent %ld 0 R\n"
860  " /MediaBox [0 0 %.2f %.2f]\n"
861  " /Contents %ld 0 R\n"
862  " /Resources\n"
863  " <<\n"
864  " %s"
865  " /ProcSet [ /PDF /Text /ImageB /ImageI /ImageC ]\n"
866  " /Font << /f-0-0 %ld 0 R >>\n"
867  " >>\n"
868  ">>\n"
869  "endobj\n",
870  obj_,
871  2L, // Pages object
872  width, height,
873  obj_ + 1, // Contents object
874  xobject, // Image object
875  3L); // Type0 Font
876  if (n >= sizeof(buf)) return false;
877  pages_.push_back(obj_);
878  AppendPDFObject(buf);
879 
880  // CONTENTS
881  const std::unique_ptr<char[]> pdftext(GetPDFTextObjects(api, width, height));
882  const size_t pdftext_len = strlen(pdftext.get());
883  size_t len;
884  unsigned char *comp_pdftext = zlibCompress(
885  reinterpret_cast<unsigned char *>(pdftext.get()), pdftext_len, &len);
886  long comp_pdftext_len = len;
887  n = snprintf(buf, sizeof(buf),
888  "%ld 0 obj\n"
889  "<<\n"
890  " /Length %ld /Filter /FlateDecode\n"
891  ">>\n"
892  "stream\n", obj_, comp_pdftext_len);
893  if (n >= sizeof(buf)) {
894  lept_free(comp_pdftext);
895  return false;
896  }
897  AppendString(buf);
898  long objsize = strlen(buf);
899  AppendData(reinterpret_cast<char *>(comp_pdftext), comp_pdftext_len);
900  objsize += comp_pdftext_len;
901  lept_free(comp_pdftext);
902  const char *b2 =
903  "endstream\n"
904  "endobj\n";
905  AppendString(b2);
906  objsize += strlen(b2);
907  AppendPDFObjectDIY(objsize);
908 
909  if (!textonly_) {
910  char *pdf_object = nullptr;
911  if (!imageToPDFObj(pix, filename, obj_, &pdf_object, &objsize)) {
912  return false;
913  }
914  AppendData(pdf_object, objsize);
915  AppendPDFObjectDIY(objsize);
916  delete[] pdf_object;
917  }
918  return true;
919 }
int push_back(T object)
void AppendString(const char *s)
Definition: renderer.cpp:102
void AppendData(const char *s, int len)
Definition: renderer.cpp:106

◆ BeginDocumentHandler()

bool tesseract::TessPDFRenderer::BeginDocumentHandler ( )
protectedvirtual

Reimplemented from tesseract::TessResultRenderer.

Definition at line 496 of file pdfrenderer.cpp.

496  {
497  char buf[kBasicBufSize];
498  size_t n;
499 
500  n = snprintf(buf, sizeof(buf),
501  "%%PDF-1.5\n"
502  "%%%c%c%c%c\n",
503  0xDE, 0xAD, 0xBE, 0xEB);
504  if (n >= sizeof(buf)) return false;
505  AppendPDFObject(buf);
506 
507  // CATALOG
508  n = snprintf(buf, sizeof(buf),
509  "1 0 obj\n"
510  "<<\n"
511  " /Type /Catalog\n"
512  " /Pages %ld 0 R\n"
513  ">>\n"
514  "endobj\n",
515  2L);
516  if (n >= sizeof(buf)) return false;
517  AppendPDFObject(buf);
518 
519  // We are reserving object #2 for the /Pages
520  // object, which I am going to create and write
521  // at the end of the PDF file.
522  AppendPDFObject("");
523 
524  // TYPE0 FONT
525  n = snprintf(buf, sizeof(buf),
526  "3 0 obj\n"
527  "<<\n"
528  " /BaseFont /GlyphLessFont\n"
529  " /DescendantFonts [ %ld 0 R ]\n"
530  " /Encoding /Identity-H\n"
531  " /Subtype /Type0\n"
532  " /ToUnicode %ld 0 R\n"
533  " /Type /Font\n"
534  ">>\n"
535  "endobj\n",
536  4L, // CIDFontType2 font
537  6L // ToUnicode
538  );
539  if (n >= sizeof(buf)) return false;
540  AppendPDFObject(buf);
541 
542  // CIDFONTTYPE2
543  n = snprintf(buf, sizeof(buf),
544  "4 0 obj\n"
545  "<<\n"
546  " /BaseFont /GlyphLessFont\n"
547  " /CIDToGIDMap %ld 0 R\n"
548  " /CIDSystemInfo\n"
549  " <<\n"
550  " /Ordering (Identity)\n"
551  " /Registry (Adobe)\n"
552  " /Supplement 0\n"
553  " >>\n"
554  " /FontDescriptor %ld 0 R\n"
555  " /Subtype /CIDFontType2\n"
556  " /Type /Font\n"
557  " /DW %d\n"
558  ">>\n"
559  "endobj\n",
560  5L, // CIDToGIDMap
561  7L, // Font descriptor
562  1000 / kCharWidth);
563  if (n >= sizeof(buf)) return false;
564  AppendPDFObject(buf);
565 
566  // CIDTOGIDMAP
567  const int kCIDToGIDMapSize = 2 * (1 << 16);
568  const std::unique_ptr<unsigned char[]> cidtogidmap(
569  new unsigned char[kCIDToGIDMapSize]);
570  for (int i = 0; i < kCIDToGIDMapSize; i++) {
571  cidtogidmap[i] = (i % 2) ? 1 : 0;
572  }
573  size_t len;
574  unsigned char *comp = zlibCompress(cidtogidmap.get(), kCIDToGIDMapSize, &len);
575  n = snprintf(buf, sizeof(buf),
576  "5 0 obj\n"
577  "<<\n"
578  " /Length %lu /Filter /FlateDecode\n"
579  ">>\n"
580  "stream\n",
581  (unsigned long)len);
582  if (n >= sizeof(buf)) {
583  lept_free(comp);
584  return false;
585  }
586  AppendString(buf);
587  long objsize = strlen(buf);
588  AppendData(reinterpret_cast<char *>(comp), len);
589  objsize += len;
590  lept_free(comp);
591  const char *endstream_endobj =
592  "endstream\n"
593  "endobj\n";
594  AppendString(endstream_endobj);
595  objsize += strlen(endstream_endobj);
596  AppendPDFObjectDIY(objsize);
597 
598  const char *stream =
599  "/CIDInit /ProcSet findresource begin\n"
600  "12 dict begin\n"
601  "begincmap\n"
602  "/CIDSystemInfo\n"
603  "<<\n"
604  " /Registry (Adobe)\n"
605  " /Ordering (UCS)\n"
606  " /Supplement 0\n"
607  ">> def\n"
608  "/CMapName /Adobe-Identify-UCS def\n"
609  "/CMapType 2 def\n"
610  "1 begincodespacerange\n"
611  "<0000> <FFFF>\n"
612  "endcodespacerange\n"
613  "1 beginbfrange\n"
614  "<0000> <FFFF> <0000>\n"
615  "endbfrange\n"
616  "endcmap\n"
617  "CMapName currentdict /CMap defineresource pop\n"
618  "end\n"
619  "end\n";
620 
621  // TOUNICODE
622  n = snprintf(buf, sizeof(buf),
623  "6 0 obj\n"
624  "<< /Length %lu >>\n"
625  "stream\n"
626  "%s"
627  "endstream\n"
628  "endobj\n", (unsigned long) strlen(stream), stream);
629  if (n >= sizeof(buf)) return false;
630  AppendPDFObject(buf);
631 
632  // FONT DESCRIPTOR
633  n = snprintf(buf, sizeof(buf),
634  "7 0 obj\n"
635  "<<\n"
636  " /Ascent %d\n"
637  " /CapHeight %d\n"
638  " /Descent -1\n" // Spec says must be negative
639  " /Flags 5\n" // FixedPitch + Symbolic
640  " /FontBBox [ 0 0 %d %d ]\n"
641  " /FontFile2 %ld 0 R\n"
642  " /FontName /GlyphLessFont\n"
643  " /ItalicAngle 0\n"
644  " /StemV 80\n"
645  " /Type /FontDescriptor\n"
646  ">>\n"
647  "endobj\n",
648  1000,
649  1000,
650  1000 / kCharWidth,
651  1000,
652  8L // Font data
653  );
654  if (n >= sizeof(buf)) return false;
655  AppendPDFObject(buf);
656 
657  n = snprintf(buf, sizeof(buf), "%s/pdf.ttf", datadir_);
658  if (n >= sizeof(buf)) return false;
659  FILE *fp = fopen(buf, "rb");
660  if (!fp) {
661  tprintf("Can not open file \"%s\"!\n", buf);
662  return false;
663  }
664  fseek(fp, 0, SEEK_END);
665  long int size = ftell(fp);
666  fseek(fp, 0, SEEK_SET);
667  const std::unique_ptr<char[]> buffer(new char[size]);
668  if (fread(buffer.get(), 1, size, fp) != static_cast<size_t>(size)) {
669  fclose(fp);
670  return false;
671  }
672  fclose(fp);
673  // FONTFILE2
674  n = snprintf(buf, sizeof(buf),
675  "8 0 obj\n"
676  "<<\n"
677  " /Length %ld\n"
678  " /Length1 %ld\n"
679  ">>\n"
680  "stream\n", size, size);
681  if (n >= sizeof(buf)) {
682  return false;
683  }
684  AppendString(buf);
685  objsize = strlen(buf);
686  AppendData(buffer.get(), size);
687  objsize += size;
688  AppendString(endstream_endobj);
689  objsize += strlen(endstream_endobj);
690  AppendPDFObjectDIY(objsize);
691  return true;
692 }
#define tprintf(...)
Definition: tprintf.h:31
void AppendString(const char *s)
Definition: renderer.cpp:102
void AppendData(const char *s, int len)
Definition: renderer.cpp:106

◆ EndDocumentHandler()

bool tesseract::TessPDFRenderer::EndDocumentHandler ( )
protectedvirtual

Reimplemented from tesseract::TessResultRenderer.

Definition at line 922 of file pdfrenderer.cpp.

922  {
923  size_t n;
924  char buf[kBasicBufSize];
925 
926  // We reserved the /Pages object number early, so that the /Page
927  // objects could refer to their parent. We finally have enough
928  // information to go fill it in. Using lower level calls to manipulate
929  // the offset record in two spots, because we are placing objects
930  // out of order in the file.
931 
932  // PAGES
933  const long int kPagesObjectNumber = 2;
934  offsets_[kPagesObjectNumber] = offsets_.back(); // manipulation #1
935  n = snprintf(buf, sizeof(buf),
936  "%ld 0 obj\n"
937  "<<\n"
938  " /Type /Pages\n"
939  " /Kids [ ", kPagesObjectNumber);
940  if (n >= sizeof(buf)) return false;
941  AppendString(buf);
942  size_t pages_objsize = strlen(buf);
943  for (size_t i = 0; i < pages_.unsigned_size(); i++) {
944  n = snprintf(buf, sizeof(buf),
945  "%ld 0 R ", pages_[i]);
946  if (n >= sizeof(buf)) return false;
947  AppendString(buf);
948  pages_objsize += strlen(buf);
949  }
950  n = snprintf(buf, sizeof(buf),
951  "]\n"
952  " /Count %d\n"
953  ">>\n"
954  "endobj\n", pages_.size());
955  if (n >= sizeof(buf)) return false;
956  AppendString(buf);
957  pages_objsize += strlen(buf);
958  offsets_.back() += pages_objsize; // manipulation #2
959 
960  // INFO
961  STRING utf16_title = "FEFF"; // byte_order_marker
962  std::vector<char32> unicodes = UNICHAR::UTF8ToUTF32(title());
963  char utf16[kMaxBytesPerCodepoint];
964  for (char32 code : unicodes) {
965  if (CodepointToUtf16be(code, utf16)) {
966  utf16_title += utf16;
967  }
968  }
969 
970  char* datestr = l_getFormattedDate();
971  n = snprintf(buf, sizeof(buf),
972  "%ld 0 obj\n"
973  "<<\n"
974  " /Producer (Tesseract %s)\n"
975  " /CreationDate (D:%s)\n"
976  " /Title <%s>\n"
977  ">>\n"
978  "endobj\n",
979  obj_, TESSERACT_VERSION_STR, datestr, utf16_title.c_str());
980  lept_free(datestr);
981  if (n >= sizeof(buf)) return false;
982  AppendPDFObject(buf);
983  n = snprintf(buf, sizeof(buf),
984  "xref\n"
985  "0 %ld\n"
986  "0000000000 65535 f \n", obj_);
987  if (n >= sizeof(buf)) return false;
988  AppendString(buf);
989  for (int i = 1; i < obj_; i++) {
990  n = snprintf(buf, sizeof(buf), "%010ld 00000 n \n", offsets_[i]);
991  if (n >= sizeof(buf)) return false;
992  AppendString(buf);
993  }
994  n = snprintf(buf, sizeof(buf),
995  "trailer\n"
996  "<<\n"
997  " /Size %ld\n"
998  " /Root %ld 0 R\n"
999  " /Info %ld 0 R\n"
1000  ">>\n"
1001  "startxref\n"
1002  "%ld\n"
1003  "%%%%EOF\n",
1004  obj_,
1005  1L, // catalog
1006  obj_ - 1, // info
1007  offsets_.back());
1008  if (n >= sizeof(buf)) return false;
1009  AppendString(buf);
1010  return true;
1011 }
bool CodepointToUtf16be(int code, char utf16[kMaxBytesPerCodepoint])
T & back() const
int size() const
Definition: genericvector.h:72
signed int char32
Definition: strngs.h:45
size_t unsigned_size() const
Definition: genericvector.h:76
static std::vector< char32 > UTF8ToUTF32(const char *utf8_str)
Definition: unichar.cpp:213
const char * title() const
Definition: renderer.h:81
void AppendString(const char *s)
Definition: renderer.cpp:102
const char * c_str() const
Definition: strngs.cpp:209
#define TESSERACT_VERSION_STR
Definition: version.h:8

The documentation for this class was generated from the following files: