3 #include "config_auto.h"
11 #include "allheaders.h"
171 void TessPDFRenderer::AppendPDFObjectDIY(
size_t objectsize) {
176 void TessPDFRenderer::AppendPDFObject(
const char *data) {
177 AppendPDFObjectDIY(strlen(data));
185 double kPrecision = 1000.0;
186 double a =
round(x * kPrecision) / kPrecision;
192 long dist2(
int x1,
int y1,
int x2,
int y2) {
193 return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
205 int word_x1,
int word_y1,
int word_x2,
int word_y2,
206 int line_x1,
int line_y1,
int line_x2,
int line_y2,
207 double *x0,
double *y0,
double *length) {
209 Swap(&word_x1, &word_x2);
210 Swap(&word_y1, &word_y2);
217 double l2 =
dist2(line_x1, line_y1, line_x2, line_y2);
222 double t = ((px - line_x2) * (line_x2 - line_x1) +
223 (py - line_y2) * (line_y2 - line_y1)) / l2;
224 x = line_x2 + t * (line_x2 - line_x1);
225 y = line_y2 + t * (line_y2 - line_y1);
227 word_length = sqrt(static_cast<double>(
dist2(word_x1, word_y1,
229 word_length = word_length * 72.0 / ppi;
231 y = height - (y * 72.0 / ppi);
235 *length = word_length;
247 int line_x1,
int line_y1,
int line_x2,
int line_y2,
248 double *a,
double *b,
double *c,
double *d) {
249 double theta = atan2(static_cast<double>(line_y1 - line_y2),
250 static_cast<double>(line_x2 - line_x1));
255 switch(writing_direction) {
276 int *line_x1,
int *line_y1,
277 int *line_x2,
int *line_y2) {
282 double rise = abs(y2 - y1) * 72 / ppi;
283 double run = abs(x2 - x1) * 72 / ppi;
284 if (rise < 2.0 && 2.0 < run)
285 *line_y1 = *line_y2 = (y1 + y2) / 2;
289 double width,
double height) {
291 double ppi = api->GetSourceYResolution();
294 double old_x = 0.0, old_y = 0.0;
295 int old_fontsize = 0;
298 bool new_block =
true;
309 pdf_str.add_str_double(
"",
prec(width));
311 pdf_str.add_str_double(
"",
prec(height));
312 pdf_str +=
" 0 0 cm /Im1 Do Q\n";
314 ResultIterator *res_it = api->GetIterator();
316 if (res_it->IsAtBeginningOf(
RIL_BLOCK)) {
317 pdf_str +=
"BT\n3 Tr";
322 int line_x1, line_y1, line_x2, line_y2;
326 ClipBaseline(ppi, x1, y1, x2, y2, &line_x1, &line_y1, &line_x2, &line_y2);
340 res_it->Orientation(&orientation, &writing_direction,
341 &textline_order, &deskew_angle);
343 switch (res_it->WordDirection()) {
351 writing_direction = old_writing_direction;
357 double x, y, word_length;
359 int word_x1, word_y1, word_x2, word_y2;
360 res_it->Baseline(
RIL_WORD, &word_x1, &word_y1, &word_x2, &word_y2);
362 word_x1, word_y1, word_x2, word_y2,
363 line_x1, line_y1, line_x2, line_y2,
364 &x, &y, &word_length);
367 if (writing_direction != old_writing_direction || new_block) {
369 line_x1, line_y1, line_x2, line_y2, &a, &b, &c, &d);
370 pdf_str.add_str_double(
" ",
prec(a));
371 pdf_str.add_str_double(
" ",
prec(b));
372 pdf_str.add_str_double(
" ",
prec(c));
373 pdf_str.add_str_double(
" ",
prec(d));
374 pdf_str.add_str_double(
" ",
prec(x));
375 pdf_str.add_str_double(
" ",
prec(y));
379 double dx = x - old_x;
380 double dy = y - old_y;
381 pdf_str.add_str_double(
" ",
prec(dx * a + dy * b));
382 pdf_str.add_str_double(
" ",
prec(dx * c + dy * d));
387 old_writing_direction = writing_direction;
394 bool bold, italic, underlined, monospace, serif, smallcaps;
396 res_it->WordFontAttributes(&bold, &italic, &underlined, &monospace,
397 &serif, &smallcaps, &fontsize, &font_id);
398 const int kDefaultFontsize = 8;
400 fontsize = kDefaultFontsize;
401 if (fontsize != old_fontsize) {
403 snprintf(textfont,
sizeof(textfont),
"/f-0-0 %d Tf ", fontsize);
405 old_fontsize = fontsize;
412 int pdf_word_len = 0;
414 const char *grapheme = res_it->GetUTF8Text(
RIL_SYMBOL);
415 if (grapheme && grapheme[0] !=
'\0') {
421 for (
int i = 0; i < static_cast<int>(utf32.length()); i++) {
422 snprintf(utf16,
sizeof(utf16),
"<%04X>", utf32[i]);
430 if (word_length > 0 && pdf_word_len > 0 && fontsize > 0) {
432 kCharWidth *
prec(100.0 * word_length / (fontsize * pdf_word_len));
433 pdf_str.add_str_double(
"", h_stretch);
439 if (last_word_in_line) {
442 if (last_word_in_block) {
446 char *ret =
new char[pdf_str.length() + 1];
447 strcpy(ret, pdf_str.string());
456 n = snprintf(buf,
sizeof(buf),
459 0xDE, 0xAD, 0xBE, 0xEB);
460 if (n >=
sizeof(buf))
return false;
461 AppendPDFObject(buf);
464 n = snprintf(buf,
sizeof(buf),
472 if (n >=
sizeof(buf))
return false;
473 AppendPDFObject(buf);
481 n = snprintf(buf,
sizeof(buf),
484 " /BaseFont /GlyphLessFont\n"
485 " /DescendantFonts [ %ld 0 R ]\n"
486 " /Encoding /Identity-H\n"
488 " /ToUnicode %ld 0 R\n"
495 if (n >=
sizeof(buf))
return false;
496 AppendPDFObject(buf);
499 n = snprintf(buf,
sizeof(buf),
502 " /BaseFont /GlyphLessFont\n"
503 " /CIDToGIDMap %ld 0 R\n"
506 " /Ordering (Identity)\n"
507 " /Registry (Adobe)\n"
510 " /FontDescriptor %ld 0 R\n"
511 " /Subtype /CIDFontType2\n"
519 if (n >=
sizeof(buf))
return false;
520 AppendPDFObject(buf);
523 const int kCIDToGIDMapSize = 2 * (1 << 16);
524 unsigned char *cidtogidmap =
new unsigned char[kCIDToGIDMapSize];
525 for (
int i = 0; i < kCIDToGIDMapSize; i++) {
526 cidtogidmap[i] = (i % 2) ? 1 : 0;
529 unsigned char *comp =
530 zlibCompress(cidtogidmap, kCIDToGIDMapSize, &len);
531 delete[] cidtogidmap;
532 n = snprintf(buf,
sizeof(buf),
535 " /Length %ld /Filter /FlateDecode\n"
538 if (n >=
sizeof(buf)) {
543 long objsize = strlen(buf);
544 AppendData(reinterpret_cast<char *>(comp), len);
547 const char *endstream_endobj =
551 objsize += strlen(endstream_endobj);
552 AppendPDFObjectDIY(objsize);
555 "/CIDInit /ProcSet findresource begin\n"
560 " /Registry (Adobe)\n"
564 "/CMapName /Adobe-Identify-UCS def\n"
566 "1 begincodespacerange\n"
568 "endcodespacerange\n"
570 "<0000> <FFFF> <0000>\n"
573 "CMapName currentdict /CMap defineresource pop\n"
578 n = snprintf(buf,
sizeof(buf),
580 "<< /Length %lu >>\n"
584 "endobj\n", (
unsigned long) strlen(stream), stream);
585 if (n >=
sizeof(buf))
return false;
586 AppendPDFObject(buf);
589 const int kCharHeight = 2;
590 n = snprintf(buf,
sizeof(buf),
597 " /FontBBox [ 0 0 %d %d ]\n"
598 " /FontFile2 %ld 0 R\n"
599 " /FontName /GlyphLessFont\n"
602 " /Type /FontDescriptor\n"
611 if (n >=
sizeof(buf))
return false;
612 AppendPDFObject(buf);
614 n = snprintf(buf,
sizeof(buf),
"%s/pdf.ttf", datadir_);
615 if (n >=
sizeof(buf))
return false;
616 FILE *fp = fopen(buf,
"rb");
618 tprintf(
"Can not open file \"%s\"!\n", buf);
621 fseek(fp, 0, SEEK_END);
622 long int size = ftell(fp);
623 fseek(fp, 0, SEEK_SET);
624 char *buffer =
new char[size];
625 if (fread(buffer, 1, size, fp) != size) {
632 n = snprintf(buf,
sizeof(buf),
638 "stream\n", size, size);
639 if (n >=
sizeof(buf)) {
644 objsize = strlen(buf);
649 objsize += strlen(endstream_endobj);
650 AppendPDFObjectDIY(objsize);
654 bool TessPDFRenderer::imageToPDFObj(Pix *pix,
658 long int *pdf_object_size) {
663 if (!pdf_object_size || !pdf_object)
666 *pdf_object_size = 0;
670 L_COMP_DATA *cid =
NULL;
671 const int kJpegQuality = 85;
679 findFileFormat(filename, &format);
680 if (pixGetSpp(pix) == 4 && format == IFF_PNG) {
682 sad = pixGenerateCIData(pix, L_FLATE_ENCODE, 0, 0, &cid);
684 sad = l_generateCIDataForPdf(filename, pix, kJpegQuality, &cid);
688 l_CIDataDestroy(&cid);
692 const char *group4 =
"";
696 filter =
"/FlateDecode";
699 filter =
"/DCTDecode";
702 filter =
"/CCITTFaxDecode";
706 filter =
"/JPXDecode";
709 l_CIDataDestroy(&cid);
716 const char *colorspace;
717 if (cid->ncolors > 0) {
718 n = snprintf(b0,
sizeof(b0),
719 " /ColorSpace [ /Indexed /DeviceRGB %d %s ]\n",
720 cid->ncolors - 1, cid->cmapdatahex);
721 if (n >=
sizeof(b0)) {
722 l_CIDataDestroy(&cid);
729 colorspace =
" /ColorSpace /DeviceGray\n";
732 colorspace =
" /ColorSpace /DeviceRGB\n";
735 l_CIDataDestroy(&cid);
740 int predictor = (cid->predictor) ? 14 : 1;
743 n = snprintf(b1,
sizeof(b1),
747 " /Subtype /Image\n",
748 objnum, (
unsigned long) cid->nbytescomp);
749 if (n >=
sizeof(b1)) {
750 l_CIDataDestroy(&cid);
754 n = snprintf(b2,
sizeof(b2),
757 " /BitsPerComponent %d\n"
765 " /BitsPerComponent %d\n"
769 cid->w, cid->h, cid->bps, filter, predictor, cid->spp,
770 group4, cid->w, cid->bps);
771 if (n >=
sizeof(b2)) {
772 l_CIDataDestroy(&cid);
780 size_t b1_len = strlen(b1);
781 size_t b2_len = strlen(b2);
782 size_t b3_len = strlen(b3);
783 size_t colorspace_len = strlen(colorspace);
786 b1_len + colorspace_len + b2_len + cid->nbytescomp + b3_len;
787 *pdf_object =
new char[*pdf_object_size];
789 l_CIDataDestroy(&cid);
793 char *p = *pdf_object;
794 memcpy(p, b1, b1_len);
796 memcpy(p, colorspace, colorspace_len);
798 memcpy(p, b2, b2_len);
800 memcpy(p, cid->datacomp, cid->nbytescomp);
801 p += cid->nbytescomp;
802 memcpy(p, b3, b3_len);
803 l_CIDataDestroy(&cid);
813 if (!pix || ppi <= 0)
815 double width = pixGetWidth(pix) * 72.0 / ppi;
816 double height = pixGetHeight(pix) * 72.0 / ppi;
819 n = snprintf(buf,
sizeof(buf),
824 " /MediaBox [0 0 %.2f %.2f]\n"
825 " /Contents %ld 0 R\n"
828 " /XObject << /Im1 %ld 0 R >>\n"
829 " /ProcSet [ /PDF /Text /ImageB /ImageI /ImageC ]\n"
830 " /Font << /f-0-0 %ld 0 R >>\n"
841 if (n >=
sizeof(buf))
return false;
843 AppendPDFObject(buf);
846 char* pdftext = GetPDFTextObjects(api, width, height);
847 long pdftext_len = strlen(pdftext);
848 unsigned char *pdftext_casted =
reinterpret_cast<unsigned char *
>(pdftext);
850 unsigned char *comp_pdftext =
851 zlibCompress(pdftext_casted, pdftext_len, &len);
852 long comp_pdftext_len = len;
853 n = snprintf(buf,
sizeof(buf),
856 " /Length %ld /Filter /FlateDecode\n"
858 "stream\n", obj_, comp_pdftext_len);
859 if (n >=
sizeof(buf)) {
861 lept_free(comp_pdftext);
865 long objsize = strlen(buf);
866 AppendData(reinterpret_cast<char *>(comp_pdftext), comp_pdftext_len);
867 objsize += comp_pdftext_len;
868 lept_free(comp_pdftext);
874 objsize += strlen(b2);
875 AppendPDFObjectDIY(objsize);
878 if (!imageToPDFObj(pix, filename, obj_, &pdf_object, &objsize)) {
882 AppendPDFObjectDIY(objsize);
899 const long int kPagesObjectNumber = 2;
900 offsets_[kPagesObjectNumber] = offsets_.
back();
901 n = snprintf(buf,
sizeof(buf),
905 " /Kids [ ", kPagesObjectNumber);
906 if (n >=
sizeof(buf))
return false;
908 size_t pages_objsize = strlen(buf);
909 for (
size_t i = 0; i < pages_.
size(); i++) {
910 n = snprintf(buf,
sizeof(buf),
911 "%ld 0 R ", pages_[i]);
912 if (n >=
sizeof(buf))
return false;
914 pages_objsize += strlen(buf);
916 n = snprintf(buf,
sizeof(buf),
920 "endobj\n", pages_.
size());
921 if (n >=
sizeof(buf))
return false;
923 pages_objsize += strlen(buf);
924 offsets_.
back() += pages_objsize;
927 char* datestr = l_getFormattedDate();
928 n = snprintf(buf,
sizeof(buf),
931 " /Producer (Tesseract %s)\n"
932 " /CreationDate (D:%s)\n"
937 if (n >=
sizeof(buf))
return false;
938 AppendPDFObject(buf);
939 n = snprintf(buf,
sizeof(buf),
942 "0000000000 65535 f \n", obj_);
943 if (n >=
sizeof(buf))
return false;
945 for (
int i = 1; i < obj_; i++) {
946 n = snprintf(buf,
sizeof(buf),
"%010ld 00000 n \n", offsets_[i]);
947 if (n >=
sizeof(buf))
return false;
950 n = snprintf(buf,
sizeof(buf),
964 if (n >=
sizeof(buf))
return false;
long dist2(int x1, int y1, int x2, int y2)
virtual bool BeginDocumentHandler()
virtual bool EndDocumentHandler()
basic_string< char_32 > string_32
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 * GetInputName()
void AppendString(const char *s)
struct TessBaseAPI TessBaseAPI
const char * title() const
static void UTF8ToUTF32(const char *utf8_str, string_32 *str32)
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)
int GetSourceYResolution()
#define TESSERACT_VERSION_STR
virtual bool AddImageHandler(TessBaseAPI *api)
void ClipBaseline(int ppi, int x1, int y1, int x2, int y2, int *line_x1, int *line_y1, int *line_x2, int *line_y2)
TessPDFRenderer(const char *outputbase, const char *datadir)
void AppendData(const char *s, int len)