123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- // available only in FastReport.Core (non Skia) and Mono (for Linux)
- #if !SKIA && ((MONO && !WPF) || FRCORE)
- using FastReport.Fonts;
- using FastReport.Utils;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- namespace FastReport
- {
- public static partial class FontManager
- {
- private static readonly Dictionary<string, DictionaryFont> _fonts = new();
- private static void RegisterFontInternal(string filename)
- {
- IList<TrueTypeFont> ttfs = TrueTypeCollection.CheckFile(Fonts.TrueTypeCollection.CacheOptions.DoNotUpdate, filename);
- foreach (TrueTypeFont font in ttfs)
- {
- string fontName = font.FastName;
- string family2016 = TrueTypeFont.GetFontKey(font.Family2016, font);
- if (!_fonts.ContainsKey(fontName))
- {
- _fonts.Add(fontName, new FontFromFile(filename));
- }
- else
- {
- Debug.WriteLine($"Font '{fontName}' already present in collection.\n Files:\n {_fonts[fontName]}\n {filename}\n");
- }
- if (!_fonts.ContainsKey(family2016))
- {
- _fonts.Add(family2016, new FontFromFile(filename));
- }
- }
- }
- /// <summary>
- /// Checks if the font name is contained in this collection.
- /// </summary>
- /// <param name="fontName">The name of the font.</param>
- /// <returns>true if the font is contained in this collection.</returns>
- public static bool HasFont(string fontName)
- {
- return _fonts.ContainsKey(fontName);
- }
- /// <summary>
- /// Returns the font's stream.
- /// </summary>
- /// <param name="fontName">The name of the font.</param>
- /// <returns>Either FileStream or MemoryStream containing font data.</returns>
- public static Stream GetFontStream(string fontName)
- {
- if (_fonts.TryGetValue(fontName, out var font))
- {
- return font.GetFontStream();
- }
- return null;
- }
- /// <summary>
- /// Adds a font from the specified file to this collection.
- /// </summary>
- /// <param name="filename">A System.String that contains the file name of the font to add.</param>
- /// <returns>true if the font is registered by application.</returns>
- public static bool AddFont(string filename)
- {
- bool success = false;
- if (File.Exists(filename))
- {
- if (!_fonts.Values.OfType<FontFromFile>().Any(fontFile => fontFile._filepath == filename))
- {
- PrivateFontCollection.AddFontFile(filename);
- RegisterFontInternal(filename);
- success = true;
- }
- else
- {
- Debug.WriteLine($"Font file '{filename}' already present in collection");
- }
- }
- else
- {
- Debug.WriteLine($"Font file '{filename}' not found");
- }
- return success;
- }
- /// <summary>
- /// Adds a font contained in system memory to this collection.
- /// </summary>
- /// <param name="memory">The memory address of the font to add.</param>
- /// <param name="length">The memory length of the font to add.</param>
- public static void AddFont(IntPtr memory, int length)
- {
- PrivateFontCollection.AddMemoryFont(memory, length);
- string fontName = PrivateFontCollection.Families[PrivateFontCollection.Families.Length - 1].Name;
- if (!_fonts.ContainsKey(fontName))
- _fonts.Add(fontName, new MemoryFont(memory, length));
- }
- private abstract class DictionaryFont
- {
- public abstract Stream GetFontStream();
- }
- private sealed class FontFromFile : DictionaryFont
- {
- internal readonly string _filepath;
- public FontFromFile(string filepath)
- {
- _filepath = filepath;
- }
- public override Stream GetFontStream()
- {
- return new FileStream(_filepath, FileMode.Open, FileAccess.Read);
- }
- public override string ToString()
- {
- return _filepath;
- }
- }
- private sealed class FontFromStream : DictionaryFont
- {
- private readonly Stream _stream;
- public FontFromStream(Stream stream)
- {
- _stream = stream;
- }
- public override Stream GetFontStream()
- {
- var newStream = new MemoryStream();
- _stream.CopyTo(newStream);
- _stream.Position = 0;
- newStream.Position = 0;
- return newStream;
- }
- }
- private sealed class MemoryFont : DictionaryFont
- {
- private readonly IntPtr Memory;
- private readonly int Length;
- public MemoryFont(IntPtr memory, int length)
- {
- Memory = memory;
- Length = length;
- }
- public override Stream GetFontStream()
- {
- byte[] buffer = new byte[Length];
- Marshal.Copy(Memory, buffer, 0, Length);
- return new MemoryStream(buffer);
- }
- }
- }
- }
- #endif
|