Gorgon
Show / Hide Table of Contents

Class GorgonTextFormat

Functionality for formatting text using a GorgonFont.

Inheritance
object
GorgonTextFormat
Inherited Members
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
Namespace: Gorgon.Graphics.Fonts
Assembly: Gorgon.Graphics.Fonts.dll
Syntax
public static class GorgonTextFormat

Methods

| Edit this page View Source

FormatStringForRendering(string, int)

Function to format a string for rendering using a GorgonFont.

Declaration
public static string FormatStringForRendering(this string renderText, int tabSpacing = 4)
Parameters
Type Name Description
string renderText

The text to render.

int tabSpacing

[Optional] The number of spaces used to replace a tab control character.

Returns
Type Description
string

The formatted text.

Remarks

This method will format the string so that all control characters such as carriage return, and tabs are converted into spaces.

If the tabSpacing parameter is changed from its default of 4, then that will be the number of spaces substituted for the tab control character.

| Edit this page View Source

MeasureLine(string, GorgonFont, bool, float)

Function to measure a single line of text using a Gorgon font.

Declaration
public static Size2F MeasureLine(this string text, GorgonFont font, bool useOutline = false, float lineSpacing = 1)
Parameters
Type Name Description
string text

The single line of text to measure.

GorgonFont font

The font to use.

bool useOutline

[Optional] true to include the outline in the measurement, false to exclude.

float lineSpacing

[Optional] The factor used to determine the amount of space between each line.

Returns
Type Description
Size2F

A vector containing the width and height of the text line when rendered using this font.

Remarks

This will measure the specified text and return the size, in pixels, of the region containing the text. Unlike the MeasureText(string, GorgonFont, bool, int, float, float?) method, this method does not format the text or take into account newline/carriage returns. It is meant for a single line of text only.

If the useOutline parameter is true, then the outline size is taken into account when measuring, otherwise only the standard glyph size is taken into account. If the font HasOutline property is false, then this parameter is ignored.

The lineSpacing parameter adjusts the amount of space between each line by multiplying it with the FontHeight value (and the OutlineSize * 2 if useOutline is true and HasOutline is true). For example, to achieve a double spacing effect, change this value to 2.0f.

Exceptions
Type Condition
ArgumentNullException

Thrown when the font parameter is null.

GorgonException

Thrown if the font does not have a glyph for its default character.

See Also
MeasureText(string, GorgonFont, bool, int, float, float?)
| Edit this page View Source

MeasureText(string, GorgonFont, bool, int, float, float?)

Function to measure the specified text using a Gorgon font.

Declaration
public static Size2F MeasureText(this string text, GorgonFont font, bool useOutline = false, int tabSpaceCount = 4, float lineSpacing = 1, float? wordWrapWidth = null)
Parameters
Type Name Description
string text

The text to measure.

GorgonFont font

The font to use.

bool useOutline

[Optional] true to include the outline in the measurement, false to exclude.

int tabSpaceCount

[Optional] The number of spaces represented by a tab control character.

float lineSpacing

[Optional] The factor used to determine the amount of space between each line.

float? wordWrapWidth

[Optional] The maximum width to return if word wrapping is required.

Returns
Type Description
Size2F

A vector containing the width and height of the text when rendered using this font.

Remarks

This will measure the specified text and return the size, in pixels, of the region containing the text.

If the wordWrapWidth is specified and greater than zero, then word wrapping is assumed to be on and the text will be handled using word wrapping.

If the useOutline parameter is true, then the outline size is taken into account when measuring, otherwise only the standard glyph size is taken into account. If the font HasOutline property is false, then this parameter is ignored.

The lineSpacing parameter adjusts the amount of space between each line by multiplying it with the FontHeight value (and the OutlineSize * 2 if useOutline is true and HasOutline is true). For example, to achieve a double spacing effect, change this value to 2.0f.

If measuring a single line of text with no breaks (i.e. newline or carriage return), and no word wrapping, then call the MeasureLine(string, GorgonFont, bool, float) method instead for better performance.

Exceptions
Type Condition
ArgumentNullException

Thrown when the font parameter is null.

GorgonException

Thrown if the font does not have a glyph for its default character.

See Also
MeasureLine(string, GorgonFont, bool, float)
| Edit this page View Source

WordWrap(string, GorgonFont, float)

Function to perform word wrapping on a string based on a Gorgon font.

Declaration
public static string WordWrap(this string text, GorgonFont font, float wordWrapWidth)
Parameters
Type Name Description
string text

The text to word wrap.

GorgonFont font

The font to use.

float wordWrapWidth

The maximum width, in pixels, that must be met for word wrapping to occur.

Returns
Type Description
string

The string with word wrapping.

Remarks

The wordWrapWidth is the maximum number of pixels required for word wrapping, if an individual font glyph cell width (the Offset + Advance) exceeds that of the wordWrapWidth, then the parameter value is updated to glyph cell width.

Exceptions
Type Condition
ArgumentNullException

Thrown when the font parameter is null.

GorgonException

Thrown if the font does not have a glyph for its default character.

  • Edit this page
  • View Source
In this article
Back to top Copyright 2023 - Licensed under the MIT license by Michael Winsor (Tape_Worm).
Send comments on this topic to the author