internal static TrueTypeFont CreateFontProgram(PdfDictionary fontDictionary, CMapToUnicode toUnicode) { iText.Kernel.Font.DocTrueTypeFont fontProgram = new iText.Kernel.Font.DocTrueTypeFont(fontDictionary); PdfDictionary fontDescriptor = fontDictionary.GetAsDictionary(PdfName.FontDescriptor); FillFontDescriptor(fontProgram, fontDescriptor); int dw; if (fontDescriptor != null && fontDescriptor.ContainsKey(PdfName.DW)) { dw = (int)fontDescriptor.GetAsInt(PdfName.DW); } else { if (fontDictionary.ContainsKey(PdfName.DW)) { dw = (int)fontDictionary.GetAsInt(PdfName.DW); } else { dw = 1000; } } IntHashtable widths = null; if (toUnicode != null) { widths = FontUtil.ConvertCompositeWidthsArray(fontDictionary.GetAsArray(PdfName.W)); fontProgram.avgWidth = 0; foreach (int cid in toUnicode.GetCodes()) { int width = widths.ContainsKey(cid) ? widths.Get(cid) : dw; Glyph glyph = new Glyph(cid, width, toUnicode.Lookup(cid)); if (glyph.HasValidUnicode()) { fontProgram.unicodeToGlyph.Put(glyph.GetUnicode(), glyph); } fontProgram.codeToGlyph.Put(cid, glyph); fontProgram.avgWidth += width; } if (fontProgram.codeToGlyph.Count != 0) { fontProgram.avgWidth /= fontProgram.codeToGlyph.Count; } } if (fontProgram.codeToGlyph.Get(0) == null) { fontProgram.codeToGlyph.Put(0, new Glyph(0, widths != null && widths.ContainsKey(0) ? widths.Get(0) : dw, -1)); } return(fontProgram); }
/// <summary> /// Converts a /// <c>String</c> /// to a /// <c>byte</c> /// array according to the encoding. /// String could contain a unicode symbols or font specific codes. /// </summary> /// <param name="text"> /// the /// <c>String</c> /// to be converted. /// </param> /// <returns> /// an array of /// <c>byte</c> /// representing the conversion according to the encoding /// </returns> public virtual byte[] ConvertToBytes(String text) { if (text == null || text.Length == 0) { return emptyBytes; } int ptr = 0; byte[] bytes = new byte[text.Length]; for (int i = 0; i < text.Length; i++) { if (unicodeToCode.ContainsKey(text[i])) { bytes[ptr++] = (byte)ConvertToByte(text[i]); } } return ArrayUtil.ShortenArray(bytes, ptr); }
private static void FillDifferences(iText.Kernel.Font.DocFontEncoding fontEncoding, PdfArray diffs, CMapToUnicode toUnicode) { IntHashtable byte2uni = toUnicode != null?toUnicode.CreateDirectMapping() : new IntHashtable(); if (diffs != null) { int currentNumber = 0; for (int k = 0; k < diffs.Size(); ++k) { PdfObject obj = diffs.Get(k); if (obj.IsNumber()) { currentNumber = ((PdfNumber)obj).IntValue(); } else { if (currentNumber > 255) { ILog LOGGER = LogManager.GetLogger(typeof(iText.Kernel.Font.DocFontEncoding)); LOGGER.Warn(MessageFormatUtil.Format(iText.IO.LogMessageConstant.DOCFONT_HAS_ILLEGAL_DIFFERENCES, ((PdfName )obj).GetValue())); } else { /* don't return or break, because differences subarrays may * be in any order: * e.g. [255 /space /one 250 /two /three] * /one is invalid but all others should be parsed */ String glyphName = ((PdfName)obj).GetValue(); int unicode = AdobeGlyphList.NameToUnicode(glyphName); if (unicode != -1) { fontEncoding.codeToUnicode[currentNumber] = (int)unicode; fontEncoding.unicodeToCode.Put((int)unicode, currentNumber); fontEncoding.differences[currentNumber] = glyphName; fontEncoding.unicodeDifferences.Put((int)unicode, (int)unicode); } else { if (byte2uni.ContainsKey(currentNumber)) { unicode = byte2uni.Get(currentNumber); fontEncoding.codeToUnicode[currentNumber] = (int)unicode; fontEncoding.unicodeToCode.Put((int)unicode, currentNumber); fontEncoding.differences[currentNumber] = glyphName; fontEncoding.unicodeDifferences.Put((int)unicode, (int)unicode); } } currentNumber++; } } } } }
private void InitializeCidFontProperties(IDictionary <String, Object> fontDesc) { fontIdentification.SetPanose((String)fontDesc.Get("Panose")); fontMetrics.SetItalicAngle(Convert.ToInt32((String)fontDesc.Get("ItalicAngle"), System.Globalization.CultureInfo.InvariantCulture )); fontMetrics.SetCapHeight(Convert.ToInt32((String)fontDesc.Get("CapHeight"), System.Globalization.CultureInfo.InvariantCulture )); fontMetrics.SetTypoAscender(Convert.ToInt32((String)fontDesc.Get("Ascent"), System.Globalization.CultureInfo.InvariantCulture )); fontMetrics.SetTypoDescender(Convert.ToInt32((String)fontDesc.Get("Descent"), System.Globalization.CultureInfo.InvariantCulture )); fontMetrics.SetStemV(Convert.ToInt32((String)fontDesc.Get("StemV"), System.Globalization.CultureInfo.InvariantCulture )); pdfFontFlags = Convert.ToInt32((String)fontDesc.Get("Flags"), System.Globalization.CultureInfo.InvariantCulture ); String fontBBox = (String)fontDesc.Get("FontBBox"); StringTokenizer tk = new StringTokenizer(fontBBox, " []\r\n\t\f"); int llx = Convert.ToInt32(tk.NextToken(), System.Globalization.CultureInfo.InvariantCulture); int lly = Convert.ToInt32(tk.NextToken(), System.Globalization.CultureInfo.InvariantCulture); int urx = Convert.ToInt32(tk.NextToken(), System.Globalization.CultureInfo.InvariantCulture); int ury = Convert.ToInt32(tk.NextToken(), System.Globalization.CultureInfo.InvariantCulture); fontMetrics.UpdateBbox(llx, lly, urx, ury); registry = (String)fontDesc.Get("Registry"); String uniMap = GetCompatibleUniMap(registry); if (uniMap != null) { IntHashtable metrics = (IntHashtable)fontDesc.Get("W"); CMapCidUni cid2Uni = FontCache.GetCid2UniCmap(uniMap); avgWidth = 0; foreach (int cid in cid2Uni.GetCids()) { int uni = cid2Uni.Lookup(cid); int width = metrics.ContainsKey(cid) ? metrics.Get(cid) : DEFAULT_WIDTH; Glyph glyph = new Glyph(cid, width, uni); avgWidth += glyph.GetWidth(); codeToGlyph.Put(cid, glyph); unicodeToGlyph.Put(uni, glyph); } FixSpaceIssue(); if (codeToGlyph.Count != 0) { avgWidth /= codeToGlyph.Count; } } }
public override bool HasSubstitution(int index) { return(substMap.ContainsKey(index)); }
public virtual bool HasClass(int glyph) { return(mapClass.ContainsKey(glyph)); }
private void SerObject(PdfObject obj, int level, ByteBufferOutputStream bb, IntHashtable serialized) { if (level <= 0) { return; } if (obj == null) { bb.Append("$Lnull"); return; } PdfIndirectReference reference = null; ByteBufferOutputStream savedBb = null; if (obj.IsIndirectReference()) { reference = (PdfIndirectReference)obj; int key = GetCopyObjectKey(obj); if (serialized.ContainsKey(key)) { bb.Append((int)serialized.Get(key)); return; } else { savedBb = bb; bb = new ByteBufferOutputStream(); } } if (obj.IsStream()) { bb.Append("$B"); SerDic((PdfDictionary)obj, level - 1, bb, serialized); if (level > 0) { md5.Reset(); bb.Append(md5.Digest(((PdfStream)obj).GetBytes(false))); } } else { if (obj.IsDictionary()) { SerDic((PdfDictionary)obj, level - 1, bb, serialized); } else { if (obj.IsArray()) { SerArray((PdfArray)obj, level - 1, bb, serialized); } else { if (obj.IsString()) { bb.Append("$S").Append(obj.ToString()); } else { if (obj.IsName()) { bb.Append("$N").Append(obj.ToString()); } else { bb.Append("$L").Append(obj.ToString()); } } } } } if (savedBb != null) { int key = GetCopyObjectKey(reference); if (!serialized.ContainsKey(key)) { serialized.Put(key, CalculateHash(bb.GetBuffer())); } savedBb.Append(bb); } }