@@ -608,7 +608,14 @@ protected PdfDictionary getCidFontType2(TrueTypeFont ttf, PdfDictionary fontDesc
608608 * @return the stream representing this CMap or <CODE>null</CODE>
609609 */
610610 public PdfStream getToUnicode (Object [] metrics ) {
611- if (metrics .length == 0 )
611+ ArrayList <Integer > unicodeGlyphs = new ArrayList <>(metrics .length );
612+ for (int i = 0 ; i < metrics .length ; i ++) {
613+ int [] metric = (int []) metrics [i ];
614+ if (fontProgram .getGlyphByCode (metric [0 ]).getChars () != null ) {
615+ unicodeGlyphs .add (metric [0 ]);
616+ }
617+ }
618+ if (unicodeGlyphs .size () == 0 )
612619 return null ;
613620 StringBuilder buf = new StringBuilder (
614621 "/CIDInit /ProcSet findresource begin\n " +
@@ -625,18 +632,17 @@ public PdfStream getToUnicode(Object[] metrics) {
625632 "<0000><FFFF>\n " +
626633 "endcodespacerange\n " );
627634 int size = 0 ;
628- for (int k = 0 ; k < metrics . length ; ++k ) {
635+ for (int k = 0 ; k < unicodeGlyphs . size () ; ++k ) {
629636 if (size == 0 ) {
630637 if (k != 0 ) {
631638 buf .append ("endbfrange\n " );
632639 }
633- size = Math .min (100 , metrics . length - k );
640+ size = Math .min (100 , unicodeGlyphs . size () - k );
634641 buf .append (size ).append (" beginbfrange\n " );
635642 }
636643 --size ;
637- int [] metric = (int []) metrics [k ];
638- String fromTo = CMapContentParser .toHex (metric [0 ]);
639- Glyph glyph = fontProgram .getGlyphByCode (metric [0 ]);
644+ String fromTo = CMapContentParser .toHex (unicodeGlyphs .get (k ));
645+ Glyph glyph = fontProgram .getGlyphByCode (unicodeGlyphs .get (k ));
640646 if (glyph .getChars () != null ) {
641647 StringBuilder uni = new StringBuilder (glyph .getChars ().length );
642648 for (char ch : glyph .getChars ()) {
0 commit comments