/// <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); }
public override bool Equals(object obj) { XFontSource fontSource = obj as XFontSource; if (fontSource == null) { return(false); } return(Key == fontSource.Key); }
const string KeyPrefix = "tk:"; // "typeface key" XGlyphTypeface(string key, XFontFamily fontFamily, XFontSource fontSource, XStyleSimulations styleSimulations) { _key = key; _fontFamily = fontFamily; _fontSource = fontSource; _fontface = OpenTypeFontface.CetOrCreateFrom(fontSource); Debug.Assert(ReferenceEquals(_fontSource.Fontface, _fontface)); _styleSimulations = styleSimulations; Initialize(); }
/// <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); XFontSource fontSource; if (!FontFactory.TryGetFontSourceByKey(key, out 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); }
/// <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) { XFontSource fontSource; ulong key = FontHelper.CalcChecksum(fontBytes); if (FontFactory.TryGetFontSourceByKey(key, out 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); }
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."); } // Now create the font family at the first. // Create new and exclusively used font family for custom font resolver retrieved font source. XFontFamily 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. glyphTypeface = new XGlyphTypeface(typefaceKey, fontFamily, fontSource, fontResolverInfo.StyleSimulations); GlyphTypefaceCache.AddGlyphTypeface(glyphTypeface); return(glyphTypeface); }
public static XFontSource CreateCompiledFont(byte[] bytes) { XFontSource fontSource = new XFontSource(bytes, 0); 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 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); }