-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathImageSharpTextDraw.cs
More file actions
111 lines (96 loc) · 4.03 KB
/
Copy pathImageSharpTextDraw.cs
File metadata and controls
111 lines (96 loc) · 4.03 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using AGS.API;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System;
using System.Diagnostics;
namespace AGS.Engine
{
public class ImageSharpTextDraw : IBitmapTextDraw
{
private ITextConfig _config;
private Image<Rgba32> _bitmap;
private int _maxWidth, _height;
private string _text;
private static SolidBrush _transparentBrush = Brushes.Solid(SixLabors.ImageSharp.Color.Transparent);
private readonly TextGraphicsOptions _noWrapOptions = new TextGraphicsOptions();
private class DummyDisposable : IDisposable
{
public void Dispose() {}
}
DummyDisposable _dummy = new DummyDisposable();
public ImageSharpTextDraw(Image<Rgba32> bitmap)
{
_bitmap = bitmap;
}
public IDisposable CreateContext()
{
_bitmap.Clear(_transparentBrush);
return _dummy;
}
public void DrawText(string text, ITextConfig config, API.SizeF textSize, API.SizeF baseSize,
int maxWidth, int height, float xOffset)
{
_height = height;
_text = text;
_config = config;
_maxWidth = maxWidth;
IFont font = _config.Font;
_bitmap.Mutate(gfx =>
{
var outlineBrush = _config.OutlineBrush;
float left = xOffset + _config.AlignX(textSize.Width, baseSize);
float top = _config.AlignY(_bitmap.Height, textSize.Height, baseSize);
float centerX = left + _config.OutlineWidth / 2f;
float centerY = top + _config.OutlineWidth / 2f;
float right = left + _config.OutlineWidth;
float bottom = top + _config.OutlineWidth;
if (_config.OutlineWidth > 0f)
{
drawString(gfx, outlineBrush, left, top);
drawString(gfx, outlineBrush, centerX, top);
drawString(gfx, outlineBrush, right, top);
drawString(gfx, outlineBrush, left, centerY);
drawString(gfx, outlineBrush, right, centerY);
drawString(gfx, outlineBrush, left, bottom);
drawString(gfx, outlineBrush, centerX, bottom);
drawString(gfx, outlineBrush, right, bottom);
}
if (_config.ShadowBrush != null)
{
drawString(gfx, _config.ShadowBrush, centerX + _config.ShadowOffsetX,
centerY + _config.ShadowOffsetY);
}
drawString(gfx, _config.Brush, centerX, centerY);
});
}
private void drawString(IImageProcessingContext context, API.IBrush ibrush, float x, float y)
{
var brush = getBrush(ibrush);
if (brush == null)
return;
SixLabors.Fonts.Font font = getFont(_config.Font);
if (_maxWidth == int.MaxValue)
{
context.DrawText(_noWrapOptions, _text, font, brush, new SixLabors.ImageSharp.PointF(x, y));
}
else
{
//todo: draw in rectangle (not constrained by height)
var options = new TextGraphicsOptions { TextOptions = new TextOptions { WrapTextWidth = _maxWidth } };
context.DrawText(options, _text, font, brush, new SixLabors.ImageSharp.PointF(x, y));
//gfx.DrawString(_text, font, brush, new System.Drawing.RectangleF(x, y, _maxWidth, _height),
// _wrapFormat);
}
}
private SixLabors.ImageSharp.Drawing.Processing.IBrush getBrush(API.IBrush brush)
{
return ((ImageSharpBrush)brush).InnerBrush; //todo: build brush based on BrushType
}
private SixLabors.Fonts.Font getFont(IFont font)
{
return ((ImageSharpFont)font).InnerFont;
}
}
}