@@ -87,6 +87,10 @@ public class PdfType0Font extends PdfFont {
87
87
88
88
private static final long serialVersionUID = -8033620300884193397L ;
89
89
90
+ /**
91
+ * The code length shall not be greater than 4.
92
+ */
93
+ private static final int MAX_CID_CODE_LENGTH = 4 ;
90
94
private static final byte [] rotbits = {(byte ) 0x80 , (byte ) 0x40 , (byte ) 0x20 , (byte ) 0x10 , (byte ) 0x08 , (byte ) 0x04 , (byte ) 0x02 , (byte ) 0x01 };
91
95
92
96
/**
@@ -523,49 +527,66 @@ public String decode(PdfString content) {
523
527
* {@inheritDoc}
524
528
*/
525
529
@ Override
526
- public GlyphLine decodeIntoGlyphLine (PdfString content ) {
527
- //A sequence of one or more bytes shall be extracted from the string and matched against the codespace
528
- //ranges in the CMap. That is, the first byte shall be matched against 1-byte codespace ranges; if no match is
529
- //found, a second byte shall be extracted, and the 2-byte code shall be matched against 2-byte codespace
530
- //ranges. This process continues for successively longer codes until a match is found or all codespace ranges
531
- //have been tested. There will be at most one match because codespace ranges shall not overlap.
532
- String cids = content .getValue ();
530
+ public GlyphLine decodeIntoGlyphLine (PdfString characterCodes ) {
533
531
List <Glyph > glyphs = new ArrayList <>();
534
- for (int i = 0 ; i < cids .length (); i ++) {
535
- //The code length shall not be greater than 4.
532
+ appendDecodedCodesToGlyphsList (glyphs , characterCodes );
533
+ return new GlyphLine (glyphs );
534
+ }
535
+
536
+ /**
537
+ * {@inheritDoc}
538
+ */
539
+ @ Override
540
+ public boolean appendDecodedCodesToGlyphsList (List <Glyph > list , PdfString characterCodes ) {
541
+ boolean allCodesDecoded = true ;
542
+
543
+ String charCodesSequence = characterCodes .getValue ();
544
+ // A sequence of one or more bytes shall be extracted from the string and matched against the codespace
545
+ // ranges in the CMap. That is, the first byte shall be matched against 1-byte codespace ranges; if no match is
546
+ // found, a second byte shall be extracted, and the 2-byte code shall be matched against 2-byte codespace
547
+ // ranges. This process continues for successively longer codes until a match is found or all codespace ranges
548
+ // have been tested. There will be at most one match because codespace ranges shall not overlap.
549
+ for (int i = 0 ; i < charCodesSequence .length (); i ++) {
536
550
int code = 0 ;
537
551
Glyph glyph = null ;
538
552
int codeSpaceMatchedLength = 1 ;
539
- for (int codeLength = 1 ; codeLength <= 4 && i + codeLength <= cids .length (); codeLength ++) {
540
- code = (code << 8 ) + cids .charAt (i + codeLength - 1 );
541
- if (!cmapEncoding .containsCodeInCodeSpaceRange (code , codeLength )) {
553
+ for (int codeLength = 1 ; codeLength <= MAX_CID_CODE_LENGTH && i + codeLength <= charCodesSequence .length ();
554
+ codeLength ++) {
555
+ code = (code << 8 ) + charCodesSequence .charAt (i + codeLength - 1 );
556
+ if (!getCmap ().containsCodeInCodeSpaceRange (code , codeLength )) {
542
557
continue ;
543
558
} else {
544
559
codeSpaceMatchedLength = codeLength ;
545
560
}
546
- int glyphCode = cmapEncoding .getCidCode (code );
547
- glyph = fontProgram .getGlyphByCode (glyphCode );
561
+ int glyphCode = getCmap () .getCidCode (code );
562
+ glyph = getFontProgram () .getGlyphByCode (glyphCode );
548
563
if (glyph != null ) {
549
564
i += codeLength - 1 ;
550
565
break ;
551
566
}
552
567
}
553
568
if (glyph == null ) {
554
- StringBuilder failedCodes = new StringBuilder ();
555
- for (int codeLength = 1 ; codeLength <= 4 && i + codeLength <= cids .length (); codeLength ++) {
556
- failedCodes .append ((int ) cids .charAt (i + codeLength - 1 )).append (" " );
557
- }
558
569
Logger logger = LoggerFactory .getLogger (PdfType0Font .class );
559
- logger .warn (MessageFormatUtil .format (LogMessageConstant .COULD_NOT_FIND_GLYPH_WITH_CODE , failedCodes .toString ()));
570
+ if (logger .isWarnEnabled ()) {
571
+ StringBuilder failedCodes = new StringBuilder ();
572
+ for (int codeLength = 1 ;
573
+ codeLength <= MAX_CID_CODE_LENGTH && i + codeLength <= charCodesSequence .length ();
574
+ codeLength ++) {
575
+ failedCodes .append ((int ) charCodesSequence .charAt (i + codeLength - 1 )).append (" " );
576
+ }
577
+ logger .warn (MessageFormatUtil
578
+ .format (LogMessageConstant .COULD_NOT_FIND_GLYPH_WITH_CODE , failedCodes .toString ()));
579
+ }
560
580
i += codeSpaceMatchedLength - 1 ;
561
581
}
562
582
if (glyph != null && glyph .getChars () != null ) {
563
- glyphs .add (glyph );
583
+ list .add (glyph );
564
584
} else {
565
- glyphs .add (new Glyph (0 , fontProgram .getGlyphByCode (0 ).getWidth (), -1 ));
585
+ list .add (new Glyph (0 , getFontProgram ().getGlyphByCode (0 ).getWidth (), -1 ));
586
+ allCodesDecoded = false ;
566
587
}
567
588
}
568
- return new GlyphLine ( glyphs ) ;
589
+ return allCodesDecoded ;
569
590
}
570
591
571
592
@ Override
0 commit comments