Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Drawing.Text;
- using System.IO;
- namespace CamshaftSearch.DataClasses
- {
- /// <summary>
- /// Static class for loading fonts into memory
- /// This allows for them to be used without being installed on the host machine
- /// </summary>
- public static class MemoryFont
- {
- /// <summary>
- /// Gets the Helvetica font that is loaded into memory
- /// </summary>
- public static Font Helvetica { get { return _helvetica; } }
- /// <summary>
- /// Gets the Consolas font that is loaded into memory
- /// </summary>
- public static Font Consolas { get { return _consolas; } }
- private static Font _helvetica;
- private static Font _consolas;
- private static PrivateFontCollection _fonts;
- private static bool _initialized = false;
- public static void LoadFonts(object assembly)
- {
- if (_initialized)
- return;
- _fonts = new PrivateFontCollection();
- string[] resources = new string[] {
- "CamshaftSearch.Properties.Helvetica.ttf",
- "CamshaftSearch.Properties.consola.ttf" };
- foreach (string resource in resources)
- {
- using (Stream stream = assembly.GetType().Assembly.GetManifestResourceStream(resource))
- {
- byte[] fontdata = new byte[stream.Length];
- stream.Read(fontdata, 0, (int)stream.Length);
- unsafe
- {
- fixed (byte* pFontData = fontdata)
- {
- _fonts.AddMemoryFont((IntPtr)pFontData, fontdata.Length);
- }
- }
- }
- }
- _helvetica = new Font(_fonts.Families[1], 8.25f, FontStyle.Regular);
- _consolas = new Font(_fonts.Families[0], 8.5f, FontStyle.Regular);
- _initialized = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement