using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Win32;
// todo: compile/run only on windows
namespace Gwen.Platform
{
///
/// Windows-specific utility functions.
///
public static class Windows
{
private const String FontRegKey = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts";
private static Dictionary m_FontPaths;
///
/// Gets a font file path from font name.
///
/// Font name.
/// Font file path.
public static String GetFontPath(String fontName)
{
// is this reliable? we rely on lazy jitting to not run win32 code on linux
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
return null;
if (m_FontPaths == null)
InitFontPaths();
if (!m_FontPaths.ContainsKey(fontName))
return null;
return m_FontPaths[fontName];
}
private static void InitFontPaths()
{
// very hacky but better than nothing
m_FontPaths = new Dictionary();
String fontsDir = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default);
RegistryKey subkey = key.OpenSubKey(FontRegKey);
foreach (String fontName in subkey.GetValueNames())
{
String fontFile = (String)subkey.GetValue(fontName);
if (!fontName.EndsWith(" (TrueType)"))
continue;
String font = fontName.Replace(" (TrueType)", "");
m_FontPaths[font] = Path.Combine(fontsDir, fontFile);
}
key.Dispose();
}
}
}