TextMetrics Class

The measurements of a text (https://html.spec.whatwg.org/multipage/canvas.html#textmetrics), returned by measureText(): the distances are in CSS px, relative to the alignment point and to the baseline of the text attributes of the context.

public sealed class TextMetrics

Properties

NameDescription
ActualBoundingBoxAscentGets the distance from the baseline to the top of the bounding box of the glyphs (positive upwards).
ActualBoundingBoxDescentGets the distance from the baseline to the bottom of the bounding box of the glyphs (positive downwards).
ActualBoundingBoxLeftGets the distance from the alignment point to the left of the bounding box of the glyphs (positive to the left).
ActualBoundingBoxRightGets the distance from the alignment point to the right of the bounding box of the glyphs (positive to the right).
AlphabeticBaselineGets the distance from the baseline to the alphabetic baseline (positive when the baseline is below it).
EmHeightAscentGets the distance from the baseline to the top of the em square (positive when the baseline is below it).
EmHeightDescentGets the distance from the baseline to the bottom of the em square (positive when the baseline is above it).
FontBoundingBoxAscentGets the distance from the baseline to the ascent of the first available font (positive upwards).
FontBoundingBoxDescentGets the distance from the baseline to the descent of the first available font (positive downwards).
HangingBaselineGets the distance from the baseline to the hanging baseline (positive when the baseline is below it).
IdeographicBaselineGets the distance from the baseline to the ideographic-under baseline (positive when the baseline is below it).
WidthGets the advance width of the text.

Gets the distance from the baseline to the top of the bounding box of the glyphs (positive upwards).

public double ActualBoundingBoxAscent { get; }

ActualBoundingBoxDescent

CanvasRenderingContext2DBase.Text.cs

Gets the distance from the baseline to the bottom of the bounding box of the glyphs (positive downwards).

public double ActualBoundingBoxDescent { get; }

Gets the distance from the alignment point to the left of the bounding box of the glyphs (positive to the left).

public double ActualBoundingBoxLeft { get; }

Gets the distance from the alignment point to the right of the bounding box of the glyphs (positive to the right).

public double ActualBoundingBoxRight { get; }

Gets the distance from the baseline to the alphabetic baseline (positive when the baseline is below it).

public double AlphabeticBaseline { get; }

Gets the distance from the baseline to the top of the em square (positive when the baseline is below it).

public double EmHeightAscent { get; }

Gets the distance from the baseline to the bottom of the em square (positive when the baseline is above it).

public double EmHeightDescent { get; }

Gets the distance from the baseline to the ascent of the first available font (positive upwards).

public double FontBoundingBoxAscent { get; }

Gets the distance from the baseline to the descent of the first available font (positive downwards).

public double FontBoundingBoxDescent { get; }

Gets the distance from the baseline to the hanging baseline (positive when the baseline is below it).

public double HangingBaseline { get; }

Gets the distance from the baseline to the ideographic-under baseline (positive when the baseline is below it).

public double IdeographicBaseline { get; }

Gets the advance width of the text.

public double Width { get; }

Methods

NameDescription
GetActualBoundingBox(uint, uint)Returns the bounding box of the glyphs of a range of the text (https://github.com/whatwg/html/pull/11000), relative to the alignment point and to the baseline: the box that the actualBoundingBox metrics give for the text where the other characters are not drawn.
GetIndexFromOffset(double)Returns the index (in UTF-16 code units) of the caret position closest to a horizontal offset from the alignment point (https://github.com/whatwg/html/pull/11000): the position before the character under the offset in the reading direction when the offset is on its first half, else the position after it.
GetSelectionRects(uint, uint)Returns the rectangles of the selection of a range of the text (https://github.com/whatwg/html/pull/11000), relative to the alignment point and to the baseline: one per bidi run of the range, of the height of the font bounding box.
GetTextClusters(TextClusterOptions?)Returns the clusters of the text (https://github.com/whatwg/html/pull/11000), in logical order: its grapheme clusters (merged when a glyph spans several of them), each drawn by CanvasRenderingContext2DBase.FillTextCluster at the position that draws the text as fillText().
GetTextClusters(uint, uint, TextClusterOptions?)Returns the clusters of the text that have characters in a range (https://github.com/whatwg/html/pull/11000): see TextMetrics.GetTextClusters.

GetActualBoundingBox(uint, uint)

CanvasRenderingContext2DBase.TextClusters.cs
  • Experimental (WEBPLATEXP0024)

Returns the bounding box of the glyphs of a range of the text (https://github.com/whatwg/html/pull/11000), relative to the alignment point and to the baseline: the box that the actualBoundingBox metrics give for the text where the other characters are not drawn.

[Experimental("WEBPLATEXP0024", UrlFormat = "https://github.com/meziantou/Meziantou.Framework/blob/main/src/Meziantou.Framework.WebPlatform/docs/experimental-apis.md#{0}")]
public DomRectReadOnly GetActualBoundingBox(uint start, uint end)

Parameters

start uint
The index (in UTF-16 code units) of the start of the range.
end uint
The index (in UTF-16 code units) of the end of the range.

Returns

DomRectReadOnly

Exceptions

DomException
An IndexSizeError when the start is not the index of a character, or the end is after the end of the text.

See also

  • Experimental (WEBPLATEXP0024)

Returns the index (in UTF-16 code units) of the caret position closest to a horizontal offset from the alignment point (https://github.com/whatwg/html/pull/11000): the position before the character under the offset in the reading direction when the offset is on its first half, else the position after it.

[Experimental("WEBPLATEXP0024", UrlFormat = "https://github.com/meziantou/Meziantou.Framework/blob/main/src/Meziantou.Framework.WebPlatform/docs/experimental-apis.md#{0}")]
public uint GetIndexFromOffset(double offset)

Parameters

offset double
The offset from the alignment point, in CSS px.

Returns

uint

See also

  • Experimental (WEBPLATEXP0024)

Returns the rectangles of the selection of a range of the text (https://github.com/whatwg/html/pull/11000), relative to the alignment point and to the baseline: one per bidi run of the range, of the height of the font bounding box.

[Experimental("WEBPLATEXP0024", UrlFormat = "https://github.com/meziantou/Meziantou.Framework/blob/main/src/Meziantou.Framework.WebPlatform/docs/experimental-apis.md#{0}")]
public IReadOnlyList<DomRectReadOnly> GetSelectionRects(uint start, uint end)

Parameters

start uint
The index (in UTF-16 code units) of the start of the range.
end uint
The index (in UTF-16 code units) of the end of the range: a range that ends at or before its start is a rectangle without width at the caret position of the end.

Returns

IReadOnlyList<DomRectReadOnly>

Exceptions

DomException
An IndexSizeError when an index is after the end of the text.

See also

GetTextClusters overloads

GetTextClusters(TextClusterOptions?)

CanvasRenderingContext2DBase.TextClusters.cs
  • Experimental (WEBPLATEXP0024)

Returns the clusters of the text (https://github.com/whatwg/html/pull/11000), in logical order: its grapheme clusters (merged when a glyph spans several of them), each drawn by CanvasRenderingContext2DBase.FillTextCluster at the position that draws the text as fillText().

[Experimental("WEBPLATEXP0024", UrlFormat = "https://github.com/meziantou/Meziantou.Framework/blob/main/src/Meziantou.Framework.WebPlatform/docs/experimental-apis.md#{0}")]
public IReadOnlyList<TextCluster> GetTextClusters(TextClusterOptions? options = null)
Parameters
options TextClusterOptions?
The alignment and the baseline of the positions of the clusters (by default, the ones of the context when the text was measured).
Returns

IReadOnlyList<TextCluster>

See also

GetTextClusters(uint, uint, TextClusterOptions?)

CanvasRenderingContext2DBase.TextClusters.cs
  • Experimental (WEBPLATEXP0024)

Returns the clusters of the text that have characters in a range (https://github.com/whatwg/html/pull/11000): see TextMetrics.GetTextClusters.

[Experimental("WEBPLATEXP0024", UrlFormat = "https://github.com/meziantou/Meziantou.Framework/blob/main/src/Meziantou.Framework.WebPlatform/docs/experimental-apis.md#{0}")]
public IReadOnlyList<TextCluster> GetTextClusters(uint start, uint end, TextClusterOptions? options = null)
Parameters
start uint
The index (in UTF-16 code units) of the start of the range.
end uint
The index (in UTF-16 code units) of the end of the range.
options TextClusterOptions?
The alignment and the baseline of the positions of the clusters (by default, the ones of the context when the text was measured).
Returns

IReadOnlyList<TextCluster>

Exceptions
DomException
An IndexSizeError when the start is not the index of a character, or the end is after the end of the text.
See also