// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Collections.Generic;
using System.Numerics;
using SixLabors.Fonts;
using SixLabors.Fonts.Rendering;
namespace SixLabors.ImageSharp.Drawing.Text {
///
/// Builds vector shapes from text using the provided layout and rendering options.
///
public static class TextBuilder
{
///
/// Generates the combined outline paths for all rendered glyphs in .
/// The result merges per-glyph outlines into a single suitable for filling or stroking as one unit.
///
/// The text to shape and render.
/// The text rendering and layout options.
/// The combined for the rendered glyphs.
public static IPathCollection GeneratePaths(string text, TextOptions textOptions)
{
GlyphBuilder glyphBuilder = new();
TextRenderer renderer = new(glyphBuilder);
renderer.RenderText(text, textOptions);
return glyphBuilder.Paths;
}
///
/// Generates per-glyph path data and metadata for the rendered .
/// Each entry contains the combined outline paths for a glyph and associated metadata that enables intelligent fill or stroke decisions at the glyph level.
///
/// The text to shape and render.
/// The text rendering and layout options.
/// A read-only list of entries, one for each rendered glyph.
public static IReadOnlyList GenerateGlyphs(string text, TextOptions textOptions)
{
GlyphBuilder glyphBuilder = new();
TextRenderer renderer = new(glyphBuilder);
renderer.RenderText(text, textOptions);
return glyphBuilder.Glyphs;
}
///
/// Generates the combined outline paths for all rendered glyphs in ,
/// laid out along the supplied baseline.
/// The result merges per-glyph outlines into a single .
///
/// The text to shape and render.
/// The path that defines the text baseline.
/// The text rendering and layout options.
/// The combined for the rendered glyphs.
public static IPathCollection GeneratePaths(string text, IPath path, TextOptions textOptions)
{
(IPath Path, TextOptions TextOptions) transformed = ConfigureOptions(textOptions, path);
PathGlyphBuilder glyphBuilder = new(transformed.Path);
TextRenderer renderer = new(glyphBuilder);
renderer.RenderText(text, transformed.TextOptions);
return glyphBuilder.Paths;
}
///
/// Generates per-glyph path data and metadata for the rendered ,
/// laid out along the supplied baseline.
/// Each entry contains the combined outline paths for a glyph and associated metadata.
///
/// The text to shape and render.
/// The path that defines the text baseline.
/// The text rendering and layout options.
/// A read-only list of entries, one for each rendered glyph.
public static IReadOnlyList GenerateGlyphs(string text, IPath path, TextOptions textOptions)
{
(IPath Path, TextOptions TextOptions) transformed = ConfigureOptions(textOptions, path);
PathGlyphBuilder glyphBuilder = new(transformed.Path);
TextRenderer renderer = new(glyphBuilder);
renderer.RenderText(text, transformed.TextOptions);
return glyphBuilder.Glyphs;
}
private static (IPath Path, TextOptions TextOptions) ConfigureOptions(TextOptions options, IPath path)
{
// When a path is specified we should explicitly follow that path
// and not adjust the origin. Any translation should be applied to the path.
if (options.Origin != Vector2.Zero)
{
TextOptions clone = new(options)
{
Origin = Vector2.Zero
};
return (path.Translate(options.Origin), clone);
}
return (path, options);
}
}
}