-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathTextAlignmentSample.cs
More file actions
91 lines (77 loc) · 2.6 KB
/
TextAlignmentSample.cs
File metadata and controls
91 lines (77 loc) · 2.6 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.Fonts;
using SixLabors.Fonts.Rendering;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
namespace DrawWithImageSharp;
public static class TextAlignmentSample
{
public static void Generate(Font font)
{
using Image<Rgba32> img = new(1000, 1000);
img.Mutate(x => x.Fill(Color.White));
foreach (VerticalAlignment v in Enum.GetValues(typeof(VerticalAlignment)))
{
foreach (HorizontalAlignment h in Enum.GetValues(typeof(HorizontalAlignment)))
{
Draw(img, font, v, h);
}
}
img.Save("Output/Alignment.png");
}
public static void Draw(Image<Rgba32> img, Font font, VerticalAlignment vert, HorizontalAlignment horiz)
{
Vector2 location = Vector2.Zero;
switch (vert)
{
case VerticalAlignment.Top:
location.Y = 0;
break;
case VerticalAlignment.Center:
location.Y = img.Height / 2F;
break;
case VerticalAlignment.Bottom:
location.Y = img.Height;
break;
default:
break;
}
switch (horiz)
{
case HorizontalAlignment.Left:
location.X = 0;
break;
case HorizontalAlignment.Right:
location.X = img.Width;
break;
case HorizontalAlignment.Center:
location.X = img.Width / 2F;
break;
default:
break;
}
CustomGlyphBuilder glyphBuilder = new();
TextRenderer renderer = new(glyphBuilder);
TextOptions textOptions = new(font)
{
TabWidth = 4,
WrappingLength = 0,
HorizontalAlignment = horiz,
VerticalAlignment = vert,
Origin = location
};
string text = $"{horiz} x y z\n{vert} x y z";
renderer.RenderText(text, textOptions);
IEnumerable<IPath> shapesToDraw = glyphBuilder.Paths;
img.Mutate(x => x.Fill(Color.Black, glyphBuilder.Paths));
Color f = Color.Fuchsia.WithAlpha(.5F);
img.Mutate(x => x.Fill(Color.Black, glyphBuilder.Paths));
img.Mutate(x => x.Draw(f, 1, glyphBuilder.Boxes));
img.Mutate(x => x.Draw(Color.Lime, 1, glyphBuilder.TextBox));
}
}