@@ -87,6 +87,10 @@ public class PdfType0Font extends PdfFont {
8787
8888 private static final long serialVersionUID = -8033620300884193397L ;
8989
90+ /**
91+ * The code length shall not be greater than 4.
92+ */
93+ private static final int MAX_CID_CODE_LENGTH = 4 ;
9094 private static final byte [] rotbits = {(byte ) 0x80 , (byte ) 0x40 , (byte ) 0x20 , (byte ) 0x10 , (byte ) 0x08 , (byte ) 0x04 , (byte ) 0x02 , (byte ) 0x01 };
9195
9296 /**
@@ -523,49 +527,66 @@ public String decode(PdfString content) {
523527 * {@inheritDoc}
524528 */
525529 @ 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 ) {
533531 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 ++) {
536550 int code = 0 ;
537551 Glyph glyph = null ;
538552 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 )) {
542557 continue ;
543558 } else {
544559 codeSpaceMatchedLength = codeLength ;
545560 }
546- int glyphCode = cmapEncoding .getCidCode (code );
547- glyph = fontProgram .getGlyphByCode (glyphCode );
561+ int glyphCode = getCmap () .getCidCode (code );
562+ glyph = getFontProgram () .getGlyphByCode (glyphCode );
548563 if (glyph != null ) {
549564 i += codeLength - 1 ;
550565 break ;
551566 }
552567 }
553568 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- }
558569 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+ }
560580 i += codeSpaceMatchedLength - 1 ;
561581 }
562582 if (glyph != null && glyph .getChars () != null ) {
563- glyphs .add (glyph );
583+ list .add (glyph );
564584 } 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 ;
566587 }
567588 }
568- return new GlyphLine ( glyphs ) ;
589+ return allCodesDecoded ;
569590 }
570591
571592 @ Override
0 commit comments