/**
* @see TtfUnicodeWriter#writeFont(TrueTypeFontUnicode, PdfIndirectReference, Object[], byte[])
*/
internal protected override void WriteFont(TrueTypeFontUnicode font, PdfIndirectReference refer, Object[] parameters, byte[] rotbits)
{
Dictionary <int, int[]> longTag = (Dictionary <int, int[]>)parameters[0];
font.AddRangeUni(longTag, true, font.Subset);
int [][] metrics = new int[longTag.Count][];
longTag.Values.CopyTo(metrics, 0);
Array.Sort(metrics, font);
PdfIndirectReference ind_font = null;
PdfObject pobj = null;
PdfIndirectObject obj = null;
if (font.Cff)
{
byte[] b = font.ReadCffFont();
if (font.Subset || font.SubsetRanges != null)
{
CFFFontSubset cff = new CFFFontSubset(new RandomAccessFileOrArray(b), longTag);
try
{
b = cff.Process(cff.GetNames()[0]);
}
catch (Exception e)
{
//temporary fix for cff subset failure
font.Subset = false;
font.AddRangeUni(longTag, true, font.Subset);
metrics = new int[longTag.Count][];
longTag.Values.CopyTo(metrics, 0);
Array.Sort(metrics, font);
}
}
pobj = new BaseFont.StreamFont(b, "CIDFontType0C", font.CompressionLevel);
obj = writer.AddToBody(pobj);
ind_font = obj.IndirectReference;
}
else
{
byte[] b;
if (font.Subset || font.DirectoryOffset != 0)
{
lock (font.Rf) {
TrueTypeFontSubSet sb = new TrueTypeFontSubSet(font.FileName, new RandomAccessFileOrArray(font.Rf), new HashSet2 <int>(longTag.Keys), font.DirectoryOffset, false, false);
b = sb.Process();
}
}
else
{
b = font.GetFullFont();
}
int[] lengths = new int[] { b.Length };
pobj = new BaseFont.StreamFont(b, lengths, font.CompressionLevel);
obj = writer.AddToBody(pobj);
ind_font = obj.IndirectReference;
}
// CIDSet shall be based on font.maxGlyphId property of the font, it is maxp.numGlyphs for ttf,
// because technically we convert all unused glyphs to space, e.g. just remove outlines.
byte[] cidSetBytes = new byte[font.MaxGlyphId / 8 + 1];
for (int i = 0; i < font.MaxGlyphId / 8; i++)
{
cidSetBytes[i] |= 0xff;
}
for (int i = 0; i < font.MaxGlyphId % 8; i++)
{
cidSetBytes[cidSetBytes.Length - 1] |= rotbits[i];
}
PdfStream stream = new PdfStream(cidSetBytes);
stream.FlateCompress(font.CompressionLevel);
PdfIndirectReference cidset = writer.AddToBody(stream).IndirectReference;
String subsetPrefix = "";
if (font.Subset)
{
subsetPrefix = BaseFont.CreateSubsetPrefix();
}
PdfDictionary dic = font.GetFontDescriptor(ind_font, subsetPrefix, cidset);
obj = writer.AddToBody(dic);
ind_font = obj.IndirectReference;
pobj = font.GetCIDFontType2(ind_font, subsetPrefix, metrics);
obj = writer.AddToBody(pobj);
ind_font = obj.IndirectReference;
pobj = font.GetToUnicode(metrics);
PdfIndirectReference toUnicodeRef = null;
if (pobj != null)
{
obj = writer.AddToBody(pobj);
toUnicodeRef = obj.IndirectReference;
}
pobj = font.GetFontBaseType(ind_font, subsetPrefix, toUnicodeRef);
writer.AddToBody(pobj, refer);
}