20 #include "config_auto.h" 24 #include "allheaders.h" 173 static const int kBasicBufSize = 2048;
176 static const int kCharWidth = 2;
181 static const int kMaxBytesPerCodepoint = 20;
192 textonly_ = textonly;
196 void TessPDFRenderer::AppendPDFObjectDIY(
size_t objectsize) {
201 void TessPDFRenderer::AppendPDFObject(
const char *data) {
202 AppendPDFObjectDIY(strlen(data));
210 double kPrecision = 1000.0;
211 double a =
round(x * kPrecision) / kPrecision;
217 long dist2(
int x1,
int y1,
int x2,
int y2) {
218 return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
230 int word_x1,
int word_y1,
int word_x2,
int word_y2,
231 int line_x1,
int line_y1,
int line_x2,
int line_y2,
232 double *x0,
double *y0,
double *length) {
234 Swap(&word_x1, &word_x2);
235 Swap(&word_y1, &word_y2);
242 double l2 =
dist2(line_x1, line_y1, line_x2, line_y2);
247 double t = ((px - line_x2) * (line_x2 - line_x1) +
248 (py - line_y2) * (line_y2 - line_y1)) / l2;
249 x = line_x2 + t * (line_x2 - line_x1);
250 y = line_y2 + t * (line_y2 - line_y1);
252 word_length = sqrt(static_cast<double>(
dist2(word_x1, word_y1,
254 word_length = word_length * 72.0 / ppi;
256 y = height - (y * 72.0 / ppi);
260 *length = word_length;
272 int line_x1,
int line_y1,
int line_x2,
int line_y2,
273 double *a,
double *b,
double *c,
double *d) {
274 double theta = atan2(static_cast<double>(line_y1 - line_y2),
275 static_cast<double>(line_x2 - line_x1));
280 switch(writing_direction) {
301 int *line_x1,
int *line_y1,
302 int *line_x2,
int *line_y2) {
307 double rise = abs(y2 - y1) * 72 / ppi;
308 double run = abs(x2 - x1) * 72 / ppi;
309 if (rise < 2.0 && 2.0 < run)
310 *line_y1 = *line_y2 = (y1 + y2) / 2;
314 if ((code > 0xD7FF && code < 0xE000) || code > 0x10FFFF) {
315 tprintf(
"Dropping invalid codepoint %d\n", code);
318 if (code < 0x10000) {
319 snprintf(utf16, kMaxBytesPerCodepoint,
"%04X", code);
321 int a = code - 0x010000;
322 int high_surrogate = (0x03FF & (a >> 10)) + 0xD800;
323 int low_surrogate = (0x03FF & a) + 0xDC00;
324 snprintf(utf16, kMaxBytesPerCodepoint,
325 "%04X%04X", high_surrogate, low_surrogate);
331 double width,
double height) {
336 double old_x = 0.0, old_y = 0.0;
337 int old_fontsize = 0;
340 bool new_block =
true;
354 pdf_str +=
" 0 0 cm";
356 pdf_str +=
" /Im1 Do";
368 pdf_str +=
"BT\n3 Tr";
376 ClipBaseline(ppi, x1, y1, x2, y2, &line_x1, &line_y1, &line_x2, &line_y2);
390 res_it->
Orientation(&orientation, &writing_direction,
391 &textline_order, &deskew_angle);
401 writing_direction = old_writing_direction;
407 double x, y, word_length;
409 int word_x1, word_y1, word_x2, word_y2;
412 word_x1, word_y1, word_x2, word_y2,
413 line_x1, line_y1, line_x2, line_y2,
414 &x, &y, &word_length);
417 if (writing_direction != old_writing_direction || new_block) {
419 line_x1, line_y1, line_x2, line_y2, &a, &b, &c, &d);
429 double dx = x - old_x;
430 double dy = y - old_y;
437 old_writing_direction = writing_direction;
444 bool bold, italic, underlined, monospace, serif, smallcaps;
447 &serif, &smallcaps, &fontsize, &font_id);
448 const int kDefaultFontsize = 8;
450 fontsize = kDefaultFontsize;
451 if (fontsize != old_fontsize) {
453 snprintf(textfont,
sizeof(textfont),
"/f-0-0 %d Tf ", fontsize);
455 old_fontsize = fontsize;
462 int pdf_word_len = 0;
465 if (grapheme && grapheme[0] !=
'\0') {
468 char utf16[kMaxBytesPerCodepoint];
469 for (
int i = 0; i < unicodes.
length(); i++) {
470 int code = unicodes[i];
479 if (word_length > 0 && pdf_word_len > 0 && fontsize > 0) {
481 kCharWidth *
prec(100.0 * word_length / (fontsize * pdf_word_len));
488 if (last_word_in_line) {
491 if (last_word_in_block) {
495 char *ret =
new char[pdf_str.
length() + 1];
496 strcpy(ret, pdf_str.
string());
502 char buf[kBasicBufSize];
505 n = snprintf(buf,
sizeof(buf),
508 0xDE, 0xAD, 0xBE, 0xEB);
509 if (n >=
sizeof(buf))
return false;
510 AppendPDFObject(buf);
513 n = snprintf(buf,
sizeof(buf),
521 if (n >=
sizeof(buf))
return false;
522 AppendPDFObject(buf);
530 n = snprintf(buf,
sizeof(buf),
533 " /BaseFont /GlyphLessFont\n" 534 " /DescendantFonts [ %ld 0 R ]\n" 535 " /Encoding /Identity-H\n" 537 " /ToUnicode %ld 0 R\n" 544 if (n >=
sizeof(buf))
return false;
545 AppendPDFObject(buf);
548 n = snprintf(buf,
sizeof(buf),
551 " /BaseFont /GlyphLessFont\n" 552 " /CIDToGIDMap %ld 0 R\n" 555 " /Ordering (Identity)\n" 556 " /Registry (Adobe)\n" 559 " /FontDescriptor %ld 0 R\n" 560 " /Subtype /CIDFontType2\n" 568 if (n >=
sizeof(buf))
return false;
569 AppendPDFObject(buf);
572 const int kCIDToGIDMapSize = 2 * (1 << 16);
573 const std::unique_ptr<
unsigned char[]> cidtogidmap(
new unsigned char[kCIDToGIDMapSize]);
574 for (
int i = 0; i < kCIDToGIDMapSize; i++) {
575 cidtogidmap[i] = (i % 2) ? 1 : 0;
578 unsigned char *comp =
579 zlibCompress(cidtogidmap.get(), kCIDToGIDMapSize, &len);
580 n = snprintf(buf,
sizeof(buf),
583 " /Length %lu /Filter /FlateDecode\n" 587 if (n >=
sizeof(buf)) {
592 long objsize = strlen(buf);
593 AppendData(reinterpret_cast<char *>(comp), len);
596 const char *endstream_endobj =
600 objsize += strlen(endstream_endobj);
601 AppendPDFObjectDIY(objsize);
604 "/CIDInit /ProcSet findresource begin\n" 609 " /Registry (Adobe)\n" 613 "/CMapName /Adobe-Identify-UCS def\n" 615 "1 begincodespacerange\n" 617 "endcodespacerange\n" 619 "<0000> <FFFF> <0000>\n" 622 "CMapName currentdict /CMap defineresource pop\n" 627 n = snprintf(buf,
sizeof(buf),
629 "<< /Length %lu >>\n" 633 "endobj\n", (
unsigned long) strlen(stream), stream);
634 if (n >=
sizeof(buf))
return false;
635 AppendPDFObject(buf);
638 n = snprintf(buf,
sizeof(buf),
645 " /FontBBox [ 0 0 %d %d ]\n" 646 " /FontFile2 %ld 0 R\n" 647 " /FontName /GlyphLessFont\n" 650 " /Type /FontDescriptor\n" 659 if (n >=
sizeof(buf))
return false;
660 AppendPDFObject(buf);
662 n = snprintf(buf,
sizeof(buf),
"%s/pdf.ttf", datadir_);
663 if (n >=
sizeof(buf))
return false;
664 FILE *fp = fopen(buf,
"rb");
666 tprintf(
"Can not open file \"%s\"!\n", buf);
670 long int size = ftell(fp);
672 const std::unique_ptr<
char[]> buffer(
new char[size]);
673 if (fread(buffer.get(), 1,
size, fp) != static_cast<unsigned long>(size)) {
679 n = snprintf(buf,
sizeof(buf),
685 "stream\n", size, size);
686 if (n >=
sizeof(buf)) {
690 objsize = strlen(buf);
694 objsize += strlen(endstream_endobj);
695 AppendPDFObjectDIY(objsize);
699 bool TessPDFRenderer::imageToPDFObj(Pix *pix,
703 long int *pdf_object_size) {
705 char b0[kBasicBufSize];
706 char b1[kBasicBufSize];
707 char b2[kBasicBufSize];
708 if (!pdf_object_size || !pdf_object)
711 *pdf_object_size = 0;
715 L_Compressed_Data *cid = NULL;
716 const int kJpegQuality = 85;
719 findFileFormat(filename, &format);
720 if (pixGetSpp(pix) == 4 && format == IFF_PNG) {
721 Pix *p1 = pixAlphaBlendUniform(pix, 0xffffff00);
722 sad = pixGenerateCIData(p1, L_FLATE_ENCODE, 0, 0, &cid);
725 sad = l_generateCIDataForPdf(filename, pix, kJpegQuality, &cid);
729 l_CIDataDestroy(&cid);
733 const char *group4 =
"";
737 filter =
"/FlateDecode";
740 filter =
"/DCTDecode";
743 filter =
"/CCITTFaxDecode";
747 filter =
"/JPXDecode";
750 l_CIDataDestroy(&cid);
757 const char *colorspace;
758 if (cid->ncolors > 0) {
759 n = snprintf(b0,
sizeof(b0),
760 " /ColorSpace [ /Indexed /DeviceRGB %d %s ]\n",
761 cid->ncolors - 1, cid->cmapdatahex);
762 if (n >=
sizeof(b0)) {
763 l_CIDataDestroy(&cid);
770 colorspace =
" /ColorSpace /DeviceGray\n";
773 colorspace =
" /ColorSpace /DeviceRGB\n";
776 l_CIDataDestroy(&cid);
781 int predictor = (cid->predictor) ? 14 : 1;
784 n = snprintf(b1,
sizeof(b1),
788 " /Subtype /Image\n",
789 objnum, (
unsigned long) cid->nbytescomp);
790 if (n >=
sizeof(b1)) {
791 l_CIDataDestroy(&cid);
795 n = snprintf(b2,
sizeof(b2),
798 " /BitsPerComponent %d\n" 806 " /BitsPerComponent %d\n" 810 cid->w, cid->h, cid->bps, filter, predictor, cid->spp,
811 group4, cid->w, cid->bps);
812 if (n >=
sizeof(b2)) {
813 l_CIDataDestroy(&cid);
821 size_t b1_len = strlen(b1);
822 size_t b2_len = strlen(b2);
823 size_t b3_len = strlen(b3);
824 size_t colorspace_len = strlen(colorspace);
827 b1_len + colorspace_len + b2_len + cid->nbytescomp + b3_len;
828 *pdf_object =
new char[*pdf_object_size];
830 char *p = *pdf_object;
831 memcpy(p, b1, b1_len);
833 memcpy(p, colorspace, colorspace_len);
835 memcpy(p, b2, b2_len);
837 memcpy(p, cid->datacomp, cid->nbytescomp);
838 p += cid->nbytescomp;
839 memcpy(p, b3, b3_len);
840 l_CIDataDestroy(&cid);
846 char buf[kBasicBufSize];
847 char buf2[kBasicBufSize];
851 if (!pix || ppi <= 0)
853 double width = pixGetWidth(pix) * 72.0 / ppi;
854 double height = pixGetHeight(pix) * 72.0 / ppi;
856 snprintf(buf2,
sizeof(buf2),
"/XObject << /Im1 %ld 0 R >>\n", obj_ + 2);
857 const char *xobject = (textonly_) ?
"" : buf2;
860 n = snprintf(buf,
sizeof(buf),
865 " /MediaBox [0 0 %.2f %.2f]\n" 866 " /Contents %ld 0 R\n" 870 " /ProcSet [ /PDF /Text /ImageB /ImageI /ImageC ]\n" 871 " /Font << /f-0-0 %ld 0 R >>\n" 881 if (n >=
sizeof(buf))
return false;
883 AppendPDFObject(buf);
886 const std::unique_ptr<
char[]> pdftext(GetPDFTextObjects(api, width, height));
887 const long pdftext_len = strlen(pdftext.get());
889 unsigned char *comp_pdftext =
890 zlibCompress(reinterpret_cast<unsigned char *>(pdftext.get()), pdftext_len, &len);
891 long comp_pdftext_len = len;
892 n = snprintf(buf,
sizeof(buf),
895 " /Length %ld /Filter /FlateDecode\n" 897 "stream\n", obj_, comp_pdftext_len);
898 if (n >=
sizeof(buf)) {
899 lept_free(comp_pdftext);
903 long objsize = strlen(buf);
904 AppendData(reinterpret_cast<char *>(comp_pdftext), comp_pdftext_len);
905 objsize += comp_pdftext_len;
906 lept_free(comp_pdftext);
911 objsize += strlen(b2);
912 AppendPDFObjectDIY(objsize);
915 char *pdf_object =
nullptr;
916 if (!imageToPDFObj(pix, filename, obj_, &pdf_object, &objsize)) {
920 AppendPDFObjectDIY(objsize);
929 char buf[kBasicBufSize];
938 const long int kPagesObjectNumber = 2;
939 offsets_[kPagesObjectNumber] = offsets_.
back();
940 n = snprintf(buf,
sizeof(buf),
944 " /Kids [ ", kPagesObjectNumber);
945 if (n >=
sizeof(buf))
return false;
947 size_t pages_objsize = strlen(buf);
949 n = snprintf(buf,
sizeof(buf),
950 "%ld 0 R ", pages_[i]);
951 if (n >=
sizeof(buf))
return false;
953 pages_objsize += strlen(buf);
955 n = snprintf(buf,
sizeof(buf),
959 "endobj\n", pages_.
size());
960 if (n >=
sizeof(buf))
return false;
962 pages_objsize += strlen(buf);
963 offsets_.
back() += pages_objsize;
966 STRING utf16_title =
"FEFF";
969 char utf16[kMaxBytesPerCodepoint];
970 for (
int i = 0; i < unicodes.
length(); i++) {
971 int code = unicodes[i];
973 utf16_title += utf16;
977 char* datestr = l_getFormattedDate();
978 n = snprintf(buf,
sizeof(buf),
981 " /Producer (Tesseract %s)\n" 982 " /CreationDate (D:%s)\n" 988 if (n >=
sizeof(buf))
return false;
989 AppendPDFObject(buf);
990 n = snprintf(buf,
sizeof(buf),
993 "0000000000 65535 f \n", obj_);
994 if (n >=
sizeof(buf))
return false;
996 for (
int i = 1; i < obj_; i++) {
997 n = snprintf(buf,
sizeof(buf),
"%010ld 00000 n \n", offsets_[i]);
998 if (n >=
sizeof(buf))
return false;
1001 n = snprintf(buf,
sizeof(buf),
1015 if (n >=
sizeof(buf))
return false;
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 Empty(PageIteratorLevel level) const
unsigned int unsigned_size() const
virtual bool Next(PageIteratorLevel level)
virtual bool IsAtBeginningOf(PageIteratorLevel level) const
int GetSourceYResolution()
virtual char * GetUTF8Text(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)
const char * string() const
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)
virtual bool IsAtFinalElement(PageIteratorLevel level, PageIteratorLevel element) const
virtual bool EndDocumentHandler()
bool CodepointToUtf16be(int code, char utf16[kMaxBytesPerCodepoint])
virtual bool AddImageHandler(TessBaseAPI *api)
void add_str_double(const char *str, double number)
TessPDFRenderer(const char *outputbase, const char *datadir, bool textonly)
void AppendData(const char *s, int len)
ResultIterator * GetIterator()
virtual bool BeginDocumentHandler()
StrongScriptDirection WordDirection() const
bool Baseline(PageIteratorLevel level, int *x1, int *y1, int *x2, int *y2) const
const char * GetInputName()
const char * c_str() const
static bool UTF8ToUnicode(const char *utf8_str, GenericVector< int > *unicodes)
void ClipBaseline(int ppi, int x1, int y1, int x2, int y2, int *line_x1, int *line_y1, int *line_x2, int *line_y2)
#define TESSERACT_VERSION_STR
long dist2(int x1, int y1, int x2, int y2)
void AppendString(const char *s)
void Orientation(tesseract::Orientation *orientation, tesseract::WritingDirection *writing_direction, tesseract::TextlineOrder *textline_order, float *deskew_angle) const
const char * title() const