public override void Draw(TCanvas canvas, TextAlignment alignment = TextAlignment.Center, Thickness padding = default, float offsetX = 0, float offsetY = 0) { var c = WrapCanvas(canvas); UpdateDisplay(float.NaN); DrawCore(c, Display, Display == null ? new PointF?() : IPainterExtensions.GetDisplayPosition(Display.Width, Display.Ascent, Display.Descent, FontSize, c.Width, c.Height, alignment, padding, offsetX, offsetY)); }
private void DrawCore(TCanvas canvas, float?width, TextAlignment alignment, Thickness padding, float offsetX, float offsetY) { var c = WrapCanvas(canvas); UpdateDisplay(width ?? c.Width); if (ErrorMessage == null) { _relativeXCoordDisplay.Position = _relativeXCoordDisplay.Position.Plus(IPainterExtensions.GetDisplayPosition( System.Math.Max(_relativeXCoordDisplay.Width, _absoluteXCoordDisplay.Width), System.Math.Max(_relativeXCoordDisplay.Ascent, _absoluteXCoordDisplay.Ascent), System.Math.Max(_relativeXCoordDisplay.Descent, _absoluteXCoordDisplay.Descent), FontSize, width ?? c.Width, c.Height, alignment, padding, offsetX, offsetY )); //offsetY is already included in _relativeXCoordDisplay.Position, //no need to add it again below _absoluteXCoordDisplay.Position = new PointF(_absoluteXCoordDisplay.Position.X + offsetX, _absoluteXCoordDisplay.Position.Y + _relativeXCoordDisplay.Position.Y); Display = new ListDisplay <Fonts, Glyph>(new[] { _relativeXCoordDisplay, _absoluteXCoordDisplay }); } DrawCore(c, Display); }
private void DrawCore(TCanvas canvas, float?width, TextAlignment alignment, Thickness padding, float offsetX, float offsetY) { var c = WrapCanvas(canvas); UpdateDisplay(width ?? c.Width); if (ErrorMessage == null) { _relativeXCoordDisplay.Position = _relativeXCoordDisplay.Position.Plus(IPainterExtensions.GetDisplayPosition( System.Math.Max(_relativeXCoordDisplay.Width, _absoluteXCoordDisplay.Width), System.Math.Max(_relativeXCoordDisplay.Ascent, _absoluteXCoordDisplay.Ascent), System.Math.Max(_relativeXCoordDisplay.Descent, _absoluteXCoordDisplay.Descent), FontSize, width ?? c.Width, c.Height, alignment, padding, offsetX, offsetY )); var adjustedCanvasWidth = float.IsInfinity(c.Width) || float.IsNaN(c.Width) ? System.Math.Max(_relativeXCoordDisplay.Displays.CollectionWidth(), _absoluteXCoordDisplay.Displays.IsNonEmpty() ? _absoluteXCoordDisplay.Displays.Max(d => d.Width) : 0) : c.Width; // https://github.com/verybadcat/CSharpMath/issues/123 // Take into account padding, offset etc. on both sides adjustedCanvasWidth -= _relativeXCoordDisplay.Position.X * 2; float Δx = 0; var y = float.NegativeInfinity; var leftRightFlags = alignment & (TextAlignment.Left | TextAlignment.Right); if (leftRightFlags != TextAlignment.Left) { foreach (var relDisplay in _relativeXCoordDisplay.Displays.Reverse()) { if (relDisplay.Position.Y > y) { y = relDisplay.Position.Y; var rightSpace = adjustedCanvasWidth - (relDisplay.Position.X + relDisplay.Width); Δx = leftRightFlags switch { TextAlignment.Center => rightSpace / 2, TextAlignment.Right => rightSpace, _ => throw new InvalidCodePathException("The left flag has been set. This foreach loop should have been skipped.") }; } relDisplay.Position = new PointF(relDisplay.Position.X + Δx, y); } } //offsetY is already included in _relativeXCoordDisplay.Position, //no need to add it again below _absoluteXCoordDisplay.Position = new PointF(_absoluteXCoordDisplay.Position.X + offsetX, _absoluteXCoordDisplay.Position.Y + _relativeXCoordDisplay.Position.Y); Display = new ListDisplay <Fonts, Glyph>(new[] { _relativeXCoordDisplay, _absoluteXCoordDisplay }); } DrawCore(c, Display); }