Table of Contents

Class GdiPlusText

Namespace
Egami.Drawing
Assembly
Egami.Drawing.dll

Text placement as GDI+ (System.Drawing) does it, for callers that put text in a box themselves and want it where Graphics.DrawString would put it. Measured from GDI+ on Windows; Egami's own layout (DrawText(string, RectF, ParagraphStyle, VerticalAlign)) is unchanged.

public static class GdiPlusText
Inheritance
GdiPlusText
Inherited Members

Examples

// Centred in a box as DrawString with StringFormat { Alignment = Center, LineAlignment = Center } puts it.
Font label = new Font(face, 16).WithGdiPlusLook();
var box = new RectF(0, 0, 200, 36);
float baseline = GdiPlusText.Baseline(label, box, VerticalAlign.Center);
using (RasterCanvas<Rgb8> canvas = button.CreateCanvas(CanvasOptions.GdiPlus))
{
    float width = label.MeasureText("Start the course").Advance;
    canvas.DrawText("Start the course", new Vector2(MathF.Round((box.Width - width) / 2), baseline), label, Rgba8.White);
}

// The ascent it used: whole pixels for a grid-fitted font.
float ascent = label.GdiPlusAscent;

Methods

Baseline(Font, RectF, VerticalAlign, bool)

Gets the baseline DrawString puts one line of text on in a layout box with a line alignment (StringFormat.LineAlignment): the line is a cell tall (ascent and descent), plus 1/8 em with the default format, placed at the box's top, middle or bottom, and the baseline is GdiPlusAscent below the line's top. For a grid-fitted font (as WithGdiPlusLook() makes) the line's top is first rounded to a whole pixel, as ClearTypeGridFit does, so the baseline is whole; draw with an untransformed canvas, or one moved by whole pixels, to keep it on the pixel grid.

public static float Baseline(Font font, RectF box, VerticalAlign align, bool typographic = false)

Parameters

font Font

The font.

box RectF

The layout box, in canvas units, y down.

align VerticalAlign

Where the line goes in the box: Near (Top), Center or Far (Bottom).

typographic bool

Whether the format is StringFormat.GenericTypographic, which adds no 1/8 em.

Returns

float

The baseline's y.

Examples

// Centred in a box as DrawString with StringFormat { Alignment = Center, LineAlignment = Center } puts it.
Font label = new Font(face, 16).WithGdiPlusLook();
var box = new RectF(0, 0, 200, 36);
float baseline = GdiPlusText.Baseline(label, box, VerticalAlign.Center);
using (RasterCanvas<Rgb8> canvas = button.CreateCanvas(CanvasOptions.GdiPlus))
{
    float width = label.MeasureText("Start the course").Advance;
    canvas.DrawText("Start the course", new Vector2(MathF.Round((box.Width - width) / 2), baseline), label, Rgba8.White);
}

// The ascent it used: whole pixels for a grid-fitted font.
float ascent = label.GdiPlusAscent;

Exceptions

InvalidOperationException

The font is default(Font), which has no typeface.

ArgumentOutOfRangeException

align is not defined.