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
|
/*
** wpdf.c PDF support routines to process WPDFBOX, WPDFPAGEINFO, and WTEXTCHAR
** structures.
**
** Part of willus.com general purpose C code library.
**
** Copyright (C) 2023 http://willus.com
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
*/
#include <stdio.h>
#include <math.h>
#include "willus.h"
static int wpdfbox_compare(WPDFBOX *b1,WPDFBOX *b2);
static void wtextchar_scale(WTEXTCHAR *wtc,double scale_factor);
static void wtextchar_rotate_clockwise(WTEXTCHAR *wch,int rot,double page_width_pts,
double page_height_pts);
static void point_sort(double *x1,double *x2);
static void point_rotate(double *x,double *y,int rot,double page_width_pts,double page_height_pts);
static int wtextchar_compare_vert_ex(WTEXTCHAR *c1,WTEXTCHAR *c2,int index);
static int wtextchar_compare_vert(WTEXTCHAR *c1,WTEXTCHAR *c2,int index);
static int wtextchar_compare_horiz(WTEXTCHAR *c1,WTEXTCHAR *c2);
/*
** Extracting text to STRBUF
*/
static void wtextchars_to_strbuf_one_line_1(WTEXTCHARS *wtcs,int i0,int i1,STRBUF *sbuf,
double xmin,double spacesize,int nnl);
static void wtextchars_to_strbuf_one_line(WTEXTCHARS *wtcs,STRBUF *sbuf,double xmin,double spacesize);
static void strbuf_newline_with_spaces(STRBUF *sbuf,int nnl,int n);
static int wtextchars_ligature_pattern(WTEXTCHARS *wtcs,int index);
void wpdfboxes_init(WPDFBOXES *boxes)
{
boxes->n=boxes->na=0;
boxes->box=NULL;
}
void wpdfboxes_free(WPDFBOXES *boxes)
{
static char *funcname="wpdfboxes_free";
willus_mem_free((double **)&boxes->box,funcname);
}
/*
** index should be <= boxes->n, otherwise box is just appended
*/
void wpdfboxes_insert_box(WPDFBOXES *boxes,WPDFBOX *box,int index)
{
wpdfboxes_add_box(boxes,box);
if (index>=boxes->n-1)
return;
memmove(&boxes->box[index+1],&boxes->box[index],sizeof(WPDFBOX)*(boxes->n-1-index));
boxes->box[index]=(*box);
}
void wpdfboxes_add_box(WPDFBOXES *boxes,WPDFBOX *box)
{
static char *funcname="wpdfboxes_add_box";
if (boxes->n>=boxes->na)
{
int newsize;
newsize = boxes->na < 1024 ? 2048 : boxes->na*2;
willus_mem_realloc_robust_warn((void **)&boxes->box,newsize*sizeof(WPDFBOX),
boxes->na*sizeof(WPDFBOX),funcname,10);
boxes->na=newsize;
}
boxes->box[boxes->n++]=(*box);
}
void wpdfboxes_delete(WPDFBOXES *boxes,int n)
{
if (n>0 && n<boxes->n)
{
int i;
for (i=0;i<boxes->n-n;i++)
boxes->box[i]=boxes->box[i+n];
}
boxes->n -= n;
if (boxes->n < 0)
boxes->n = 0;
}
/*
** Undo source page /Rotate
*/
void wpdfbox_unrotate(WPDFBOX *box,double deg)
{
double rot1;
int i,nrot;
/* Now do 90-degree rotations (full page) */
rot1=fmod(-deg,360.);
while (rot1<0.)
rot1+=360.;
nrot=(rot1+45.)/90.;
for (i=0;i<nrot;i++)
{
double t;
t=box->x0;
box->x0=box->y0;
box->y0=box->src_width_pts-t;
t=box->h;
box->h=box->w;
box->w=t;
t=box->src_height_pts;
box->src_height_pts=box->src_width_pts;
box->src_width_pts=t;
}
box->srcrot_deg -= nrot*90.;
}
void wpdfbox_determine_original_source_position(WPDFBOX *box)
{
double rot1,sw,sh;
int i,nrot;
WPDFSRCBOX *srcbox;
srcbox=&box->srcbox;
/* First undo fine rotation about center */
if (fabs(srcbox->finerot_deg)<1e-5)
{
box->srcrot_deg=0.;
box->x0=srcbox->x0_pts;
box->y0=srcbox->y0_pts;
}
else
{
double xc,yc,dx,dy,th,costh,sinth;
box->srcrot_deg=-srcbox->finerot_deg;
xc=srcbox->page_width_pts/2.;
yc=srcbox->page_height_pts/2.;
dx=srcbox->x0_pts-xc;
dy=srcbox->y0_pts-yc;
th=box->srcrot_deg*PI/180.;
costh=cos(th);
sinth=sin(th);
box->x0=xc + dx*costh - dy*sinth;
box->y0=yc + dy*costh + dx*sinth;
}
/* Now do 90-degree rotations (full page) */
rot1=fmod(-srcbox->rot_deg,360.);
while (rot1<0.)
rot1+=360.;
nrot=(rot1+45.)/90.;
sw=srcbox->page_width_pts;
sh=srcbox->page_height_pts;
box->w=srcbox->crop_width_pts;
box->h=srcbox->crop_height_pts;
for (i=0;i<nrot;i++)
{
double t;
t=box->y0;
box->y0=box->x0;
box->x0=sh-t;
t=box->h;
box->h=box->w;
box->w=t;
t=sh;
sh=sw;
sw=t;
box->srcrot_deg += 90.;
}
box->src_width_pts=sw;
box->src_height_pts=sh;
}
void wpdfpageinfo_sort(WPDFPAGEINFO *pageinfo)
{
WPDFBOX *x;
int n,top,n1;
WPDFBOX x0;
x=pageinfo->boxes.box;
n=pageinfo->boxes.n;
if (n<2)
return;
top=n/2;
n1=n-1;
while (1)
{
if (top>0)
{
top--;
x0=x[top];
}
else
{
x0=x[n1];
x[n1]=x[0];
n1--;
if (!n1)
{
x[0]=x0;
return;
}
}
{
int parent,child;
parent=top;
child=top*2+1;
while (child<=n1)
{
if (child<n1 && wpdfbox_compare(&x[child],&x[child+1])<0)
child++;
if (wpdfbox_compare(&x0,&x[child])<0)
{
x[parent]=x[child];
parent=child;
child+=(parent+1);
}
else
break;
}
x[parent]=x0;
}
}
}
static int wpdfbox_compare(WPDFBOX *b1,WPDFBOX *b2)
{
int x;
double d;
x=(b1->dstpage-b2->dstpage);
if (x)
return(x);
x=(b1->srcbox.pageno-b2->srcbox.pageno);
if (x)
return(x);
d=(b2->y1-b1->y1);
if (d)
return(d<0. ? -1 : 1);
d=(b1->x1-b2->x1);
if (d)
return(d<0. ? -1 : 1);
return(0);
}
void wpdfpageinfo_scale_source_boxes(WPDFPAGEINFO *pageinfo,double doc_scale_factor)
{
int i;
for (i=0;i<pageinfo->boxes.n;i++)
{
WPDFBOX *box;
box=&pageinfo->boxes.box[i];
box->scale /= doc_scale_factor;
box->srcbox.page_width_pts *= doc_scale_factor;
box->srcbox.page_height_pts *= doc_scale_factor;
box->srcbox.x0_pts *= doc_scale_factor;
box->srcbox.y0_pts *= doc_scale_factor;
box->srcbox.crop_width_pts *= doc_scale_factor;
box->srcbox.crop_height_pts *= doc_scale_factor;
}
}
void wtextchars_init(WTEXTCHARS *wtc)
{
wtc->n=wtc->na=0;
wtc->wtextchar=NULL;
wtc->sorted=0;
}
void wtextchars_free(WTEXTCHARS *wtc)
{
static char *funcname="wtextchars_free";
willus_mem_free((double **)&wtc->wtextchar,funcname);
wtc->n=wtc->na=0;
wtc->sorted=0;
}
void wtextchars_clear(WTEXTCHARS *wtc)
{
wtc->n=0;
wtc->sorted=0;
}
void wtextchars_add_wtextchar(WTEXTCHARS *wtc,WTEXTCHAR *textchar)
{
static char *funcname="wtextchars_add_wtextchar";
if (wtc->n>=wtc->na)
{
int newsize;
newsize = wtc->na < 512 ? 1024 : wtc->na*2;
willus_mem_realloc_robust_warn((void **)&wtc->wtextchar,newsize*sizeof(WTEXTCHAR),
wtc->na*sizeof(WTEXTCHAR),funcname,10);
wtc->na=newsize;
}
wtc->wtextchar[wtc->n++]=(*textchar);
wtc->sorted=0;
}
void wtextchars_remove_wtextchar(WTEXTCHARS *wtc,int index)
{
if (index>=wtc->n)
return;
if (index<wtc->n-1)
memmove(&wtc->wtextchar[index],&wtc->wtextchar[index+1],sizeof(WTEXTCHAR)*(wtc->n-index-1));
wtc->n--;
}
/*
** rot_deg s/b multiple of 90.
*/
void wtextchars_rotate_clockwise(WTEXTCHARS *wtc,int rot_deg)
{
int i;
while (rot_deg<0)
rot_deg += 360;
rot_deg = rot_deg % 360;
rot_deg = (rot_deg+45)/90;
rot_deg = rot_deg&3;
if (rot_deg==0)
return;
for (i=0;i<wtc->n;i++)
wtextchar_rotate_clockwise(&wtc->wtextchar[i],rot_deg,wtc->width,wtc->height);
if (rot_deg&1)
{
double t;
t=wtc->width;
wtc->width=wtc->height;
wtc->height=t;
}
}
void wtextchars_scale_page(WTEXTCHARS *wtextchars,double scale_factor)
{
int i;
wtextchars->width *= scale_factor;
wtextchars->height *= scale_factor;
for (i=0;i<wtextchars->n;i++)
wtextchar_scale(&wtextchars->wtextchar[i],scale_factor);
}
static void wtextchar_scale(WTEXTCHAR *wtc,double scale_factor)
{
wtc->xp *= scale_factor;
wtc->yp *= scale_factor;
wtc->x1 *= scale_factor;
wtc->y1 *= scale_factor;
wtc->x2 *= scale_factor;
wtc->y2 *= scale_factor;
}
/*
** rot = 1 for 90
** 2 for 180
** 3 for 270
*/
static void wtextchar_rotate_clockwise(WTEXTCHAR *wch,int rot,double page_width_pts,
double page_height_pts)
{
point_rotate(&wch->xp,&wch->yp,rot,page_width_pts,page_height_pts);
point_rotate(&wch->x1,&wch->y1,rot,page_width_pts,page_height_pts);
point_rotate(&wch->x2,&wch->y2,rot,page_width_pts,page_height_pts);
point_sort(&wch->x1,&wch->x2);
point_sort(&wch->y1,&wch->y2);
}
static void point_sort(double *x1,double *x2)
{
if ((*x2) < (*x1))
{
double t;
t=(*x1);
(*x1)=(*x2);
(*x2)=t;
}
}
static void point_rotate(double *x,double *y,int rot,double page_width_pts,double page_height_pts)
{
double x0,y0;
x0=(*x);
y0=(*y);
switch (rot)
{
case 1:
(*y)=x0;
(*x)=page_height_pts-y0;
break;
case 2:
(*x)=page_width_pts-x0;
(*y)=page_height_pts-y0;
break;
case 3:
(*y)=page_width_pts-x0;
(*x)=y0;
break;
}
}
/*
** x1,y1 = upper left bounding box of text
** x2,y2 = lower right bounding box of text
** (*text) gets allocated and then a UTF-8 string of the text inside the bounding box.
*/
void wtextchars_text_inside(WTEXTCHARS *src,char **text,double x1,double y1,double x2,double y2)
{
WTEXTCHARS _dst,*dst;
int *unicode,utf8len,i,i2,j,n;
char *t;
static char *funcname="wtextchars_text_inside";
dst=&_dst;
wtextchars_init(dst);
wtextchars_get_chars_inside(src,dst,x1,y1,x2,y2);
willus_mem_alloc_warn((void **)&unicode,sizeof(int)*dst->n,funcname,10);
/* Clean off spaces/tabs from ends */
for (i=0;i<dst->n && (dst->wtextchar[i].ucs==32 || dst->wtextchar[i].ucs==9);i++);
for (i2=dst->n-1;i2>=i && (dst->wtextchar[i2].ucs==32 || dst->wtextchar[i2].ucs==9);i2--);
for (j=0;i<=i2;i++)
unicode[j++]=dst->wtextchar[i].ucs;
n=j;
wtextchars_free(dst);
utf8len=n==0 ? 0 : utf8_length(unicode,n);
willus_mem_alloc_warn((void **)text,utf8len+1,funcname,10);
t=(*text);
unicode_to_utf8(t,unicode,n);
willus_mem_free((double **)&unicode,funcname);
}
void wtextchars_get_chars_inside(WTEXTCHARS *src,WTEXTCHARS *dst,double x1,double y1,
double x2,double y2)
{
int i,i1,i2;
double dy,xl,xr,yt,yb,xc,yc;
wtextchars_clear(dst);
dy=y2-y1;
/*
i1=wtextchars_index_by_yp(src_sort2,y1-dy*.001,2);
if (i1>=src->n)
return;
i2=wtextchars_index_by_yp(src_sort1,y2+dy*.001,1);
if (i2<=i1)
return;
*/
i1=0;
i2=src->n;
yt=y1-dy*.001;
yb=y2+dy*.001;
xl=x1-dy*.1;
xr=x2;
xc=(x1+x2)/2.;
yc=(y1+y2)/2.;
for (i=i1;i<i2;i++)
{
double cxc,cyc;
/* If word box is completely outside char box, skip */
if (src->wtextchar[i].x2 < x1 || src->wtextchar[i].x1 > x2
|| src->wtextchar[i].y2 < y1 || src->wtextchar[i].y1 > y2)
continue;
/* There is some overlap */
cxc = (src->wtextchar[i].x1 + src->wtextchar[i].x2)/2.;
cyc = (src->wtextchar[i].y1 + src->wtextchar[i].y2)/2.;
/*
** In both directions (horizontal and vertical), determine if either
** A. The center of the char box is inside the word box, or
** B. The center of the word box is inside the char box.
** If this is true in both directions, keep the char.
*/
if (((cxc >= xl && src->wtextchar[i].x1 <= xr) || (xc >= src->wtextchar[i].x1 && xc <= src->wtextchar[i].x2))
&& ((cyc>=yt && cyc<=yb) || (yc >= src->wtextchar[i].y1 && yc <= src->wtextchar[i].y2)))
wtextchars_add_wtextchar(dst,&src->wtextchar[i]);
}
wtextchars_sort_horizontally_by_position(dst);
}
#if 0
/*
** Return index of the first character that has y_type >= yp.
** If wtc->n==0, returns -1.
** If all chars are < yp, returns wtc->n.
*/
static int wtextchars_index_by_yp(WTEXTCHARS *wtc,double yp,int type)
{
int i1,i2,c;
if (wtc->n<=0)
return(-1);
wtextchars_sort_vertically_by_position(wtc,type);
c = (type==1) ? (wtc->wtextchar[0].y1 >= yp) : (wtc->wtextchar[0].y2 >= yp);
if (c)
return(0);
c = (type==1) ? (wtc->wtextchar[wtc->n-1].y1 < yp) : (wtc->wtextchar[wtc->n-1].y2 < yp);
if (c)
return(wtc->n);
i1=0;
i2=wtc->n-1;
while (i2-i1>1)
{
int imid;
imid=(i1+i2)/2;
c = (type==1) ? (wtc->wtextchar[imid].y1 < yp) : (wtc->wtextchar[imid].y2 < yp);
if (c)
i1=imid;
else
i2=imid;
}
return(i2);
}
#endif
void wtextchars_sort_vertically_by_position(WTEXTCHARS *wtc,int type)
{
int top,n1,n;
WTEXTCHAR x0,*x;
if (wtc->sorted==10+type)
return;
x=wtc->wtextchar;
n=wtc->n;
if (n<2)
return;
top=n/2;
n1=n-1;
while (1)
{
if (top>0)
{
top--;
x0=x[top];
}
else
{
x0=x[n1];
x[n1]=x[0];
n1--;
if (!n1)
{
x[0]=x0;
return;
}
}
{
int parent,child;
parent=top;
child=top*2+1;
while (child<=n1)
{
if (child<n1 && wtextchar_compare_vert_ex(&x[child],&x[child+1],type)<0)
child++;
if (wtextchar_compare_vert_ex(&x0,&x[child],type)<0)
{
x[parent]=x[child];
parent=child;
child+=(parent+1);
}
else
break;
}
x[parent]=x0;
}
}
wtc->sorted=10+type;
}
static int wtextchar_compare_vert_ex(WTEXTCHAR *c1,WTEXTCHAR *c2,int index)
{
double dy;
dy=wtextchar_compare_vert(c1,c2,index);
if (fabs(dy)>1e-8)
return(dy);
return(wtextchar_compare_horiz(c1,c2));
}
static int wtextchar_compare_vert(WTEXTCHAR *c1,WTEXTCHAR *c2,int index)
{
if (index==1)
return(c1->y1-c2->y1);
else if (index==2)
return(c1->y2-c2->y2);
else if (index==3)
{
double percentage_overlap;
double h,ol;
if (c1->y2 <= c2->y1)
return(-1);
if (c1->y1 >= c2->y2)
return(1);
h=(c2->y2-c2->y1) < (c1->y2-c1->y1) ? c2->y2-c2->y1 : c1->y2-c1->y1;
if (h<0.1)
h=0.1;
ol=c1->y2<c2->y2 ? c1->y2-c2->y1 : c2->y2-c1->y1;
percentage_overlap=ol*100./h;
if (percentage_overlap < 7.)
return(c1->y2 < c2->y2 ? -1 : 1);
return(0);
}
else
return(c1->yp-c2->yp);
}
void wtextchars_sort_horizontally_by_position(WTEXTCHARS *wtc)
{
if (wtc->sorted==2)
return;
wtextchar_array_sort_horizontally_by_position(wtc->wtextchar,wtc->n);
wtc->sorted=2;
}
void wtextchar_array_sort_horizontally_by_position(WTEXTCHAR *x,int n)
{
int top,n1;
WTEXTCHAR x0;
if (n<2)
return;
top=n/2;
n1=n-1;
while (1)
{
if (top>0)
{
top--;
x0=x[top];
}
else
{
x0=x[n1];
x[n1]=x[0];
n1--;
if (!n1)
{
x[0]=x0;
return;
}
}
{
int parent,child;
parent=top;
child=top*2+1;
while (child<=n1)
{
if (child<n1 && wtextchar_compare_horiz(&x[child],&x[child+1])<0)
child++;
if (wtextchar_compare_horiz(&x0,&x[child])<0)
{
x[parent]=x[child];
parent=child;
child+=(parent+1);
}
else
break;
}
x[parent]=x0;
}
}
}
static int wtextchar_compare_horiz(WTEXTCHAR *c1,WTEXTCHAR *c2)
{
if (c1->xp==c2->xp)
return((c1->x1+c1->x2) - (c2->x1+c2->x2));
return(c1->xp-c2->xp);
}
void wtextchars_to_strbuf_formatted(WTEXTCHARS *wtcs,STRBUF *sbuf)
{
double vert_spacing_threshold;
static char *funcname="wtextchars_group_by_words";
int i,i0,nnl;
double xmin,spacesize; /* Left most character position in points and size of space in points */
if (wtcs->n<=0)
return;
{
double *ch; /* population of character heights */
willus_mem_alloc_warn((void **)&ch,sizeof(double)*wtcs->n,funcname,10);
/* Create population character heights to determine row spacing threshold */
for (xmin=99999.,i=0;i<wtcs->n-1;i++)
{
ch[i]=wtcs->wtextchar[i].y2-wtcs->wtextchar[i].y1;
if (wtcs->wtextchar[i].xp<xmin)
xmin=wtcs->wtextchar[i].xp;
}
sortd(ch,wtcs->n);
vert_spacing_threshold=0.67*ch[wtcs->n/10];
if (vert_spacing_threshold<=2.0)
vert_spacing_threshold=2.0;
spacesize=0.5*ch[wtcs->n/5];
if (spacesize < 2.0)
spacesize = 2.0;
willus_mem_free(&ch,funcname);
}
/* Group characters row by row and add one row at a time */
for (nnl=0,i0=0,i=1;i<wtcs->n;i++)
{
double dy,h;
h=wtcs->wtextchar[i].y2 - wtcs->wtextchar[i].y1;
if (h<2.)
h=2.;
dy=wtcs->wtextchar[i].yp - wtcs->wtextchar[i-1].yp;
if (dy >= h*.75 || dy >= vert_spacing_threshold)
{
wtextchars_to_strbuf_one_line_1(wtcs,i0,i-1,sbuf,xmin,spacesize,nnl);
nnl=dy/vert_spacing_threshold/1.5;
if (nnl>2)
nnl=2;
if (nnl<1)
nnl=1;
i0=i;
}
}
wtextchars_to_strbuf_one_line_1(wtcs,i0,wtcs->n-1,sbuf,xmin,spacesize,nnl);
}
static void wtextchars_to_strbuf_one_line_1(WTEXTCHARS *wtcs,int i0,int i1,STRBUF *sbuf,
double xmin,double spacesize,int nnl)
{
WTEXTCHARS *line,_line;
int i;
if (i0>i1)
return;
line=&_line;
wtextchars_init(line);
for (i=i0;i<=i1;i++)
wtextchars_add_wtextchar(line,&wtcs->wtextchar[i]);
wtextchars_sort_horizontally_by_position(line);
strbuf_newline_with_spaces(sbuf,nnl,0);
wtextchars_to_strbuf_one_line(line,sbuf,xmin,spacesize);
wtextchars_free(line);
}
static void strbuf_newline_with_spaces(STRBUF *sbuf,int nnl,int n)
{
for (;nnl>0;nnl--)
strbuf_cat_ex(sbuf,"\n");
for (;n>0;n--)
strbuf_cat_ex(sbuf," ");
}
/*
** xmin = left margin in points
** spacesize = approx. size of space in points
*/
static void wtextchars_to_strbuf_one_line(WTEXTCHARS *wtcs,STRBUF *sbuf,double xmin,double spacesize)
{
int i;
if (wtcs->n<=0)
return;
for (i=0;i<wtcs->n;i++)
{
double dx;
int nc;
int u16str[2];
char utf8str[8];
/* If letters are physically separated, add space(s) */
dx = wtcs->wtextchar[i].x1 - (i==0 ? xmin : wtcs->wtextchar[i-1].x2);
if (dx > spacesize/2.)
{
int n1;
n1 = dx>spacesize*3. ? (int)dx/spacesize : 1;
if (wtcs->wtextchar[i].ucs==' ')
n1--;
for (;n1>0;n1--)
strbuf_cat_ex(sbuf," ");
}
/*
** Look for ligatured pattern--v2.33 with MuPDF v1.7
** Ligature pattern, e.g. "fi"
** As of MuPDF 1.7, a ligatured "fi" is like this:
** f has zero width, followed by space at same position w/zero width,
** followed by i at same position, followed by space of zero width.
*/
nc=0;
if (wtextchars_ligature_pattern(wtcs,i))
{
u16str[nc++]=wtcs->wtextchar[i].ucs;
u16str[nc++]=wtcs->wtextchar[i+2].ucs;
i+=3;
}
else
u16str[nc++]=wtcs->wtextchar[i].ucs;
unicode_to_utf8(utf8str,u16str,nc);
strbuf_cat_ex(sbuf,utf8str);
}
}
/*
** For MuPDF v1.7 and up
*/
static int wtextchars_ligature_pattern(WTEXTCHARS *wtcs,int index)
{
return(index<wtcs->n-3
&& wtcs->wtextchar[index].ucs!=' '
&& wtcs->wtextchar[index+1].ucs==' '
&& wtcs->wtextchar[index+2].ucs!=' '
&& wtcs->wtextchar[index+3].ucs==' '
&& fabs(wtcs->wtextchar[index+1].x1-wtcs->wtextchar[index].x1)<.01
&& fabs(wtcs->wtextchar[index+2].x1 - wtcs->wtextchar[index].x1)<.01);
}
void wtextchars_to_easyplot(WTEXTCHARS *wtcs,char *filename)
{
FILE *f;
int i;
f=fopen(filename,"w");
if (f==NULL)
return;
fprintf(f,"/sc on\n/sd off\n/sm off\n");
for (i=0;i<wtcs->n;i++)
{
WTEXTCHAR *wtc;
double x1,y1,x2,y2;
wtc=&wtcs->wtextchar[i];
x1=wtc->x1;
x2=wtc->x2;
y1=792.-wtc->y2;
y2=792.-wtc->y1;
fprintf(f,"/sa m 1 2\n%g %g\n%g %g\n%g %g\n%g %g\n%g %g\n//nc\n",x1,y1,x1,y2,x2,y2,x2,y1,x1,y1);
fprintf(f,"/aa %g %g \"%d.%c\"\n",x1,y1+(y2-y1)*.2,i+1,wtc->ucs);
}
fclose(f);
}
|