internal static XFontSource GetOrCreateFromGdi(string typefaceKey, GdiFont gdiFont) { byte[] bytes = ReadFontBytesFromGdi(gdiFont); XFontSource fontSource = GetOrCreateFrom(typefaceKey, bytes); return(fontSource); }
/// <summary> /// Adds the specified font data to the global PrivateFontCollection. /// Family name and style are automatically retrieved from the font. /// </summary> public static void Add(byte[] font) { IntPtr unmanagedPointer = Marshal.AllocCoTaskMem(font.Length); Marshal.Copy(font, 0, unmanagedPointer, font.Length); Singleton.GetPrivateFontCollection().AddMemoryFont(unmanagedPointer, font.Length); // Do not free the memory here, AddMemoryFont stores a pointer, not a copy! //Marshal.FreeCoTaskMem(ip); XFontSource fontSource = XFontSource.GetOrCreateFrom(font); string familyName = fontSource.FontName; if (familyName.EndsWith(" Regular", StringComparison.OrdinalIgnoreCase)) { familyName = familyName.Substring(0, familyName.Length - 8); } bool bold = fontSource.Fontface.os2.IsBold; bool italic = fontSource.Fontface.os2.IsItalic; IncompetentlyMakeAHackToFixAProblemYouWoldNeverHaveIfYouUseAFontResolver(fontSource, ref familyName, ref bold, ref italic); string key = MakeKey(familyName, bold, italic); Singleton._fontSources.Add(key, fontSource); string typefaceKey = XGlyphTypeface.ComputeKey(familyName, bold, italic); FontFactory.CacheExistingFontSourceWithNewTypefaceKey(typefaceKey, fontSource); }
internal static XFontSource GetOrCreateFromWpf(string typefaceKey, WpfGlyphTypeface wpfGlyphTypeface) { byte[] bytes = ReadFontBytesFromWpf(wpfGlyphTypeface); XFontSource fontSource = GetOrCreateFrom(typefaceKey, bytes); return(fontSource); }
public static XGlyphTypeface GetOrCreateFromGdi(GdiFont gdiFont) { // $TODO THHO Lock??? string typefaceKey = ComputeKey(gdiFont); if (GlyphTypefaceCache.TryGetGlyphTypeface(typefaceKey, out XGlyphTypeface glyphTypeface)) { // We have the glyph typeface already in cache. return(glyphTypeface); } XFontFamily fontFamily = XFontFamily.GetOrCreateFromGdi(gdiFont); XFontSource fontSource = XFontSource.GetOrCreateFromGdi(typefaceKey, gdiFont); // Check if styles must be simulated. XStyleSimulations styleSimulations = XStyleSimulations.None; if (gdiFont.Bold && !fontSource.Fontface.os2.IsBold) { styleSimulations |= XStyleSimulations.BoldSimulation; } if (gdiFont.Italic && !fontSource.Fontface.os2.IsItalic) { styleSimulations |= XStyleSimulations.ItalicSimulation; } glyphTypeface = new XGlyphTypeface(typefaceKey, fontFamily, fontSource, styleSimulations, gdiFont); GlyphTypefaceCache.AddGlyphTypeface(glyphTypeface); return(glyphTypeface); }
public static XGlyphTypeface GetOrCreateFromWpf(WpfTypeface wpfTypeface) { #if DEBUG if (wpfTypeface.FontFamily.Source == "Segoe UI Semilight") { wpfTypeface.GetType(); } #endif //string typefaceKey = ComputeKey(wpfTypeface); //XGlyphTypeface glyphTypeface; //if (GlyphTypefaceCache.TryGetGlyphTypeface(typefaceKey, out glyphTypeface)) //{ // // We have the glyph typeface already in cache. // return glyphTypeface; //} // Lock around TryGetGlyphTypeface and AddGlyphTypeface. try { Lock.EnterFontFactory(); // Create WPF glyph typeface. WpfGlyphTypeface wpfGlyphTypeface; if (!wpfTypeface.TryGetGlyphTypeface(out wpfGlyphTypeface)) { return(null); } string typefaceKey = ComputeKey(wpfGlyphTypeface); string name1 = wpfGlyphTypeface.DesignerNames[FontHelper.CultureInfoEnUs]; string name2 = wpfGlyphTypeface.FaceNames[FontHelper.CultureInfoEnUs]; string name3 = wpfGlyphTypeface.FamilyNames[FontHelper.CultureInfoEnUs]; string name4 = wpfGlyphTypeface.ManufacturerNames[FontHelper.CultureInfoEnUs]; string name5 = wpfGlyphTypeface.Win32FaceNames[FontHelper.CultureInfoEnUs]; string name6 = wpfGlyphTypeface.Win32FamilyNames[FontHelper.CultureInfoEnUs]; XGlyphTypeface glyphTypeface; if (GlyphTypefaceCache.TryGetGlyphTypeface(typefaceKey, out glyphTypeface)) { // We have the glyph typeface already in cache. return(glyphTypeface); } XFontFamily fontFamily = XFontFamily.GetOrCreateFromWpf(wpfTypeface.FontFamily); XFontSource fontSource = XFontSource.GetOrCreateFromWpf(typefaceKey, wpfGlyphTypeface); glyphTypeface = new XGlyphTypeface(typefaceKey, fontFamily, fontSource, (XStyleSimulations)wpfGlyphTypeface.StyleSimulations, wpfTypeface, wpfGlyphTypeface); GlyphTypefaceCache.AddGlyphTypeface(glyphTypeface); return(glyphTypeface); } finally { Lock.ExitFontFactory(); } }
/// <summary> /// Gets an existing font source or creates a new one. /// A new font source is cached in font factory. /// </summary> public static XFontSource GetOrCreateFrom(byte[] bytes) { ulong key = FontHelper.CalcChecksum(bytes); if (!FontFactory.TryGetFontSourceByKey(key, out XFontSource fontSource)) { fontSource = new XFontSource(bytes, key); // Theoretically the font source could be created by a differend thread in the meantime. fontSource = FontFactory.CacheFontSource(fontSource); } return(fontSource); }
const string KeyPrefix = "tk:"; // "typeface key" #if CORE || GDI XGlyphTypeface(string key, XFontFamily fontFamily, XFontSource fontSource, XStyleSimulations styleSimulations, GdiFont gdiFont) { Key = key; FontFamily = fontFamily; FontSource = fontSource; Fontface = OpenTypeFontface.CetOrCreateFrom(fontSource); Debug.Assert(ReferenceEquals(FontSource.Fontface, Fontface)); GdiFont = gdiFont; StyleSimulations = styleSimulations; Initialize(); }
XGlyphTypeface(string key, XFontFamily fontFamily, XFontSource fontSource, XStyleSimulations styleSimulations) { _key = key; _fontFamily = fontFamily; _fontSource = fontSource; _styleSimulations = styleSimulations; _fontface = OpenTypeFontface.CetOrCreateFrom(fontSource); Debug.Assert(ReferenceEquals(_fontSource.Fontface, _fontface)); //_wpfTypeface = wpfTypeface; //_wpfGlyphTypeface = wpfGlyphTypeface; Initialize(); }
/// <summary> /// Initializes a new instance of the <see cref="XGlyphTypeface"/> class by a font source. /// </summary> public XGlyphTypeface(XFontSource fontSource) { string familyName = fontSource.Fontface.name.Name; _fontFamily = new XFontFamily(familyName, false); _fontface = fontSource.Fontface; _isBold = _fontface.os2.IsBold; _isItalic = _fontface.os2.IsItalic; _key = ComputeKey(familyName, _isBold, _isItalic); //_fontFamily =xfont FontFamilyCache.GetFamilyByName(familyName); _fontSource = fontSource; Initialize(); }
static XFontSource GetOrCreateFrom(string typefaceKey, byte[] fontBytes) { ulong key = FontHelper.CalcChecksum(fontBytes); if (FontFactory.TryGetFontSourceByKey(key, out XFontSource fontSource)) { // The font source already exists, but is not yet cached under the specified typeface key. FontFactory.CacheExistingFontSourceWithNewTypefaceKey(typefaceKey, fontSource); } else { // No font source exists. Create new one and cache it. fontSource = new XFontSource(fontBytes, key); FontFactory.CacheNewFontSource(typefaceKey, fontSource); } return(fontSource); }
//internal static XGlyphTypeface TryGetXGlyphTypeface(string familyName, XFontStyle style) //{ // string name = MakeName(familyName, style); // XGlyphTypeface typeface; // _global._typefaces.TryGetValue(name, out typeface); // return typeface; //} #if GDI internal static GdiFont TryCreateFont(string name, double size, GdiFontStyle style, out XFontSource fontSource) { fontSource = null; try { GdiPrivateFontCollection pfc = Singleton._privateFontCollection; if (pfc == null) { return(null); } #if true string key = MakeKey(name, (XFontStyle)style); if (Singleton._fontSources.TryGetValue(key, out fontSource)) { GdiFont font = new GdiFont(name, (float)size, style, GraphicsUnit.World); #if DEBUG_ Debug.Assert(StringComparer.OrdinalIgnoreCase.Compare(name, font.Name) == 0); Debug.Assert(font.Bold == ((style & GdiFontStyle.Bold) != 0)); Debug.Assert(font.Italic == ((style & GdiFontStyle.Italic) != 0)); #endif return(font); } return(null); #else foreach (GdiFontFamily family in pfc.Families) { if (string.Compare(family.Name, name, StringComparison.OrdinalIgnoreCase) == 0) { GdiFont font = new GdiFont(family, (float)size, style, GraphicsUnit.World); if (string.Compare(font.Name, name, StringComparison.OrdinalIgnoreCase) != 0) { // Style simulation is not implemented in GDI+. // Use WPF build. } return(font); } } #endif } catch (Exception ex) { // Ignore exception and return null. Debug.WriteLine(ex.ToString()); } return(null); }
static void IncompetentlyMakeAHackToFixAProblemYouWoldNeverHaveIfYouUseAFontResolver(XFontSource fontSource, ref string familyName, ref bool bold, ref bool italic) { const string regularSuffix = " Regular"; const string boldSuffix = " Bold"; const string italicSuffix = " Italic"; const string boldItalicSuffix = " Bold Italic"; const string italicBoldSuffix = " Italic Bold"; if (familyName.EndsWith(regularSuffix, StringComparison.OrdinalIgnoreCase)) { familyName = familyName.Substring(0, familyName.Length - regularSuffix.Length); Debug.Assert(!bold && !italic); bold = italic = false; } else if (familyName.EndsWith(boldItalicSuffix, StringComparison.OrdinalIgnoreCase) || familyName.EndsWith(italicBoldSuffix, StringComparison.OrdinalIgnoreCase)) { familyName = familyName.Substring(0, familyName.Length - boldItalicSuffix.Length); Debug.Assert(bold && italic); bold = italic = true; } else if (familyName.EndsWith(boldSuffix, StringComparison.OrdinalIgnoreCase)) { familyName = familyName.Substring(0, familyName.Length - boldSuffix.Length); Debug.Assert(bold && !italic); bold = true; italic = false; } else if (familyName.EndsWith(italicSuffix, StringComparison.OrdinalIgnoreCase)) { familyName = familyName.Substring(0, familyName.Length - italicSuffix.Length); Debug.Assert(!bold && italic); bold = false; italic = true; } else { Debug.Assert(!bold && !italic); bold = false; italic = false; } }
public static XFontSource CreateCompiledFont(byte[] bytes) { XFontSource fontSource = new XFontSource(bytes, 0); return(fontSource); }
public static XGlyphTypeface GetOrCreateFrom(string familyName, FontResolvingOptions fontResolvingOptions) { // Check cache for requested type face. string typefaceKey = ComputeKey(familyName, fontResolvingOptions); XGlyphTypeface glyphTypeface; try { // Lock around TryGetGlyphTypeface and AddGlyphTypeface. Lock.EnterFontFactory(); if (GlyphTypefaceCache.TryGetGlyphTypeface(typefaceKey, out glyphTypeface)) { // Just return existing one. return(glyphTypeface); } // Resolve typeface by FontFactory. FontResolverInfo fontResolverInfo = FontFactory.ResolveTypeface(familyName, fontResolvingOptions, typefaceKey); if (fontResolverInfo == null) { // No fallback - just stop. throw new InvalidOperationException("No appropriate font found."); } #if CORE || GDI GdiFont gdiFont = null; #endif #if WPF WpfFontFamily wpfFontFamily = null; WpfTypeface wpfTypeface = null; WpfGlyphTypeface wpfGlyphTypeface = null; #endif #if UWP // Nothing to do. #endif // Now create the font family at the first. XFontFamily fontFamily; if (fontResolverInfo is PlatformFontResolverInfo platformFontResolverInfo) { // Case: fontResolverInfo was created by platform font resolver // and contains platform specific objects that are reused. #if CORE || GDI // Reuse GDI+ font from platform font resolver. gdiFont = platformFontResolverInfo.GdiFont; fontFamily = XFontFamily.GetOrCreateFromGdi(gdiFont); #endif #if WPF #if !SILVERLIGHT // Reuse WPF font family created from platform font resolver. wpfFontFamily = platformFontResolverInfo.WpfFontFamily; wpfTypeface = platformFontResolverInfo.WpfTypeface; wpfGlyphTypeface = platformFontResolverInfo.WpfGlyphTypeface; fontFamily = XFontFamily.GetOrCreateFromWpf(wpfFontFamily); #else fontFamily = XFontFamily.GetOrCreateFromWpf(new WpfFontFamily(familyName)); #endif #endif #if NETFX_CORE || UWP fontFamily = null; #endif } else { // Case: fontResolverInfo was created by custom font resolver. // Get or create font family for custom font resolver retrieved font source. fontFamily = XFontFamily.GetOrCreateFontFamily(familyName); } // We have a valid font resolver info. That means we also have an XFontSource object loaded in the cache. XFontSource fontSource = FontFactory.GetFontSourceByFontName(fontResolverInfo.FaceName); Debug.Assert(fontSource != null); // Each font source already contains its OpenTypeFontface. #if CORE || GDI glyphTypeface = new XGlyphTypeface(typefaceKey, fontFamily, fontSource, fontResolverInfo.StyleSimulations, gdiFont); #endif #if WPF glyphTypeface = new XGlyphTypeface(typefaceKey, fontFamily, fontSource, fontResolverInfo.StyleSimulations, wpfTypeface, wpfGlyphTypeface); #endif #if NETFX_CORE || UWP glyphTypeface = new XGlyphTypeface(typefaceKey, fontFamily, fontSource, fontResolverInfo.StyleSimulations); #endif GlyphTypefaceCache.AddGlyphTypeface(glyphTypeface); } finally { Lock.ExitFontFactory(); } return(glyphTypeface); }
public static GdiFont CreateFont(string familyName, double emSize, GdiFontStyle style, out XFontSource fontSource) { fontSource = null; // ReSharper disable once JoinDeclarationAndInitializer GdiFont font; // Use font resolver in CORE build. XPrivateFontCollection exists only in GDI and WPF build. #if GDI // Try private font collection first. font = XPrivateFontCollection.TryCreateFont(familyName, emSize, style, out fontSource); if (font != null) { // Get font source is different for this font because Win32 does not know it. return(font); } #endif // Create ordinary Win32 font. font = new GdiFont(familyName, (float)emSize, style, GraphicsUnit.World); return(font); }