FRPrivateFontCollection.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Drawing.Text;
  6. using System.Drawing;
  7. namespace FastReport.Utils
  8. {
  9. /// <summary>
  10. /// A wrapper around PrivateFontCollection.
  11. /// </summary>
  12. public class FRPrivateFontCollection
  13. {
  14. private PrivateFontCollection collection = TypeConverters.FontConverter.PrivateFontCollection;
  15. private Dictionary<string, string> FontFiles = new Dictionary<string, string>();
  16. private Dictionary<string, MemoryFont> MemoryFonts = new Dictionary<string, MemoryFont>();
  17. /// <summary>
  18. /// Gets the array of FontFamily objects associated with this collection.
  19. /// </summary>
  20. public FontFamily[] Families { get { return collection.Families; } }
  21. /// <summary>
  22. /// Checks if the font name is contained in this collection.
  23. /// </summary>
  24. /// <param name="fontName">The name of the font.</param>
  25. /// <returns>true if the font is contained in this collection.</returns>
  26. public bool HasFont(string fontName)
  27. {
  28. return FontFiles.ContainsKey(fontName) || MemoryFonts.ContainsKey(fontName);
  29. }
  30. /// <summary>
  31. /// Returns the font's stream.
  32. /// </summary>
  33. /// <param name="fontName">The name of the font.</param>
  34. /// <returns>Either FileStream or MemoryStream containing font data.</returns>
  35. public Stream GetFontStream(string fontName)
  36. {
  37. if (FontFiles.ContainsKey(fontName))
  38. {
  39. return new FileStream(FontFiles[fontName], FileMode.Open, FileAccess.Read);
  40. }
  41. else if (MemoryFonts.ContainsKey(fontName))
  42. {
  43. MemoryFont font = MemoryFonts[fontName];
  44. byte[] buffer = new byte[font.Length];
  45. Marshal.Copy(font.Memory, buffer, 0, font.Length);
  46. return new MemoryStream(buffer);
  47. }
  48. return null;
  49. }
  50. /// <summary>
  51. /// Adds a font from the specified file to this collection.
  52. /// </summary>
  53. /// <param name="filename">A System.String that contains the file name of the font to add.</param>
  54. public void AddFontFile(string filename)
  55. {
  56. collection.AddFontFile(filename);
  57. string fontName = Families[Families.Length - 1].Name;
  58. if (!FontFiles.ContainsKey(fontName))
  59. FontFiles.Add(fontName, filename);
  60. }
  61. /// <summary>
  62. /// Adds a font contained in system memory to this collection.
  63. /// </summary>
  64. /// <param name="memory">The memory address of the font to add.</param>
  65. /// <param name="length">The memory length of the font to add.</param>
  66. public void AddMemoryFont(IntPtr memory, int length)
  67. {
  68. collection.AddMemoryFont(memory, length);
  69. string fontName = Families[Families.Length - 1].Name;
  70. if (!FontFiles.ContainsKey(fontName))
  71. MemoryFonts.Add(fontName, new MemoryFont(memory, length));
  72. }
  73. private struct MemoryFont
  74. {
  75. public IntPtr Memory;
  76. public int Length;
  77. public MemoryFont(IntPtr memory, int length)
  78. {
  79. Memory = memory;
  80. Length = length;
  81. }
  82. }
  83. }
  84. }