dotnet / Microsoft.Maui.Graphics

An experimental cross-platform native graphics library.
MIT License
682 stars 63 forks source link

Measured text does not fit in bounding rectangle #433

Open rick-palmsens opened 2 years ago

rick-palmsens commented 2 years ago

Description The following code first measures a piece of text and then tries to draw that text into a bounding box of the same size. But as you can see in the screenshot, the text doesn't fit and overflows, even though its exact size was measured.

Example

public void Draw(ICanvas canvas, RectF dirtyRect)
{
    var font = new Font("Arial");
    var fontSize = 16.0f;

    // Measure text
    var text = "abcdef ABCDEF vwxyz VWXYZ";
    var textSize = canvas.GetStringSize(text, font, fontSize, HorizontalAlignment.Left, VerticalAlignment.Top);
    var textRect = new Rect(new Point(100, 100), textSize);

    // Draw background 
    canvas.FillColor = Colors.LightBlue;
    canvas.FillRectangle(textRect);

    // Draw text
    canvas.Font = font;
    canvas.FontSize = fontSize;;
    canvas.DrawString(text, textRect, HorizontalAlignment.Left, VerticalAlignment.Top, TextFlow.OverflowBounds);
}

Screenshots image

Versions

<ItemGroup>
  <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.3" />
  <PackageReference Include="Microsoft.Graphics.Win2D" Version="1.0.3.1" />
  <PackageReference Include="Microsoft.Maui.Graphics.Win2D.WinUI.Desktop" Version="6.0.300-rc.3.1336" />
</ItemGroup>
swharden commented 2 years ago

Note there's some related discussion in #279