1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
|
%{
/*
# ------------------------------------------------------------------------
# Copyright 2020-2022, Harald Lieder, mailto:harald.lieder@outlook.com
# License: GNU AFFERO GPL 3.0, https://www.gnu.org/licenses/agpl-3.0.html
#
# Part of "PyMuPDF", a Python binding for "MuPDF" (http://mupdf.com), a
# lightweight PDF, XPS, and E-book viewer, renderer and toolkit which is
# maintained and developed by Artifex Software, Inc. https://artifex.com.
# ------------------------------------------------------------------------
*/
// need own versions of ascender / descender
static const float
JM_font_ascender(fz_context *ctx, fz_font *font)
{
if (skip_quad_corrections) {
return 0.8f;
}
return fz_font_ascender(ctx, font);
}
static const float
JM_font_descender(fz_context *ctx, fz_font *font)
{
if (skip_quad_corrections) {
return -0.2f;
}
return fz_font_descender(ctx, font);
}
/* inactive
//-----------------------------------------------------------------------------
// Make OCR text page directly from an fz_page
//-----------------------------------------------------------------------------
fz_stext_page *
JM_new_stext_page_ocr_from_page(fz_context *ctx, fz_page *page, fz_rect rect, int flags,
const char *lang)
{
if (!page) return NULL;
int with_list = 1;
fz_stext_page *tp = NULL;
fz_device *dev = NULL, *ocr_dev = NULL;
fz_var(dev);
fz_var(ocr_dev);
fz_var(tp);
fz_stext_options options;
memset(&options, 0, sizeof options);
options.flags = flags;
//fz_matrix ctm = fz_identity;
fz_matrix ctm1 = fz_make_matrix(100/72, 0, 0, 100/72, 0, 0);
fz_matrix ctm2 = fz_make_matrix(400/72, 0, 0, 400/72, 0, 0);
fz_try(ctx) {
tp = fz_new_stext_page(ctx, rect);
dev = fz_new_stext_device(ctx, tp, &options);
ocr_dev = fz_new_ocr_device(ctx, dev, fz_identity, rect, with_list, lang, NULL, NULL);
fz_run_page(ctx, page, ocr_dev, fz_identity, NULL);
fz_close_device(ctx, ocr_dev);
fz_close_device(ctx, dev);
}
fz_always(ctx) {
fz_drop_device(ctx, dev);
fz_drop_device(ctx, ocr_dev);
}
fz_catch(ctx) {
fz_drop_stext_page(ctx, tp);
fz_rethrow(ctx);
}
return tp;
}
*/
//---------------------------------------------------------------------------
// APPEND non-ascii runes in unicode escape format to fz_buffer
//---------------------------------------------------------------------------
void JM_append_rune(fz_context *ctx, fz_buffer *buff, int ch)
{
if ((ch >= 32 && ch <= 255) || ch == 10) {
fz_append_byte(ctx, buff, ch);
} else if (ch <= 0xffff) { // 4 hex digits
fz_append_printf(ctx, buff, "\\u%04x", ch);
} else { // 8 hex digits
fz_append_printf(ctx, buff, "\\U%08x", ch);
}
}
// re-compute char quad if ascender/descender values make no sense
static fz_quad
JM_char_quad(fz_context *ctx, fz_stext_line *line, fz_stext_char *ch)
{
if (skip_quad_corrections) { // no special handling
return ch->quad;
}
if (line->wmode) { // never touch vertical write mode
return ch->quad;
}
fz_font *font = ch->font;
float asc = JM_font_ascender(ctx, font);
float dsc = JM_font_descender(ctx, font);
float c, s, fsize = ch->size;
float asc_dsc = asc - dsc + FLT_EPSILON;
if (asc_dsc >= 1 && small_glyph_heights == 0) { // no problem
return ch->quad;
}
if (asc < 1e-3) { // probably Tesseract glyphless font
dsc = -0.1f;
asc = 0.9f;
asc_dsc = 1.0f;
}
if (small_glyph_heights || asc_dsc < 1) {
dsc = dsc / asc_dsc;
asc = asc / asc_dsc;
}
asc_dsc = asc - dsc;
asc = asc * fsize / asc_dsc;
dsc = dsc * fsize / asc_dsc;
/* ------------------------------
Re-compute quad with the adjusted ascender / descender values:
Move ch->origin to (0,0) and de-rotate quad, then adjust the corners,
re-rotate and move back to ch->origin location.
------------------------------ */
fz_matrix trm1, trm2, xlate1, xlate2;
fz_quad quad;
c = line->dir.x; // cosine
s = line->dir.y; // sine
trm1 = fz_make_matrix(c, -s, s, c, 0, 0); // derotate
trm2 = fz_make_matrix(c, s, -s, c, 0, 0); // rotate
if (c == -1) { // left-right flip
trm1.d = 1;
trm2.d = 1;
}
xlate1 = fz_make_matrix(1, 0, 0, 1, -ch->origin.x, -ch->origin.y);
xlate2 = fz_make_matrix(1, 0, 0, 1, ch->origin.x, ch->origin.y);
quad = fz_transform_quad(ch->quad, xlate1); // move origin to (0,0)
quad = fz_transform_quad(quad, trm1); // de-rotate corners
// adjust vertical coordinates
if (c == 1 && quad.ul.y > 0) { // up-down flip
quad.ul.y = asc;
quad.ur.y = asc;
quad.ll.y = dsc;
quad.lr.y = dsc;
} else {
quad.ul.y = -asc;
quad.ur.y = -asc;
quad.ll.y = -dsc;
quad.lr.y = -dsc;
}
// adjust horizontal coordinates that are too crazy:
// (1) left x must be >= 0
// (2) if bbox width is 0, lookup char advance in font.
if (quad.ll.x < 0) {
quad.ll.x = 0;
quad.ul.x = 0;
}
float cwidth = quad.lr.x - quad.ll.x;
if (cwidth < FLT_EPSILON) {
int glyph = fz_encode_character(ctx, font, ch->c);
if (glyph) {
float fwidth = fz_advance_glyph(ctx, font, glyph, line->wmode);
quad.lr.x = quad.ll.x + fwidth * fsize;
quad.ur.x = quad.lr.x;
}
}
quad = fz_transform_quad(quad, trm2); // rotate back
quad = fz_transform_quad(quad, xlate2); // translate back
return quad;
}
// return rect of char quad
static fz_rect
JM_char_bbox(fz_context *ctx, fz_stext_line *line, fz_stext_char *ch)
{
fz_rect r = fz_rect_from_quad(JM_char_quad(ctx, line, ch));
if (!line->wmode) {
return r;
}
if (r.y1 < r.y0 + ch->size) {
r.y0 = r.y1 - ch->size;
}
return r;
}
//-------------------------------------------
// make a buffer from an stext_page's text
//-------------------------------------------
fz_buffer *
JM_new_buffer_from_stext_page(fz_context *ctx, fz_stext_page *page)
{
fz_stext_block *block;
fz_stext_line *line;
fz_stext_char *ch;
fz_rect rect = page->mediabox;
fz_buffer *buf = NULL;
fz_try(ctx)
{
buf = fz_new_buffer(ctx, 256);
for (block = page->first_block; block; block = block->next) {
if (block->type == FZ_STEXT_BLOCK_TEXT) {
for (line = block->u.t.first_line; line; line = line->next) {
for (ch = line->first_char; ch; ch = ch->next) {
if (!fz_contains_rect(rect, JM_char_bbox(ctx, line, ch)) &&
!fz_is_infinite_rect(rect)) {
continue;
}
fz_append_rune(ctx, buf, ch->c);
}
fz_append_byte(ctx, buf, '\n');
}
fz_append_byte(ctx, buf, '\n');
}
}
}
fz_catch(ctx) {
fz_drop_buffer(ctx, buf);
fz_rethrow(ctx);
}
return buf;
}
static float hdist(fz_point *dir, fz_point *a, fz_point *b)
{
float dx = b->x - a->x;
float dy = b->y - a->y;
return fz_abs(dx * dir->x + dy * dir->y);
}
static float vdist(fz_point *dir, fz_point *a, fz_point *b)
{
float dx = b->x - a->x;
float dy = b->y - a->y;
return fz_abs(dx * dir->y + dy * dir->x);
}
struct highlight
{
Py_ssize_t len;
PyObject *quads;
float hfuzz, vfuzz;
};
static void on_highlight_char(fz_context *ctx, void *arg, fz_stext_line *line, fz_stext_char *ch)
{
struct highlight *hits = arg;
float vfuzz = ch->size * hits->vfuzz;
float hfuzz = ch->size * hits->hfuzz;
fz_quad ch_quad = JM_char_quad(ctx, line, ch);
if (hits->len > 0) {
PyObject *quad = PySequence_ITEM(hits->quads, hits->len - 1);
fz_quad end = JM_quad_from_py(quad);
Py_DECREF(quad);
if (hdist(&line->dir, &end.lr, &ch_quad.ll) < hfuzz
&& vdist(&line->dir, &end.lr, &ch_quad.ll) < vfuzz
&& hdist(&line->dir, &end.ur, &ch_quad.ul) < hfuzz
&& vdist(&line->dir, &end.ur, &ch_quad.ul) < vfuzz)
{
end.ur = ch_quad.ur;
end.lr = ch_quad.lr;
quad = JM_py_from_quad(end);
PyList_SetItem(hits->quads, hits->len - 1, quad);
return;
}
}
LIST_APPEND_DROP(hits->quads, JM_py_from_quad(ch_quad));
hits->len++;
}
static inline int canon(int c)
{
/* TODO: proper unicode case folding */
/* TODO: character equivalence (a matches รค, etc) */
if (c == 0xA0 || c == 0x2028 || c == 0x2029)
return ' ';
if (c == '\r' || c == '\n' || c == '\t')
return ' ';
if (c >= 'A' && c <= 'Z')
return c - 'A' + 'a';
return c;
}
static inline int chartocanon(int *c, const char *s)
{
int n = fz_chartorune(c, s);
*c = canon(*c);
return n;
}
static const char *match_string(const char *h, const char *n)
{
int hc, nc;
const char *e = h;
h += chartocanon(&hc, h);
n += chartocanon(&nc, n);
while (hc == nc)
{
e = h;
if (hc == ' ')
do
h += chartocanon(&hc, h);
while (hc == ' ');
else
h += chartocanon(&hc, h);
if (nc == ' ')
do
n += chartocanon(&nc, n);
while (nc == ' ');
else
n += chartocanon(&nc, n);
}
return nc == 0 ? e : NULL;
}
static const char *find_string(const char *s, const char *needle, const char **endp)
{
const char *end;
while (*s)
{
end = match_string(s, needle);
if (end)
return *endp = end, s;
++s;
}
return *endp = NULL, NULL;
}
PyObject *
JM_search_stext_page(fz_context *ctx, fz_stext_page *page, const char *needle)
{
struct highlight hits;
fz_stext_block *block;
fz_stext_line *line;
fz_stext_char *ch;
fz_buffer *buffer = NULL;
const char *haystack, *begin, *end;
fz_rect rect = page->mediabox;
int c, inside;
if (strlen(needle) == 0) Py_RETURN_NONE;
PyObject *quads = PyList_New(0);
hits.len = 0;
hits.quads = quads;
hits.hfuzz = 0.2f; /* merge kerns but not large gaps */
hits.vfuzz = 0.1f;
fz_try(ctx) {
buffer = JM_new_buffer_from_stext_page(ctx, page);
haystack = fz_string_from_buffer(ctx, buffer);
begin = find_string(haystack, needle, &end);
if (!begin) goto no_more_matches;
inside = 0;
for (block = page->first_block; block; block = block->next) {
if (block->type != FZ_STEXT_BLOCK_TEXT) {
continue;
}
for (line = block->u.t.first_line; line; line = line->next) {
for (ch = line->first_char; ch; ch = ch->next) {
if (!fz_is_infinite_rect(rect) &&
!fz_contains_rect(rect, JM_char_bbox(ctx, line, ch))) {
goto next_char;
}
try_new_match:
if (!inside) {
if (haystack >= begin) inside = 1;
}
if (inside) {
if (haystack < end) {
on_highlight_char(ctx, &hits, line, ch);
} else {
inside = 0;
begin = find_string(haystack, needle, &end);
if (!begin) goto no_more_matches;
else goto try_new_match;
}
}
haystack += fz_chartorune(&c, haystack);
next_char:;
}
assert(*haystack == '\n');
++haystack;
}
assert(*haystack == '\n');
++haystack;
}
no_more_matches:;
}
fz_always(ctx)
fz_drop_buffer(ctx, buffer);
fz_catch(ctx)
fz_rethrow(ctx);
return quads;
}
//-----------------------------------------------------------------------------
// Plain text output. An identical copy of fz_print_stext_page_as_text,
// but lines within a block are concatenated by space instead a new-line
// character (which else leads to 2 new-lines).
//-----------------------------------------------------------------------------
void
JM_print_stext_page_as_text(fz_context *ctx, fz_output *out, fz_stext_page *page)
{
fz_stext_block *block;
fz_stext_line *line;
fz_stext_char *ch;
fz_rect rect = page->mediabox;
fz_rect chbbox;
int last_char = 0;
char utf[10];
int i, n;
for (block = page->first_block; block; block = block->next) {
if (block->type == FZ_STEXT_BLOCK_TEXT) {
for (line = block->u.t.first_line; line; line = line->next) {
last_char = 0;
for (ch = line->first_char; ch; ch = ch->next) {
chbbox = JM_char_bbox(ctx, line, ch);
if (fz_is_infinite_rect(rect) ||
fz_contains_rect(rect, chbbox)) {
last_char = ch->c;
n = fz_runetochar(utf, ch->c);
for (i = 0; i < n; i++) {
fz_write_byte(ctx, out, utf[i]);
}
}
}
if (last_char != 10 && last_char > 0) {
fz_write_string(ctx, out, "\n");
}
}
}
}
}
//-----------------------------------------------------------------------------
// Functions for wordlist output
//-----------------------------------------------------------------------------
int JM_append_word(fz_context *ctx, PyObject *lines, fz_buffer *buff, fz_rect *wbbox,
int block_n, int line_n, int word_n)
{
PyObject *s = JM_EscapeStrFromBuffer(ctx, buff);
PyObject *litem = Py_BuildValue("ffffOiii",
wbbox->x0,
wbbox->y0,
wbbox->x1,
wbbox->y1,
s,
block_n, line_n, word_n);
LIST_APPEND_DROP(lines, litem);
Py_DECREF(s);
*wbbox = fz_empty_rect;
return word_n + 1; // word counter
}
//-----------------------------------------------------------------------------
// Functions for dictionary output
//-----------------------------------------------------------------------------
static int detect_super_script(fz_stext_line *line, fz_stext_char *ch)
{
if (line->wmode == 0 && line->dir.x == 1 && line->dir.y == 0)
return ch->origin.y < line->first_char->origin.y - ch->size * 0.1f;
return 0;
}
static int JM_char_font_flags(fz_context *ctx, fz_font *font, fz_stext_line *line, fz_stext_char *ch)
{
int flags = detect_super_script(line, ch);
flags += fz_font_is_italic(ctx, font) * TEXT_FONT_ITALIC;
flags += fz_font_is_serif(ctx, font) * TEXT_FONT_SERIFED;
flags += fz_font_is_monospaced(ctx, font) * TEXT_FONT_MONOSPACED;
flags += fz_font_is_bold(ctx, font) * TEXT_FONT_BOLD;
return flags;
}
static const char *
JM_font_name(fz_context *ctx, fz_font *font)
{
const char *name = fz_font_name(ctx, font);
const char *s = strchr(name, '+');
if (subset_fontnames || s == NULL || s-name != 6) {
return name;
}
return s + 1;
}
static fz_rect
JM_make_spanlist(fz_context *ctx, PyObject *line_dict,
fz_stext_line *line, int raw, fz_buffer *buff,
fz_rect tp_rect)
{
PyObject *span = NULL, *char_list = NULL, *char_dict;
PyObject *span_list = PyList_New(0);
fz_clear_buffer(ctx, buff);
fz_stext_char *ch;
fz_rect span_rect = fz_empty_rect;
fz_rect line_rect = fz_empty_rect;
fz_point span_origin = {0, 0};
typedef struct style_s {
float size; int flags; const char *font; int color;
float asc; float desc;
} char_style;
char_style old_style = { -1, -1, "", -1, 0, 0 }, style;
for (ch = line->first_char; ch; ch = ch->next) {
fz_rect r = JM_char_bbox(ctx, line, ch);
if (!fz_contains_rect(tp_rect, r) &&
!fz_is_infinite_rect(tp_rect)) {
continue;
}
int flags = JM_char_font_flags(ctx, ch->font, line, ch);
fz_point origin = ch->origin;
style.size = ch->size;
style.flags = flags;
style.font = JM_font_name(ctx, ch->font);
style.color = ch->color;
style.asc = JM_font_ascender(ctx, ch->font);
style.desc = JM_font_descender(ctx, ch->font);
if (style.size != old_style.size ||
style.flags != old_style.flags ||
style.color != old_style.color ||
strcmp(style.font, old_style.font) != 0) {
if (old_style.size >= 0) {
// not first one, output previous
if (raw) {
// put character list in the span
DICT_SETITEM_DROP(span, dictkey_chars, char_list);
char_list = NULL;
} else {
// put text string in the span
DICT_SETITEM_DROP(span, dictkey_text, JM_EscapeStrFromBuffer(ctx, buff));
fz_clear_buffer(ctx, buff);
}
DICT_SETITEM_DROP(span, dictkey_origin,
JM_py_from_point(span_origin));
DICT_SETITEM_DROP(span, dictkey_bbox,
JM_py_from_rect(span_rect));
line_rect = fz_union_rect(line_rect, span_rect);
LIST_APPEND_DROP(span_list, span);
span = NULL;
}
span = PyDict_New();
float asc = style.asc, desc = style.desc;
if (style.asc < 1e-3) {
asc = 0.9f;
desc = -0.1f;
}
DICT_SETITEM_DROP(span, dictkey_size, Py_BuildValue("f", style.size));
DICT_SETITEM_DROP(span, dictkey_flags, Py_BuildValue("i", style.flags));
DICT_SETITEM_DROP(span, dictkey_font, JM_EscapeStrFromStr(style.font));
DICT_SETITEM_DROP(span, dictkey_color, Py_BuildValue("i", style.color));
DICT_SETITEMSTR_DROP(span, "ascender", Py_BuildValue("f", asc));
DICT_SETITEMSTR_DROP(span, "descender", Py_BuildValue("f", desc));
old_style = style;
span_rect = r;
span_origin = origin;
}
span_rect = fz_union_rect(span_rect, r);
if (raw) { // make and append a char dict
char_dict = PyDict_New();
DICT_SETITEM_DROP(char_dict, dictkey_origin,
JM_py_from_point(ch->origin));
DICT_SETITEM_DROP(char_dict, dictkey_bbox,
JM_py_from_rect(r));
DICT_SETITEM_DROP(char_dict, dictkey_c,
Py_BuildValue("C", ch->c));
if (!char_list) {
char_list = PyList_New(0);
}
LIST_APPEND_DROP(char_list, char_dict);
} else { // add character byte to buffer
JM_append_rune(ctx, buff, ch->c);
}
}
// all characters processed, now flush remaining span
if (span) {
if (raw) {
DICT_SETITEM_DROP(span, dictkey_chars, char_list);
char_list = NULL;
} else {
DICT_SETITEM_DROP(span, dictkey_text, JM_EscapeStrFromBuffer(ctx, buff));
fz_clear_buffer(ctx, buff);
}
DICT_SETITEM_DROP(span, dictkey_origin, JM_py_from_point(span_origin));
DICT_SETITEM_DROP(span, dictkey_bbox, JM_py_from_rect(span_rect));
if (!fz_is_empty_rect(span_rect)) {
LIST_APPEND_DROP(span_list, span);
line_rect = fz_union_rect(line_rect, span_rect);
} else {
Py_DECREF(span);
}
span = NULL;
}
if (!fz_is_empty_rect(line_rect)) {
DICT_SETITEM_DROP(line_dict, dictkey_spans, span_list);
} else {
DICT_SETITEM_DROP(line_dict, dictkey_spans, span_list);
}
return line_rect;
}
static void JM_make_image_block(fz_context *ctx, fz_stext_block *block, PyObject *block_dict)
{
fz_image *image = block->u.i.image;
fz_buffer *buf = NULL, *freebuf = NULL;
fz_compressed_buffer *buffer = fz_compressed_image_buffer(ctx, image);
fz_var(buf);
fz_var(freebuf);
int n = fz_colorspace_n(ctx, image->colorspace);
int w = image->w;
int h = image->h;
const char *ext = NULL;
int type = FZ_IMAGE_UNKNOWN;
if (buffer)
type = buffer->params.type;
if (type < FZ_IMAGE_BMP || type == FZ_IMAGE_JBIG2)
type = FZ_IMAGE_UNKNOWN;
PyObject *bytes = NULL;
fz_var(bytes);
fz_try(ctx) {
if (buffer && type != FZ_IMAGE_UNKNOWN) {
buf = buffer->buffer;
ext = JM_image_extension(type);
} else {
buf = freebuf = fz_new_buffer_from_image_as_png(ctx, image, fz_default_color_params);
ext = "png";
}
bytes = JM_BinFromBuffer(ctx, buf);
}
fz_always(ctx) {
if (!bytes)
bytes = JM_BinFromChar("");
DICT_SETITEM_DROP(block_dict, dictkey_width,
Py_BuildValue("i", w));
DICT_SETITEM_DROP(block_dict, dictkey_height,
Py_BuildValue("i", h));
DICT_SETITEM_DROP(block_dict, dictkey_ext,
Py_BuildValue("s", ext));
DICT_SETITEM_DROP(block_dict, dictkey_colorspace,
Py_BuildValue("i", n));
DICT_SETITEM_DROP(block_dict, dictkey_xres,
Py_BuildValue("i", image->xres));
DICT_SETITEM_DROP(block_dict, dictkey_yres,
Py_BuildValue("i", image->xres));
DICT_SETITEM_DROP(block_dict, dictkey_bpc,
Py_BuildValue("i", (int) image->bpc));
DICT_SETITEM_DROP(block_dict, dictkey_matrix,
JM_py_from_matrix(block->u.i.transform));
DICT_SETITEM_DROP(block_dict, dictkey_size,
Py_BuildValue("n", (Py_ssize_t) fz_image_size(ctx, image)));
DICT_SETITEM_DROP(block_dict, dictkey_image, bytes);
fz_drop_buffer(ctx, freebuf);
}
fz_catch(ctx) {;}
return;
}
static void JM_make_text_block(fz_context *ctx, fz_stext_block *block, PyObject *block_dict, int raw, fz_buffer *buff, fz_rect tp_rect)
{
fz_stext_line *line;
PyObject *line_list = PyList_New(0), *line_dict;
fz_rect block_rect = fz_empty_rect;
for (line = block->u.t.first_line; line; line = line->next) {
if (fz_is_empty_rect(fz_intersect_rect(tp_rect, line->bbox)) &&
!fz_is_infinite_rect(tp_rect)) {
continue;
}
line_dict = PyDict_New();
fz_rect line_rect = JM_make_spanlist(ctx, line_dict, line, raw, buff, tp_rect);
block_rect = fz_union_rect(block_rect, line_rect);
DICT_SETITEM_DROP(line_dict, dictkey_wmode,
Py_BuildValue("i", line->wmode));
DICT_SETITEM_DROP(line_dict, dictkey_dir, JM_py_from_point(line->dir));
DICT_SETITEM_DROP(line_dict, dictkey_bbox,
JM_py_from_rect(line_rect));
LIST_APPEND_DROP(line_list, line_dict);
}
DICT_SETITEM_DROP(block_dict, dictkey_bbox, JM_py_from_rect(block_rect));
DICT_SETITEM_DROP(block_dict, dictkey_lines, line_list);
return;
}
void JM_make_textpage_dict(fz_context *ctx, fz_stext_page *tp, PyObject *page_dict, int raw)
{
fz_stext_block *block;
fz_buffer *text_buffer = fz_new_buffer(ctx, 128);
PyObject *block_dict, *block_list = PyList_New(0);
fz_rect tp_rect = tp->mediabox;
int block_n = -1;
for (block = tp->first_block; block; block = block->next) {
block_n++;
if (!fz_contains_rect(tp_rect, block->bbox) &&
!fz_is_infinite_rect(tp_rect) &&
block->type == FZ_STEXT_BLOCK_IMAGE) {
continue;
}
if (!fz_is_infinite_rect(tp_rect) &&
fz_is_empty_rect(fz_intersect_rect(tp_rect, block->bbox))) {
continue;
}
block_dict = PyDict_New();
DICT_SETITEM_DROP(block_dict, dictkey_number, Py_BuildValue("i", block_n));
DICT_SETITEM_DROP(block_dict, dictkey_type, Py_BuildValue("i", block->type));
if (block->type == FZ_STEXT_BLOCK_IMAGE) {
DICT_SETITEM_DROP(block_dict, dictkey_bbox, JM_py_from_rect(block->bbox));
JM_make_image_block(ctx, block, block_dict);
} else {
JM_make_text_block(ctx, block, block_dict, raw, text_buffer, tp_rect);
}
LIST_APPEND_DROP(block_list, block_dict);
}
DICT_SETITEM_DROP(page_dict, dictkey_blocks, block_list);
fz_drop_buffer(ctx, text_buffer);
}
//---------------------------------------------------------------------
char *
JM_copy_rectangle(fz_context *ctx, fz_stext_page *page, fz_rect area)
{
fz_stext_block *block;
fz_stext_line *line;
fz_stext_char *ch;
fz_buffer *buffer;
unsigned char *s;
int need_new_line = 0;
buffer = fz_new_buffer(ctx, 1024);
fz_try(ctx) {
for (block = page->first_block; block; block = block->next) {
if (block->type != FZ_STEXT_BLOCK_TEXT)
continue;
for (line = block->u.t.first_line; line; line = line->next) {
int line_had_text = 0;
for (ch = line->first_char; ch; ch = ch->next) {
fz_rect r = JM_char_bbox(ctx, line, ch);
if (fz_contains_rect(area, r)) {
line_had_text = 1;
if (need_new_line) {
fz_append_string(ctx, buffer, "\n");
need_new_line = 0;
}
fz_append_rune(ctx, buffer, ch->c < 32 ? FZ_REPLACEMENT_CHARACTER : ch->c);
}
}
if (line_had_text)
need_new_line = 1;
}
}
fz_terminate_buffer(ctx, buffer);
}
fz_catch(ctx) {
fz_drop_buffer(ctx, buffer);
fz_rethrow(ctx);
}
fz_buffer_extract(ctx, buffer, &s); /* take over the data */
fz_drop_buffer(ctx, buffer);
return (char*)s;
}
//---------------------------------------------------------------------
fz_buffer *JM_object_to_buffer(fz_context *ctx, pdf_obj *what, int compress, int ascii)
{
fz_buffer *res=NULL;
fz_output *out=NULL;
fz_try(ctx) {
res = fz_new_buffer(ctx, 512);
out = fz_new_output_with_buffer(ctx, res);
pdf_print_obj(ctx, out, what, compress, ascii);
}
fz_always(ctx) {
fz_drop_output(ctx, out);
}
fz_catch(ctx) {
fz_rethrow(ctx);
}
fz_terminate_buffer(ctx, res);
return res;
}
//-----------------------------------------------------------------------------
// Merge the /Resources object created by a text pdf device into the page.
// The device may have created multiple /ExtGState/Alp? and /Font/F? objects.
// These need to be renamed (renumbered) to not overwrite existing page
// objects from previous executions.
// Returns the next available numbers n, m for objects /Alp<n>, /F<m>.
//-----------------------------------------------------------------------------
PyObject *JM_merge_resources(fz_context *ctx, pdf_page *page, pdf_obj *temp_res)
{
// page objects /Resources, /Resources/ExtGState, /Resources/Font
pdf_obj *resources = pdf_dict_get(ctx, page->obj, PDF_NAME(Resources));
pdf_obj *main_extg = pdf_dict_get(ctx, resources, PDF_NAME(ExtGState));
pdf_obj *main_fonts = pdf_dict_get(ctx, resources, PDF_NAME(Font));
// text pdf device objects /ExtGState, /Font
pdf_obj *temp_extg = pdf_dict_get(ctx, temp_res, PDF_NAME(ExtGState));
pdf_obj *temp_fonts = pdf_dict_get(ctx, temp_res, PDF_NAME(Font));
int max_alp = -1, max_fonts = -1, i, n;
char text[20];
// Handle /Alp objects
if (pdf_is_dict(ctx, temp_extg)) // any created at all?
{
n = pdf_dict_len(ctx, temp_extg);
if (pdf_is_dict(ctx, main_extg)) { // does page have /ExtGState yet?
for (i = 0; i < pdf_dict_len(ctx, main_extg); i++) {
// get highest number of objects named /Alpxxx
char *alp = (char *) pdf_to_name(ctx, pdf_dict_get_key(ctx, main_extg, i));
if (strncmp(alp, "Alp", 3) != 0) continue;
int j = fz_atoi(alp + 3);
if (j > max_alp) max_alp = j;
}
}
else // create a /ExtGState for the page
main_extg = pdf_dict_put_dict(ctx, resources, PDF_NAME(ExtGState), n);
max_alp += 1;
for (i = 0; i < n; i++) // copy over renumbered /Alp objects
{
char *alp = (char *) pdf_to_name(ctx, pdf_dict_get_key(ctx, temp_extg, i));
int j = fz_atoi(alp + 3) + max_alp;
fz_snprintf(text, sizeof(text), "Alp%d", j); // new name
pdf_obj *val = pdf_dict_get_val(ctx, temp_extg, i);
pdf_dict_puts(ctx, main_extg, text, val);
}
}
if (pdf_is_dict(ctx, main_fonts)) { // has page any fonts yet?
for (i = 0; i < pdf_dict_len(ctx, main_fonts); i++) { // get max font number
char *font = (char *) pdf_to_name(ctx, pdf_dict_get_key(ctx, main_fonts, i));
if (strncmp(font, "F", 1) != 0) continue;
int j = fz_atoi(font + 1);
if (j > max_fonts) max_fonts = j;
}
}
else // create a Resources/Font for the page
main_fonts = pdf_dict_put_dict(ctx, resources, PDF_NAME(Font), 2);
max_fonts += 1;
for (i = 0; i < pdf_dict_len(ctx, temp_fonts); i++) { // copy renumbered fonts
char *font = (char *) pdf_to_name(ctx, pdf_dict_get_key(ctx, temp_fonts, i));
int j = fz_atoi(font + 1) + max_fonts;
fz_snprintf(text, sizeof(text), "F%d", j);
pdf_obj *val = pdf_dict_get_val(ctx, temp_fonts, i);
pdf_dict_puts(ctx, main_fonts, text, val);
}
return Py_BuildValue("ii", max_alp, max_fonts); // next available numbers
}
//-----------------------------------------------------------------------------
// version of fz_show_string, which covers SMALL CAPS
//-----------------------------------------------------------------------------
fz_matrix
JM_show_string_cs(fz_context *ctx, fz_text *text, fz_font *user_font, fz_matrix trm, const char *s,
int wmode, int bidi_level, fz_bidi_direction markup_dir, fz_text_language language)
{
fz_font *font=NULL;
int gid, ucs;
float adv;
while (*s)
{
s += fz_chartorune(&ucs, s);
gid = fz_encode_character_sc(ctx, user_font, ucs);
if (gid == 0) {
gid = fz_encode_character_with_fallback(ctx, user_font, ucs, 0, language, &font);
} else {
font = user_font;
}
fz_show_glyph(ctx, text, font, trm, gid, ucs, wmode, bidi_level, markup_dir, language);
adv = fz_advance_glyph(ctx, font, gid, wmode);
if (wmode == 0)
trm = fz_pre_translate(trm, adv, 0);
else
trm = fz_pre_translate(trm, 0, -adv);
}
return trm;
}
//-----------------------------------------------------------------------------
// version of fz_show_string, which also covers UCDN script
//-----------------------------------------------------------------------------
fz_matrix JM_show_string(fz_context *ctx, fz_text *text, fz_font *user_font, fz_matrix trm, const char *s, int wmode, int bidi_level, fz_bidi_direction markup_dir, fz_text_language language, int script)
{
fz_font *font;
int gid, ucs;
float adv;
while (*s) {
s += fz_chartorune(&ucs, s);
gid = fz_encode_character_with_fallback(ctx, user_font, ucs, script, language, &font);
fz_show_glyph(ctx, text, font, trm, gid, ucs, wmode, bidi_level, markup_dir, language);
adv = fz_advance_glyph(ctx, font, gid, wmode);
if (wmode == 0)
trm = fz_pre_translate(trm, adv, 0);
else
trm = fz_pre_translate(trm, 0, -adv);
}
return trm;
}
//-----------------------------------------------------------------------------
// return a fz_font from a number of parameters
//-----------------------------------------------------------------------------
fz_font *JM_get_font(fz_context *ctx,
char *fontname,
char *fontfile,
PyObject *fontbuffer,
int script,
int lang,
int ordering,
int is_bold,
int is_italic,
int is_serif)
{
const unsigned char *data = NULL;
int size, index=0;
fz_buffer *res = NULL;
fz_font *font = NULL;
fz_try(ctx) {
if (fontfile) goto have_file;
if (EXISTS(fontbuffer)) goto have_buffer;
if (ordering > -1) goto have_cjk;
if (fontname) goto have_base14;
goto have_noto;
// Base-14 font
have_base14:;
data = fz_lookup_base14_font(ctx, fontname, &size);
if (data) font = fz_new_font_from_memory(ctx, fontname, data, size, 0, 0);
if(font) goto fertig;
data = fz_lookup_builtin_font(ctx, fontname, is_bold, is_italic, &size);
if (data) font = fz_new_font_from_memory(ctx, fontname, data, size, 0, 0);
goto fertig;
// CJK font
have_cjk:;
data = fz_lookup_cjk_font(ctx, ordering, &size, &index);
if (data) font = fz_new_font_from_memory(ctx, NULL, data, size, index, 0);
goto fertig;
// fontfile
have_file:;
font = fz_new_font_from_file(ctx, NULL, fontfile, index, 0);
goto fertig;
// fontbuffer
have_buffer:;
res = JM_BufferFromBytes(ctx, fontbuffer);
font = fz_new_font_from_buffer(ctx, NULL, res, index, 0);
goto fertig;
// Check for NOTO font
have_noto:;
data = fz_lookup_noto_font(ctx, script, lang, &size, &index);
if (data) font = fz_new_font_from_memory(ctx, NULL, data, size, index, 0);
if (font) goto fertig;
font = fz_load_fallback_font(ctx, script, lang, is_serif, is_bold, is_italic);
goto fertig;
fertig:;
if (!font) {
RAISEPY(ctx, MSG_FONT_FAILED, PyExc_RuntimeError);
}
}
fz_always(ctx) {
fz_drop_buffer(ctx, res);
}
fz_catch(ctx) {
fz_rethrow(ctx);
}
return font;
}
%}
|