23 #include "allheaders.h"
188 void TessPDFRenderer::AppendPDFObjectDIY(
size_t objectsize) {
193 void TessPDFRenderer::AppendPDFObject(
const char *data) {
194 AppendPDFObjectDIY(strlen(data));
202 double kPrecision = 1000.0;
203 double a = round(x * kPrecision) / kPrecision;
209 long dist2(
int x1,
int y1,
int x2,
int y2) {
210 return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
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) {
226 Swap(&word_x1, &word_x2);
227 Swap(&word_y1, &word_y2);
234 double l2 =
dist2(line_x1, line_y1, line_x2, line_y2);
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);
244 word_length = sqrt(static_cast<double>(
dist2(word_x1, word_y1,
246 word_length = word_length * 72.0 / ppi;
248 y = height - (y * 72.0 / ppi);
252 *length = word_length;
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));
272 switch(writing_direction) {
293 int *line_x1,
int *line_y1,
294 int *line_x2,
int *line_y2) {
299 double rise = abs(y2 - y1) * 72 / ppi;
300 double run = abs(x2 - x1) * 72 / ppi;
301 if (rise < 2.0 && 2.0 < run)
302 *line_y1 = *line_y2 = (y1 + y2) / 2;
306 double width,
double height) {
308 double ppi = api->GetSourceYResolution();
311 double old_x = 0.0, old_y = 0.0;
312 int old_fontsize = 0;
315 bool new_block =
true;
326 pdf_str.add_str_double(
"",
prec(width));
328 pdf_str.add_str_double(
"",
prec(height));
329 pdf_str +=
" 0 0 cm /Im1 Do Q\n";
336 ResultIterator *res_it = api->GetIterator();
338 if (res_it->IsAtBeginningOf(
RIL_BLOCK)) {
339 pdf_str +=
"BT\n3 Tr";
347 ClipBaseline(ppi, x1, y1, x2, y2, &line_x1, &line_y1, &line_x2, &line_y2);
361 res_it->Orientation(&orientation, &writing_direction,
362 &textline_order, &deskew_angle);
364 switch (res_it->WordDirection()) {
372 writing_direction = old_writing_direction;
378 double x, y, word_length;
380 int word_x1, word_y1, word_x2, word_y2;
381 res_it->Baseline(
RIL_WORD, &word_x1, &word_y1, &word_x2, &word_y2);
383 word_x1, word_y1, word_x2, word_y2,
384 line_x1, line_y1, line_x2, line_y2,
385 &x, &y, &word_length);
388 if (writing_direction != old_writing_direction || new_block) {
390 line_x1, line_y1, line_x2, line_y2, &a, &b, &c, &d);
391 pdf_str.add_str_double(
" ",
prec(a));
392 pdf_str.add_str_double(
" ",
prec(b));
393 pdf_str.add_str_double(
" ",
prec(c));
394 pdf_str.add_str_double(
" ",
prec(d));
395 pdf_str.add_str_double(
" ",
prec(x));
396 pdf_str.add_str_double(
" ",
prec(y));
400 double dx = x - old_x;
401 double dy = y - old_y;
402 pdf_str.add_str_double(
" ",
prec(dx * a + dy * b));
403 pdf_str.add_str_double(
" ",
prec(dx * c + dy * d));
408 old_writing_direction = writing_direction;
415 bool bold, italic, underlined, monospace, serif, smallcaps;
417 res_it->WordFontAttributes(&bold, &italic, &underlined, &monospace,
418 &serif, &smallcaps, &fontsize, &font_id);
419 const int kDefaultFontsize = 8;
421 fontsize = kDefaultFontsize;
422 if (fontsize != old_fontsize) {
424 snprintf(textfont,
sizeof(textfont),
"/f-0-0 %d Tf ", fontsize);
426 old_fontsize = fontsize;
433 int pdf_word_len = 0;
435 const char *grapheme = res_it->GetUTF8Text(
RIL_SYMBOL);
436 if (grapheme && grapheme[0] !=
'\0') {
440 for (
int i = 0; i < unicodes.
length(); i++) {
441 int code = unicodes[i];
443 if ((code > 0xD7FF && code < 0xE000) || code > 0x10FFFF) {
444 tprintf(
"Dropping invalid codepoint %d\n", code);
447 if (code < 0x10000) {
448 snprintf(utf16,
sizeof(utf16),
"<%04X>", code);
450 int a = code - 0x010000;
451 int high_surrogate = (0x03FF & (a >> 10)) + 0xD800;
452 int low_surrogate = (0x03FF & a) + 0xDC00;
453 snprintf(utf16,
sizeof(utf16),
"<%04X%04X>",
454 high_surrogate, low_surrogate);
463 if (word_length > 0 && pdf_word_len > 0 && fontsize > 0) {
465 kCharWidth *
prec(100.0 * word_length / (fontsize * pdf_word_len));
466 pdf_str.add_str_double(
"", h_stretch);
472 if (last_word_in_line) {
475 if (last_word_in_block) {
479 char *ret =
new char[pdf_str.length() + 1];
480 strcpy(ret, pdf_str.string());
489 n = snprintf(buf,
sizeof(buf),
492 0xDE, 0xAD, 0xBE, 0xEB);
493 if (n >=
sizeof(buf))
return false;
494 AppendPDFObject(buf);
497 n = snprintf(buf,
sizeof(buf),
505 if (n >=
sizeof(buf))
return false;
506 AppendPDFObject(buf);
514 n = snprintf(buf,
sizeof(buf),
517 " /BaseFont /GlyphLessFont\n"
518 " /DescendantFonts [ %ld 0 R ]\n"
519 " /Encoding /Identity-H\n"
521 " /ToUnicode %ld 0 R\n"
528 if (n >=
sizeof(buf))
return false;
529 AppendPDFObject(buf);
532 n = snprintf(buf,
sizeof(buf),
535 " /BaseFont /GlyphLessFont\n"
536 " /CIDToGIDMap %ld 0 R\n"
539 " /Ordering (Identity)\n"
540 " /Registry (Adobe)\n"
543 " /FontDescriptor %ld 0 R\n"
544 " /Subtype /CIDFontType2\n"
552 if (n >=
sizeof(buf))
return false;
553 AppendPDFObject(buf);
556 const int kCIDToGIDMapSize = 2 * (1 << 16);
557 unsigned char *cidtogidmap =
new unsigned char[kCIDToGIDMapSize];
558 for (
int i = 0; i < kCIDToGIDMapSize; i++) {
559 cidtogidmap[i] = (i % 2) ? 1 : 0;
562 unsigned char *comp =
563 zlibCompress(cidtogidmap, kCIDToGIDMapSize, &len);
564 delete[] cidtogidmap;
565 n = snprintf(buf,
sizeof(buf),
568 " /Length %lu /Filter /FlateDecode\n"
572 if (n >=
sizeof(buf)) {
577 long objsize = strlen(buf);
578 AppendData(reinterpret_cast<char *>(comp), len);
581 const char *endstream_endobj =
585 objsize += strlen(endstream_endobj);
586 AppendPDFObjectDIY(objsize);
589 "/CIDInit /ProcSet findresource begin\n"
594 " /Registry (Adobe)\n"
598 "/CMapName /Adobe-Identify-UCS def\n"
600 "1 begincodespacerange\n"
602 "endcodespacerange\n"
604 "<0000> <FFFF> <0000>\n"
607 "CMapName currentdict /CMap defineresource pop\n"
612 n = snprintf(buf,
sizeof(buf),
614 "<< /Length %lu >>\n"
618 "endobj\n", (
unsigned long) strlen(stream), stream);
619 if (n >=
sizeof(buf))
return false;
620 AppendPDFObject(buf);
623 n = snprintf(buf,
sizeof(buf),
630 " /FontBBox [ 0 0 %d %d ]\n"
631 " /FontFile2 %ld 0 R\n"
632 " /FontName /GlyphLessFont\n"
635 " /Type /FontDescriptor\n"
644 if (n >=
sizeof(buf))
return false;
645 AppendPDFObject(buf);
647 n = snprintf(buf,
sizeof(buf),
"%s/pdf.ttf", datadir_);
648 if (n >=
sizeof(buf))
return false;
649 FILE *fp = fopen(buf,
"rb");
651 tprintf(
"Can not open file \"%s\"!\n", buf);
654 fseek(fp, 0, SEEK_END);
655 long int size = ftell(fp);
656 fseek(fp, 0, SEEK_SET);
657 char *buffer =
new char[size];
658 if (fread(buffer, 1, size, fp) != size) {
665 n = snprintf(buf,
sizeof(buf),
671 "stream\n", size, size);
672 if (n >=
sizeof(buf)) {
677 objsize = strlen(buf);
682 objsize += strlen(endstream_endobj);
683 AppendPDFObjectDIY(objsize);
687 bool TessPDFRenderer::imageToPDFObj(Pix *pix,
691 long int *pdf_object_size) {
696 if (!pdf_object_size || !pdf_object)
699 *pdf_object_size = 0;
703 L_COMP_DATA *cid = NULL;
704 const int kJpegQuality = 85;
712 findFileFormat(filename, &format);
713 if (pixGetSpp(pix) == 4 && format == IFF_PNG) {
714 Pix *p1 = pixAlphaBlendUniform(pix, 0xffffff00);
715 sad = pixGenerateCIData(p1, L_FLATE_ENCODE, 0, 0, &cid);
718 sad = l_generateCIDataForPdf(filename, pix, kJpegQuality, &cid);
722 l_CIDataDestroy(&cid);
726 const char *group4 =
"";
730 filter =
"/FlateDecode";
733 filter =
"/DCTDecode";
736 filter =
"/CCITTFaxDecode";
740 filter =
"/JPXDecode";
743 l_CIDataDestroy(&cid);
750 const char *colorspace;
751 if (cid->ncolors > 0) {
752 n = snprintf(b0,
sizeof(b0),
753 " /ColorSpace [ /Indexed /DeviceRGB %d %s ]\n",
754 cid->ncolors - 1, cid->cmapdatahex);
755 if (n >=
sizeof(b0)) {
756 l_CIDataDestroy(&cid);
763 colorspace =
" /ColorSpace /DeviceGray\n";
766 colorspace =
" /ColorSpace /DeviceRGB\n";
769 l_CIDataDestroy(&cid);
774 int predictor = (cid->predictor) ? 14 : 1;
777 n = snprintf(b1,
sizeof(b1),
781 " /Subtype /Image\n",
782 objnum, (
unsigned long) cid->nbytescomp);
783 if (n >=
sizeof(b1)) {
784 l_CIDataDestroy(&cid);
788 n = snprintf(b2,
sizeof(b2),
791 " /BitsPerComponent %d\n"
799 " /BitsPerComponent %d\n"
803 cid->w, cid->h, cid->bps, filter, predictor, cid->spp,
804 group4, cid->w, cid->bps);
805 if (n >=
sizeof(b2)) {
806 l_CIDataDestroy(&cid);
814 size_t b1_len = strlen(b1);
815 size_t b2_len = strlen(b2);
816 size_t b3_len = strlen(b3);
817 size_t colorspace_len = strlen(colorspace);
820 b1_len + colorspace_len + b2_len + cid->nbytescomp + b3_len;
821 *pdf_object =
new char[*pdf_object_size];
823 char *p = *pdf_object;
824 memcpy(p, b1, b1_len);
826 memcpy(p, colorspace, colorspace_len);
828 memcpy(p, b2, b2_len);
830 memcpy(p, cid->datacomp, cid->nbytescomp);
831 p += cid->nbytescomp;
832 memcpy(p, b3, b3_len);
833 l_CIDataDestroy(&cid);
843 if (!pix || ppi <= 0)
845 double width = pixGetWidth(pix) * 72.0 / ppi;
846 double height = pixGetHeight(pix) * 72.0 / ppi;
849 n = snprintf(buf,
sizeof(buf),
854 " /MediaBox [0 0 %.2f %.2f]\n"
855 " /Contents %ld 0 R\n"
858 " /XObject << /Im1 %ld 0 R >>\n"
859 " /ProcSet [ /PDF /Text /ImageB /ImageI /ImageC ]\n"
860 " /Font << /f-0-0 %ld 0 R >>\n"
871 if (n >=
sizeof(buf))
return false;
873 AppendPDFObject(buf);
876 char* pdftext = GetPDFTextObjects(api, width, height);
877 long pdftext_len = strlen(pdftext);
878 unsigned char *pdftext_casted =
reinterpret_cast<unsigned char *
>(pdftext);
880 unsigned char *comp_pdftext =
881 zlibCompress(pdftext_casted, pdftext_len, &len);
882 long comp_pdftext_len = len;
883 n = snprintf(buf,
sizeof(buf),
886 " /Length %ld /Filter /FlateDecode\n"
888 "stream\n", obj_, comp_pdftext_len);
889 if (n >=
sizeof(buf)) {
891 lept_free(comp_pdftext);
895 long objsize = strlen(buf);
896 AppendData(reinterpret_cast<char *>(comp_pdftext), comp_pdftext_len);
897 objsize += comp_pdftext_len;
898 lept_free(comp_pdftext);
904 objsize += strlen(b2);
905 AppendPDFObjectDIY(objsize);
908 if (!imageToPDFObj(pix, filename, obj_, &pdf_object, &objsize)) {
912 AppendPDFObjectDIY(objsize);
929 const long int kPagesObjectNumber = 2;
930 offsets_[kPagesObjectNumber] = offsets_.
back();
931 n = snprintf(buf,
sizeof(buf),
935 " /Kids [ ", kPagesObjectNumber);
936 if (n >=
sizeof(buf))
return false;
938 size_t pages_objsize = strlen(buf);
939 for (
size_t i = 0; i < pages_.
size(); i++) {
940 n = snprintf(buf,
sizeof(buf),
941 "%ld 0 R ", pages_[i]);
942 if (n >=
sizeof(buf))
return false;
944 pages_objsize += strlen(buf);
946 n = snprintf(buf,
sizeof(buf),
950 "endobj\n", pages_.
size());
951 if (n >=
sizeof(buf))
return false;
953 pages_objsize += strlen(buf);
954 offsets_.
back() += pages_objsize;
957 char* datestr = l_getFormattedDate();
958 n = snprintf(buf,
sizeof(buf),
961 " /Producer (Tesseract %s)\n"
962 " /CreationDate (D:%s)\n"
967 if (n >=
sizeof(buf))
return false;
968 AppendPDFObject(buf);
969 n = snprintf(buf,
sizeof(buf),
972 "0000000000 65535 f \n", obj_);
973 if (n >=
sizeof(buf))
return false;
975 for (
int i = 1; i < obj_; i++) {
976 n = snprintf(buf,
sizeof(buf),
"%010ld 00000 n \n", offsets_[i]);
977 if (n >=
sizeof(buf))
return false;
980 n = snprintf(buf,
sizeof(buf),
994 if (n >=
sizeof(buf))
return false;
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 ClipBaseline(int ppi, int x1, int y1, int x2, int y2, int *line_x1, int *line_y1, int *line_x2, int *line_y2)
struct TessBaseAPI TessBaseAPI
void AppendString(const char *s)
virtual bool EndDocumentHandler()
TessPDFRenderer(const char *outputbase, const char *datadir)
virtual bool BeginDocumentHandler()
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 AddImageHandler(TessBaseAPI *api)
#define TESSERACT_VERSION_STR
int GetSourceYResolution()
const char * title() const
void AppendData(const char *s, int len)
static bool UTF8ToUnicode(const char *utf8_str, GenericVector< int > *unicodes)
const char * GetInputName()
long dist2(int x1, int y1, int x2, int y2)