public static XGlyphTypeface GetOrCreateFromGdi(GdiFont gdiFont) { // $TODO THHO Lock??? string typefaceKey = ComputeKey(gdiFont); XGlyphTypeface glyphTypeface; if (GlyphTypefaceCache.TryGetGlyphTypeface(typefaceKey, out 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 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."); } GdiFont gdiFont = null; // Now create the font family at the first. XFontFamily fontFamily; PlatformFontResolverInfo platformFontResolverInfo = fontResolverInfo as PlatformFontResolverInfo; if (platformFontResolverInfo != null) { // Case: fontResolverInfo was created by platform font resolver // and contains platform specific objects that are reused. // Reuse GDI+ font from platform font resolver. gdiFont = platformFontResolverInfo.GdiFont; fontFamily = XFontFamily.GetOrCreateFromGdi(gdiFont); } 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. glyphTypeface = new XGlyphTypeface(typefaceKey, fontFamily, fontSource, fontResolverInfo.StyleSimulations, gdiFont); GlyphTypefaceCache.AddGlyphTypeface(glyphTypeface); } finally { Lock.ExitFontFactory(); } return(glyphTypeface); }
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 && !WITHOUT_DRAWING || 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 !WITHOUT_DRAWING PlatformFontResolverInfo platformFontResolverInfo = fontResolverInfo as PlatformFontResolverInfo; if (platformFontResolverInfo != null) { // 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 #endif { // 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 #if !WITHOUT_DRAWING , gdiFont #endif ); #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 XGlyphTypeface GetOrCreateFrom(string familyName, FontResolvingOptions fontResolvingOptions) { // Check cache for requested type face. string typefaceKey = ComputeKey(familyName, fontResolvingOptions); XGlyphTypeface glyphTypeface; 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; PlatformFontResolverInfo platformFontResolverInfo = fontResolverInfo as PlatformFontResolverInfo; if (platformFontResolverInfo != null) { #if CORE || GDI // $TODO THHO Lock??? // 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 { // Create new and exclusively used font family for custom font resolver retrieved font source. fontFamily = XFontFamily.CreateSolitary(fontResolverInfo.FaceName); } // We have a valid font resolver info. That means we also have an XFontSource object loaded in the cache. ////XFontSource fontSource = FontFactory.GetFontSourceByTypefaceKey(fontResolverInfo.FaceName); 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); return(glyphTypeface); }