public void DrawString(string text, float x, float y, FontWeight fw, SharpDX.DirectWrite.FontStyle fs, SharpDX.Color fontColor, float fontHeight, bool isUnderlined) { if ((y > -2) && (y <= tbHeight - fontHeight)) { gv.DrawText(text, x + tbXloc, y + tbYloc, fw, fs, 1.0f, fontColor, isUnderlined); } }
/// <summary> /// Builds the text geometry for the given string. /// </summary> /// <param name="stringToBuild">The string to build within the geometry.</param> /// <param name="geometryOptions">Some configuration for geometry creation.</param> public void BuildTextGeometry(string stringToBuild, TextGeometryOptions geometryOptions) { DWrite.Factory writeFactory = GraphicsCore.Current.FactoryDWrite; //TODO: Cache font objects //Get DirectWrite font weight DWrite.FontWeight fontWeight = DWrite.FontWeight.Normal; switch (geometryOptions.FontWeight) { case FontGeometryWeight.Bold: fontWeight = DWrite.FontWeight.Bold; break; default: fontWeight = DWrite.FontWeight.Normal; break; } //Get DirectWrite font style DWrite.FontStyle fontStyle = DWrite.FontStyle.Normal; switch (geometryOptions.FontStyle) { case FontGeometryStyle.Italic: fontStyle = DWrite.FontStyle.Italic; break; case FontGeometryStyle.Oblique: fontStyle = DWrite.FontStyle.Oblique; break; default: fontStyle = DWrite.FontStyle.Normal; break; } //Create the text layout object try { DWrite.TextLayout textLayout = new DWrite.TextLayout( writeFactory, stringToBuild, new DWrite.TextFormat( writeFactory, geometryOptions.FontFamily, fontWeight, fontStyle, geometryOptions.FontSize), float.MaxValue, float.MaxValue, 1f, true); //Render the text using the vertex structure text renderer using (VertexStructureTextRenderer textRenderer = new VertexStructureTextRenderer(this, geometryOptions)) { textLayout.Draw(textRenderer, 0f, 0f); } } catch (SharpDX.SharpDXException) { //TODO: Display some error } }
void textbox_FontChanged(object sender, EventArgs e) { FooTextBox textbox = (FooTextBox)sender; Font font = textbox.Font; this.fontName = font.Name; this.fontSize = font.Size; DW.FontWeight weigth = font.Bold ? DW.FontWeight.Bold : DW.FontWeight.Normal; DW.FontStyle style = font.Italic ? DW.FontStyle.Italic : DW.FontStyle.Normal; this.InitTextFormat(font.Name, font.Size, weigth, style); }
public static void Convert(FontStyle value, out sw.FontStyle fontStyle, out sw.FontWeight fontWeight) { fontStyle = sw.FontStyle.Normal; fontWeight = sw.FontWeight.Normal; if (value.HasFlag(FontStyle.Italic)) { fontStyle = sw.FontStyle.Italic; } if (value.HasFlag(FontStyle.Bold)) { fontWeight = sw.FontWeight.Bold; } }
public void DrawString(string text, float x, float y, FontWeight fw, SharpDX.DirectWrite.FontStyle fs, SharpDX.Color fontColor, float fontHeight, bool isUnderlined) { if ((y > -2) && (y <= (int)(tbHeight * gv.screenDensity) - fontHeight)) { //hurgh21 if (gv.mod.useMinimalisticUI) { gv.DrawText(text, x + (int)(tbXloc * gv.screenDensity) + 2 * gv.pS, y, fw, fs, 1.0f, fontColor, isUnderlined); } else { gv.DrawText(text, x + (int)(tbXloc * gv.screenDensity) + gv.pS, y, fw, fs, 1.0f, fontColor, isUnderlined); } } }
public static void Convert( FontStyle value, out sw.FontStyle fontStyle, out sw.FontWeight fontWeight) { fontStyle = sw.FontStyle.Normal; fontWeight = sw.FontWeight.Normal; if ((value & FontStyle.Italic) == FontStyle.Italic) { fontStyle = sw.FontStyle.Italic; } if ((value & FontStyle.Bold) == FontStyle.Bold) { fontWeight = sw.FontWeight.Bold; } }
/// <summary> /// Get font face for specified codepoint, weight and style. /// </summary> /// <param name="codePoint">The code point.</param> /// <param name="weight">The font weight.</param> /// <param name="style">The font style.</param> /// <returns>Selected font face.</returns> public DWrite.FontFace GetFontFace(int codePoint, DWrite.FontWeight weight, DWrite.FontStyle style) { foreach (var f in this.fontFamilies) { var face = f.GetFontFace(codePoint, weight, style); if (face != null) { return(face); } } using (var source = new SingleCharTextSource(this.factory, codePoint)) { this.systemFontFallback.MapCharacters( source, 0, 1, this.fontCollection, this.baseFontFamilyName, weight, style, DWrite.FontStretch.Normal, out var mappedLength, out var font, out var scale); if (font != null) { FontFamilyCacheItem.TryCreate(this.fontCollection, font.FontFamily.FamilyNames.GetString(0), out var familyCache); this.fontFamilies.Add(familyCache); font.Dispose(); var fontFace = familyCache.GetFontFace(codePoint, weight, style); if (fontFace != null) { return(fontFace); } } } // OK we don't have the glyph for this codepoint in the fallbacks. // Use primary font to show something like '?'. return(this.fontFamilies[0].GetFontFace(weight, style)); }
public static FontStyle Convert(sw.FontStyle fontStyle, sw.FontWeight fontWeight) { var style = FontStyle.Normal; if (fontStyle == sw.FontStyle.Italic) { style |= FontStyle.Italic; } if (fontStyle == sw.FontStyle.Oblique) { style |= FontStyle.Italic; } if (fontWeight == sw.FontWeight.Bold) { style |= FontStyle.Bold; } return(style); }
/// <summary> /// Creates a text format object from GDI font and StringFormat. /// Text format is used for text layout with normal weight, style /// and stretch.</summary> /// <param name="font">System.Drawing.Font used for creating TexFormat.</param> /// <returns>A new instance of D2dTextFormat</returns> public static D2dTextFormat CreateTextFormat(System.Drawing.Font font) { float fontsize = (font.SizeInPoints * 96.0f) / 72.0f; FontWeight weight = FontWeight.Regular; if ((font.Style & GdiFontStyle.Bold) == GdiFontStyle.Bold) { weight = FontWeight.Bold; } FontStyle style = FontStyle.Normal; if ((font.Style & GdiFontStyle.Italic) == GdiFontStyle.Italic) { style = FontStyle.Italic; } TextFormat txtFormat = new TextFormat(s_dwFactory, font.FontFamily.Name, weight, style, fontsize); return(new D2dTextFormat(txtFormat)); // (font.SizeInPoints * 96.0f) / 72.0f ) convert font size from Point unit to DIP unit. // Relation between pixel, dip ,dpi, logical inch, and point // DIP is (device independent pixel). // DPI is (dot per inch) in Windows is (pixel per logical inch). // 1 point = 1/72 of logical inch. // 1 DIP = 1/96 of logical inch. // a logical inch can larger or smaller than physical inch. // The length of a logical inch depends on pixel density of output device (screen). // 1 logical inch == 1 physical inch if and only if 72 pixels == 1 physical inch. }
public static SharpDX.DirectWrite.Font GetDxFont(string fontFamily, SharpDX.DirectWrite.FontWeight weight, SharpDX.DirectWrite.FontStretch stretch, SharpDX.DirectWrite.FontStyle style) { //Windows.UI.Xaml.Media.FontFamily fontFamily = textBlock.FontFamily; if (fontFamily == null) { fontFamily = LabeLRenderer._defaultTextBlock.FontFamily.Source; } var fontKey = fontFamily + "-" + weight + "-" + stretch + "-" + style; if (_loadedFonts.TryGetValue(fontKey, out SharpDX.DirectWrite.Font font)) { return(font); } using (var factory = new Factory()) { if (fontFamily.StartsWith("ms-appdata:///")) { var fontFamilyFilePath = fontFamily.Substring(14); string dir = null; if (fontFamilyFilePath.StartsWith("local/")) { dir = ApplicationData.Current.LocalFolder.Path; fontFamilyFilePath = fontFamilyFilePath.Substring(6); } else if (fontFamilyFilePath.StartsWith("localcache/")) { dir = ApplicationData.Current.LocalCacheFolder.Path; fontFamilyFilePath = fontFamilyFilePath.Substring(11); } else if (fontFamilyFilePath.StartsWith("roaming/")) { dir = ApplicationData.Current.RoamingFolder.Path; fontFamilyFilePath = fontFamilyFilePath.Substring(8); } else if (fontFamilyFilePath.StartsWith("temp/")) { dir = ApplicationData.Current.TemporaryFolder.Path; fontFamilyFilePath = fontFamilyFilePath.Substring(5); } else { System.Console.WriteLine("Unknown StorageFolder for " + fontFamily); return(null); } var path = System.IO.Path.Combine(dir, fontFamilyFilePath.Split('#')[0]); path = path.Replace('/', '\\'); var fontFile = new SharpDX.DirectWrite.FontFile(factory, path); var loader = fontFile.Loader; var key = fontFile.GetReferenceKey(); using (var fontCollectionLoader = new FontCollectionLoader(fontFile)) { factory.RegisterFontCollectionLoader(fontCollectionLoader); using (var fontCollection = new FontCollection(factory, fontCollectionLoader, key)) { var family = fontCollection.GetFontFamily(0); var familyNames = family.FamilyNames; font = family.GetFirstMatchingFont(weight, stretch, style); _loadedFonts[fontKey] = font; } } return(font); } using (var fontCollection = factory.GetSystemFontCollection(false)) { var familyCount = fontCollection.FontFamilyCount; for (int i = 0; i < familyCount; i++) { try { using (var dxFontFamily = fontCollection.GetFontFamily(i)) { var familyNames = dxFontFamily.FamilyNames; if (!familyNames.FindLocaleName(System.Globalization.CultureInfo.CurrentCulture.Name, out int index)) { familyNames.FindLocaleName("en-us", out index); } var name = familyNames.GetString(index); var display = name; using (var dxFont = dxFontFamily.GetFont(index)) { if (dxFont.IsSymbolFont) { display = "Segoe UI"; } } //fontList.Add(new InstalledFont { Name = name, DisplayFont = display }); if ((fontFamily == name || fontFamily == display) && dxFontFamily.GetFirstMatchingFont(weight, stretch, style) is SharpDX.DirectWrite.Font systemFont) { _loadedFonts[fontKey] = systemFont; return(systemFont); } } } #pragma warning disable CC0004 // Catch block cannot be empty catch { } // Corrupted font files throw an exception - ignore them #pragma warning restore CC0004 // Catch block cannot be empty } } } return(null); }
private (DWrite.Font font, DWrite.FontFace fontFace) GetOrAdd(DWrite.FontWeight weight, DWrite.FontStyle style) { if (!this.fontCache.TryGetValue((weight, style), out var v)) { var font = this.fontFamily.GetFirstMatchingFont(weight, DWrite.FontStretch.Normal, style); var fontFace = new DWrite.FontFace(font); v = (font, fontFace); this.fontCache.Add((weight, style), v); } return(v); }
/// <summary> /// Get font face from primary font family using specified weight and style. /// </summary> /// <param name="weight">Font weight.</param> /// <param name="style">Font style.</param> /// <returns>Selected font face.</returns> public DWrite.FontFace GetPrimaryFontFace(DWrite.FontWeight weight, DWrite.FontStyle style) { return(this.fontFamilies[0].GetFontFace(weight, style)); }
public static void DrawTextLayout(this MusicCanvasControl canvas, string text, RawVector2 pos, string fontFamilyName, float fontSize, Color color, TextAlignment textAlignment, ParagraphAlignment paragraphAlignment, SharpDX.DirectWrite.FontStyle fontStyle = 0, FontWeight fontWeight = 700) { Trimming trimming; InlineObject obj2; RenderTarget renderTarget = canvas.RenderTarget2D; SharpDX.DirectWrite.Factory factoryDWrite = canvas.FactoryDWrite; TextFormat textFormat = new TextFormat(factoryDWrite, fontFamilyName, fontWeight, fontStyle, FontStretch.Normal, fontSize); textFormat.GetTrimming(out trimming, out obj2); trimming.Granularity = TrimmingGranularity.Word; textFormat.SetTrimming(trimming, obj2); textFormat.TextAlignment = TextAlignment.Center; textFormat.ParagraphAlignment = ParagraphAlignment.Center; TextLayout layout = new TextLayout(factoryDWrite, text, textFormat, 1920f, 1080f); textFormat.TextAlignment = textAlignment; textFormat.ParagraphAlignment = paragraphAlignment; TextLayout textLayout = new TextLayout(factoryDWrite, text, textFormat, layout.Metrics.Width, layout.Metrics.Height); layout.Dispose(); textFormat.Dispose(); switch (textAlignment) { case TextAlignment.Trailing: pos.X -= textLayout.Metrics.Width; break; case TextAlignment.Center: case TextAlignment.Justified: pos.X -= textLayout.Metrics.Width / 2f; break; } switch (paragraphAlignment) { case ParagraphAlignment.Far: pos.Y -= textLayout.Metrics.Height; break; case ParagraphAlignment.Center: pos.Y -= textLayout.Metrics.Height / 2f; break; } SolidColorBrush defaultForegroundBrush = new SolidColorBrush(renderTarget, color.ToRawColor4(1f)); renderTarget.DrawTextLayout(pos, textLayout, defaultForegroundBrush, DrawTextOptions.Clip); defaultForegroundBrush.Dispose(); textLayout.Dispose(); }
public DWrite.FontFace GetFontFace(int codePoint, DWrite.FontWeight weight, DWrite.FontStyle style) { var(font, fontFace) = this.GetOrAdd(weight, style); if (font.HasCharacter(codePoint)) { return(fontFace); } return(null); }
public static void DrawTextLayout(this RenderTarget renderTarget, SharpDX.DirectWrite.Factory writeFactory, string text, RawVector2 pos, string fontFamilyName, float fontSize, Color color, TextAlignment textAlignment, SharpDX.DirectWrite.FontStyle fontStyle = 0) { renderTarget.DrawTextLayout(writeFactory, text, pos, fontFamilyName, fontSize, color, textAlignment, ParagraphAlignment.Near, fontStyle, FontWeight.Bold); }
public const int MiniumeWidth = 40; //これ以上ないと誤操作が起こる public void InitTextFormat(string fontName, float fontSize, DW.FontWeight fontWeigth = DW.FontWeight.Normal, DW.FontStyle fontStyle = DW.FontStyle.Normal) { if (this.format != null) { this.format.Dispose(); } float dpix, dpiy; this.GetDpi(out dpix, out dpiy); float fontSizeInDIP = fontSize * (dpix / 72.0f); this.format = this._factory.GetTextFormat(fontName, fontSizeInDIP, fontWeigth, fontStyle); this.format.WordWrapping = DW.WordWrapping.NoWrap; this.format.ReadingDirection = GetDWRightDirect(_RightToLeft); MyTextLayout layout = this._factory.GetTextLayout("0", this.format, float.MaxValue, float.MaxValue, dpix, false); layout.RightToLeft = false; this.emSize = new Size(layout.Width, layout.Height); layout.Dispose(); this.TabWidthChar = this.TabWidthChar; this.hasCache = false; layout = this._factory.GetTextLayout("+", this.format, float.MaxValue, float.MaxValue, dpix, false); layout.RightToLeft = false; #if METRO this.FoldingWidth = Math.Max(D2DRenderCommon.MiniumeWidth, layout.Width); #else this.FoldingWidth = layout.Width; #endif layout.Dispose(); this._factory.Clear(); this.OnChangedRenderResource(this, new ChangedRenderRsourceEventArgs(ResourceType.Font)); }
public DW.TextFormat GetTextFormat(string fontName, double fontSize, DW.FontWeight fontWeigth = DW.FontWeight.Normal, DW.FontStyle fontStyle = DW.FontStyle.Normal) { return(new DW.TextFormat(this._DWFactory, fontName, fontWeigth, fontStyle, (float)fontSize)); }
public static void DrawTextLayout(this MusicCanvasControl canvas, string text, RawVector2 pos, string fontFamilyName, float fontSize, Color color, TextAlignment textAlignment, SharpDX.DirectWrite.FontStyle fontStyle = 0) { canvas.DrawTextLayout(text, pos, fontFamilyName, fontSize, color, textAlignment, ParagraphAlignment.Near, fontStyle, FontWeight.Bold); }
public DWrite.FontFace GetFontFace(DWrite.FontWeight weight, DWrite.FontStyle style) { return(this.GetOrAdd(weight, style).fontFace); }