22
33namespace PrinsFrank \PdfParser \Document \ContentStream \PositionedText \TextSegment ;
44
5+ use PrinsFrank \PdfParser \Document \CMap \ToUnicode \ToUnicodeCMap ;
6+ use PrinsFrank \PdfParser \Document \Dictionary \DictionaryValue \Array \DifferencesArrayValue ;
57use PrinsFrank \PdfParser \Document \Dictionary \DictionaryValue \Name \EncodingNameValue ;
68use PrinsFrank \PdfParser \Document \Dictionary \DictionaryValue \TextString \TextStringValue ;
7- use PrinsFrank \PdfParser \Document \Object \Decorator \Font ;
89use PrinsFrank \PdfParser \Exception \ParseFailureException ;
910
1011readonly class TextSegment {
@@ -13,34 +14,19 @@ public function __construct(
1314 public int |float |null $ offset ,
1415 ) {}
1516
16- public function getText (Font $ font ): string {
17- $ text = '' ;
18- if (str_starts_with ($ this ->textString ->textStringValue , '( ' ) && str_ends_with ($ this ->textString ->textStringValue , ') ' )) {
19- $ unescapedChars = $ this ->textString ->getBinaryString ();
20- if (strlen ($ unescapedChars ) === 1 && ($ glyph = $ font ->getDifferences ()?->getGlyph(ord ($ unescapedChars ))) !== null ) {
21- $ chars = $ glyph ->getChar ();
22- } elseif (in_array ($ encoding = $ font ->getEncoding (), [EncodingNameValue::MacExpertEncoding, EncodingNameValue::WinAnsiEncoding], true ) && $ font ->getDifferences () === null ) {
23- $ chars = $ encoding ->decodeString ($ unescapedChars );
24- } elseif (($ toUnicodeCMap = $ font ->getToUnicodeCMap () ?? $ font ->getToUnicodeCMapDescendantFont ()) !== null ) {
25- $ chars = $ toUnicodeCMap ->textToUnicode (bin2hex ($ unescapedChars ));
26- } elseif ($ encoding !== null ) {
27- $ chars = $ encoding ->decodeString ($ unescapedChars );
28- } else {
29- $ chars = $ unescapedChars ;
30- }
31-
32- $ text .= $ chars ;
33- } elseif (str_starts_with ($ this ->textString ->textStringValue , '< ' ) && str_ends_with ($ this ->textString ->textStringValue , '> ' )) {
34- $ chars = substr ($ this ->textString ->textStringValue , 1 , -1 );
35- if (($ toUnicodeCMap = $ font ->getToUnicodeCMap () ?? $ font ->getToUnicodeCMapDescendantFont ()) !== null ) {
36- $ text .= $ toUnicodeCMap ->textToUnicode ($ chars );
37- } elseif (($ encoding = $ font ->getEncoding ()) !== null ) {
38- $ text .= $ encoding ->decodeString (implode ('' , array_map (fn (string $ character ) => mb_chr ((int ) hexdec ($ character )), str_split ($ chars , 2 ))));
39- } else {
40- $ text .= EncodingNameValue::IdentityH->decodeString ($ chars );
41- }
17+ public function getText (?DifferencesArrayValue $ differences , ?EncodingNameValue $ encoding , ?ToUnicodeCMap $ toUnicodeCMap ): string {
18+ $ binaryString = $ this ->textString ->getBinaryString ();
19+ if (strlen ($ binaryString ) === 1 && ($ glyph = $ differences ?->getGlyph(ord ($ binaryString ))) !== null ) {
20+ $ text = $ glyph ->getChar ();
21+ } elseif (in_array ($ encoding , [EncodingNameValue::MacExpertEncoding, EncodingNameValue::WinAnsiEncoding], true )
22+ && $ differences === null ) {
23+ $ text = $ encoding ->decodeString ($ binaryString );
24+ } elseif ($ toUnicodeCMap !== null ) {
25+ $ text = $ toUnicodeCMap ->textToUnicode (bin2hex ($ binaryString ));
26+ } elseif ($ encoding !== null ) {
27+ $ text = $ encoding ->decodeString ($ binaryString );
4228 } else {
43- throw new ParseFailureException ( sprintf ( ' Unrecognized character group format "%s" ' , $ this -> textString -> textStringValue )) ;
29+ $ text = $ binaryString ;
4430 }
4531
4632 if ($ this ->offset !== null && $ this ->offset < -100 ) {
0 commit comments