com.lowagie.text.pdf

Class CFFFontSubset


public class CFFFontSubset
extends CFFFont

This Class subsets a CFF Type Font. The subset is preformed for CID fonts and NON CID fonts. The Charstring is subseted for both types. For CID fonts only the FDArray which are used are embedded. The Lsubroutines of the FDArrays used are subsetted as well. The Subroutine subset supports both Type1 and Type2 formatting altough only tested on Type2 Format. For Non CID the Lsubroutines are subsetted. On both types the Gsubroutines is subsetted. A font which was not of CID type is transformed into CID as a part of the subset process. The CID synthetic creation was written by Sivan Toledo <sivan@math.tau.ac.il>
Author:
Oren Manor & Ygal Blum

Nested Class Summary

Nested classes/interfaces inherited from class com.lowagie.text.pdf.CFFFont

CFFFont.DictNumberItem, CFFFont.DictOffsetItem, CFFFont.Font, CFFFont.IndexBaseItem, CFFFont.IndexMarkerItem, CFFFont.IndexOffsetItem, CFFFont.Item, CFFFont.MarkerItem, CFFFont.OffsetItem, CFFFont.RangeItem, CFFFont.StringItem, CFFFont.SubrMarkerItem, CFFFont.UInt16Item, CFFFont.UInt24Item, CFFFont.UInt32Item, CFFFont.UInt8Item

Field Summary

(package private) HashMap
FDArrayUsed
A HashMap for keeping the FDArrays being used by the font
(package private) int
GBias
The bias for the global subroutines
(package private) HashMap
GlyphsUsed
A HashMap containing the glyphs used in the text after being converted to glyph number by the CMap
(package private) byte[]
NewCharStringsIndex
The new CharString of the font
(package private) byte[]
NewGSubrsIndex
The new global subroutines index of the font
(package private) byte[][]
NewLSubrsIndex
An array of the new Indexs for the local Subr.
(package private) byte[]
NewSubrsIndexNonCID
The new subroutines index for a non-cid font
(package private) int
NumOfHints
Number of arguments to the stem operators in a subroutine calculated recursivly
(package private) LinkedList
OutputList
The linked list for generating the new font stream
(package private) static String[]
SubrsEscapeFuncs
The Strings in this array represent Type1/Type2 escape operator names
(package private) static String[]
SubrsFunctions
The Strings in this array represent Type1/Type2 operator names
(package private) ArrayList
glyphsInList
The GlyphsUsed keys as an ArrayList
(package private) HashMap
hGSubrsUsed
A HashMap for keeping the Global subroutines used in the font
(package private) HashMap[]
hSubrsUsed
A HashMaps array for keeping the subroutines used in each FontDict
(package private) HashMap
hSubrsUsedNonCID
A HashMap for keeping the subroutines used in a non-cid font
(package private) ArrayList
lGSubrsUsed
The Global SubroutinesUsed HashMaps as ArrayLists
(package private) ArrayList[]
lSubrsUsed
The SubroutinesUsed HashMaps as ArrayLists
(package private) ArrayList
lSubrsUsedNonCID
The SubroutinesUsed HashMap as ArrayList

Fields inherited from class com.lowagie.text.pdf.CFFFont

arg_count, args, buf, fonts, gsubrIndexOffset, gsubrOffsets, key, nameIndexOffset, nameOffsets, nextIndexOffset, offSize, operatorNames, standardStrings, stringIndexOffset, stringOffsets, topdictIndexOffset, topdictOffsets

Constructor Summary

CFFFontSubset(RandomAccessFileOrArray rf, HashMap GlyphsUsed)
C'tor for CFFFontSubset

Method Summary

