TextStyle Class

How a text, or a part of a text, is set: its fonts, its size, and what is drawn with it.

public sealed class TextStyle

Remarks

A style is immutable and can be shared by any number of texts and layouts.

Constructors

NameDescription
TextStyle(FontStack, double)Initializes a style.

TextStyle(FontStack, double)

TextStyle.cs

Initializes a style.

public TextStyle(FontStack fonts, double size)

Parameters

fonts FontStack
The fonts of the text; a Font converts to a stack of one font.
size double
The size of the fonts, in pixels per em: a positive number up to 65536.

Exceptions

ArgumentNullException
fonts is null.
ArgumentOutOfRangeException
size is not a positive number up to 65536.

Properties

NameDescription
BackgroundBrushGets the paint behind the text of the style, or null (the default) for none: a highlight, the background of a label.
BrushGets the paint of the text, or null (the default) for the brush given to DrawingCanvas.FillText.
ColorPaletteIndexGets the palette the color glyphs of the text take their colors from: an index in Font.ColorPalettes of the font of each glyph. 0 by default; a font that does not have the palette uses its first one.
DecorationBrushGets the paint of the decorations, or null (the default) for the paint of the text.
DecorationsGets the lines drawn with the text. TextDecorations.None by default.
FeaturesGets the features of the OpenType Layout tables to turn on or off, besides the ones that are applied by default. The last setting of a feature wins. Empty by default.
FontsGets the fonts of the text, by order of preference.
HyphenatorGets the hyphenation patterns of the language of the text, or null (the default). They are used when the layout hyphenates automatically (TextHyphenation.Automatic).
IsCombinedUprightGets a value indicating whether the text of the style is set horizontally in the room of one upright character when it is in a vertical line (tate-chu-yoko): a few digits or letters across a column of Japanese or Chinese text. false by default.
LanguageGets the language of the text, as a BCP 47 language tag ("sr", "tr", "zh-Hant"), or null (the default) when it is not known.
LetterSpacingGets the space added after each character, in pixels; negative to tighten. 0 by default.
OutlineGets the pen that strokes the outlines of the glyphs of the style once the text is filled, or null (the default) for none: a text with a contour.
SizeGets the size of the fonts, in pixels per em.
SynthesisGets what is made from the glyphs of the fonts to imitate a bold or an italic font that the caller does not have. FontSynthesis.None by default.
WordSpacingGets the space added to each space between words, in pixels; negative to tighten. 0 by default.

BackgroundBrush

TextStyle.cs

Gets the paint behind the text of the style, or null (the default) for none: a highlight, the background of a label.

public Brush? BackgroundBrush { get; init; }

Property value

Brush?

Remarks

The background is painted by DrawingCanvas.FillText before the text of the whole layout. On each line, it is one rectangle as wide as the advances of the text of the style, the white space between its words included (not the white space that hangs at the end of a wrapped line), and as tall as the primary font of the style: from its ascent to its descent, whatever the height of the line. It is not part of the outlines of the text: DrawingCanvas.StrokeText and TextLayout.ToPath leave it out.

Gets the paint of the text, or null (the default) for the brush given to DrawingCanvas.FillText.

public Brush? Brush { get; init; }

Property value

Brush?

ColorPaletteIndex

TextStyle.cs

Gets the palette the color glyphs of the text take their colors from: an index in Font.ColorPalettes of the font of each glyph. 0 by default; a font that does not have the palette uses its first one.

public int ColorPaletteIndex { get; init; }

Property value

int

Exceptions

ArgumentOutOfRangeException
The value is negative or greater than 65535.

DecorationBrush

TextStyle.cs

Gets the paint of the decorations, or null (the default) for the paint of the text.

public Brush? DecorationBrush { get; init; }

Property value

Brush?

Decorations

TextStyle.cs

Gets the lines drawn with the text. TextDecorations.None by default.

public TextDecorations Decorations { get; init; }

Property value

TextDecorations

Exceptions

ArgumentOutOfRangeException
The value has an undefined flag.

Features

TextStyle.cs

Gets the features of the OpenType Layout tables to turn on or off, besides the ones that are applied by default. The last setting of a feature wins. Empty by default.

public IReadOnlyList<FontFeature> Features { get; init; }

Property value

IReadOnlyList<FontFeature>

Exceptions

ArgumentNullException
The value is null.

Gets the fonts of the text, by order of preference.

public FontStack Fonts { get; }

Property value

FontStack

Hyphenator

TextStyle.cs

Gets the hyphenation patterns of the language of the text, or null (the default). They are used when the layout hyphenates automatically (TextHyphenation.Automatic).

public Hyphenator? Hyphenator { get; init; }

Property value

Hyphenator?

IsCombinedUpright

TextStyle.cs

