public void RealizeFont(Glyphs glyphs) { var absURI = glyphs.FontUri; if (!glyphs.FontUri.IsAbsoluteUri) { var uriCtx = glyphs as System.Windows.Markup.IUriContext; var host = uriCtx.BaseUri.Host; absURI = new Uri("pack://" + host + glyphs.FontUri.OriginalString, UriKind.Absolute); } var nameInPdf = ""; KeyValuePair <string, PdfFont> exist; if (_fonts.TryGetValue(absURI.OriginalString, out exist)) { this.realizedFont = exist.Value; nameInPdf = exist.Key; } else { GlyphTypeface typeFace = new GlyphTypeface(absURI); string name = String.Format("XPS-Font-{0:00}", ++fontCount); name = PdfFont.CreateEmbeddedFontSubsetName(name); var fontStream = typeFace.GetFontStream(); var memStream = new System.IO.MemoryStream(); var buff = new byte[512]; int len = 0; while ((len = fontStream.Read(buff, 0, buff.Length)) > 0) { memStream.Write(buff, 0, len); } var fontData = memStream.ToArray(); Font font = new Font(name, fontData); nameInPdf = writer.GetFontName(font); _fonts.Add(absURI.OriginalString, new KeyValuePair <string, PdfFont>( nameInPdf, font.PdfFont)); this.realizedFont = font.PdfFont; } //if (fontName != this.realizedFontName || this.realizedFontSize != font.Size) { //this.writer.WriteLiteral("{0} {1:0.###} Tf\n", fontName, -glyphs.FontRenderingEmSize); this.writer.WriteLiteral("{0} {1:0.###} Tf\n", nameInPdf, -glyphs.FontRenderingEmSize); //this.realizedFontName = fontName; //this.realizedFontSize = font.Size; } }
// string realizedFontName = String.Empty; // double realizedFontSize = 0; public void RealizeFont(Glyphs glyphs) { // So far rendering mode 0 only //RealizeBrush(brush, this.renderer.colorMode); // this.renderer.page.document.Options.ColorMode); try { // Get fixed payload which contains the font manager FixedPage fpage = glyphs.GetParent <FixedPage>(); if (fpage == null) { GetType(); this.realizedFont = new PdfFont(null); } FixedPayload payload = fpage.Document.Payload; // Get the font object. // Important: font.PdfFont is not yet defined here on the first call string uriString = glyphs.FontUri; Font font = payload.GetFont(uriString); // Get the page local resource name and define font.PdfFont if it is not yet defined string fontName = writer.GetFontName(font); this.realizedFont = font.PdfFont; //if (fontName != this.realizedFontName || this.realizedFontSize != font.Size) { //this.writer.WriteLiteral("{0} {1:0.###} Tf\n", fontName, -glyphs.FontRenderingEmSize); this.writer.WriteLiteral("{0} {1:0.###} Tf\n", fontName, -glyphs.FontRenderingEmSize); //this.realizedFontName = fontName; //this.realizedFontSize = font.Size; } } catch (Exception ex) { Debug.WriteLine(ex.Message); } }