-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathXmlAltoOutputDev.cc
More file actions
10395 lines (9119 loc) · 359 KB
/
Copy pathXmlAltoOutputDev.cc
File metadata and controls
10395 lines (9119 loc) · 359 KB
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
#include <aconf.h>
#include <set>
#ifdef USE_GCC_PRAGMAS
#pragma implementation
#endif
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/uri.h>
#include <time.h>
#include <string>
#include <list>
#include <vector>
#include <stack>
#include <algorithm> // std::min_element, std::max_element
#define _USE_MATH_DEFINES
#include <cmath> // PL: for using std::abs
#include <iostream>
// ── WINDOWS SUPPORT: explicit "using" declarations instead of ──────────────
// "using namespace std;"
//
// What "using namespace std;" normally does:
// The C++ standard library puts all its names (string, vector, cout, etc.)
// inside a container called "std" (short for "standard"). Normally you must
// write "std::string" or "std::vector" every time. The shortcut
// "using namespace std;" dumps ALL of those names into the global scope so
// you can write just "string" or "vector" without the "std::" prefix.
//
// Why that shortcut causes a crash on Windows with modern compilers:
// Starting with C++17 (a version of the C++ language standard), the standard
// library added a new type called "std::byte" — a type for raw byte values.
// Separately, the Windows SDK (Microsoft's set of header files for Windows
// programming) defines its own type also called "byte" (in the file
// rpcndr.h, which gets pulled in indirectly when you include libxml2's
// headers, because libxml2 on Windows includes windows.h -> objbase.h ->
// rpcndr.h).
//
// When you write "using namespace std;", the compiler sees TWO different
// things both called "byte" — the C++ standard library's "std::byte" AND
// the Windows SDK's "byte". It cannot tell which one you mean, so it
// refuses to compile and prints an ambiguity error.
//
// This is a known issue with GCC 15+ and MinGW (the toolchain used to build
// this project on Windows). It also affects MSVC (Microsoft's compiler) in
// C++17 mode.
//
// The fix:
// Instead of importing EVERYTHING from "std" (which drags in std::byte),
// we import only the specific names this file actually uses. That way
// std::byte is never brought into the global scope, and the Windows SDK's
// "byte" is the only "byte" visible — no conflict.
//
// Each "using std::X;" line below makes one specific name available without
// the "std::" prefix. For example, "using std::string;" lets us write
// "string" instead of "std::string" throughout the file.
// ───────────────────────────────────────────────────────────────────────────
using std::string; using std::vector; using std::list; using std::set;
using std::stack; using std::cout; using std::endl;
using std::sort; using std::min; using std::max; using std::abs;
using std::min_element; using std::max_element;
using std::find; using std::distance; using std::reverse; using std::swap;
using std::to_string;
#include "ConstantsUtils.h"
using namespace ConstantsUtils;
#include "ConstantsXML.h"
using namespace ConstantsXML;
#include "AnnotsXrce.h"
#include <stdio.h>
#include <stddef.h>
#include <math.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include "unicode/normalizer2.h"
using namespace icu;
#ifndef WIN32
#include <libgen.h>
#endif
#ifdef WIN32
#include <fcntl.h> // for O_BINARY
#include <io.h> // for setmode
#include <direct.h> // for _mkdir
#endif
#include "gmem.h"
#include "GList.h"
#include "GHash.h"
#include "config.h"
#include "Error.h"
#include "GlobalParams.h"
#include "UnicodeMap.h"
#include "UnicodeTypeTable.h"
#include "GfxState.h"
#include "PDFDoc.h"
#include "Outline.h"
#include "XmlAltoOutputDev.h"
#include "Link.h"
#include "Catalog.h"
#include "Parameters.h"
#include "UnicodeRemapping.h"
//#include "Page.h"
// PNG lib
#include "png.h"
#include "pngstruct.h"
#include "pnginfo.h"
#include "CharCodeToUnicode.h"
#include <splash/SplashBitmap.h>
#include <splash/Splash.h>
#include <splash/SplashFontEngine.h>
#include "FoFiTrueType.h"
#include "FoFiType1C.h"
#include <splash/SplashFontFile.h>
#include <splash/SplashFontFileID.h>
#include <splash/SplashGlyphBitmap.h>
#include <splash/SplashPath.h>
#include <splash/SplashPattern.h>
#include "BuiltinFont.h"
#include "BuiltinFontTables.h"
// Type 3 font cache size parameters
#define type3FontCacheAssoc 8
#define type3FontCacheMaxSets 8
#define type3FontCacheSize (128*1024)
//#ifdef MACOS
//// needed for setting type/creator of MacOS files
//#include "ICSupport.h"
//#endif
//------------------------------------------------------------------------
// parameters
//------------------------------------------------------------------------
// Inter-character space width which will cause addChar to start a new word.
#define minWordBreakSpace 0.1
// Negative inter-character space width, i.e., overlap, which will
// cause addChar to start a new word.
#define minDupBreakOverlap 0.3
// Maximum distance between baselines of two words on the same line,
// e.g., distance between subscript or superscript and the primary
// baseline, as a fraction of the font size.
#define maxIntraLineDelta 0.5
// Maximum distance value between the baseline of a word in a line
// and the yMin of an other word in a other line
#define maxSpacingWordsBetweenTwoLines 5
// Maximum inter-word spacing, as a fraction of the font size.
#define maxWordSpacing 1.5
// Maximum horizontal spacing which will allow a word to be pulled
// into a block.
#define maxColSpacing 0.3
// Max distance between baselines of two lines within a block, as a
// fraction of the font size.
#define maxLineSpacingDelta 1.5
// Max difference in primary font sizes on two lines in the same
// block. Delta1 is used when examining new lines above and below the
// current block.
#define maxBlockFontSizeDelta1 0.05
// Max difference in primary,secondary coordinates (as a fraction of
// the font size) allowed for duplicated text (fake boldface, drop
// shadows) which is to be discarded.
#define dupMaxPriDelta 0.1
#define dupMaxSecDelta 0.2
//------------------------------------------------------------------------
// Cut parameters for reading order
//------------------------------------------------------------------------
// Size of bins used for horizontal and vertical profiles is
// splitPrecisionMul * minFontSize.
#define splitPrecisionMul 0.05
// Minimum allowed split precision.
#define minSplitPrecision 0.01
// yMin and yMax (or xMin and xMax for rot=1,3) are adjusted by this
// fraction of the text height, to allow for slightly overlapping
// lines (or large ascent/descent values).
#define ascentAdjustFactor 0
#define descentAdjustFactor 0.35
// Gaps larger than max{gap} - splitGapSlack * avgFontSize are
// considered to be equivalent.
#define splitGapSlack 0.2
// The vertical gap threshold (minimum gap required to split
// vertically) depends on the (approximate) number of lines in the
// block:
// threshold = (max + slope * nLines) * avgFontSize
// with a min value of vertGapThresholdMin * avgFontSize.
#define vertGapThresholdMin 0.8
#define vertGapThresholdMax 3
#define vertGapThresholdSlope -0.5
// Vertical gap threshold for table mode.
#define vertGapThresholdTableMin 0.2
#define vertGapThresholdTableMax 0.5
#define vertGapThresholdTableSlope -0.02
// A large character has a font size larger than
// largeCharThreshold * avgFontSize.
#define largeCharThreshold 1.5
// A block will be split vertically only if the resulting chunk
// widths are greater than vertSplitChunkThreshold * avgFontSize.
#define vertSplitChunkThreshold 2
// Max difference in primary,secondary coordinates (as a fraction of
// the font size) allowed for duplicated text (fake boldface, drop
// shadows) which is to be discarded.
#define dupMaxPriDelta 0.1
#define dupMaxSecDelta 0.2
// Inter-character spacing that varies by less than this multiple of
// font size is assumed to be equivalent.
#define uniformSpacing 0.07
// Typical word spacing, as a fraction of font size. This will be
// added to the minimum inter-character spacing, to account for wide
// character spacing.
#define wordSpacing 0.1
// Minimum paragraph indent from left margin, as a fraction of font
// size.
#define minParagraphIndent 0.5
// If the space between two lines is greater than
// paragraphSpacingThreshold * avgLineSpacing, start a new paragraph.
#define paragraphSpacingThreshold 1.25
// If font size changes by at least this much (measured in points)
// between lines, start a new paragraph.
#define paragraphFontSizeDelta 1
// Spaces at the start of a line in physical layout mode are this wide
// (as a multiple of font size).
#define physLayoutSpaceWidth 0.33
// In simple layout mode, lines are broken at gaps larger than this
// value multiplied by font size.
#define simpleLayoutGapThreshold 0.4
// Table cells (TextColumns) are allowed to overlap by this much
// in table layout mode (as a fraction of cell width or height).
#define tableCellOverlapSlack 0.05
// Primary axis delta which will cause a line break in raw mode
// (as a fraction of font size).
#define rawModeLineDelta 0.5
// Secondary axis delta which will cause a word break in raw mode
// (as a fraction of font size).
#define rawModeWordSpacing 0.15
// Secondary axis overlap which will cause a line break in raw mode
// (as a fraction of font size).
#define rawModeCharOverlap 0.2
// Max spacing (as a multiple of font size) allowed between the end of
// a line and a clipped character to be included in that line.
#define clippedTextMaxWordSpace 0.5
// Max width of underlines (in points).
#define maxUnderlineWidth 3
// Max horizontal distance between edge of word and start of underline
// (as a fraction of font size).
#define underlineSlack 0.2
// Max vertical distance between baseline of word and start of
// underline (as a fraction of font size).
#define underlineBaselineSlack 0.2
// Max distance between edge of text and edge of link border (as a
// fraction of font size).
#define hyperlinkSlack 0.2
// Text is considered diagonal if abs(tan(angle)) > diagonalThreshold.
// (Or 1/tan(angle) for 90/270 degrees.)
#define diagonalThreshold 0.1
// This value is used as the ascent when computing selection
// rectangles, in order to work around flakey ascent values in fonts.
#define selectionAscent 0.8
//------------------------------------------------------------------------
// Static methods
//------------------------------------------------------------------------
static GBool isUnicodeSpace(Unicode u) {
static const set<Unicode> unicodeSpaces = {
0x20, // SPACE
0x09, // CHARACTER TABULATION
0x0A, // LINE FEED
0x0D, // CARRIAGE RETURN
0x0C, // FORM FEED
0xA0, // NO-BREAK SPACE
0x1680, // OGHAM SPACE MARK
0x2000, // EN QUAD
0x2001, // EM QUAD
0x2002, // EN SPACE
0x2003, // EM SPACE
0x2004, // THREE-PER-EM SPACE
0x2005, // FOUR-PER-EM SPACE
0x2006, // SIX-PER-EM SPACE
0x2007, // FIGURE SPACE
0x2008, // PUNCTUATION SPACE
0x2009, // THIN SPACE
0x200A, // HAIR SPACE
0x2028, // LINE SEPARATOR
0x2029, // PARAGRAPH SEPARATOR
0x202F, // NARROW NO-BREAK SPACE
0x205F, // MEDIUM MATHEMATICAL SPACE
0x3000 // IDEOGRAPHIC SPACE
};
return unicodeSpaces.count(u) ? gTrue : gFalse;
}
//------------------------------------------------------------------------
// TextFontInfo
//------------------------------------------------------------------------
TextFontInfo::TextFontInfo(GfxState *state) {
gfxFont = state->getFont();
if (gfxFont) {
fontID = *gfxFont->getID();
ascent = gfxFont->getAscent();
descent = gfxFont->getDescent();
// "odd" ascent/descent values cause trouble more often than not
// (in theory these could be legitimate values for oddly designed
// fonts -- but they are more often due to buggy PDF generators)
// (values that are too small are a different issue -- those seem
// to be more commonly legitimate)
if (ascent > 1) {
ascent = 0.75;
}
if (descent < -0.5) {
descent = -0.25;
}
} else {
fontID.num = -1;
fontID.gen = -1;
ascent = 0.75;
descent = -0.25;
}
fontName = (gfxFont && gfxFont->getName()) ? gfxFont->getName()->copy()
: (GString *) NULL;
flags = gfxFont ? gfxFont->getFlags() : 0;
mWidth = 0;
if (gfxFont && !gfxFont->isCIDFont()) {
char *name;
int code;
for (code = 0; code < 256; ++code) {
if ((name = ((Gfx8BitFont *) gfxFont)->getCharName(code)) &&
name[0] == 'm' && name[1] == '\0') {
mWidth = ((Gfx8BitFont *) gfxFont)->getWidth(code);
break;
}
}
}
}
TextFontInfo::~TextFontInfo() {
//#if TEXTOUT_WORD_LIST
if (fontName) {
delete fontName;
}
//#endif
}
GBool TextFontInfo::matches(GfxState *state) {
Ref *id;
if (!state->getFont()) {
return gFalse;
}
id = state->getFont()->getID();
return id->num == fontID.num && id->gen == fontID.gen;
}
TextFontStyleInfo::~TextFontStyleInfo() {
if (fontName) {
delete fontName;
}
// if (fontSize) {
// delete fontSize;
// }
if (fontColor) {
delete fontColor;
}
}
//inline bool operator==(const TextFontStyleInfo& lhs, const TextFontStyleInfo& rhs){
// return (((lhs.getFontName())->cmp(rhs.getFontName()) == 0 ) && (lhs.getFontSize() == rhs.getFontSize())
// && (lhs.getFontColor()->cmp(rhs.getFontColor()) == 0 )
// && (lhs.getFontType() == rhs.getFontType())
// && (lhs.getFontStyle() == rhs.getFontStyle()));
//}
//------------------------------------------------------------------------
// ImageInline
//------------------------------------------------------------------------
/*ImageInline::ImageInline(double xPosition, double yPosition, double width,
double height, int idWord, int idImage, GString *href, int index) {
xPositionImage = xPosition;
yPositionImage = yPosition;
widthImage = width;
heightImage = height;
idWordBefore = idWord;
idImageCurrent = idImage;
hrefImage = href;
idx = index;
}
ImageInline::~ImageInline() {
}
*/
//------------------------------------------------------------------------
// Image
//------------------------------------------------------------------------
Image::Image(double xPosition, double yPosition, double width,
double height, GString *id, GString *sid, GString *href, GString *clipzone, GBool isinline, double rotationA, GString* typeA) {
xPositionImage = xPosition;
yPositionImage = yPosition;
widthImage = width;
heightImage = height;
imageId = id;
imageSid = sid;
hrefImage = href;
clipZone = clipzone;
isInline = isinline;
type = typeA;
rotation = rotationA;
}
Image::~Image() {
}
//------------------------------------------------------------------------
// TextChar
//------------------------------------------------------------------------
TextChar::TextChar(GfxState *stateA, Unicode cA, CharCode charCodeA, int charPosA, int charLenA,
double xMinA, double yMinA, double xMaxA, double yMaxA,
int rotA, GBool clippedA, GBool invisibleA,
TextFontInfo *fontA, double fontSizeA, SplashFont *splashFontA,
double colorRA, double colorGA, double colorBA, GBool isNonUnicodeGlyphA) {
double t;
state = stateA;
c = cA;
charCode = charCodeA;
charPos = charPosA;
charLen = charLenA;
xMin = xMinA;
yMin = yMinA;
xMax = xMaxA;
yMax = yMaxA;
// this can happen with vertical writing mode, or with odd values
// for the char/word spacing parameters
if (xMin > xMax) {
t = xMin;
xMin = xMax;
xMax = t;
}
if (yMin > yMax) {
t = yMin;
yMin = yMax;
yMax = t;
}
// TextPage::findGaps uses integer coordinates, so clip the char
// bbox to fit in a 32-bit int (this is generally only a problem in
// damaged PDF files)
if (xMin < -1e8) {
xMin = -1e8;
}
if (xMax > 1e8) {
xMax = 1e8;
}
if (yMin < -1e8) {
yMin = -1e8;
}
if (yMax > 1e8) {
yMax = 1e8;
}
rot = (Guchar) rotA;
clipped = (char) clippedA;
invisible = (char) invisibleA;
spaceAfter = (char) gFalse;
font = fontA;
fontSize = fontSizeA;
splashfont = splashFontA;
isNonUnicodeGlyph = isNonUnicodeGlyphA;
colorR = colorRA;
colorG = colorGA;
colorB = colorBA;
}
int TextChar::cmpX(const void *p1, const void *p2) {
const TextChar *ch1 = *(const TextChar **) p1;
const TextChar *ch2 = *(const TextChar **) p2;
if (ch1->xMin < ch2->xMin) {
return -1;
} else if (ch1->xMin > ch2->xMin) {
return 1;
} else {
return 0;
}
}
int TextChar::cmpY(const void *p1, const void *p2) {
const TextChar *ch1 = *(const TextChar **) p1;
const TextChar *ch2 = *(const TextChar **) p2;
if (ch1->yMin < ch2->yMin) {
return -1;
} else if (ch1->yMin > ch2->yMin) {
return 1;
} else {
return 0;
}
}
#if 0
//------------------------------------------------------------------------
// TextWord
//------------------------------------------------------------------------
TextWord::TextWord(GList *charsA, int start, int lenA,
int rotA, int dirA, GBool spaceAfterA, GfxState *state,
TextFontInfo *fontA, double fontSizeA, int idCurrentWord,
int index) {
GfxFont *gfxFont;
double ascent, descent;
TextChar *chPrev, *ch;
font = fontA;
italic = gFalse;
bold = gFalse;
serif = gFalse;
symbolic = gFalse;
lineNumber = false;
spaceAfter = spaceAfterA;
dir = dirA;
idWord = idCurrentWord;
idx = index;
double *fontm;
double m[4];
double m2[4];
double tan;
// Compute the rotation
state->getFontTransMat(&m[0], &m[1], &m[2], &m[3]);
if (state->getFont()->getType() == fontType3) {
fontm = state->getFont()->getFontMatrix();
m2[0] = fontm[0] * m[0] + fontm[1] * m[2];
m2[1] = fontm[0] * m[1] + fontm[1] * m[3];
m2[2] = fontm[2] * m[0] + fontm[3] * m[2];
m2[3] = fontm[2] * m[1] + fontm[3] * m[3];
m[0] = m2[0];
m[1] = m2[1];
m[2] = m2[2];
m[3] = m2[3];
}
if (fabs(m[0] * m[3]) > fabs(m[1] * m[2])) {
rot = (m[3] < 0) ? 0 : 2;
} else {
rot = (m[2] > 0) ? 3 : 1;
}
// Get the tangent
tan = m[2] / m[0];
// Get the angle value in radian
tan = atan(tan);
// To convert radian angle to degree angle
tan = 180 * tan / M_PI;
angle = static_cast<int>(tan);
// Adjust the angle value
switch (rot) {
case 0:
if (angle > 0)
angle = 360 - angle;
else
angle = static_cast<int>(fabs(static_cast<double>(angle)));
break;
case 1:
if (angle > 0)
angle = 180 - angle;
else
angle = static_cast<int>(fabs(static_cast<double>(angle)));
break;
case 2:
if (angle > 0)
angle = 180 - angle;
else
angle = static_cast<int>(fabs(static_cast<double>(angle))) + 180;
break;
case 3:
if (angle > 0)
angle = 360 - angle;
else
angle = static_cast<int>(fabs(static_cast<double>(angle))) + 180;
break;
}
// Recover the skewing angle value
if (m[1] == 0 && m[2] != 0) {
double angSkew = atan(m[2]);
angSkew = (180 * angSkew / M_PI) - 90;
angleSkewing_Y = static_cast<int>(angSkew);
if (rot == 0) {
angle = 0;
}
} else
angleSkewing_Y = 0;
if (m[1] != 0 && m[2] == 0) {
double angSkew = atan(m[1]);
angSkew = 180 * angSkew / M_PI;
angleSkewing_X = static_cast<int>(angSkew);
if (rot == 0) {
angle = 0;
}
} else
angleSkewing_X = 0;
//base = 0;
//baseYmin = 0;
fontName = NULL;
if (fontA) {
if (fontA->getFontName()) {
// PDF reference guide 5.5.3 For a font subset, the PostScript name of the font�the value of the font�s
//BaseFont entry and the font descriptor�s FontName entry�begins with a tag
//followed by a plus sign (+). The tag consists of exactly six uppercase letters; the
//choice of letters is arbitrary, but different subsets in the same PDF file must have
//different tags. For example, EOODIA+Poetica is the name of a subset of Poetica�, a
//Type 1 font. (See implementation note 62 in Appendix H.)
fontName = strdup(fontA->getFontName()->getCString());
char* localLowerFontName = fontA->getFontName()->lowerCase()->getCString();
if (strstr(localLowerFontName, "bold") ||
strstr(localLowerFontName, "_bd")) {
bold = gTrue;
}
if (strstr(localLowerFontName, "italic") ||
strstr(localLowerFontName, "oblique") ||
strstr(localLowerFontName, "_it")) {
italic = gTrue;
}
} else {
fontName = NULL;
}
if (bold == gFalse) {
bold = state->getFont()->isBold();
}
if (italic == gFalse) {
italic = state->getFont()->isItalic();
}
symbolic = state->getFont()->isSymbolic();
serif = state->getFont()->isSerif();
}
horizScaling = state->getHorizScaling();
wordSpace = state->getWordSpace();
charSpace = state->getCharSpace();
rise = state->getRise();
render = state->getRender();
leading = state->getLeading();
fontSize = fontSizeA;
if ((gfxFont = font->gfxFont)) {
ascent = gfxFont->getAscent() * fontSize;
descent = gfxFont->getDescent() * fontSize;
} else {
// this means that the PDF file draws text without a current font,
// which should never happen
ascent = 0.95 * fontSize;
descent = -0.35 * fontSize;
gfxFont = NULL;
}
len = lenA;
chars = new GList();
//text = (Unicode *)gmallocn(len, sizeof(Unicode));
edge = (double *) gmallocn(len + 1, sizeof(double));
charPos = (int *) gmallocn(len + 1, sizeof(int));
rot = rotA;
if (rot & 1) {
ch = (TextChar *) charsA->get(start);
xMin = ch->xMin;
xMax = ch->xMax;
yMin = ch->yMin;
ch = (TextChar *) charsA->get(start + len - 1);
yMax = ch->yMax;
base = xMin - descent;
baseYmin = xMin;
} else {
ch = (TextChar *) charsA->get(start);
xMin = ch->xMin;
yMin = ch->yMin;
yMax = ch->yMax;
ch = (TextChar *) charsA->get(start + len - 1);
xMax = ch->xMax;
base = yMin + ascent;
baseYmin = yMin;
}
int i = 0, j = 0;
GBool overlap;
double delta;
while (j < len) {
GBool isUpdateAccentedChar = gFalse;
ModifierClass leftClass = NOT_A_MODIFIER, rightClass = NOT_A_MODIFIER;
ch = (TextChar *) charsA->get(rot >= 2 ? start + len - 1 - j : start + j);
if (j > 0) {
chPrev = (TextChar *) charsA->get(rot >= 2 ? start + len - 1 - j : start + j - 1);
delta = ch->rot % 2 == 0 ? ch->xMin - chPrev->xMin : ch->yMin - chPrev->yMin;
//compute overlaping
overlap = fabs(delta) < dupMaxPriDelta * ch->fontSize && fabs(ch->yMin
- chPrev->yMin) <
dupMaxSecDelta * ch->fontSize;
//Do char composition + Update coords and char
//compose here somehow
rightClass = classifyChar(ch->c);
leftClass = classifyChar(chPrev->c);
// if one of the left or right characters is a modifier and they overlap.
if ((rightClass != NOT_A_MODIFIER || leftClass != NOT_A_MODIFIER) && overlap) {
Unicode diactritic = 0;
UnicodeString *baseChar;
UnicodeString *diacriticChar;
UnicodeString resultChar;
if (rightClass != NOT_A_MODIFIER) {
if (leftClass == NOT_A_MODIFIER) {
diactritic = getCombiningDiacritic(rightClass);
baseChar = new UnicodeString(UChar32(getStandardBaseChar(chPrev->c)));
}
} else if (leftClass != NOT_A_MODIFIER) {
diactritic = getCombiningDiacritic(leftClass);
baseChar = new UnicodeString(UChar32(getStandardBaseChar(ch->c)));
}
if (diactritic != 0) {
diacriticChar = new UnicodeString(UChar32(diactritic));
UErrorCode errorCode = U_ZERO_ERROR;
const Normalizer2 *nfkc = Normalizer2::getNFKCInstance(errorCode);
if (!nfkc->isNormalized(*baseChar, errorCode)) {
resultChar = nfkc->normalize(*baseChar, errorCode);
resultChar = nfkc->normalizeSecondAndAppend(resultChar, *diacriticChar, errorCode);
} else
resultChar = nfkc->normalizeSecondAndAppend(*baseChar, *diacriticChar, errorCode);
--i;
//text[i] = resultChar.charAt(0);
((TextChar *) chars->get(i))->c = resultChar.charAt(0);
isUpdateAccentedChar = gTrue;
}
}
}
if (!isUpdateAccentedChar) {
//text[i] = ch->c;
chars->append(ch);
}
charPos[i] = ch->charPos;
if (i == len - 1) {
charPos[len] = ch->charPos + ch->charLen;
}
switch (rot) {
case 0:
default:
edge[i] = ch->xMin;
if (i == len - 1) {
edge[len] = ch->xMax;
}
break;
case 1:
edge[i] = ch->yMin;
if (i == len - 1) {
edge[len] = ch->yMax;
}
break;
case 2:
edge[i] = ch->xMax;
if (i == len - 1) {
edge[len] = ch->xMin;
}
break;
case 3:
edge[i] = ch->yMax;
if (i == len - 1) {
edge[len] = ch->yMin;
}
break;
}
++j;
++i;
}
if (j == len)
len = i;
GfxRGB rgb;
if ((state->getRender() & 3) == 1) {
state->getStrokeRGB(&rgb);
} else {
state->getFillRGB(&rgb);
}
colorR = colToDbl(rgb.r);
colorG = colToDbl(rgb.g);
colorB = colToDbl(rgb.b);
}
Unicode TextWord::getChar(int idx) { return ((TextChar *) chars->get(idx))->c; }
void TextWord::setLineNumber(bool theBool) {
lineNumber = theBool;
}
TextWord::TextWord(TextWord *word) {
*this = *word;
// text = (Unicode *)gmallocn(len, sizeof(Unicode));
// memcpy(text, word->text, len * sizeof(Unicode));
chars = word->chars->copy();
edge = (double *) gmallocn(len + 1, sizeof(double));
memcpy(edge, word->edge, (len + 1) * sizeof(double));
charPos = (int *) gmallocn(len + 1, sizeof(int));
memcpy(charPos, word->charPos, (len + 1) * sizeof(int));
}
TextWord::~TextWord() {
//gfree(text);
gfree(edge);
gfree(charPos);
// Clean up the chars GList to prevent memory leak
if (chars) {
deleteGList(chars, TextChar);
chars = nullptr;
}
}
#endif
//------------------------------------------------------------------------
// TextRawWord
//------------------------------------------------------------------------
TextRawWord::TextRawWord(GfxState *state, double x0, double y0,
TextFontInfo *fontA, double fontSizeA, int idCurrentWord,
int index) {
GfxFont *gfxFont;
double x, y, ascent, descent;
charLen = 0;
font = fontA;
italic = gFalse;
bold = gFalse;
serif = gFalse;
symbolic = gFalse;
lineNumber = false;
double *fontm;
double m[4];
double m2[4];
double tan;
// Compute the rotation
state->getFontTransMat(&m[0], &m[1], &m[2], &m[3]);
if (state->getFont()->getType() == fontType3) {
fontm = state->getFont()->getFontMatrix();
m2[0] = fontm[0] * m[0] + fontm[1] * m[2];
m2[1] = fontm[0] * m[1] + fontm[1] * m[3];
m2[2] = fontm[2] * m[0] + fontm[3] * m[2];
m2[3] = fontm[2] * m[1] + fontm[3] * m[3];
m[0] = m2[0];
m[1] = m2[1];
m[2] = m2[2];
m[3] = m2[3];
}
if (fabs(m[0] * m[3]) > fabs(m[1] * m[2])) {
rot = (m[0] > 0 || m[3] < 0) ? 0 : 2;
} else {
rot = (m[2] > 0) ? 3 : 1;
}
// Get the tangent
tan = m[2] / m[0];
// Get the angle value in radian
tan = atan(tan);
// To convert radian angle to degree angle
tan = 180 * tan / M_PI;
angle = static_cast<int>(tan);
// Adjust the angle value
switch (rot) {
case 0:
if (angle > 0)
angle = 360 - angle;
else
angle = static_cast<int>(fabs(static_cast<double>(angle)));
break;
case 1:
if (angle > 0)
angle = 180 - angle;
else
angle = static_cast<int>(fabs(static_cast<double>(angle)));
break;
case 2:
if (angle > 0)
angle = 180 - angle;
else
angle = static_cast<int>(fabs(static_cast<double>(angle))) + 180;
break;
case 3:
if (angle > 0)
angle = 360 - angle;
else
angle = static_cast<int>(fabs(static_cast<double>(angle))) + 180;
break;
}
// Recover the skewing angle value
if (m[1] == 0 && m[2] != 0) {
double angSkew = atan(m[2]);
angSkew = (180 * angSkew / M_PI) - 90;
angleSkewing_Y = static_cast<int>(angSkew);
if (rot == 0) {
angle = 0;
}
}
if (m[1] != 0 && m[2] == 0) {
double angSkew = atan(m[1]);
angSkew = 180 * angSkew / M_PI;
angleSkewing_X = static_cast<int>(angSkew);
if (rot == 0) {
angle = 0;
}
}