//
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
//
namespace ImageSharp.Drawing
{
using System.Collections.Generic;
using System.Numerics;
using SixLabors.Fonts;
using SixLabors.Shapes;
///
/// rendering surface that Fonts can use to generate Shapes.
///
internal class GlyphBuilder : IGlyphRenderer
{
private readonly PathBuilder builder = new PathBuilder();
private readonly List paths = new List();
private Vector2 currentPoint = default(Vector2);
///
/// Initializes a new instance of the class.
///
public GlyphBuilder()
: this(Vector2.Zero)
{
// glyphs are renderd realative to bottom left so invert the Y axis to allow it to render on top left origin surface
this.builder = new PathBuilder();
}
///
/// Initializes a new instance of the class.
///
/// The origin.
public GlyphBuilder(Vector2 origin)
{
this.builder = new PathBuilder();
this.builder.SetOrigin(origin);
}
///
/// Gets the paths that have been rendered by this.
///
public IEnumerable Paths => this.paths;
///
/// Begins the glyph.
///
/// The offset that the glyph will be rendered at.
void IGlyphRenderer.BeginGlyph(Vector2 location)
{
this.builder.Clear();
}
///
/// Begins the figure.
///
void IGlyphRenderer.BeginFigure()
{
this.builder.StartFigure();
}
///
/// Draws a cubic bezier from the current point to the
///
/// The second control point.
/// The third control point.
/// The point.
void IGlyphRenderer.CubicBezierTo(Vector2 secondControlPoint, Vector2 thirdControlPoint, Vector2 point)
{
this.builder.AddBezier(this.currentPoint, secondControlPoint, thirdControlPoint, point);
this.currentPoint = point;
}
///
/// Ends the glyph.
///
void IGlyphRenderer.EndGlyph()
{
this.paths.Add(this.builder.Build());
}
///
/// Ends the figure.
///
void IGlyphRenderer.EndFigure()
{
this.builder.CloseFigure();
}
///
/// Draws a line from the current point to the .
///
/// The point.
void IGlyphRenderer.LineTo(Vector2 point)
{
this.builder.AddLine(this.currentPoint, point);
this.currentPoint = point;
}
///
/// Moves to current point to the supplied vector.
///
/// The point.
void IGlyphRenderer.MoveTo(Vector2 point)
{
this.builder.StartFigure();
this.currentPoint = point;
}
///
/// Draws a quadratics bezier from the current point to the
///
/// The second control point.
/// The point.
void IGlyphRenderer.QuadraticBezierTo(Vector2 secondControlPoint, Vector2 point)
{
Vector2 c1 = (((secondControlPoint - this.currentPoint) * 2) / 3) + this.currentPoint;
Vector2 c2 = (((secondControlPoint - point) * 2) / 3) + point;
this.builder.AddBezier(this.currentPoint, c1, c2, point);
this.currentPoint = point;
}
}
}