-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTextDrawingExtensions.cs
More file actions
72 lines (65 loc) · 2.4 KB
/
Copy pathTextDrawingExtensions.cs
File metadata and controls
72 lines (65 loc) · 2.4 KB
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
using SkiaSharp;
namespace EasyFortniteStats_ImageApi;
public enum VerticalTextAlignment
{
Top,
Center,
Baseline,
Bottom
}
public static class TextDrawingExtensions
{
extension(SKCanvas canvas)
{
public void DrawAlignedText(string text,
SKPoint anchor,
SKPaint paint,
SKTextAlign horizontalAlignment = SKTextAlign.Left,
VerticalTextAlignment verticalAlignment = VerticalTextAlignment.Top)
{
var bounds = new SKRect();
paint.MeasureText(text, ref bounds);
var baseline = verticalAlignment switch
{
VerticalTextAlignment.Top => anchor.Y - bounds.Top,
VerticalTextAlignment.Center => anchor.Y - bounds.MidY,
VerticalTextAlignment.Baseline => anchor.Y,
VerticalTextAlignment.Bottom => anchor.Y - bounds.Bottom,
_ => throw new ArgumentOutOfRangeException(nameof(verticalAlignment), verticalAlignment, null)
};
var previousAlignment = paint.TextAlign;
try
{
paint.TextAlign = horizontalAlignment;
canvas.DrawText(text, anchor.X, baseline, paint);
}
finally
{
paint.TextAlign = previousAlignment;
}
}
public void DrawAlignedText(string text,
SKRect area,
SKPaint paint,
SKTextAlign horizontalAlignment = SKTextAlign.Left,
VerticalTextAlignment verticalAlignment = VerticalTextAlignment.Top)
{
var x = horizontalAlignment switch
{
SKTextAlign.Left => area.Left,
SKTextAlign.Center => area.MidX,
SKTextAlign.Right => area.Right,
_ => throw new ArgumentOutOfRangeException(nameof(horizontalAlignment), horizontalAlignment, null)
};
var y = verticalAlignment switch
{
VerticalTextAlignment.Top => area.Top,
VerticalTextAlignment.Center => area.MidY,
VerticalTextAlignment.Baseline => area.Top,
VerticalTextAlignment.Bottom => area.Bottom,
_ => throw new ArgumentOutOfRangeException(nameof(verticalAlignment), verticalAlignment, null)
};
canvas.DrawAlignedText(text, new SKPoint(x, y), paint, horizontalAlignment, verticalAlignment);
}
}
}