Microsoft.Maui.Graphics icon indicating copy to clipboard operation
Microsoft.Maui.Graphics copied to clipboard

DrawString is drawing outside the text rectangle

Open CBrauer opened this issue 3 years ago • 1 comments

Hello, I have a Maui “DrawText” test program that does not do what I expected. The complete reproducible source code solution is at: CBrauer/MauiTest (github.com) The text is drawn outsize the text bounding rectangle.

My test program produces the following output:

test

The code that produced this output is:

using System.Windows.Forms;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Graphics.Skia;

namespace MauiTest;
public partial class Form1:Form {
  public Form1() {
    InitializeComponent();
  }
  private void skglControl1_PaintSurface(object sender, 
                                         SkiaSharp.Views.Desktop.SKPaintGLSurfaceEventArgs e) {
    ICanvas canvas = new SkiaCanvas() {
      Canvas = e.Surface.Canvas
    };
    var x = 200.0f;
    var y = 200.0f;
    //canvas.FillColor = Colors.White;
    canvas.FillRectangle(0, 0, skglControl1.Width, skglControl1.Height);
    canvas.FontColor = Colors.DarkBlue;
    float fontSize = 48;
    canvas.FontSize = fontSize;
    Font font = new();
    var text = "1.2345";
    SizeF stringSize = canvas.GetStringSize(text, font, fontSize);
    var textWidth = stringSize.Width;
    var textHeight = stringSize.Height;

    PointF stringLocation = new(x, y);
    Rectangle stringRect = new(stringLocation, stringSize);
    canvas.StrokeColor = Colors.Red;
    canvas.DrawRectangle(stringRect);

    canvas.DrawString(value: text,
                  x: x,
                  y: y,
                  width: textWidth,
                  height: textHeight,
                  horizontalAlignment: Microsoft.Maui.Graphics.HorizontalAlignment.Center,
                  verticalAlignment: Microsoft.Maui.Graphics.VerticalAlignment.Top,
                  textFlow: TextFlow.OverflowBounds,
                  lineSpacingAdjustment: 0);
  }
}

Please explain. Charles

CBrauer avatar Mar 04 '22 21:03 CBrauer

I have a Maui “DrawText” test program that does not do what I expected.

Hi Charles,

This topic was discussed in #279 and you may find the solution there helpful (reduce font size by 1pt).

Please explain

Please note that at this time this project is not officially supported and that all the NuGet packages are all marked preview. It is not surprising to me that some features are not yet fully implemented. This library is being actively developed, and text support is likely to improve in the future.

https://github.com/dotnet/Microsoft.Maui.Graphics/blob/957bb2653adef8409ef3dc949f25c737db378ead/README.md#L22-L23

swharden avatar Mar 04 '22 22:03 swharden