private void HandleDrawString(RenderTarget target, String str, System.Drawing.Rectangle rect, int color, System.Drawing.Font font = null) { if (String.IsNullOrEmpty(str)) { return; } using (Brush brush = GetBrushFromColor(target, color)) { SharpDX.DirectWrite.FontWeight weight = WeightFromFontStyle(font.Style); SharpDX.DirectWrite.FontStyle style = StyleFromFontStyle(font.Style); SharpDX.DirectWrite.TextFormat stringFormat = new SharpDX.DirectWrite.TextFormat(this.FactoryDWrite, font.FontFamily.Name, weight, style, font.Size); stringFormat.TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading; stringFormat.WordWrapping = SharpDX.DirectWrite.WordWrapping.Wrap; if (font == null) { font = this.Font; } int maxsize = this.Width; if (pushedArgument.ContainsKey("TrimOutOfBounds") && ((bool)pushedArgument["TrimOutOfBounds"])) { using (SharpDX.DirectWrite.TextLayout layout = new SharpDX.DirectWrite.TextLayout(FactoryDWrite, str, stringFormat, maxsize, font.Height)) { var lines = layout.GetLineMetrics(); StringBuilder strb = new StringBuilder(); var hittests = layout.HitTestTextRange(0, str.Length, rect.X, rect.Y); int trimPos = -1; for (int i = 0; i < hittests.Length; ++i) { if (hittests[i].Top + hittests[i].Height > rect.Bottom) { trimPos = hittests[i].TextPosition; break; } } String trimmedStr = str; if (trimPos > -1) { trimmedStr = str.Substring(0, trimPos); } target.PushAxisAlignedClip(new RectangleF(rect.X, rect.Y, rect.Right, rect.Bottom), AntialiasMode.Aliased); target.DrawText(trimmedStr, stringFormat, new RawRectangleF(rect.X, rect.Y, rect.Right, rect.Bottom), brush, DrawTextOptions.EnableColorFont | DrawTextOptions.Clip); target.PopAxisAlignedClip(); } } else { target.PushAxisAlignedClip(new RectangleF(rect.X, rect.Y, rect.Right, rect.Bottom), AntialiasMode.Aliased); target.DrawText(str, stringFormat, new RawRectangleF(rect.X, rect.Y, rect.Right, rect.Bottom), brush, DrawTextOptions.EnableColorFont | DrawTextOptions.Clip); target.PopAxisAlignedClip(); } stringFormat.Dispose(); } }
// Method to marshal from native to managed struct internal unsafe void __MarshalFrom(ref __Native @ref) { this.FontFamily = (@ref.FontFamily == IntPtr.Zero)?null:Marshal.PtrToStringUni(@ref.FontFamily); this.FontWeight = @ref.FontWeight; this.FontStyle = @ref.FontStyle; this.FontStretch = @ref.FontStretch; this.Locale = (@ref.Locale == IntPtr.Zero)?null:Marshal.PtrToStringUni(@ref.Locale); }
private SharpDX.DirectWrite.FontWeight WeightFromFontStyle(System.Drawing.FontStyle style) { SharpDX.DirectWrite.FontWeight ret = SharpDX.DirectWrite.FontWeight.Normal; if (style.HasFlag(System.Drawing.FontStyle.Bold)) { ret = SharpDX.DirectWrite.FontWeight.Bold; } return(ret); }
public TextWriter(IDrawing.IDevice device, System.Drawing.Font font)// float FontSize = 10, String FontName = "Arial") { this.device = ((Device)device); this.FontName = font.Name; this.FontSize = font.Size; renderTarget =this.device.renderTarget; SharpDX.DirectWrite.Factory factory = new SharpDX.DirectWrite.Factory(); SharpDX.DirectWrite.FontWeight fontW = SharpDX.DirectWrite.FontWeight.Normal; if(font.Style==System.Drawing.FontStyle.Bold) fontW = SharpDX.DirectWrite.FontWeight.Heavy; textFormat = new SharpDX.DirectWrite.TextFormat(factory, FontName,fontW, SharpDX.DirectWrite.FontStyle.Normal, FontSize); // this.device.ChangeRenderTarget += TextWriter_ChangeRenderTarget; }
/// <summary> /// Creates a text format object used for text layout. /// </summary> /// <param name="factory">an instance of <see cref = "SharpDX.DirectWrite.Factory" /></param> /// <param name="fontFamilyName">An array of characters that contains the name of the font family</param> /// <param name="fontCollection">A pointer to a font collection object. When this is NULL, indicates the system font collection.</param> /// <param name="fontWeight">A value that indicates the font weight for the text object created by this method.</param> /// <param name="fontStyle">A value that indicates the font style for the text object created by this method.</param> /// <param name="fontStretch">A value that indicates the font stretch for the text object created by this method.</param> /// <param name="fontSize">The logical size of the font in DIP ("device-independent pixel") units. A DIP equals 1/96 inch.</param> /// <param name="localeName">An array of characters that contains the locale name.</param> /// <unmanaged>HRESULT CreateTextFormat([In] const wchar* fontFamilyName,[None] IDWriteFontCollection* fontCollection,[None] DWRITE_FONT_WEIGHT fontWeight,[None] DWRITE_FONT_STYLE fontStyle,[None] DWRITE_FONT_STRETCH fontStretch,[None] FLOAT fontSize,[In] const wchar* localeName,[Out] IDWriteTextFormat** textFormat)</unmanaged> public TextFormat(Factory factory, string fontFamilyName, SharpDX.DirectWrite.FontCollection fontCollection, SharpDX.DirectWrite.FontWeight fontWeight, SharpDX.DirectWrite.FontStyle fontStyle, SharpDX.DirectWrite.FontStretch fontStretch, float fontSize, string localeName) : base(IntPtr.Zero) { factory.CreateTextFormat(fontFamilyName, fontCollection, fontWeight, fontStyle, fontStretch, fontSize, localeName, this); }
/// <summary> /// Creates a text format object used for text layout. /// </summary> /// <param name="factory">an instance of <see cref = "SharpDX.DirectWrite.Factory" /></param> /// <param name="fontFamilyName">An array of characters that contains the name of the font family</param> /// <param name="fontCollection">A pointer to a font collection object. When this is NULL, indicates the system font collection.</param> /// <param name="fontWeight">A value that indicates the font weight for the text object created by this method.</param> /// <param name="fontStyle">A value that indicates the font style for the text object created by this method.</param> /// <param name="fontStretch">A value that indicates the font stretch for the text object created by this method.</param> /// <param name="fontSize">The logical size of the font in DIP ("device-independent pixel") units. A DIP equals 1/96 inch.</param> /// <unmanaged>HRESULT CreateTextFormat([In] const wchar* fontFamilyName,[None] IDWriteFontCollection* fontCollection,[None] DWRITE_FONT_WEIGHT fontWeight,[None] DWRITE_FONT_STYLE fontStyle,[None] DWRITE_FONT_STRETCH fontStretch,[None] FLOAT fontSize,[In] const wchar* localeName,[Out] IDWriteTextFormat** textFormat)</unmanaged> public TextFormat(Factory factory, string fontFamilyName, SharpDX.DirectWrite.FontCollection fontCollection, SharpDX.DirectWrite.FontWeight fontWeight, SharpDX.DirectWrite.FontStyle fontStyle, SharpDX.DirectWrite.FontStretch fontStretch, float fontSize) : this(factory, fontFamilyName, fontCollection, fontWeight, fontStyle, fontStretch, fontSize, "") { }
/// <summary> /// Creates a text format object used for text layout with normal stretch. /// </summary> /// <param name="factory">an instance of <see cref = "SharpDX.DirectWrite.Factory" /></param> /// <param name="fontFamilyName">An array of characters that contains the name of the font family</param> /// <param name="fontWeight">A value that indicates the font weight for the text object created by this method.</param> /// <param name="fontStyle">A value that indicates the font style for the text object created by this method.</param> /// <param name="fontSize">The logical size of the font in DIP ("device-independent pixel") units. A DIP equals 1/96 inch.</param> /// <unmanaged>HRESULT CreateTextFormat([In] const wchar* fontFamilyName,[None] IDWriteFontCollection* fontCollection,[None] DWRITE_FONT_WEIGHT fontWeight,[None] DWRITE_FONT_STYLE fontStyle,[None] DWRITE_FONT_STRETCH fontStretch,[None] FLOAT fontSize,[In] const wchar* localeName,[Out] IDWriteTextFormat** textFormat)</unmanaged> public TextFormat(Factory factory, string fontFamilyName, SharpDX.DirectWrite.FontWeight fontWeight, SharpDX.DirectWrite.FontStyle fontStyle, float fontSize) : this(factory, fontFamilyName, null, fontWeight, fontStyle, DirectWrite.FontStretch.Normal, fontSize, "") { }