public List <InstalledFont> GetFonts() //该方法返回系统的所有字体 { var fontList = new List <InstalledFont>(); var factory = new SharpDX.DirectWrite.Factory(); //引入了SharpDX这个包,,,枚举类型 var fontCollection = factory.GetSystemFontCollection(false); var familyCount = fontCollection.FontFamilyCount; 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)) { if (!familyNames.FindLocaleName("en-us", out index)) { index = 0; } } string name = familyNames.GetString(index); fontList.Add(new InstalledFont() { Name = name, FamilyIndex = i, Index = index //共3个属性,当需要获取系统字体库的时候,只需要Name属性就行了,将该List集合中的所有Name属性添加到ComFont控件中即可 }); } return(fontList); }
private Dictionary<string, string> LoadFonts() { Dictionary<string, string> result = new Dictionary<string, string>(); var factory = new Factory(); var fontCollection = factory.GetSystemFontCollection(false); var familyCount = fontCollection.FontFamilyCount; 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); } string name = familyNames.GetString(index); result.Add(name, name); } return result; }
private static sw.FontCollection FontCollection() { if (fontCollection == null) { fontCollection = Factory.GetSystemFontCollection( checkForUpdates: false); } return(fontCollection); }
public DirectWriteTextRenderer(DWrite.Factory dWriteFactory) { renderTarget = dWriteFactory.GdiInterop.CreateBitmapRenderTarget(IntPtr.Zero, MAX_GLYPH_SIZE, MAX_GLYPH_SIZE); renderTarget.PixelsPerDip = 1f; hdc = renderTarget.MemoryDC; hBrush = GDI.CreateSolidBrush(0x00000000); hbitmap = GDI.GetCurrentObject(hdc, GDI.OBJ_BITMAP); GDI.DIBSECTION dib; GDI.GetObject(hbitmap, Marshal.SizeOf(typeof(GDI.DIBSECTION)), out dib); bytesPerPixel = dib.dsBm.bmBitsPixel / 8; bmBits = dib.dsBm.bmBits; fontCollection = dWriteFactory.GetSystemFontCollection(false); renderParams = new RenderingParams(dWriteFactory, 1.2f, 0, 0, PixelGeometry.Flat, RenderingMode.Default); pages.Add(new Texture2D(TEXT_PAGE_SIZE, TEXT_PAGE_SIZE, false, SurfaceFormat.Color)); }
private FontFace GetFontFaceFromSystemFonts(Factory factory, SpriteFontAsset options) { SharpDX.DirectWrite.Font font; using (var fontCollection = factory.GetSystemFontCollection(false)) { int index; if (!fontCollection.FindFamilyName(options.FontName, out index)) { // Lets try to import System.Drawing for old system bitmap fonts (like MS Sans Serif) throw new FontNotFoundException(options.FontName); } using (var fontFamily = fontCollection.GetFontFamily(index)) { var weight = options.Style.IsBold()? FontWeight.Bold: FontWeight.Regular; var style = options.Style.IsItalic() ? SharpDX.DirectWrite.FontStyle.Italic : SharpDX.DirectWrite.FontStyle.Normal; font = fontFamily.GetFirstMatchingFont(weight, FontStretch.Normal, style); } } return(new FontFace(font)); }
public override string GetFontPath(AssetCompilerResult result = null) { using (var factory = new Factory()) { Font font; using (var fontCollection = factory.GetSystemFontCollection(false)) { int index; if (!fontCollection.FindFamilyName(FontName, out index)) { result?.Error("Cannot find system font '{0}'. Make sure it is installed on this machine.", FontName); return null; } using (var fontFamily = fontCollection.GetFontFamily(index)) { var weight = Style.IsBold() ? FontWeight.Bold : FontWeight.Regular; var style = Style.IsItalic() ? SharpDX.DirectWrite.FontStyle.Italic : SharpDX.DirectWrite.FontStyle.Normal; font = fontFamily.GetFirstMatchingFont(weight, FontStretch.Normal, style); if (font == null) { result?.Error("Cannot find style '{0}' for font family {1}. Make sure it is installed on this machine.", Style, FontName); return null; } } } var fontFace = new FontFace(font); // get the font path on the hard drive var file = fontFace.GetFiles().First(); var referenceKey = file.GetReferenceKey(); var originalLoader = (FontFileLoaderNative)file.Loader; var loader = originalLoader.QueryInterface<LocalFontFileLoader>(); return loader.GetFilePath(referenceKey); } }
public List<Character> GetCharacters() { var factory = new Factory(); var fontCollection = factory.GetSystemFontCollection(false); var fontFamily = fontCollection.GetFontFamily(FamilyIndex); var font = fontFamily.GetFont(Index); var characters = new List<Character>(); var count = 65535; for (var i = 0; i < count; i++) { if (font.HasCharacter(i)) { characters.Add(new Character() { Char = char.ConvertFromUtf32(i), UnicodeIndex = i }); } } return characters; }
public static List<InstalledFont> GetFonts() { var fontList = new List<InstalledFont>(); var factory = new Factory(); var fontCollection = factory.GetSystemFontCollection(false); var familyCount = fontCollection.FontFamilyCount; 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)) { if (!familyNames.FindLocaleName("en-us", out index)) { index = 0; } } bool isSymbolFont = fontFamily.GetFont(index).IsSymbolFont; string name = familyNames.GetString(index); fontList.Add(new InstalledFont() { Name = name, FamilyIndex = i, Index = index, IsSymbolFont = isSymbolFont }); } return fontList; }
/// <inheritdoc/> public override FontFace GetFontFace() { var factory = new Factory(); SharpDX.DirectWrite.Font font; using (var fontCollection = factory.GetSystemFontCollection(false)) { int index; if (!fontCollection.FindFamilyName(FontName, out index)) { // Lets try to import System.Drawing for old system bitmap fonts (like MS Sans Serif) throw new FontNotFoundException(FontName); } using (var fontFamily = fontCollection.GetFontFamily(index)) { var weight = Style.IsBold() ? FontWeight.Bold : FontWeight.Regular; var style = Style.IsItalic() ? SharpDX.DirectWrite.FontStyle.Italic : SharpDX.DirectWrite.FontStyle.Normal; font = fontFamily.GetFirstMatchingFont(weight, FontStretch.Normal, style); } } return new FontFace(font); }
private void LoadFonts() { Task.Run(() => { var x = new List<string>(); var factory = new Factory(); FontCollection fontCollection = factory.GetSystemFontCollection(false); int familyCount = fontCollection.FontFamilyCount; for (int i = 0; i < familyCount; i++) { FontFamily fontFamily = fontCollection.GetFontFamily(i); LocalizedStrings familyNames = fontFamily.FamilyNames; int index; if (!familyNames.FindLocaleName(CultureInfo.CurrentCulture.Name, out index)) familyNames.FindLocaleName("en-us", out index); string name = familyNames.GetString(index); x.Add(name); } Fonts = new ObservableCollection<string>(x.OrderBy(y => y)); }); }
private void TextByGlyph(RectangleF rect, string text, TextInfo info, SolidColorBrush brush) { FontFace fontFace; if (!m_faceMap.TryGetValue(info.Font, out fontFace)) { using (var f = new SharpDX.DirectWrite.Factory()) using (var collection = f.GetSystemFontCollection(false)) { int familyIndex; if (!collection.FindFamilyName(info.Font.FamilylName, out familyIndex)) { return; } using (var family = collection.GetFontFamily(familyIndex)) using (var font = family.GetFont(0)) { fontFace = new FontFace(font); m_faceMap.Add(info.Font, fontFace); } } } var codePoints = EnumCodePoints(text).ToArray(); var indices = fontFace.GetGlyphIndices(codePoints); // Get glyph var metrices = fontFace.GetDesignGlyphMetrics(indices, false); // draw var glyphRun = new GlyphRun { FontFace = fontFace, Indices = indices, FontSize = info.Font.Size, }; bool done = false; using (var f = new SharpDX.DirectWrite.Factory()) using (var ff = f.QueryInterface <SharpDX.DirectWrite.Factory4>()) { var desc = new GlyphRunDescription { }; ColorGlyphRunEnumerator it; var result = ff.TryTranslateColorGlyphRun(0, 0, glyphRun, null, MeasuringMode.Natural, null, 0, out it); if (result.Code == DWRITE_E_NOCOLORLOR) { m_device.D2DDeviceContext.DrawGlyphRun(rect.TopLeft + new Vector2(0, info.Font.Size), glyphRun, brush, MeasuringMode.Natural); } else { while (true) { var colorBrush = GetOrCreateBrush(new Color4( it.CurrentRun.RunColor.R, it.CurrentRun.RunColor.G, it.CurrentRun.RunColor.B, it.CurrentRun.RunColor.A)); m_device.D2DDeviceContext.DrawGlyphRun(rect.TopLeft + new Vector2(0, info.Font.Size), it.CurrentRun.GlyphRun, colorBrush, MeasuringMode.Natural); done = true; SharpDX.Mathematics.Interop.RawBool hasNext; it.MoveNext(out hasNext); if (!hasNext) { break;; } } } } }