@@ -55,6 +55,9 @@ source product.
5555
5656namespace iText . Kernel . Font {
5757 public class PdfType0Font : PdfFont {
58+ /// <summary>The code length shall not be greater than 4.</summary>
59+ private const int MAX_CID_CODE_LENGTH = 4 ;
60+
5861 private static readonly byte [ ] rotbits = new byte [ ] { ( byte ) 0x80 , ( byte ) 0x40 , ( byte ) 0x20 , ( byte ) 0x10 , ( byte
5962 ) 0x08 , ( byte ) 0x04 , ( byte ) 0x02 , ( byte ) 0x01 } ;
6063
@@ -533,52 +536,63 @@ public override String Decode(PdfString content) {
533536 }
534537
535538 /// <summary><inheritDoc/></summary>
536- public override GlyphLine DecodeIntoGlyphLine ( PdfString content ) {
537- //A sequence of one or more bytes shall be extracted from the string and matched against the codespace
538- //ranges in the CMap. That is, the first byte shall be matched against 1-byte codespace ranges; if no match is
539- //found, a second byte shall be extracted, and the 2-byte code shall be matched against 2-byte codespace
540- //ranges. This process continues for successively longer codes until a match is found or all codespace ranges
541- //have been tested. There will be at most one match because codespace ranges shall not overlap.
542- String cids = content . GetValue ( ) ;
539+ public override GlyphLine DecodeIntoGlyphLine ( PdfString characterCodes ) {
543540 IList < Glyph > glyphs = new List < Glyph > ( ) ;
544- for ( int i = 0 ; i < cids . Length ; i ++ ) {
545- //The code length shall not be greater than 4.
541+ AppendDecodedCodesToGlyphsList ( glyphs , characterCodes ) ;
542+ return new GlyphLine ( glyphs ) ;
543+ }
544+
545+ /// <summary><inheritDoc/></summary>
546+ public override bool AppendDecodedCodesToGlyphsList ( IList < Glyph > list , PdfString characterCodes ) {
547+ bool allCodesDecoded = true ;
548+ String charCodesSequence = characterCodes . GetValue ( ) ;
549+ // A sequence of one or more bytes shall be extracted from the string and matched against the codespace
550+ // ranges in the CMap. That is, the first byte shall be matched against 1-byte codespace ranges; if no match is
551+ // found, a second byte shall be extracted, and the 2-byte code shall be matched against 2-byte codespace
552+ // ranges. This process continues for successively longer codes until a match is found or all codespace ranges
553+ // have been tested. There will be at most one match because codespace ranges shall not overlap.
554+ for ( int i = 0 ; i < charCodesSequence . Length ; i ++ ) {
546555 int code = 0 ;
547556 Glyph glyph = null ;
548557 int codeSpaceMatchedLength = 1 ;
549- for ( int codeLength = 1 ; codeLength <= 4 && i + codeLength <= cids . Length ; codeLength ++ ) {
550- code = ( code << 8 ) + cids [ i + codeLength - 1 ] ;
551- if ( ! cmapEncoding . ContainsCodeInCodeSpaceRange ( code , codeLength ) ) {
558+ for ( int codeLength = 1 ; codeLength <= MAX_CID_CODE_LENGTH && i + codeLength <= charCodesSequence . Length ;
559+ codeLength ++ ) {
560+ code = ( code << 8 ) + charCodesSequence [ i + codeLength - 1 ] ;
561+ if ( ! GetCmap ( ) . ContainsCodeInCodeSpaceRange ( code , codeLength ) ) {
552562 continue ;
553563 }
554564 else {
555565 codeSpaceMatchedLength = codeLength ;
556566 }
557- int glyphCode = cmapEncoding . GetCidCode ( code ) ;
558- glyph = fontProgram . GetGlyphByCode ( glyphCode ) ;
567+ int glyphCode = GetCmap ( ) . GetCidCode ( code ) ;
568+ glyph = GetFontProgram ( ) . GetGlyphByCode ( glyphCode ) ;
559569 if ( glyph != null ) {
560570 i += codeLength - 1 ;
561571 break ;
562572 }
563573 }
564574 if ( glyph == null ) {
565- StringBuilder failedCodes = new StringBuilder ( ) ;
566- for ( int codeLength = 1 ; codeLength <= 4 && i + codeLength <= cids . Length ; codeLength ++ ) {
567- failedCodes . Append ( ( int ) cids [ i + codeLength - 1 ] ) . Append ( " " ) ;
568- }
569575 ILog logger = LogManager . GetLogger ( typeof ( iText . Kernel . Font . PdfType0Font ) ) ;
570- logger . Warn ( MessageFormatUtil . Format ( iText . IO . LogMessageConstant . COULD_NOT_FIND_GLYPH_WITH_CODE , failedCodes
571- . ToString ( ) ) ) ;
576+ if ( logger . IsWarnEnabled ) {
577+ StringBuilder failedCodes = new StringBuilder ( ) ;
578+ for ( int codeLength = 1 ; codeLength <= MAX_CID_CODE_LENGTH && i + codeLength <= charCodesSequence . Length ;
579+ codeLength ++ ) {
580+ failedCodes . Append ( ( int ) charCodesSequence [ i + codeLength - 1 ] ) . Append ( " " ) ;
581+ }
582+ logger . Warn ( MessageFormatUtil . Format ( iText . IO . LogMessageConstant . COULD_NOT_FIND_GLYPH_WITH_CODE , failedCodes
583+ . ToString ( ) ) ) ;
584+ }
572585 i += codeSpaceMatchedLength - 1 ;
573586 }
574587 if ( glyph != null && glyph . GetChars ( ) != null ) {
575- glyphs . Add ( glyph ) ;
588+ list . Add ( glyph ) ;
576589 }
577590 else {
578- glyphs . Add ( new Glyph ( 0 , fontProgram . GetGlyphByCode ( 0 ) . GetWidth ( ) , - 1 ) ) ;
591+ list . Add ( new Glyph ( 0 , GetFontProgram ( ) . GetGlyphByCode ( 0 ) . GetWidth ( ) , - 1 ) ) ;
592+ allCodesDecoded = false ;
579593 }
580594 }
581- return new GlyphLine ( glyphs ) ;
595+ return allCodesDecoded ;
582596 }
583597
584598 public override float GetContentWidth ( PdfString content ) {
0 commit comments