protected byte[]
AssembleIndex(int[] NewOffsets, byte[] NewObjects)
Function creates the new index, inserting the count,offsetsize,offset array and object array.
protected void
BuildFDArrayUsed(int Font)
Function reads the FDSelect and builds the FDArrayUsed HashMap According to the glyphs used
protected void
BuildFDSubrsOffsets(int Font, int FD)
The function finds for the FD array processed the local subr offset and its offset array.
protected void
BuildGSubrsUsed(int Font)
Function scans the Glsubr used ArrayList to find recursive calls to Gsubrs and adds to Hashmap & ArrayList
protected void
BuildIndexHeader(int Count, int Offsize, int First)
Function Build the header of an index
protected void
BuildNewCharString(int FontIndex)
Function uses BuildNewIndex to create the new index of the subset charstrings
protected byte[]
BuildNewFile(int Font)
The function builds the new output stream according to the subset process
protected byte[]
BuildNewIndex(int[] Offsets, HashMap Used)
Function builds the new offset array, object array and assembles the index.
protected void
BuildNewLGSubrs(int Font)
Function builds the new local & global subsrs indices.
protected void
BuildSubrUsed(int Font, int FD, int SubrOffset, int[] SubrsOffsets, HashMap hSubr, ArrayList lSubr)
Function uses ReadAsubr on the glyph used to build the LSubr & Gsubr HashMap.
protected int
CalcBias(int Offset, int Font)
Function calcs bias according to the CharString type and the count of the subrs
protected int
CalcHints(int begin, int end, int LBias, int GBias, int[] LSubrsOffsets)
The function reads the subroutine and returns the number of the hint in it.
(package private) int
CalcSubrOffsetSize(int Offset, int Size)
Calculates how many byte it took to write the offset for the subrs in a specific private dict.
protected void
CopyHeader()
Function Copies the header from the original fileto the output list
(package private) int
CountCharset(int Offset, int NumofGlyphs)
Calculates the length of the charset according to its format
(package private) int
CountRange(int NumofGlyphs, int Type)
Function calculates the number of ranges in the Charset
protected void
CreateCharset(CFFFont.OffsetItem charsetRef, int nglyphs)
Function creates new CharSet for non-CID fonts.
protected void
CreateFDArray(CFFFont.OffsetItem fdarrayRef, CFFFont.OffsetItem privateRef, int Font)
Function creates new FDArray for non-CID fonts.
protected void
CreateFDSelect(CFFFont.OffsetItem fdselectRef, int nglyphs)
Function creates new FDSelect for non-CID fonts.
protected void
CreateKeys(CFFFont.OffsetItem fdarrayRef, CFFFont.OffsetItem fdselectRef, CFFFont.OffsetItem charsetRef, CFFFont.OffsetItem charstringsRef)
Function adds the keys into the TopDict
protected void
CreateNewStringIndex(int Font)
Function takes the original string item and adds the new strings to accomodate the CID rules
(package private) void
CreateNonCIDPrivate(int Font, CFFFont.OffsetItem Subr)
The function creates a private dict for a font that was not CID All the keys are copied as is except for the subrs key
(package private) void
CreateNonCIDSubrs(int Font, CFFFont.IndexBaseItem PrivateBase, CFFFont.OffsetItem Subrs)
the function marks the beginning of the subrs index and adds the subsetted subrs index to the output list.
protected void
EmptyStack()
Empty the Type2 Stack
protected void
HandelStack()
Function Checks how the current operator effects the run time stack after being run An operator may increase or decrease the stack size
protected void
PopStack()
Pop one element from the stack
byte[]
Process(String fontName)
The Process function extracts one font out of the CFF file and returns a subset version of the original.
protected void
PushStack()
Add an item to the stack
protected void
ReadASubr(int begin, int end, int GBias, int LBias, HashMap hSubr, ArrayList lSubr, int[] LSubrsOffsets)
The function reads a subrs (glyph info) between begin and end.
protected void
ReadCommand()
The function reads the next command after the file pointer is set
protected void
ReadFDArray(int Font)
Read the FDArray count, offsize and Offset array
(package private) void
Reconstruct(int Font)
Function reconstructs the FDArray, PrivateDict and LSubr for CID fonts
(package private) void
ReconstructFDArray(int Font, CFFFont.OffsetItem[] fdPrivate)
Function subsets the FDArray and builds the new one with new offsets
(package private) void
ReconstructPrivateDict(int Font, CFFFont.OffsetItem[] fdPrivate, CFFFont.IndexBaseItem[] fdPrivateBase, CFFFont.OffsetItem[] fdSubrs)
Function Adds the new private dicts (only for the FDs used) to the list
(package private) void
ReconstructPrivateSubrs(int Font, CFFFont.IndexBaseItem[] fdPrivateBase, CFFFont.OffsetItem[] fdSubrs)
Function Adds the new LSubrs dicts (only for the FDs used) to the list
protected int
StackOpp()
Function checks the key and return the change to the stack after the operator
protected int
countEntireIndexRange(int indexOffset)
Function computes the size of an index
protected void
readFDSelect(int Font)
Read the FDSelect of the font and compute the array and its length

