-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathGlyphTranslatorToGdiPath.cs
106 lines (93 loc) · 3.12 KB
/
GlyphTranslatorToGdiPath.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//Apache2, 2017-present, WinterDev
//Apache2, 2014-2016, Samuel Carlsson, WinterDev
using System.Drawing;
using System.Drawing.Drawing2D;
using Typography.OpenFont;
namespace SampleWinForms
{
//------------------
//this is Gdi+ version ***
//render with System.Drawing.Drawing2D.GraphicsPath
//------------------
/// <summary>
/// read result as Gdi+ GraphicsPath
/// </summary>
public class GlyphTranslatorToGdiPath : IGlyphTranslator
{
//this gdi+ version
GraphicsPath ps;
float lastMoveX;
float lastMoveY;
float lastX;
float lastY;
bool contour_is_closed = true;
public GlyphTranslatorToGdiPath()
{
}
public void BeginRead(int countourCount)
{
ps = new GraphicsPath();
ps.Reset();
}
public void EndRead()
{
}
public void MoveTo(float x0, float y0)
{
if (!contour_is_closed)
{
CloseContour();
}
lastX = lastMoveX = (float)x0;
lastY = lastMoveY = (float)y0;
}
public void CloseContour()
{
contour_is_closed = true;
ps.CloseFigure();
lastX = lastMoveX;
lastY = lastMoveY;
}
public void Curve3(float x1, float y1, float x2, float y2)
{
//from http://stackoverflow.com/questions/9485788/convert-quadratic-curve-to-cubic-curve
//Control1X = StartX + (.66 * (ControlX - StartX))
//Control2X = EndX + (.66 * (ControlX - EndX))
contour_is_closed = false;
float c1x = lastX + (float)((2f / 3f) * (x1 - lastX));
float c1y = lastY + (float)((2f / 3f) * (y1 - lastY));
//---------------------------------------------------------------------
float c2x = (float)(x2 + ((2f / 3f) * (x1 - x2)));
float c2y = (float)(y2 + ((2f / 3f) * (y1 - y2)));
//---------------------------------------------------------------------
ps.AddBezier(
new PointF(lastX, lastY),
new PointF(c1x, c1y),
new PointF(c2x, c2y),
new PointF(lastX = (float)x2, lastY = (float)y2));
}
public void Curve4(float x1, float y1, float x2, float y2, float x3, float y3)
{
contour_is_closed = false;
ps.AddBezier(
new PointF(lastX, lastY),
new PointF((float)x1, (float)y1),
new PointF((float)x2, (float)y2),
new PointF(lastX = (float)x3, lastY = (float)y3));
}
public void LineTo(float x1, float y1)
{
contour_is_closed = false;
ps.AddLine(
new PointF(lastX, lastY),
new PointF(lastX = (float)x1, lastY = (float)y1));
}
public void Reset()
{
ps = null;
lastMoveX = lastMoveY = lastX = lastY = 0;
contour_is_closed = true;
}
public GraphicsPath ResultGraphicsPath => this.ps;
}
}