Struct TextBounds
A box in canvas units with y down, such as the ink of a GlyphRun relative to its origin.
public readonly record struct TextBounds : IEquatable<TextBounds>
- Implements
- Inherited Members
- Extension Methods
Examples
// A label shrunk until it fits a 200-pixel button, then centred on it.
const string label = "Download the full report";
var box = new RectF(20, 20, 200, 40);
var font = new Font(face, 24);
while (font.MeasureText(label).Advance > box.Width - 16 && font.Size > 8)
{
font = new Font(face, font.Size - 1);
}
TextMeasure size = font.MeasureText(label);
var origin = new Vector2(
box.X + ((box.Width - size.Advance) / 2),
box.Y + ((box.Height - size.Height) / 2) + size.Ascent);
using Raster<Rgba8> image = Raster.Create(240, 80, Rgba8.White);
using (RasterCanvas<Rgba8> canvas = image.CreateCanvas())
{
canvas.FillRoundRect(new RoundRect(box, 8), new Rgba8(30, 90, 200, 255));
canvas.DrawText(label, origin, font, Rgba8.White);
}
Constructors
TextBounds(float, float, float, float)
A box in canvas units with y down, such as the ink of a GlyphRun relative to its origin.
public TextBounds(float Left, float Top, float Right, float Bottom)
Parameters
LeftfloatThe left edge.
TopfloatThe top edge.
RightfloatThe right edge.
BottomfloatThe bottom edge.
Examples
// A label shrunk until it fits a 200-pixel button, then centred on it.
const string label = "Download the full report";
var box = new RectF(20, 20, 200, 40);
var font = new Font(face, 24);
while (font.MeasureText(label).Advance > box.Width - 16 && font.Size > 8)
{
font = new Font(face, font.Size - 1);
}
TextMeasure size = font.MeasureText(label);
var origin = new Vector2(
box.X + ((box.Width - size.Advance) / 2),
box.Y + ((box.Height - size.Height) / 2) + size.Ascent);
using Raster<Rgba8> image = Raster.Create(240, 80, Rgba8.White);
using (RasterCanvas<Rgba8> canvas = image.CreateCanvas())
{
canvas.FillRoundRect(new RoundRect(box, 8), new Rgba8(30, 90, 200, 255));
canvas.DrawText(label, origin, font, Rgba8.White);
}
Properties
Bottom
The bottom edge.
public float Bottom { get; init; }
Property Value
Empty
Gets the empty box.
public static TextBounds Empty { get; }
Property Value
Height
Gets the height.
public float Height { get; }
Property Value
IsEmpty
Gets whether the box has no area.
public bool IsEmpty { get; }
Property Value
Left
The left edge.
public float Left { get; init; }
Property Value
Right
The right edge.
public float Right { get; init; }
Property Value
Top
The top edge.
public float Top { get; init; }
Property Value
Width
Gets the width.
public float Width { get; }
Property Value
Methods
Offset(Vector2)
Returns the box moved by an offset.
public TextBounds Offset(Vector2 offset)
Parameters
offsetVector2The offset.
Returns
- TextBounds
The moved box.
Union(TextBounds)
Returns the smallest box holding this box and other; an empty box adds nothing.
public TextBounds Union(TextBounds other)
Parameters
otherTextBoundsThe other box.
Returns
- TextBounds
The union.