Methods inherited from class com.lowagie.text.pdf.CFFFont

ReadEncoding, exists, getCID, getCard16, getCard8, getDictItem, getEntireIndexRange, getIndex, getInt, getNames, getOffset, getPosition, getShort, getString, isCID, seek

Field Details

FDArrayUsed

(package private)  HashMap FDArrayUsed
A HashMap for keeping the FDArrays being used by the font

GBias

(package private)  int GBias
The bias for the global subroutines

GlyphsUsed

(package private)  HashMap GlyphsUsed
A HashMap containing the glyphs used in the text after being converted to glyph number by the CMap

NewCharStringsIndex

(package private)  byte[] NewCharStringsIndex
The new CharString of the font

NewGSubrsIndex

(package private)  byte[] NewGSubrsIndex
The new global subroutines index of the font

NewLSubrsIndex

(package private)  byte[][] NewLSubrsIndex
An array of the new Indexs for the local Subr. One index for each FontDict

NewSubrsIndexNonCID

(package private)  byte[] NewSubrsIndexNonCID
The new subroutines index for a non-cid font

NumOfHints

(package private)  int NumOfHints
Number of arguments to the stem operators in a subroutine calculated recursivly

OutputList

(package private)  LinkedList OutputList
The linked list for generating the new font stream

SubrsEscapeFuncs

(package private) static final String[] SubrsEscapeFuncs
The Strings in this array represent Type1/Type2 escape operator names

SubrsFunctions

(package private) static final String[] SubrsFunctions
The Strings in this array represent Type1/Type2 operator names

glyphsInList

(package private)  ArrayList glyphsInList
The GlyphsUsed keys as an ArrayList

hGSubrsUsed

(package private)  HashMap hGSubrsUsed
A HashMap for keeping the Global subroutines used in the font

hSubrsUsed

(package private)  HashMap[] hSubrsUsed
A HashMaps array for keeping the subroutines used in each FontDict

hSubrsUsedNonCID

(package private)  HashMap hSubrsUsedNonCID
A HashMap for keeping the subroutines used in a non-cid font

lGSubrsUsed

(package private)  ArrayList lGSubrsUsed
The Global SubroutinesUsed HashMaps as ArrayLists

lSubrsUsed

(package private)  ArrayList[] lSubrsUsed
The SubroutinesUsed HashMaps as ArrayLists

lSubrsUsedNonCID

(package private)  ArrayList lSubrsUsedNonCID
The SubroutinesUsed HashMap as ArrayList

Constructor Details

CFFFontSubset

public CFFFontSubset(RandomAccessFileOrArray rf,
                     HashMap GlyphsUsed)
C'tor for CFFFontSubset
Parameters:
rf - - The font file
GlyphsUsed - - a HashMap that contains the glyph used in the subset

Method Details

AssembleIndex

protected byte[] AssembleIndex(int[] NewOffsets,
                               byte[] NewObjects)
Function creates the new index, inserting the count,offsetsize,offset array and object array.
Parameters:
NewOffsets - the subsetted offset array
NewObjects - the subsetted object array
Returns:
the new index created

BuildFDArrayUsed

protected void BuildFDArrayUsed(int Font)
Function reads the FDSelect and builds the FDArrayUsed HashMap According to the glyphs used
Parameters:
Font - the Number of font being processed

BuildFDSubrsOffsets

protected void BuildFDSubrsOffsets(int Font,
                                   int FD)
The function finds for the FD array processed the local subr offset and its offset array.
Parameters:
Font - the font
FD - The FDARRAY processed

BuildGSubrsUsed

protected void BuildGSubrsUsed(int Font)
Function scans the Glsubr used ArrayList to find recursive calls to Gsubrs and adds to Hashmap & ArrayList
Parameters:
Font - the font

BuildIndexHeader

protected void BuildIndexHeader(int Count,
                                int Offsize,
                                int First)
Function Build the header of an index
Parameters:
Count - the count field of the index
Offsize - the offsize field of the index
First - the first offset of the index

BuildNewCharString

protected void BuildNewCharString(int FontIndex)
            throws IOException
Function uses BuildNewIndex to create the new index of the subset charstrings
Parameters:
FontIndex - the font

BuildNewFile

protected byte[] BuildNewFile(int Font)
            throws IOException
The function builds the new output stream according to the subset process
Parameters:
Font - the font
Returns:
the subseted font stream

BuildNewIndex

