/// <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); }
/// <summary> /// A GDI+ font object is used to setup the internal font objects. /// </summary> void InitializeFromGdi() { try { Lock.EnterFontFactory(); if (GdiFontFamily != null) { // Create font based on its family. GdiFont = new GdiFont(GdiFontFamily, (float)Size, (GdiFontStyle)Style, GraphicsUnit.World); } if (GdiFont != null) { #if DEBUG_ string name1 = _gdiFont.Name; string name2 = _gdiFont.OriginalFontName; string name3 = _gdiFont.SystemFontName; #endif FamilyName = GdiFont.FontFamily.Name; // TODO: _glyphTypeface = XGlyphTypeface.GetOrCreateFrom(_gdiFont); } else { Debug.Assert(false); } if (GlyphTypeface == null) { GlyphTypeface = XGlyphTypeface.GetOrCreateFromGdi(GdiFont); } CreateDescriptorAndInitializeFontMetrics(); } finally { Lock.ExitFontFactory(); } }
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> /// Creates the TextBlock. /// </summary> public static TextBlock CreateTextBlock(string text, XGlyphTypeface glyphTypeface, double emSize, Brush brush) { TextBlock textBlock = new TextBlock(); textBlock.FontFamily = glyphTypeface.FontFamily; textBlock.FontSource = glyphTypeface.FontSource; textBlock.FontSize = emSize; textBlock.FontWeight = glyphTypeface.IsBold ? FontWeights.Bold : FontWeights.Normal; textBlock.FontStyle = glyphTypeface.IsItalic ? FontStyles.Italic : FontStyles.Normal; textBlock.Foreground = brush; textBlock.Text = text; return(textBlock); }
void InitializeFromWpf() { if (_wpfFontFamily != null) { _wpfTypeface = FontHelper.CreateTypeface(_wpfFontFamily, _style); } if (_wpfTypeface != null) { _familyName = _wpfTypeface.FontFamily.FamilyNames[XmlLanguage.GetLanguage("en-US")]; _glyphTypeface = XGlyphTypeface.GetOrCreateFromWpf(_wpfTypeface); } else { Debug.Assert(false); } if (_glyphTypeface == null) { _glyphTypeface = XGlyphTypeface.GetOrCreateFrom(_familyName, new FontResolvingOptions(_style)); } CreateDescriptorAndInitializeFontMetrics(); }
public bool TryGetGlyphTypeface(out XGlyphTypeface glyphTypeface) { glyphTypeface = null; return(false); }
//// Methods //public Font(Font prototype, FontStyle newStyle); //public Font(FontFamily family, float emSize); //public Font(string familyName, float emSize); //public Font(FontFamily family, float emSize, FontStyle style); //public Font(FontFamily family, float emSize, GraphicsUnit unit); //public Font(string familyName, float emSize, FontStyle style); //public Font(string familyName, float emSize, GraphicsUnit unit); //public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit); //public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit); ////public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet); ////public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet); ////public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet, bool gdiVerticalFont); ////public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet, bool gdiVerticalFont); //public object Clone(); //private static FontFamily CreateFontFamilyWithFallback(string familyName); //private void Dispose(bool disposing); //public override bool Equals(object obj); //protected override void Finalize(); //public static Font FromHdc(IntPtr hdc); //public static Font FromHfont(IntPtr hfont); //public static Font FromLogFont(object lf); //public static Font FromLogFont(object lf, IntPtr hdc); //public override int GetHashCode(); /// <summary> /// Initializes this instance by computing the glyph typeface, font family, font source and TrueType fontface. /// (PDFSharp currently only deals with TrueType fonts.) /// </summary> void Initialize() { //#if DEBUG // if (_familyName == "Segoe UI Semilight" && (_style & XFontStyle.BoldItalic) == XFontStyle.Italic) // GetType(); //#endif FontResolvingOptions fontResolvingOptions = OverrideStyleSimulations ? new FontResolvingOptions(Style, StyleSimulations) : new FontResolvingOptions(Style); // HACK: 'PlatformDefault' is used in unit test code. if (StringComparer.OrdinalIgnoreCase.Compare(FamilyName, GlobalFontSettings.DefaultFontName) == 0) { #if CORE || GDI || WPF FamilyName = "Calibri"; #endif } // In principle an XFont is an XGlyphTypeface plus an em-size. GlyphTypeface = XGlyphTypeface.GetOrCreateFrom(FamilyName, fontResolvingOptions); #if GDI // TODO: In CORE build it is not necessary to create a GDI font at all // Create font by using font family. XFontSource fontSource; // Not needed here. _gdiFont = FontHelper.CreateFont(_familyName, (float)_emSize, (GdiFontStyle)(_style & XFontStyle.BoldItalic), out fontSource); #endif #if WPF && !SILVERLIGHT // Pure WPF _wpfFontFamily = _glyphTypeface.FontFamily.WpfFamily; _wpfTypeface = _glyphTypeface.WpfTypeface; if (_wpfFontFamily == null) { _wpfFontFamily = new WpfFontFamily(Name); } if (_wpfTypeface == null) { _wpfTypeface = FontHelper.CreateTypeface(WpfFontFamily, _style); } #endif #if WPF && SILVERLIGHT_ // Pure Silverlight 5 if (GlyphTypeface == null) { //Debug.Assert(Typeface == null); // #P F C //GlyphTypeface = XPrivateFontCollection.TryGetXGlyphTypeface(Name, _style); //if (GlyphTypeface == null) //{ // // HACK: Just make it work... // GlyphTypeface = GlobalFontSettings.TryGetXGlyphTypeface(Name, _style, out Data); //} #if DEBUG if (GlyphTypeface == null) { throw new Exception("No font: " + Name); } #endif _wpfFamily = GlyphTypeface.FontFamily; } //if (Family == null) // Family = new System.Windows.Media.FontFamily(Name); //if (Typeface == null) // Typeface = FontHelper.CreateTypeface(Family, _style); #endif CreateDescriptorAndInitializeFontMetrics(); }
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); }