@@ -48,6 +48,7 @@ This file is part of the iText (R) project.
48
48
import com .itextpdf .io .util .GenericArray ;
49
49
50
50
import java .util .ArrayList ;
51
+ import java .util .Collections ;
51
52
import java .util .HashSet ;
52
53
import java .util .LinkedList ;
53
54
import java .util .List ;
@@ -166,9 +167,16 @@ public class CFFFontSubset extends CFFFont {
166
167
* C'tor for CFFFontSubset
167
168
*
168
169
* @param cff - The font file
169
- * @param GlyphsUsed - a Map that contains the glyph used in the subset
170
170
*/
171
+ CFFFontSubset (byte [] cff ) {
172
+ this (cff , Collections .<Integer >emptySet (), true );
173
+ }
174
+
171
175
public CFFFontSubset (byte [] cff , Set <Integer > GlyphsUsed ) {
176
+ this (cff , GlyphsUsed , false );
177
+ }
178
+
179
+ CFFFontSubset (byte [] cff , Set <Integer > GlyphsUsed , boolean isCidParsingRequired ) {
172
180
// Use CFFFont c'tor in order to parse the font file.
173
181
super (cff );
174
182
this .GlyphsUsed = GlyphsUsed ;
@@ -187,6 +195,10 @@ public CFFFontSubset(byte[] cff, Set<Integer> GlyphsUsed) {
187
195
// For each font save the offset array of the charstring
188
196
fonts [i ].charstringsOffsets = getIndex (fonts [i ].charstringsOffset );
189
197
198
+ if (isCidParsingRequired ) {
199
+ initGlyphIdToCharacterIdArray (i , fonts [i ].nglyphs , fonts [i ].charsetOffset );
200
+ }
201
+
190
202
// Process the FDSelect if exist
191
203
if (fonts [i ].fdselectOffset >= 0 ) {
192
204
// Process the FDSelect
@@ -535,8 +547,8 @@ protected void BuildSubrUsed(int Font, int FD, int SubrOffset, int[] SubrsOffset
535
547
int LBias = CalcBias (SubrOffset , Font );
536
548
537
549
// For each glyph used find its GID, start & end pos
538
- for (int i = 0 ; i < glyphsInList . size (); i ++ ) {
539
- int glyph = (int ) glyphsInList . get ( i ) ;
550
+ for (Integer usedGlyph : glyphsInList ) {
551
+ int glyph = (int ) usedGlyph ;
540
552
int Start = fonts [Font ].charstringsOffsets [glyph ];
541
553
int End = fonts [Font ].charstringsOffsets [glyph + 1 ];
542
554
@@ -1704,5 +1716,77 @@ void CreateNonCIDSubrs(int Font, IndexBaseItem PrivateBase, OffsetItem Subrs) {
1704
1716
OutputList .addLast (new RangeItem (new RandomAccessFileOrArray (rasFactory .createSource (NewSubrsIndexNonCID )), 0 , NewSubrsIndexNonCID .length ));
1705
1717
}
1706
1718
}
1719
+
1720
+ /**
1721
+ * Returns the CID to which specified GID is mapped.
1722
+ *
1723
+ * @param gid glyph identifier
1724
+ *
1725
+ * @return CID value
1726
+ */
1727
+ int getCidForGlyphId (int gid ) {
1728
+ return getCidForGlyphId (0 , gid );
1729
+ }
1730
+
1731
+ /**
1732
+ * Returns the CID to which specified GID is mapped.
1733
+ *
1734
+ * @param fontIndex index of font for which cid-gid mapping is to be identified
1735
+ * @param gid glyph identifier
1736
+ *
1737
+ * @return CID value
1738
+ */
1739
+ int getCidForGlyphId (int fontIndex , int gid ) {
1740
+ if (fonts [fontIndex ].gidToCid == null ) {
1741
+ return gid ;
1742
+ }
1743
+
1744
+ // gidToCid mapping starts with value corresponding to gid == 1, becuase .notdef is omitted
1745
+ int index = gid - 1 ;
1746
+ return index >= 0 && index < fonts [fontIndex ].gidToCid .length
1747
+ ? fonts [fontIndex ].gidToCid [index ]
1748
+ : gid ;
1749
+ }
1750
+
1751
+ /**
1752
+ * Creates glyph-to-character id array.
1753
+ *
1754
+ * @param fontIndex index of font for which charsets data is to be parsed
1755
+ * @param numOfGlyphs number of glyphs in the font
1756
+ * @param offset the offset to charsets data
1757
+ */
1758
+ private void initGlyphIdToCharacterIdArray (int fontIndex , int numOfGlyphs , int offset ) {
1759
+ // Seek charset offset
1760
+ seek (offset );
1761
+
1762
+ // Read the format
1763
+ int format = getCard8 ();
1764
+
1765
+ // .notdef is omitted, therefore remaining number of elements is one less than overall number
1766
+ int numOfElements = numOfGlyphs - 1 ;
1767
+ fonts [fontIndex ].gidToCid = new int [numOfElements ];
1768
+
1769
+ switch (format ) {
1770
+ case 0 :
1771
+ for (int i = 0 ; i < numOfElements ; i ++) {
1772
+ int cid = getCard16 ();
1773
+ fonts [fontIndex ].gidToCid [i ] = cid ;
1774
+ }
1775
+ break ;
1776
+ case 1 :
1777
+ case 2 :
1778
+ int start = 0 ;
1779
+ while (start < numOfElements ) {
1780
+ int first = getCard16 ();
1781
+ int nLeft = format == 1 ? getCard8 () : getCard16 ();
1782
+ for (int i = 0 ; i <= nLeft && start < numOfElements ; i ++) {
1783
+ fonts [fontIndex ].gidToCid [start ++] = first + i ;
1784
+ }
1785
+ }
1786
+ break ;
1787
+ default :
1788
+ break ;
1789
+ }
1790
+ }
1707
1791
}
1708
1792
0 commit comments