// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System;
namespace SixLabors.Fonts {
///
/// Encapsulates logic for laying out and then measuring text properties.
///
public static class TextMeasurer
{
///
public static TextMetrics Measure(string text, TextOptions options)
=> Measure(text.AsSpan(), options);
///
/// Measures the full set of layout metrics for the supplied text in a single pass.
///
/// The text.
/// The text options. controls wrapping; use -1 to disable wrapping.
/// A instance containing every measurement for the laid-out text.
public static TextMetrics Measure(ReadOnlySpan text, TextOptions options)
{
TextBlock block = new(text, options);
return block.Measure(options.WrappingLength);
}
///
public static FontRectangle MeasureAdvance(string text, TextOptions options)
=> MeasureAdvance(text.AsSpan(), options);
///
/// Measures the logical advance of the text in pixel units.
///
/// The text.
/// The text options. controls wrapping; use -1 to disable wrapping.
/// The logical advance rectangle of the text if it was to be rendered.
public static FontRectangle MeasureAdvance(ReadOnlySpan text, TextOptions options)
{
if (text.IsEmpty)
{
return FontRectangle.Empty;
}
TextBlock block = new(text, options);
return block.MeasureAdvance(options.WrappingLength);
}
///
public static FontRectangle MeasureBounds(string text, TextOptions options)
=> MeasureBounds(text.AsSpan(), options);
///
public static FontRectangle MeasureRenderableBounds(string text, TextOptions options)
=> MeasureRenderableBounds(text.AsSpan(), options);
///
/// Measures the rendered glyph bounds of the text in pixel units.
///
/// The text.
/// The text options. controls wrapping; use -1 to disable wrapping.
/// The rendered glyph bounds of the text if it was to be rendered.
public static FontRectangle MeasureBounds(ReadOnlySpan text, TextOptions options)
{
if (text.IsEmpty)
{
return FontRectangle.Empty;
}
TextBlock block = new(text, options);
return block.MeasureBounds(options.WrappingLength);
}
///
/// Measures the full renderable bounds of the text in pixel units.
///
/// The text.
/// The text options. controls wrapping; use -1 to disable wrapping.
///
/// The union of the logical advance rectangle and the rendered glyph bounds if the text was to be rendered.
///
public static FontRectangle MeasureRenderableBounds(ReadOnlySpan text, TextOptions options)
{
if (text.IsEmpty)
{
return FontRectangle.Empty;
}
TextBlock block = new(text, options);
return block.MeasureRenderableBounds(options.WrappingLength);
}
///
public static ReadOnlyMemory GetGlyphMetrics(string text, TextOptions options)
=> GetGlyphMetrics(text.AsSpan(), options);
///
/// Gets the positioned metrics of each laid-out glyph entry in pixel units.
///
/// The text.
/// The text options. controls wrapping; use -1 to disable wrapping.
/// A read-only memory region containing the per-glyph metrics entries of the text if it was to be rendered.
public static ReadOnlyMemory GetGlyphMetrics(ReadOnlySpan text, TextOptions options)
{
if (text.IsEmpty)
{
return ReadOnlyMemory.Empty;
}
TextBlock block = new(text, options);
return block.GetGlyphMetrics(options.WrappingLength);
}
///
public static ReadOnlyMemory GetGraphemeMetrics(string text, TextOptions options)
=> GetGraphemeMetrics(text.AsSpan(), options);
///
/// Gets the positioned metrics of each laid-out grapheme in pixel units.
///
/// The text.
/// The text options. controls wrapping; use -1 to disable wrapping.
/// A read-only memory region containing the per-grapheme metrics entries of the text if it was to be rendered.
public static ReadOnlyMemory GetGraphemeMetrics(ReadOnlySpan text, TextOptions options)
{
if (text.IsEmpty)
{
return ReadOnlyMemory.Empty;
}
TextBlock block = new(text, options);
return block.GetGraphemeMetrics(options.WrappingLength);
}
///
public static ReadOnlyMemory GetWordMetrics(string text, TextOptions options)
=> GetWordMetrics(text.AsSpan(), options);
///
/// Gets the positioned metrics of each Unicode word-boundary segment in pixel units.
///
/// The text.
/// The text options. controls wrapping; use -1 to disable wrapping.
/// A read-only memory region containing the per-word-boundary segment metrics entries of the text if it was to be rendered.
public static ReadOnlyMemory GetWordMetrics(ReadOnlySpan text, TextOptions options)
{
if (text.IsEmpty)
{
return ReadOnlyMemory.Empty;
}
TextBlock block = new(text, options);
return block.GetWordMetrics(options.WrappingLength);
}
///
public static int CountLines(string text, TextOptions options)
=> CountLines(text.AsSpan(), options);
///
/// Gets the number of laid-out lines contained within the text.
///
/// The text.
/// The text options. controls wrapping; use -1 to disable wrapping.
/// The laid-out line count.
public static int CountLines(ReadOnlySpan text, TextOptions options)
{
if (text.IsEmpty)
{
return 0;
}
TextBlock block = new(text, options);
return block.CountLines(options.WrappingLength);
}
///
public static ReadOnlyMemory GetLineMetrics(string text, TextOptions options)
=> GetLineMetrics(text.AsSpan(), options);
///
/// Gets per-line layout metrics for the supplied text.
///
/// The text to measure.
/// The text options. controls wrapping; use -1 to disable wrapping.
///
/// A read-only memory region containing in pixel units, one entry per laid-out line.
///
public static ReadOnlyMemory GetLineMetrics(ReadOnlySpan text, TextOptions options)
{
if (text.IsEmpty)
{
return ReadOnlyMemory.Empty;
}
TextBlock block = new(text, options);
return block.GetLineMetrics(options.WrappingLength);
}
}
}