// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.Fonts.Rendering;
using SixLabors.Fonts.Tables.AdvancedTypographic.Variations;
namespace SixLabors.Fonts.Tables.Cff {
///
/// Represents the data for a single CFF glyph, including the raw charstring program
/// and subroutine references needed for evaluation and rendering.
///
internal struct CffGlyphData
{
private readonly byte[][] globalSubrBuffers;
private readonly byte[][] localSubrBuffers;
private readonly byte[] charStrings;
private readonly int nominalWidthX;
private readonly int version;
private readonly ItemVariationStore? itemVariationStore;
private readonly int vsIndex;
///
/// Initializes a new instance of the struct.
///
/// The glyph index (GID).
/// The global subroutine buffers.
/// The local subroutine buffers.
/// The nominal width bias for charstring width values.
/// The raw charstring byte data for this glyph.
/// The CFF version (1 or 2).
/// The optional item variation store for CFF2 blend operations.
/// The variation store index for blend operations.
public CffGlyphData(
ushort glyphIndex,
byte[][] globalSubrBuffers,
byte[][] localSubrBuffers,
int nominalWidthX,
byte[] charStrings,
int version,
ItemVariationStore? itemVariationStore = null,
int vsIndex = 0)
{
this.GlyphIndex = glyphIndex;
this.globalSubrBuffers = globalSubrBuffers;
this.localSubrBuffers = localSubrBuffers;
this.nominalWidthX = nominalWidthX;
this.charStrings = charStrings;
this.version = version;
this.itemVariationStore = itemVariationStore;
this.vsIndex = vsIndex;
this.GlyphName = null;
// Variations tables are only present for CFF2 format.
this.FVar = null;
this.AVar = null;
this.GVar = null;
}
///
/// Gets the glyph index (GID) within the font.
///
public ushort GlyphIndex { get; }
///
/// Gets or sets the glyph name from the charset data.
///
public string? GlyphName { get; set; }
///
/// Gets or sets the font variations table for CFF2 variable fonts.
///
public FVarTable? FVar { get; set; }
///
/// Gets or sets the axis variations table for CFF2 variable fonts.
///
public AVarTable? AVar { get; set; }
///
/// Gets or sets the glyph variations table for TrueType-style glyph variations.
///
public GVarTable? GVar { get; set; }
///
/// Gets or sets the FontMatrix that transforms charstring coordinates to design units.
///
public double[]? FontMatrix { get; set; }
///
/// Computes the bounding box of this glyph by evaluating the charstring program.
///
/// The of the glyph.
public readonly Bounds GetBounds()
{
using CffEvaluationEngine engine = new(
this.charStrings,
this.globalSubrBuffers,
this.localSubrBuffers,
this.nominalWidthX,
this.version,
this.itemVariationStore,
this.FVar,
this.AVar,
this.vsIndex);
return engine.GetBounds();
}
///
/// Renders this glyph to the specified renderer by evaluating the charstring program.
///
/// The glyph renderer to output path operations to.
/// The origin point for rendering.
/// The scale factor to apply.
/// The offset to apply.
/// The transformation matrix to apply.
public readonly void RenderTo(IGlyphRenderer renderer, Vector2 origin, Vector2 scale, Vector2 offset, Matrix3x2 transform)
{
using CffEvaluationEngine engine = new(
this.charStrings,
this.globalSubrBuffers,
this.localSubrBuffers,
this.nominalWidthX,
this.version,
this.itemVariationStore,
this.FVar,
this.AVar,
this.vsIndex);
engine.RenderTo(renderer, origin, scale, offset, transform);
}
}
}