/** * Creates an encoding from the given FontBox encoding. * * @param encoding FontBox encoding */ public static Type1Encoding FromFontBox(Encoding encoding) { // todo: could optimise this by looking for specific subclasses Dictionary <int, string> codeToName = encoding.CodeToNameMap; Type1Encoding enc = new Type1Encoding(); foreach (var entry in codeToName) { enc.Put(entry.Key, entry.Value); } return(enc); }
/** * This will load a PFB to be embedded into a document. * * @param doc The PDF document that will hold the embedded font. * @param dict The Font dictionary to write to. * @param pfbStream The pfb input. * @throws IOException If there is an error loading the data. */ public PdfType1FontEmbedder(Document doc, PdfDictionary dict, Bytes.IInputStream pfbStream, Encoding encoding) { dict[PdfName.Subtype] = PdfName.Type1; // read the pfb byte[] pfbBytes = pfbStream.ToByteArray(); PfbParser pfbParser = new PfbParser(pfbBytes); type1 = Type1Font.CreateWithPFB(pfbBytes); if (encoding == null) { fontEncoding = Type1Encoding.FromFontBox(type1.Encoding); } else { fontEncoding = encoding; } // build font descriptor FontDescriptor fd = BuildFontDescriptor(type1); PdfStream fontStream = new PdfStream(pfbParser.GetInputStream()); fontStream.Header[PdfName.Length] = PdfInteger.Get(pfbParser.Size); for (int i = 0; i < pfbParser.Lengths.Length; i++) { fontStream.Header[new PdfName("Length" + (i + 1))] = PdfInteger.Get(pfbParser.Lengths[i]); } fd.FontFile = new FontFile(doc, fontStream); // set the values dict[PdfName.FontDescriptor] = fd.BaseObject; dict[PdfName.BaseFont] = PdfName.Get(type1.Name); // widths List <int> widths = new List <int>(256); for (int code = 0; code <= 255; code++) { string name = fontEncoding.GetName(code); int width = (int)Math.Round(type1.GetWidth(name)); widths.Add(width); } dict[PdfName.FirstChar] = PdfInteger.Get(0); dict[PdfName.LastChar] = PdfInteger.Get(255); dict[PdfName.Widths] = new PdfArray(widths.Select(p => PdfInteger.Get(p))); dict[PdfName.Encoding] = encoding.GetPdfObject(); }