protected byte[] BuildNewIndex(int[] Offsets,
                               HashMap Used)
            throws IOException
Function builds the new offset array, object array and assembles the index. used for creating the glyph and subrs subsetted index
Parameters:
Offsets - the offset array of the original index
Used - the hashmap of the used objects
Returns:
the new index subset version

BuildNewLGSubrs

protected void BuildNewLGSubrs(int Font)
            throws IOException
Function builds the new local & global subsrs indices. IF CID then All of the FD Array lsubrs will be subsetted.
Parameters:
Font - the font

BuildSubrUsed

protected void BuildSubrUsed(int Font,
                             int FD,
                             int SubrOffset,
                             int[] SubrsOffsets,
                             HashMap hSubr,
                             ArrayList lSubr)
Function uses ReadAsubr on the glyph used to build the LSubr & Gsubr HashMap. The HashMap (of the lsub only) is then scaned recursivly for Lsubr & Gsubrs calls.
Parameters:
Font - the font
FD - FD array processed. 0 indicates function was called by non CID font
SubrOffset - the offset to the subr index to calc the bias
SubrsOffsets - the offset array of the subr index
hSubr - HashMap of the subrs used
lSubr - ArrayList of the subrs used

CalcBias

protected int CalcBias(int Offset,
                       int Font)
Function calcs bias according to the CharString type and the count of the subrs
Parameters:
Offset - The offset to the relevent subrs index
Font - the font
Returns:
The calculated Bias

CalcHints

protected int CalcHints(int begin,
                        int end,
                        int LBias,
                        int GBias,
                        int[] LSubrsOffsets)
The function reads the subroutine and returns the number of the hint in it. If a call to another subroutine is found the function calls recursively.
Parameters:
begin - the start point of the subr
end - the end point of the subr
LBias - the bias of the Local Subrs
GBias - the bias of the Global Subrs
LSubrsOffsets - The Offsets array of the subroutines
Returns:
The number of hints in the subroutine read.

CalcSubrOffsetSize

(package private)  int CalcSubrOffsetSize(int Offset,
                                          int Size)
Calculates how many byte it took to write the offset for the subrs in a specific private dict.
Parameters:
Offset - The Offset for the private dict
Size - The size of the private dict
Returns:
The size of the offset of the subrs in the private dict

CopyHeader

protected void CopyHeader()
Function Copies the header from the original fileto the output list

CountCharset

(package private)  int CountCharset(int Offset,
                                    int NumofGlyphs)
Calculates the length of the charset according to its format
Parameters:
Offset - The Charset Offset
NumofGlyphs - Number of glyphs in the font
Returns:
the length of the Charset

CountRange

(package private)  int CountRange(int NumofGlyphs,
                                  int Type)
Function calculates the number of ranges in the Charset
Parameters:
NumofGlyphs - The number of glyphs in the font
Type - The format of the Charset
Returns:
The number of ranges in the Charset data structure

CreateCharset

protected void CreateCharset(CFFFont.OffsetItem charsetRef,
                             int nglyphs)
Function creates new CharSet for non-CID fonts. The CharSet built uses a single range for all glyphs
Parameters:
charsetRef - OffsetItem for the CharSet
nglyphs - the number of glyphs in the font

CreateFDArray

protected void CreateFDArray(CFFFont.OffsetItem fdarrayRef,
                             CFFFont.OffsetItem privateRef,
                             int Font)
Function creates new FDArray for non-CID fonts. The FDArray built has only the "Private" operator that points to the font's original private dict
Parameters:
fdarrayRef - OffsetItem for the FDArray
privateRef - OffsetItem for the Private Dict
Font - the font

CreateFDSelect

protected void CreateFDSelect(CFFFont.OffsetItem fdselectRef,
                              int nglyphs)
Function creates new FDSelect for non-CID fonts. The FDSelect built uses a single range for all glyphs
Parameters:
fdselectRef - OffsetItem for the FDSelect
nglyphs - the number of glyphs in the font

CreateKeys

protected void CreateKeys(CFFFont.OffsetItem fdarrayRef,
                          CFFFont.OffsetItem fdselectRef,
                          CFFFont.OffsetItem charsetRef,
                          CFFFont.OffsetItem charstringsRef)
Function adds the keys into the TopDict
Parameters:
fdarrayRef - OffsetItem for the FDArray
fdselectRef - OffsetItem for the FDSelect
charsetRef - OffsetItem for the CharSet
charstringsRef - OffsetItem for the CharString