Gets a value indicating whether the text of the style is set horizontally in the room of one upright character when it is in a vertical line (tate-chu-yoko): a few digits or letters across a column of Japanese or Chinese text. false by default.

public bool IsCombinedUpright { get; init; }

Property value

bool

Remarks

The text of each span of the style is one unit of its line: it is shaped as a horizontal line of one font (the first font of the stack that has all its characters), centered across the line and on the middle of one em down the line, and it advances by one em. Text that is wider than one em takes the forms its font has for half, a third or a quarter of an em when it is two, three or four characters long (hwid, twid, qwid), and is narrowed to one em if it is still too wide. A line never ends inside it, and the letter spacing, the word spacing and justification do not change it. Its decorations are those of upright text.

It is the all value of the text-combine-upright property of CSS; TextLayoutOptions.CombineUprightDigits combines the short numbers of a text. It changes nothing in a horizontal layout, nor in one whose orientation is TextOrientation.Sideways.

Language

TextStyle.cs

Gets the language of the text, as a BCP 47 language tag ("sr", "tr", "zh-Hant"), or null (the default) when it is not known.

public string? Language { get; init; }

Property value

string?

Exceptions

ArgumentException
The value does not have the form of a BCP 47 language tag.

Remarks

A font can draw the same characters differently for different languages: the Cyrillic letters of Serbian and of Bulgarian, the ideographs of Chinese, Japanese and Korean, the letters around the dotless i of Turkish. It does so with a language system of its OpenType Layout tables for each of these languages. The language of the style selects the language system of each font: the first one the font has among the tags the registry of OpenType gives for the language, or else its default language system, as without a language.

The tag is not checked against the registry of language subtags, and the casing of its subtags does not matter. The language is found from the language subtag, and from the script, region and variant subtags where they select a written form: zh-Hans, zh-Hant, zh-HK, el-polyton, und-fonipa. A language the registry of OpenType has no tag for changes nothing.

With a stack that falls back to the installed fonts (FontStack.WithInstalledFallback), the language also chooses the families its Han ideographs are looked for in: those of Japanese (ja), Korean (ko), Traditional Chinese (zh-Hant, zh-TW), the Chinese of Hong Kong (zh-HK, yue) or Simplified Chinese (zh, zh-Hans, and a text without a language or in another language). The fonts of the stack itself are used in their order, whatever the language. The language changes nothing else: not where lines break.

LetterSpacing

TextStyle.cs

Gets the space added after each character, in pixels; negative to tighten. 0 by default.

public double LetterSpacing { get; init; }

Property value

double

Exceptions

ArgumentOutOfRangeException
The value is not finite, or its magnitude exceeds 65536.

Remarks

The space is added after each cluster: a character with its marks, a ligature. Ligatures the font forms by default are still formed; turn the feature liga off to space their letters.

Gets the pen that strokes the outlines of the glyphs of the style once the text is filled, or null (the default) for none: a text with a contour.

public Pen? Outline { get; init; }

Property value

Pen?

Remarks

The stroke is drawn by DrawingCanvas.FillText after the text of the whole layout is filled, over it. A centered pen covers the edge of the glyphs by half its width; with PenAlignment.Outside, the stroke is around the glyphs and leaves them as they are filled. The glyphs of one pen are stroked as one shape; the decorations, the glyphs a font draws with its own colors and what a synthetic bold adds to a glyph are not stroked. The width of the pen is in pixels, whatever the size of the text.

Gets the size of the fonts, in pixels per em.

public double Size { get; }

Property value

double

Synthesis

TextStyle.cs

Gets what is made from the glyphs of the fonts to imitate a bold or an italic font that the caller does not have. FontSynthesis.None by default.

public FontSynthesis Synthesis { get; init; }

Property value

FontSynthesis

Exceptions

ArgumentOutOfRangeException
The value has an undefined flag.

Remarks

FontSynthesis.Oblique slants every outline to the right, around the baseline; the marks of a letter follow it, the advances do not change and the decorations stay straight.

FontSynthesis.Bold thickens the glyphs of a filled text: each glyph is filled together with a stroke of its outline, 1/24 of the size wide, whose corners are kept sharp unless they are sharper than 60 degrees. Each glyph that takes room takes 1/24 of the size more and is drawn in the middle of it. A stroked text (StrokeText) and the path of a layout (TextLayout.ToPath) have the outlines of the font, which the bold does not change.

The glyphs a font draws with its own colors, such as emoji, are drawn as they are.

WordSpacing

TextStyle.cs

Gets the space added to each space between words, in pixels; negative to tighten. 0 by default.

public double WordSpacing { get; init; }

Property value

double

Exceptions

ArgumentOutOfRangeException
The value is not finite, or its magnitude exceeds 65536.