mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
4.2 KiB
127 lines
4.2 KiB
// <copyright file="GlyphBuilder.cs" company="James Jackson-South">
|
|
// Copyright (c) James Jackson-South and contributors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
// </copyright>
|
|
|
|
namespace ImageSharp.Drawing
|
|
{
|
|
using System.Collections.Generic;
|
|
using System.Numerics;
|
|
|
|
using SixLabors.Fonts;
|
|
using SixLabors.Shapes;
|
|
|
|
/// <summary>
|
|
/// rendering surface that Fonts can use to generate Shapes.
|
|
/// </summary>
|
|
internal class GlyphBuilder : IGlyphRenderer
|
|
{
|
|
private readonly PathBuilder builder = new PathBuilder();
|
|
private readonly List<IPath> paths = new List<IPath>();
|
|
private Vector2 currentPoint = default(Vector2);
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="GlyphBuilder"/> class.
|
|
/// </summary>
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="GlyphBuilder"/> class.
|
|
/// </summary>
|
|
/// <param name="origin">The origin.</param>
|
|
public GlyphBuilder(Vector2 origin)
|
|
{
|
|
this.builder = new PathBuilder();
|
|
this.builder.SetOrigin(origin);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the paths that have been rendered by this.
|
|
/// </summary>
|
|
public IEnumerable<IPath> Paths => this.paths;
|
|
|
|
/// <summary>
|
|
/// Begins the glyph.
|
|
/// </summary>
|
|
/// <param name="location">The offset that the glyph will be rendered at.</param>
|
|
void IGlyphRenderer.BeginGlyph(Vector2 location)
|
|
{
|
|
this.builder.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Begins the figure.
|
|
/// </summary>
|
|
void IGlyphRenderer.BeginFigure()
|
|
{
|
|
this.builder.StartFigure();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Draws a cubic bezier from the current point to the <paramref name="point"/>
|
|
/// </summary>
|
|
/// <param name="secondControlPoint">The second control point.</param>
|
|
/// <param name="thirdControlPoint">The third control point.</param>
|
|
/// <param name="point">The point.</param>
|
|
void IGlyphRenderer.CubicBezierTo(Vector2 secondControlPoint, Vector2 thirdControlPoint, Vector2 point)
|
|
{
|
|
this.builder.AddBezier(this.currentPoint, secondControlPoint, thirdControlPoint, point);
|
|
this.currentPoint = point;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ends the glyph.
|
|
/// </summary>
|
|
void IGlyphRenderer.EndGlyph()
|
|
{
|
|
this.paths.Add(this.builder.Build());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ends the figure.
|
|
/// </summary>
|
|
void IGlyphRenderer.EndFigure()
|
|
{
|
|
this.builder.CloseFigure();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Draws a line from the current point to the <paramref name="point"/>.
|
|
/// </summary>
|
|
/// <param name="point">The point.</param>
|
|
void IGlyphRenderer.LineTo(Vector2 point)
|
|
{
|
|
this.builder.AddLine(this.currentPoint, point);
|
|
this.currentPoint = point;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Moves to current point to the supplied vector.
|
|
/// </summary>
|
|
/// <param name="point">The point.</param>
|
|
void IGlyphRenderer.MoveTo(Vector2 point)
|
|
{
|
|
this.builder.StartFigure();
|
|
this.currentPoint = point;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Draws a quadratics bezier from the current point to the <paramref name="point"/>
|
|
/// </summary>
|
|
/// <param name="secondControlPoint">The second control point.</param>
|
|
/// <param name="point">The point.</param>
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|