Table of Contents

Struct TextBounds

Namespace
Egami.Fonts
Assembly
Egami.Fonts.dll

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

Left float

The left edge.

Top float

The top edge.

Right float

The right edge.

Bottom float

The 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

float

Empty

Gets the empty box.

public static TextBounds Empty { get; }

Property Value

TextBounds

Height

Gets the height.

public float Height { get; }

Property Value

float

IsEmpty

Gets whether the box has no area.

public bool IsEmpty { get; }

Property Value

bool

Left

The left edge.

public float Left { get; init; }

Property Value

float

Right

The right edge.

public float Right { get; init; }

Property Value

float

Top

The top edge.

public float Top { get; init; }

Property Value

float

Width

Gets the width.

public float Width { get; }

Property Value

float

Methods

Offset(Vector2)

Returns the box moved by an offset.

public TextBounds Offset(Vector2 offset)

Parameters

offset Vector2

The 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

other TextBounds

The other box.

Returns

TextBounds

The union.