@@ -48,6 +48,7 @@ This file is part of the iText (R) project.
4848import com .itextpdf .io .util .GenericArray ;
4949
5050import java .util .ArrayList ;
51+ import java .util .Collections ;
5152import java .util .HashSet ;
5253import java .util .LinkedList ;
5354import java .util .List ;
@@ -166,9 +167,16 @@ public class CFFFontSubset extends CFFFont {
166167 * C'tor for CFFFontSubset
167168 *
168169 * @param cff - The font file
169- * @param GlyphsUsed - a Map that contains the glyph used in the subset
170170 */
171+ CFFFontSubset (byte [] cff ) {
172+ this (cff , Collections .<Integer >emptySet (), true );
173+ }
174+
171175 public CFFFontSubset (byte [] cff , Set <Integer > GlyphsUsed ) {
176+ this (cff , GlyphsUsed , false );
177+ }
178+
179+ CFFFontSubset (byte [] cff , Set <Integer > GlyphsUsed , boolean isCidParsingRequired ) {
172180 // Use CFFFont c'tor in order to parse the font file.
173181 super (cff );
174182 this .GlyphsUsed = GlyphsUsed ;
@@ -187,6 +195,10 @@ public CFFFontSubset(byte[] cff, Set<Integer> GlyphsUsed) {
187195 // For each font save the offset array of the charstring
188196 fonts [i ].charstringsOffsets = getIndex (fonts [i ].charstringsOffset );
189197
198+ if (isCidParsingRequired ) {
199+ initGlyphIdToCharacterIdArray (i , fonts [i ].nglyphs , fonts [i ].charsetOffset );
200+ }
201+
190202 // Process the FDSelect if exist
191203 if (fonts [i ].fdselectOffset >= 0 ) {
192204 // Process the FDSelect
@@ -535,8 +547,8 @@ protected void BuildSubrUsed(int Font, int FD, int SubrOffset, int[] SubrsOffset
535547 int LBias = CalcBias (SubrOffset , Font );
536548
537549 // 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 ;
540552 int Start = fonts [Font ].charstringsOffsets [glyph ];
541553 int End = fonts [Font ].charstringsOffsets [glyph + 1 ];
542554
@@ -1704,5 +1716,77 @@ void CreateNonCIDSubrs(int Font, IndexBaseItem PrivateBase, OffsetItem Subrs) {
17041716 OutputList .addLast (new RangeItem (new RandomAccessFileOrArray (rasFactory .createSource (NewSubrsIndexNonCID )), 0 , NewSubrsIndexNonCID .length ));
17051717 }
17061718 }
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+ }
17071791}
17081792
0 commit comments