-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cs
101 lines (93 loc) · 3.4 KB
/
Main.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
using System;
using System.Linq;
using System.Collections.Generic;
using AntigrainSharp;
using SystemTools;
namespace Example
{
class Application : AggWindow
{
public Application(bool flip_y, FontManager fm)
: base(flip_y, fm)
{
double fontSize = 150.0;
Font font = Font.LoadFromFile("Impacted.ttf");
this.FontManager.SetFont(font, fontSize);
this.glyphs = "The godfather".Select(c => font.GetCharIndex(c))
.ToList();
this.glyphsOffset = this.ComputeGlyphsOffset(this.glyphs, font, fontSize);
System.Console.WriteLine(this.glyphsOffset);
}
public override void OnDraw(GraphicContext gctx)
{
// black background
Rasterizer rast = new Rasterizer();
Path rect = new RectanglePath(
0, 0,
gctx.Width, gctx.Height
);
rast.AddPath(rect, curves: false);
gctx.RendererSolid.Color(0, 0, 0, 1);
rast.RenderSolid(gctx.RendererSolid);
// drawing images
var magicImage = new ImageMagick.MagickImage("godfather.png");
var pixels = magicImage.GetPixels().ToByteArray(ImageMagick.PixelMapping.BGRA);
int stride = -magicImage.Width * magicImage.Depth * magicImage.ChannelCount / 8;
gctx.RendererImage.AttachSource(
pixels,
magicImage.BaseWidth,
magicImage.BaseHeight,
stride
);
rast = new Rasterizer();
rect = new RectanglePath(
0, 0,
magicImage.BaseWidth, magicImage.BaseHeight
);
rast.AddPath(rect, curves: false);
rast.RenderImage(gctx.RendererImage);
// drawing text
rast = new Rasterizer();
double a = 9.0 / 41.0;
double b = 40.0 / 41.0;
rast.SetTransform(b, -a, a, b, 150, 50);
foreach (var (glyph, offset) in this.glyphs.Zip(this.glyphsOffset)){
rast.AddGlyph((int)glyph, offset, 0, 1.0, this.FontManager);
}
gctx.RendererSolid.Color(0.98, 0.05, 0.2, 0.8);
rast.RenderSolid(gctx.RendererSolid);
}
private List<double> ComputeGlyphsOffset(List<uint> glyphs, Font font, double size){
List<double> offsets = new();
double x = 0;
uint? prevGlyph = null;
foreach (uint glyph in glyphs){
offsets.Add(x);
x += font.GetGlyphAdvance(glyph, size);
if (prevGlyph != null){
x += font.GetKerning(prevGlyph.Value, glyph, size);
}
prevGlyph = glyph;
}
return offsets;
}
private List<uint> glyphs;
private List<double> glyphsOffset;
}
public static class Program
{
public static int Main(string[] args)
{
System.Console.WriteLine("Create Application");
FontManager fm = new FontManager();
Application app = new Application(true, fm);
app.SetCaption("AggUI example");
if (app.Init(1000, 900, WindowFlags.Resize))
{
System.Console.WriteLine("Run");
return app.Run();
}
return 1;
}
}
}