using System; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; using System.Drawing.Text; using System.Drawing; namespace FastReport.Utils { /// /// A wrapper around PrivateFontCollection. /// public class FRPrivateFontCollection { private PrivateFontCollection collection = TypeConverters.FontConverter.PrivateFontCollection; private Dictionary FontFiles = new Dictionary(); private Dictionary MemoryFonts = new Dictionary(); /// /// Gets the array of FontFamily objects associated with this collection. /// public FontFamily[] Families { get { return collection.Families; } } /// /// Checks if the font name is contained in this collection. /// /// The name of the font. /// true if the font is contained in this collection. public bool HasFont(string fontName) { return FontFiles.ContainsKey(fontName) || MemoryFonts.ContainsKey(fontName); } /// /// Returns the font's stream. /// /// The name of the font. /// Either FileStream or MemoryStream containing font data. public Stream GetFontStream(string fontName) { if (FontFiles.ContainsKey(fontName)) { return new FileStream(FontFiles[fontName], FileMode.Open, FileAccess.Read); } else if (MemoryFonts.ContainsKey(fontName)) { MemoryFont font = MemoryFonts[fontName]; byte[] buffer = new byte[font.Length]; Marshal.Copy(font.Memory, buffer, 0, font.Length); return new MemoryStream(buffer); } return null; } /// /// Adds a font from the specified file to this collection. /// /// A System.String that contains the file name of the font to add. public void AddFontFile(string filename) { collection.AddFontFile(filename); string fontName = Families[Families.Length - 1].Name; if (!FontFiles.ContainsKey(fontName)) FontFiles.Add(fontName, filename); } /// /// Adds a font contained in system memory to this collection. /// /// The memory address of the font to add. /// The memory length of the font to add. public void AddMemoryFont(IntPtr memory, int length) { collection.AddMemoryFont(memory, length); string fontName = Families[Families.Length - 1].Name; if (!FontFiles.ContainsKey(fontName)) MemoryFonts.Add(fontName, new MemoryFont(memory, length)); } private struct MemoryFont { public IntPtr Memory; public int Length; public MemoryFont(IntPtr memory, int length) { Memory = memory; Length = length; } } } }