/// <summary> /// Inits the font family names from DirectWrite /// </summary> private void InitFontFamilyNames() { var fontCollection = FactoryDWrite.GetSystemFontCollection(false); var familyCount = fontCollection.FontFamilyCount; List <string> names = new List <string>(); for (int i = 0; i < familyCount; i++) { var fontFamily = fontCollection.GetFontFamily(i); var familyNames = fontFamily.FamilyNames; int index; if (!familyNames.FindLocaleName(CultureInfo.CurrentCulture.Name, out index)) { familyNames.FindLocaleName("en-us", out index); } names.Add(familyNames.GetString(index)); } names.Sort(); comboBoxFont.Items.AddRange(names.ToArray()); comboBoxFont.SelectedItem = "Gabriola"; }
private static Font GetFont(string fontName, float fontSize) { var fontFamily = GetFontFamily(fontName); if (fontFamily == null) { if (privateFontLoader == null) { privateFontLoader = new PrivateFontLoader(FactoryDWrite); privateFontCollection = new FontCollection(FactoryDWrite, privateFontLoader, privateFontLoader.Key); } _currentFontCollection = privateFontCollection; fontFamily = GetFontFamily(fontName); } if (fontFamily == null) { _currentFontCollection = FactoryDWrite.GetSystemFontCollection(true); return(GenericSanSerif()); } // This is generic right now. We should be able to handle different styles in the future var font = fontFamily.GetFirstMatchingFont(FontWeight.Regular, FontStretch.Normal, FontStyle.Normal); return(font); }
public virtual void Cleanup() { Context?.ClearState(); Context?.Flush(); BackBuffer?.Dispose(); BackBuffer = null; ZBuffer?.Dispose(); ZBuffer = null; RenderTargetViewRef?.Dispose(); RenderTargetViewRef = null; DepthStencilSRVRef?.Dispose(); DepthStencilSRVRef = null; DepthStencilViewRef?.Dispose(); DepthStencilViewRef = null; FactoryDWrite?.Dispose(); FactoryDWrite = null; Factory2D?.Dispose(); Factory2D = null; RenderTarget2D?.Dispose(); RenderTarget2D = null; Context?.Dispose(); DeviceRef?.Dispose(); DeviceRef = null; }
static float dpiScale = 96f / 72f; // default but will be recalculated below // This should be a FontCollection //private static PrivateFontCollection _loadedFonts = new PrivateFontCollection(); private static FontFamily GetFontFamily(string familyName) { var fontList = FactoryDWrite.GetSystemFontCollection(true); int fontIndex = 0; fontList.FindFamilyName(familyName, out fontIndex); if (fontIndex < 0) { return(null); } var fontFamily = fontList.GetFontFamily(fontIndex); return(fontFamily); }
/// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } if (CurrentTextFormat != null) { CurrentTextFormat.Dispose(); } if (CurrentTextLayout != null) { CurrentTextLayout.Dispose(); } if (CustomTextRenderer != null) { CustomTextRenderer.Dispose(); } if (SceneColorBrush != null) { SceneColorBrush.Dispose(); } if (RenderTarget2D != null) { RenderTarget2D.Dispose(); } if (FactoryDWrite != null) { FactoryDWrite.Dispose(); } if (Factory2D != null) { Factory2D.Dispose(); } } base.Dispose(disposing); }
//private string CreateFont(string fontName, float fontSize, CCRawList<char> charset) private Font CreateFont(string fontName, float fontSize) { Font _currentFont; if (Factory2D == null) { Factory2D = new SharpDX.Direct2D1.Factory(); FactoryDWrite = new SharpDX.DirectWrite.Factory(); FactoryImaging = new SharpDX.WIC.ImagingFactory(); dpi = Factory2D.DesktopDpi; dpiScale = dpi.Height / 72f; } _currentFontCollection = FactoryDWrite.GetSystemFontCollection(true); if (_defaultFont == null) { _defaultFont = GenericSanSerif(); } FontFamily fontFamily = GetFontFamily(fontName); if (!_fontFamilyCache.TryGetValue(fontName, out fontFamily)) { var ext = Path.GetExtension(fontName); _currentFont = _defaultFont; _currentFont = GetFont(fontName, fontSize); _fontFamilyCache.Add(fontName, _currentFont.FontFamily); CCLog.Log("{0} not found. Defaulting to {1}.", fontName, _currentFont.FontFamily.FamilyNames.GetString(0)); } else { _currentFont = fontFamily.GetFirstMatchingFont(FontWeight.Regular, FontStretch.Normal, FontStyle.Normal); } return(_currentFont); }
private static Font GetFont(string fontName, float fontSize) { var fontFamily = GetFontFamily(fontName); if (fontFamily == null) { fontFamily = GetFontFamily(fontName); } if (fontFamily == null) { _currentFontCollection = FactoryDWrite.GetSystemFontCollection(true); CCLog.Log("{0} not found. Defaulting to {1}.", fontName, GenericSanSerif().FontFamily.FamilyNames.GetString(0)); return(GenericSanSerif()); } // This is generic right now. We should be able to handle different styles in the future var font = fontFamily.GetFirstMatchingFont(FontWeight.Regular, FontStretch.Normal, FontStyle.Normal); return(font); }
private static Font GenericSanSerif() { var sanserifs = new string[] { "Microsoft San Serif", "Arial", "Tahoma" }; FontFamily _fontFamily = null; foreach (var family in sanserifs) { _fontFamily = GetFontFamily(family); if (_fontFamily != null) { break; } } if (_fontFamily == null) { var fontList = FactoryDWrite.GetSystemFontCollection(true); int fontIndex = 0; _fontFamily = fontList.GetFontFamily(fontIndex); } var _font = _fontFamily.GetFirstMatchingFont(FontWeight.Regular, FontStretch.Normal, FontStyle.Normal); return(_font); }
//private string CreateFont(string fontName, float fontSize, CCRawList<char> charset) private Font CreateFont(string fontName, float fontSize) { Font _currentFont; if (Factory2D == null) { Factory2D = new SharpDX.Direct2D1.Factory(); FactoryDWrite = new SharpDX.DirectWrite.Factory(); FactoryImaging = new SharpDX.WIC.ImagingFactory(); dpi = Factory2D.DesktopDpi; dpiScale = dpi.Height / 72f; } _currentFontCollection = FactoryDWrite.GetSystemFontCollection(true); if (_defaultFont == null) { _defaultFont = GenericSanSerif(); } FontFamily fontFamily = GetFontFamily(fontName); FontCollection fontCollection; if (!_fontCollectionCache.TryGetValue(fontName, out fontCollection)) { var ext = Path.GetExtension(fontName); if (!String.IsNullOrEmpty(ext) && ext.ToLower() == ".ttf") { try { var fileFontLoader = new FileFontLoader(FactoryDWrite, fontName); var fileFontCollection = new FontCollection(FactoryDWrite, fileFontLoader, fileFontLoader.Key); _currentFontCollection = fileFontCollection; fontFamily = _currentFontCollection.GetFontFamily(0); var ffn = fontFamily.FamilyNames; _currentFont = fontFamily.GetFirstMatchingFont(FontWeight.Regular, FontStretch.Normal, FontStyle.Normal); _fontCollectionCache.Add(fontName, fileFontCollection); _currentFontCollection = fileFontCollection; } catch { _currentFont = GetFont(fontName, fontSize); CCLog.Log("{0} not found. Defaulting to {1}.", fontName, _currentFont.FontFamily.FamilyNames.GetString(0)); } } else { _currentFont = GetFont(fontName, fontSize); } } else { fontFamily = fontCollection.GetFontFamily(0); _currentFont = fontFamily.GetFirstMatchingFont(FontWeight.Regular, FontStretch.Normal, FontStyle.Normal); _currentFontCollection = fontCollection; } return(_currentFont); }
private string CreateFont(string fontName, float fontSize, CCRawList <char> charset) { if (Factory2D == null) { Factory2D = new SharpDX.Direct2D1.Factory(); FactoryDWrite = new SharpDX.DirectWrite.Factory(); FactoryImaging = new SharpDX.WIC.ImagingFactory(); dpi = Factory2D.DesktopDpi; dpiScale = dpi.Height / 72f; } _currentFontCollection = FactoryDWrite.GetSystemFontCollection(true); if (_defaultFont == null) { _defaultFont = GenericSanSerif(); //_defaultDIP = ConvertPointSizeToDIP(_defaultFontSizeEm); } FontFamily fontFamily = GetFontFamily(fontName); if (!_fontFamilyCache.TryGetValue(fontName, out fontFamily)) { var ext = Path.GetExtension(fontName); _currentFont = _defaultFont; //if (!String.IsNullOrEmpty(ext) && ext.ToLower() == ".ttf") //{ // //var appPath = AppDomain.CurrentDomain.BaseDirectory; // //var contentPath = Path.Combine(appPath, CCContentManager.SharedContentManager.RootDirectory); // //var fontPath = Path.Combine(contentPath, fontName); // //if (File.Exists(fontPath)) // //{ // // try // // { // // if (privateFontLoader == null) // // { // // privateFontLoader = new PrivateFontLoader(FactoryDWrite, fontName); // // privateFontCollection = new FontCollection(FactoryDWrite, privateFontLoader, privateFontLoader.Key); // // } // // _currentFontCollection = privateFontCollection; // // var family = _currentFontCollection.GetFontFamily(0); // // // This is generic right now. We should be able to handle different styles in the future // // var font = family.GetFirstMatchingFont(FontWeight.Regular, FontStretch.Normal, FontStyle.Normal); // // _currentFont = font; // // _currentFontSizeEm = fontSize; // // _currentDIP = ConvertPointSizeToDIP(fontSize); // // } // // catch // // { // // _currentFont = _defaultFont; // // _currentFontSizeEm = fontSize; // // _currentDIP = ConvertPointSizeToDIP(fontSize); // // } // //} // //else // //{ // // _currentFont = _defaultFont; // // _currentFontSizeEm = fontSize; // // _currentDIP = ConvertPointSizeToDIP(fontSize); // //} //} //else //{ _currentFont = GetFont(fontName, fontSize); _currentFontSizeEm = fontSize; _currentDIP = ConvertPointSizeToDIP(fontSize); //} _fontFamilyCache.Add(fontName, _currentFont.FontFamily); } else { _currentFont = fontFamily.GetFirstMatchingFont(FontWeight.Regular, FontStretch.Normal, FontStyle.Normal); _currentFontSizeEm = fontSize; _currentDIP = ConvertPointSizeToDIP(fontSize); } fontName = _currentFont.FontFamily.FamilyNames.GetString(0); textFormat = new TextFormat(FactoryDWrite, fontName, _currentFontCollection, FontWeight.Regular, FontStyle.Normal, FontStretch.Normal, _currentDIP); GetKerningInfo(charset); return(_currentFont.ToString()); }