CreateNewStringIndex

protected void CreateNewStringIndex(int Font)
Function takes the original string item and adds the new strings to accomodate the CID rules
Parameters:
Font - the font

CreateNonCIDPrivate

(package private)  void CreateNonCIDPrivate(int Font,
                                            CFFFont.OffsetItem Subr)
The function creates a private dict for a font that was not CID All the keys are copied as is except for the subrs key
Parameters:
Font - the font
Subr - The OffsetItem for the subrs of the private

CreateNonCIDSubrs

(package private)  void CreateNonCIDSubrs(int Font,
                                          CFFFont.IndexBaseItem PrivateBase,
                                          CFFFont.OffsetItem Subrs)
            throws IOException
the function marks the beginning of the subrs index and adds the subsetted subrs index to the output list.
Parameters:
Font - the font
PrivateBase - IndexBaseItem for the private that's referencing to the subrs
Subrs - OffsetItem for the subrs

EmptyStack

protected void EmptyStack()
Empty the Type2 Stack

HandelStack

protected void HandelStack()
Function Checks how the current operator effects the run time stack after being run An operator may increase or decrease the stack size

PopStack

protected void PopStack()
Pop one element from the stack

Process

public byte[] Process(String fontName)
            throws IOException
The Process function extracts one font out of the CFF file and returns a subset version of the original.
Parameters:
fontName - - The name of the font to be taken out of the CFF
Returns:
The new font stream

PushStack

protected void PushStack()
Add an item to the stack

ReadASubr

protected void ReadASubr(int begin,
                         int end,
                         int GBias,
                         int LBias,
                         HashMap hSubr,
                         ArrayList lSubr,
                         int[] LSubrsOffsets)
The function reads a subrs (glyph info) between begin and end. Adds calls to a Lsubr to the hSubr and lSubrs. Adds calls to a Gsubr to the hGSubr and lGSubrs.
Parameters:
begin - the start point of the subr
end - the end point of the subr
GBias - the bias of the Global Subrs
LBias - the bias of the Local Subrs
hSubr - the HashMap for the lSubrs
lSubr - the ArrayList for the lSubrs

ReadCommand

protected void ReadCommand()
The function reads the next command after the file pointer is set

ReadFDArray

protected void ReadFDArray(int Font)
Read the FDArray count, offsize and Offset array
Parameters:
Font -

Reconstruct

(package private)  void Reconstruct(int Font)
            throws IOException
Function reconstructs the FDArray, PrivateDict and LSubr for CID fonts
Parameters:
Font - the font

ReconstructFDArray

(package private)  void ReconstructFDArray(int Font,
                                           CFFFont.OffsetItem[] fdPrivate)
            throws IOException
Function subsets the FDArray and builds the new one with new offsets
Parameters:
Font - The font
fdPrivate - OffsetItem Array (one for each FDArray)

ReconstructPrivateDict

(package private)  void ReconstructPrivateDict(int Font,
                                               CFFFont.OffsetItem[] fdPrivate,
                                               CFFFont.IndexBaseItem[] fdPrivateBase,
                                               CFFFont.OffsetItem[] fdSubrs)
            throws IOException
Function Adds the new private dicts (only for the FDs used) to the list
Parameters:
Font - the font
fdPrivate - OffsetItem array one element for each private
fdPrivateBase - IndexBaseItem array one element for each private
fdSubrs - OffsetItem array one element for each private

ReconstructPrivateSubrs

(package private)  void ReconstructPrivateSubrs(int Font,
                                                CFFFont.IndexBaseItem[] fdPrivateBase,
                                                CFFFont.OffsetItem[] fdSubrs)
            throws IOException
Function Adds the new LSubrs dicts (only for the FDs used) to the list
Parameters:
Font - The index of the font
fdPrivateBase - The IndexBaseItem array for the linked list
fdSubrs - OffsetItem array for the linked list

StackOpp

protected int StackOpp()
Function checks the key and return the change to the stack after the operator
Returns:
The change in the stack. 2-> flush the stack

countEntireIndexRange

protected int countEntireIndexRange(int indexOffset)
Function computes the size of an index
Parameters:
indexOffset - The offset for the computed index
Returns:
The size of the index

readFDSelect

protected void readFDSelect(int Font)
Read the FDSelect of the font and compute the array and its length
Parameters:
Font - The index of the font being processed