@@ -55,6 +55,9 @@ source product.
55
55
56
56
namespace iText . Kernel . Font {
57
57
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
+
58
61
private static readonly byte [ ] rotbits = new byte [ ] { ( byte ) 0x80 , ( byte ) 0x40 , ( byte ) 0x20 , ( byte ) 0x10 , ( byte
59
62
) 0x08 , ( byte ) 0x04 , ( byte ) 0x02 , ( byte ) 0x01 } ;
60
63
@@ -533,52 +536,63 @@ public override String Decode(PdfString content) {
533
536
}
534
537
535
538
/// <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 ) {
543
540
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 ++ ) {
546
555
int code = 0 ;
547
556
Glyph glyph = null ;
548
557
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 ) ) {
552
562
continue ;
553
563
}
554
564
else {
555
565
codeSpaceMatchedLength = codeLength ;
556
566
}
557
- int glyphCode = cmapEncoding . GetCidCode ( code ) ;
558
- glyph = fontProgram . GetGlyphByCode ( glyphCode ) ;
567
+ int glyphCode = GetCmap ( ) . GetCidCode ( code ) ;
568
+ glyph = GetFontProgram ( ) . GetGlyphByCode ( glyphCode ) ;
559
569
if ( glyph != null ) {
560
570
i += codeLength - 1 ;
561
571
break ;
562
572
}
563
573
}
564
574
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
- }
569
575
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
+ }
572
585
i += codeSpaceMatchedLength - 1 ;
573
586
}
574
587
if ( glyph != null && glyph . GetChars ( ) != null ) {
575
- glyphs . Add ( glyph ) ;
588
+ list . Add ( glyph ) ;
576
589
}
577
590
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 ;
579
593
}
580
594
}
581
- return new GlyphLine ( glyphs ) ;
595
+ return allCodesDecoded ;
582
596
}
583
597
584
598
public override float GetContentWidth ( PdfString content ) {
0 commit comments