/// <summary> /// Computes drawing commands to layout a block of text within /// certain constraints /// </summary> /// <returns></returns> public static TextRendererResult ComputeText(string text, TextRendererOptions options, UIElement target) { var TextStyle = options.TextStyle; var _Scale = options.Scale; var txtScale = TextStyle.Scale * _Scale; var m_LineHeight = TextStyle.MeasureString("W").Y - (2 * txtScale.Y); var spaceWidth = TextStyle.MeasureString(" ").X; var words = text.Split(' ').ToList(); var newWordsArray = TextRenderer.ExtractLineBreaks(words); var m_Lines = new List<UITextEditLine>(); TextRenderer.CalculateLines(m_Lines, newWordsArray, TextStyle, options.MaxWidth, spaceWidth, options.TopLeftIconSpace, m_LineHeight); var topLeft = options.Position; var position = topLeft; var result = new TextRendererResult(); var drawCommands = new List<ITextDrawCmd>(); result.DrawingCommands = drawCommands; var yPosition = topLeft.Y; var numLinesAdded = 0; for (var i = 0; i < m_Lines.Count; i++) { var lineOffset = (i*m_LineHeight < options.TopLeftIconSpace.Y) ? options.TopLeftIconSpace.X : 0; var line = m_Lines[i]; var xPosition = topLeft.X+lineOffset; /** Alignment **/ if (options.Alignment == TextAlignment.Center) { xPosition += (int)Math.Round(((options.MaxWidth-lineOffset) - line.LineWidth) / 2); } var segmentPosition = target.LocalPoint(new Vector2(xPosition, yPosition)); drawCommands.Add(new TextDrawCmd_Text { Selected = false, Text = line.Text, Style = TextStyle, Position = segmentPosition, Scale = txtScale }); numLinesAdded++; yPosition += m_LineHeight; position.Y += m_LineHeight; } result.BoundingBox = new Rectangle((int)topLeft.X, (int)topLeft.Y, (int)options.MaxWidth, (int)yPosition); foreach (var cmd in drawCommands) { cmd.Init(); } return result; }
/// <summary> /// Computes drawing commands to layout a block of text within /// certain constraints /// </summary> /// <returns></returns> public static TextRendererResult ComputeText(string text, TextRendererOptions options, UIElement target) { var TextStyle = options.TextStyle; var _Scale = options.Scale; var txtScale = TextStyle.Scale * _Scale; var m_LineHeight = TextStyle.MeasureString("W").Y - (2 * txtScale.Y); var spaceWidth = TextStyle.MeasureString(" ").X; var words = text.Split(' ').ToList(); var newWordsArray = TextRenderer.ExtractLineBreaks(words); var m_Lines = new List <UITextEditLine>(); TextRenderer.CalculateLines(m_Lines, newWordsArray, TextStyle, options.MaxWidth, spaceWidth); var topLeft = options.Position; var position = topLeft; var result = new TextRendererResult(); var drawCommands = new List <ITextDrawCmd>(); result.DrawingCommands = drawCommands; var yPosition = topLeft.Y; var numLinesAdded = 0; for (var i = 0; i < m_Lines.Count; i++) { var line = m_Lines[i]; var xPosition = topLeft.X; /** Alignment **/ if (options.Alignment == TextAlignment.Center) { xPosition += (int)Math.Round((options.MaxWidth - line.LineWidth) / 2); } var segmentPosition = target.LocalPoint(new Vector2(xPosition, yPosition)); drawCommands.Add(new TextDrawCmd_Text { Selected = false, Text = line.Text, Style = TextStyle, Position = segmentPosition, Scale = txtScale }); numLinesAdded++; yPosition += m_LineHeight; position.Y += m_LineHeight; } result.BoundingBox = new Rectangle((int)topLeft.X, (int)topLeft.Y, (int)options.MaxWidth, (int)yPosition); foreach (var cmd in drawCommands) { cmd.Init(); } return(result); }