20 #include "config_auto.h" 24 #include "allheaders.h" 169 static const int kBasicBufSize = 2048;
172 static const int kCharWidth = 2;
177 static const int kMaxBytesPerCodepoint = 20;
188 textonly_ = textonly;
192 void TessPDFRenderer::AppendPDFObjectDIY(
size_t objectsize) {
197 void TessPDFRenderer::AppendPDFObject(
const char *data) {
198 AppendPDFObjectDIY(strlen(data));
206 double kPrecision = 1000.0;
207 double a = round(x * kPrecision) / kPrecision;
213 long dist2(
int x1,
int y1,
int x2,
int y2) {
214 return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
226 int word_x1,
int word_y1,
int word_x2,
int word_y2,
227 int line_x1,
int line_y1,
int line_x2,
int line_y2,
228 double *x0,
double *y0,
double *length) {
230 Swap(&word_x1, &word_x2);
231 Swap(&word_y1, &word_y2);
238 double l2 =
dist2(line_x1, line_y1, line_x2, line_y2);
243 double t = ((px - line_x2) * (line_x2 - line_x1) +
244 (py - line_y2) * (line_y2 - line_y1)) / l2;
245 x = line_x2 + t * (line_x2 - line_x1);
246 y = line_y2 + t * (line_y2 - line_y1);
248 word_length = sqrt(static_cast<double>(
dist2(word_x1, word_y1,
250 word_length = word_length * 72.0 / ppi;
252 y = height - (y * 72.0 / ppi);
256 *length = word_length;
268 int line_x1,
int line_y1,
int line_x2,
int line_y2,
269 double *a,
double *b,
double *c,
double *d) {
270 double theta = atan2(static_cast<double>(line_y1 - line_y2),
271 static_cast<double>(line_x2 - line_x1));
276 switch(writing_direction) {
297 int *line_x1,
int *line_y1,
298 int *line_x2,
int *line_y2) {
303 double rise = abs(y2 - y1) * 72 / ppi;
304 double run = abs(x2 - x1) * 72 / ppi;
305 if (rise < 2.0 && 2.0 < run)
306 *line_y1 = *line_y2 = (y1 + y2) / 2;
310 if ((code > 0xD7FF && code < 0xE000) || code > 0x10FFFF) {
311 tprintf(
"Dropping invalid codepoint %d\n", code);
314 if (code < 0x10000) {
315 snprintf(utf16, kMaxBytesPerCodepoint,
"%04X", code);
317 int a = code - 0x010000;
318 int high_surrogate = (0x03FF & (a >> 10)) + 0xD800;
319 int low_surrogate = (0x03FF & a) + 0xDC00;
320 snprintf(utf16, kMaxBytesPerCodepoint,
321 "%04X%04X", high_surrogate, low_surrogate);
327 double width,
double height) {
332 double old_x = 0.0, old_y = 0.0;
333 int old_fontsize = 0;
336 bool new_block =
true;
350 pdf_str +=
" 0 0 cm";
352 pdf_str +=
" /Im1 Do";
364 pdf_str +=
"BT\n3 Tr";
372 ClipBaseline(ppi, x1, y1, x2, y2, &line_x1, &line_y1, &line_x2, &line_y2);
386 res_it->
Orientation(&orientation, &writing_direction,
387 &textline_order, &deskew_angle);
397 writing_direction = old_writing_direction;
403 double x, y, word_length;
405 int word_x1, word_y1, word_x2, word_y2;
408 word_x1, word_y1, word_x2, word_y2,
409 line_x1, line_y1, line_x2, line_y2,
410 &x, &y, &word_length);
413 if (writing_direction != old_writing_direction || new_block) {
415 line_x1, line_y1, line_x2, line_y2, &a, &b, &c, &d);
425 double dx = x - old_x;
426 double dy = y - old_y;
433 old_writing_direction = writing_direction;
440 bool bold, italic, underlined, monospace, serif, smallcaps;
443 &serif, &smallcaps, &fontsize, &font_id);
444 const int kDefaultFontsize = 8;
446 fontsize = kDefaultFontsize;
447 if (fontsize != old_fontsize) {
449 snprintf(textfont,
sizeof(textfont),
"/f-0-0 %d Tf ", fontsize);
451 old_fontsize = fontsize;
458 int pdf_word_len = 0;
460 const std::unique_ptr<const char[]> grapheme(
462 if (grapheme && grapheme[0] !=
'\0') {
464 char utf16[kMaxBytesPerCodepoint];
465 for (
char32 code : unicodes) {
474 if (word_length > 0 && pdf_word_len > 0 && fontsize > 0) {
476 kCharWidth *
prec(100.0 * word_length / (fontsize * pdf_word_len));
483 if (last_word_in_line) {
486 if (last_word_in_block) {
490 char *ret =
new char[pdf_str.
length() + 1];
491 strcpy(ret, pdf_str.
string());
497 char buf[kBasicBufSize];
500 n = snprintf(buf,
sizeof(buf),
503 0xDE, 0xAD, 0xBE, 0xEB);
504 if (n >=
sizeof(buf))
return false;
505 AppendPDFObject(buf);
508 n = snprintf(buf,
sizeof(buf),
516 if (n >=
sizeof(buf))
return false;
517 AppendPDFObject(buf);
525 n = snprintf(buf,
sizeof(buf),
528 " /BaseFont /GlyphLessFont\n" 529 " /DescendantFonts [ %ld 0 R ]\n" 530 " /Encoding /Identity-H\n" 532 " /ToUnicode %ld 0 R\n" 539 if (n >=
sizeof(buf))
return false;
540 AppendPDFObject(buf);
543 n = snprintf(buf,
sizeof(buf),
546 " /BaseFont /GlyphLessFont\n" 547 " /CIDToGIDMap %ld 0 R\n" 550 " /Ordering (Identity)\n" 551 " /Registry (Adobe)\n" 554 " /FontDescriptor %ld 0 R\n" 555 " /Subtype /CIDFontType2\n" 563 if (n >=
sizeof(buf))
return false;
564 AppendPDFObject(buf);
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;
574 unsigned char *comp = zlibCompress(cidtogidmap.get(), kCIDToGIDMapSize, &len);
575 n = snprintf(buf,
sizeof(buf),
578 " /Length %lu /Filter /FlateDecode\n" 582 if (n >=
sizeof(buf)) {
587 long objsize = strlen(buf);
588 AppendData(reinterpret_cast<char *>(comp), len);
591 const char *endstream_endobj =
595 objsize += strlen(endstream_endobj);
596 AppendPDFObjectDIY(objsize);
599 "/CIDInit /ProcSet findresource begin\n" 604 " /Registry (Adobe)\n" 608 "/CMapName /Adobe-Identify-UCS def\n" 610 "1 begincodespacerange\n" 612 "endcodespacerange\n" 614 "<0000> <FFFF> <0000>\n" 617 "CMapName currentdict /CMap defineresource pop\n" 622 n = snprintf(buf,
sizeof(buf),
624 "<< /Length %lu >>\n" 628 "endobj\n", (
unsigned long) strlen(stream), stream);
629 if (n >=
sizeof(buf))
return false;
630 AppendPDFObject(buf);
633 n = snprintf(buf,
sizeof(buf),
640 " /FontBBox [ 0 0 %d %d ]\n" 641 " /FontFile2 %ld 0 R\n" 642 " /FontName /GlyphLessFont\n" 645 " /Type /FontDescriptor\n" 654 if (n >=
sizeof(buf))
return false;
655 AppendPDFObject(buf);
657 n = snprintf(buf,
sizeof(buf),
"%s/pdf.ttf", datadir_);
658 if (n >=
sizeof(buf))
return false;
659 FILE *fp = fopen(buf,
"rb");
661 tprintf(
"Can not open file \"%s\"!\n", buf);
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)) {
674 n = snprintf(buf,
sizeof(buf),
680 "stream\n", size, size);
681 if (n >=
sizeof(buf)) {
685 objsize = strlen(buf);
689 objsize += strlen(endstream_endobj);
690 AppendPDFObjectDIY(objsize);
694 bool TessPDFRenderer::imageToPDFObj(Pix *pix,
698 long int *pdf_object_size) {
700 char b0[kBasicBufSize];
701 char b1[kBasicBufSize];
702 char b2[kBasicBufSize];
703 if (!pdf_object_size || !pdf_object)
706 *pdf_object_size = 0;
710 L_Compressed_Data *cid = NULL;
711 const int kJpegQuality = 85;
714 findFileFormat(filename, &format);
715 if (pixGetSpp(pix) == 4 && format == IFF_PNG) {
716 Pix *p1 = pixAlphaBlendUniform(pix, 0xffffff00);
717 sad = pixGenerateCIData(p1, L_FLATE_ENCODE, 0, 0, &cid);
720 sad = l_generateCIDataForPdf(filename, pix, kJpegQuality, &cid);
724 l_CIDataDestroy(&cid);
728 const char *group4 =
"";
732 filter =
"/FlateDecode";
735 filter =
"/DCTDecode";
738 filter =
"/CCITTFaxDecode";
742 filter =
"/JPXDecode";
745 l_CIDataDestroy(&cid);
752 const char *colorspace;
753 if (cid->ncolors > 0) {
754 n = snprintf(b0,
sizeof(b0),
755 " /ColorSpace [ /Indexed /DeviceRGB %d %s ]\n",
756 cid->ncolors - 1, cid->cmapdatahex);
757 if (n >=
sizeof(b0)) {
758 l_CIDataDestroy(&cid);
765 colorspace =
" /ColorSpace /DeviceGray\n";
768 colorspace =
" /ColorSpace /DeviceRGB\n";
771 l_CIDataDestroy(&cid);
776 int predictor = (cid->predictor) ? 14 : 1;
779 n = snprintf(b1,
sizeof(b1),
783 " /Subtype /Image\n",
784 objnum, (
unsigned long) cid->nbytescomp);
785 if (n >=
sizeof(b1)) {
786 l_CIDataDestroy(&cid);
790 n = snprintf(b2,
sizeof(b2),
793 " /BitsPerComponent %d\n" 801 " /BitsPerComponent %d\n" 805 cid->w, cid->h, cid->bps, filter, predictor, cid->spp,
806 group4, cid->w, cid->bps);
807 if (n >=
sizeof(b2)) {
808 l_CIDataDestroy(&cid);
816 size_t b1_len = strlen(b1);
817 size_t b2_len = strlen(b2);
818 size_t b3_len = strlen(b3);
819 size_t colorspace_len = strlen(colorspace);
822 b1_len + colorspace_len + b2_len + cid->nbytescomp + b3_len;
823 *pdf_object =
new char[*pdf_object_size];
825 char *p = *pdf_object;
826 memcpy(p, b1, b1_len);
828 memcpy(p, colorspace, colorspace_len);
830 memcpy(p, b2, b2_len);
832 memcpy(p, cid->datacomp, cid->nbytescomp);
833 p += cid->nbytescomp;
834 memcpy(p, b3, b3_len);
835 l_CIDataDestroy(&cid);
841 char buf[kBasicBufSize];
842 char buf2[kBasicBufSize];
846 if (!pix || ppi <= 0)
848 double width = pixGetWidth(pix) * 72.0 / ppi;
849 double height = pixGetHeight(pix) * 72.0 / ppi;
851 snprintf(buf2,
sizeof(buf2),
"/XObject << /Im1 %ld 0 R >>\n", obj_ + 2);
852 const char *xobject = (textonly_) ?
"" : buf2;
855 n = snprintf(buf,
sizeof(buf),
860 " /MediaBox [0 0 %.2f %.2f]\n" 861 " /Contents %ld 0 R\n" 865 " /ProcSet [ /PDF /Text /ImageB /ImageI /ImageC ]\n" 866 " /Font << /f-0-0 %ld 0 R >>\n" 876 if (n >=
sizeof(buf))
return false;
878 AppendPDFObject(buf);
881 const std::unique_ptr<char[]> pdftext(GetPDFTextObjects(api, width, height));
882 const size_t pdftext_len = strlen(pdftext.get());
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),
890 " /Length %ld /Filter /FlateDecode\n" 892 "stream\n", obj_, comp_pdftext_len);
893 if (n >=
sizeof(buf)) {
894 lept_free(comp_pdftext);
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);
906 objsize += strlen(b2);
907 AppendPDFObjectDIY(objsize);
910 char *pdf_object =
nullptr;
911 if (!imageToPDFObj(pix, filename, obj_, &pdf_object, &objsize)) {
915 AppendPDFObjectDIY(objsize);
924 char buf[kBasicBufSize];
933 const long int kPagesObjectNumber = 2;
934 offsets_[kPagesObjectNumber] = offsets_.
back();
935 n = snprintf(buf,
sizeof(buf),
939 " /Kids [ ", kPagesObjectNumber);
940 if (n >=
sizeof(buf))
return false;
942 size_t pages_objsize = strlen(buf);
944 n = snprintf(buf,
sizeof(buf),
945 "%ld 0 R ", pages_[i]);
946 if (n >=
sizeof(buf))
return false;
948 pages_objsize += strlen(buf);
950 n = snprintf(buf,
sizeof(buf),
954 "endobj\n", pages_.
size());
955 if (n >=
sizeof(buf))
return false;
957 pages_objsize += strlen(buf);
958 offsets_.
back() += pages_objsize;
961 STRING utf16_title =
"FEFF";
963 char utf16[kMaxBytesPerCodepoint];
964 for (
char32 code : unicodes) {
966 utf16_title += utf16;
970 char* datestr = l_getFormattedDate();
971 n = snprintf(buf,
sizeof(buf),
974 " /Producer (Tesseract %s)\n" 975 " /CreationDate (D:%s)\n" 981 if (n >=
sizeof(buf))
return false;
982 AppendPDFObject(buf);
983 n = snprintf(buf,
sizeof(buf),
986 "0000000000 65535 f \n", obj_);
987 if (n >=
sizeof(buf))
return false;
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;
994 n = snprintf(buf,
sizeof(buf),
1008 if (n >=
sizeof(buf))
return false;
void ClipBaseline(int ppi, int x1, int y1, int x2, int y2, int *line_x1, int *line_y1, int *line_x2, int *line_y2)
bool CodepointToUtf16be(int code, char utf16[kMaxBytesPerCodepoint])
void Orientation(tesseract::Orientation *orientation, tesseract::WritingDirection *writing_direction, tesseract::TextlineOrder *textline_order, float *deskew_angle) const
const char * GetInputName()
virtual bool Next(PageIteratorLevel level)
TessPDFRenderer(const char *outputbase, const char *datadir, bool textonly)
void add_str_double(const char *str, double number)
const char * string() const
long dist2(int x1, int y1, int x2, int y2)
virtual bool BeginDocumentHandler()
virtual bool IsAtFinalElement(PageIteratorLevel level, PageIteratorLevel element) const
virtual bool EndDocumentHandler()
int GetSourceYResolution()
virtual char * GetUTF8Text(PageIteratorLevel level) const
size_t unsigned_size() const
static std::vector< char32 > UTF8ToUTF32(const char *utf8_str)
const char * title() const
bool Empty(PageIteratorLevel level) const
void GetWordBaseline(int writing_direction, int ppi, int height, int word_x1, int word_y1, int word_x2, int word_y2, int line_x1, int line_y1, int line_x2, int line_y2, double *x0, double *y0, double *length)
void AppendString(const char *s)
virtual bool IsAtBeginningOf(PageIteratorLevel level) const
virtual bool AddImageHandler(TessBaseAPI *api)
void AffineMatrix(int writing_direction, int line_x1, int line_y1, int line_x2, int line_y2, double *a, double *b, double *c, double *d)
void AppendData(const char *s, int len)
bool Baseline(PageIteratorLevel level, int *x1, int *y1, int *x2, int *y2) const
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
const char * c_str() const
StrongScriptDirection WordDirection() const
#define TESSERACT_VERSION_STR
ResultIterator * GetIterator()