public TextElementState(TextElementState existing, TextElement element, RenderContext context) { props = element.Props; format = existing?.format ?? new SharpDX.DirectWrite.TextFormat(Renderer.AssertRendererType(context.Renderer).fontFactory, "Times New Roman", 18); layout = new SharpDX.DirectWrite.TextLayout(Renderer.AssertRendererType(context.Renderer).fontFactory, element.Props.Text, format, context.Bounds.Width, context.Bounds.Height); textBrush = existing?.textBrush ?? new SharpDX.Direct2D1.SolidColorBrush(Renderer.AssertRendererType(context.Renderer).d2dTarget, new RawColor4(1, 1, 1, 1)); BoundingBox = new Bounds(x: context.Bounds.X, y: context.Bounds.Y, width: (int)layout.Metrics.Width, height: (int)layout.Metrics.Height); context.Disposables.Add(format); context.Disposables.Add(layout); context.Disposables.Add(textBrush); var childStates = new List <IElementState>(); foreach (var child in element.Children) { var locations = layout.HitTestTextRange(child.Location.StartIndex, child.Location.EndIndex - child.Location.StartIndex, BoundingBox.X, BoundingBox.Y); if (locations.Length == 0) { continue; } var location = locations[0]; childStates.Add(child.Child.Update(null, context.WithBounds(new Bounds(x: (int)location.Left, y: (int)location.Top, width: (int)location.Width, height: (int)location.Height)))); } children = childStates; }
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(); } }