-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helpers.cs
320 lines (281 loc) · 12.8 KB
/
Helpers.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using SkiaSharp;
namespace ThumbnailCreator
{
public static class Helpers
{
#region Config helpers
/// <summary>
/// Get a property from the config, or log it was required
/// </summary>
/// <param name="config">The config to query/pull from</param>
/// <param name="desc">Description of what to specify (on error)</param>
/// <param name="value">Value from config</param>
/// <param name="key">Key for property to retrieve</param>
/// <returns>If the key was present</returns>
public static bool Required(this Config config, string desc, out string value, params string[] key)
{
if (config.TryGet(out value, key) && !string.IsNullOrWhiteSpace(value))
return true;
Console.WriteLine("Must specify {0} via the \"{1}\" key", desc, string.Join('/', key));
return false;
}
#endregion
#region Drawing helpers
#region Actual drawing
/// <summary>
/// Draw lines of text
/// </summary>
/// <param name="canvas">The SKCanvas to draw on</param>
/// <param name="lines">Lines of text to display</param>
/// <param name="xpos">The x coordinate of the text (meaning is dependent on <paramref name="halign"/>)</param>
/// <param name="ypos">The y coordinate of the text (meaning is dependent on <paramref name="valign"/>)</param>
/// <param name="paint">The SKPaint controlling drawing</param>
/// <param name="halign">How to horizontally align the text (default: Left)</param>
/// <param name="valign">How to vertically align the text (default: Top)</param>
/// <param name="allCapsFont">If the font is in all caps; will ignore space below baseline (default: false)</param>
public static void DrawTextExt(this SKCanvas canvas, string[] lines, float xpos,
float ypos, SKPaint paint, HAlign halign = HAlign.Left, VAlign valign = VAlign.Top,
bool allCapsFont = false)
{
// Ignore empty lines
if (lines == null || lines.Length == 0)
return;
// Determine the maximum size
float totalHeight = MeasureTextExt(
paint, lines, out float lineHeight, out float lineGap, allCapsFont
).Height;
// Save the previous alignment
var prevAlign = paint.TextAlign;
try
{
// Handle the alignment
switch (halign)
{
case HAlign.Left:
default:
paint.TextAlign = SKTextAlign.Left;
break;
case HAlign.Center:
paint.TextAlign = SKTextAlign.Center;
break;
case HAlign.Right:
paint.TextAlign = SKTextAlign.Right;
break;
}
float y = ypos - paint.FontMetrics.Top;
switch (valign)
{
case VAlign.Middle:
y -= totalHeight * 0.5f;
break;
case VAlign.Bottom:
y -= totalHeight;
break;
}
// Draw the text
foreach (string line in lines)
{
canvas.DrawText(line, xpos, y, paint);
y += lineHeight + lineGap;
}
}
finally
{
// Restore the original text alignment
paint.TextAlign = prevAlign;
}
}
/// <summary>
/// Draw a string of text (supports new line characters)
/// </summary>
/// <param name="canvas">The SKCanvas to draw on</param>
/// <param name="text">Text string of text to display</param>
/// <param name="xpos">The x coordinate of the text (meaning is dependent on <paramref name="halign"/>)</param>
/// <param name="ypos">The y coordinate of the text (meaning is dependent on <paramref name="valign"/>)</param>
/// <param name="paint">The SKPaint controlling drawing</param>
/// <param name="halign">How to horizontally align the text (default: Left)</param>
/// <param name="valign">How to vertically align the text (default: Top)</param>
/// <param name="allCapsFont">If the font is in all caps; will ignore space below baseline (default: false)</param>
public static void DrawTextExt(this SKCanvas canvas, string text, float xpos,
float ypos, SKPaint paint, HAlign halign = HAlign.Left, VAlign valign = VAlign.Top,
bool allCapsFont = false)
{
// Ignore blank text
if (string.IsNullOrWhiteSpace(text))
return;
// Draw after splitting into lines
DrawTextExt(canvas, GetLines(text), xpos, ypos, paint, halign, valign, allCapsFont);
}
#endregion
#region Helper methods
/// <summary>
/// Measure the full bounds of a text string (supports new line characters)
/// </summary>
/// <param name="paint">The SKPaint controlling drawing</param>
/// <param name="lines">Lines of text to measure</param>
/// <param name="lineHeight">How tall one line of text is</param>
/// <param name="lineGap">The gap between each line of text</param>
/// <param name="allCapsFont">If the font is in all caps; will ignore space below baseline (default: false)</param>
/// <returns>The full bounds of the text string</returns>
private static SKSize MeasureTextExt(this SKPaint paint, string[] lines,
out float lineHeight, out float lineGap, bool allCapsFont = false)
{
lineHeight = (allCapsFont ? 0 : paint.FontMetrics.Bottom) - paint.FontMetrics.Top;
lineGap = paint.FontMetrics.Leading + (allCapsFont ? paint.FontMetrics.Bottom : 0);
if (lines.Length == 0)
return new SKSize(0, 0);
return new SKSize(
lines.Max(line => paint.MeasureText(line)),
lineHeight * lines.Length + lineGap * (lines.Length - 1)
);
}
/// <summary>
/// Measure the full bounds of lines of text
/// </summary>
/// <param name="paint">The SKPaint controlling drawing</param>
/// <param name="lines">Lines of text to measure</param>
/// <param name="allCapsFont">If the font is in all caps; will ignore space below baseline (default: false)</param>
/// <returns>The full bounds of the text string</returns>
public static SKSize MeasureTextExt(this SKPaint paint, string[] lines, bool allCapsFont = false)
{
return MeasureTextExt(paint, lines, out float _, out float _, allCapsFont);
}
/// <summary>
/// Measure the full bounds of a text string (supports new line characters)
/// </summary>
/// <param name="paint">The SKPaint controlling drawing</param>
/// <param name="text">Text string of text to measure</param>
/// <param name="allCapsFont">If the font is in all caps; will ignore space below baseline (default: false)</param>
/// <returns>The full bounds of the text string</returns>
public static SKSize MeasureTextExt(this SKPaint paint, string text, bool allCapsFont = false)
{
return MeasureTextExt(paint, GetLines(text), allCapsFont);
}
#endregion
#endregion
#region Image IO
/// <summary>
/// Save an image to a file
/// </summary>
/// <param name="image">The image to save</param>
/// <param name="fileName">The path to the output file</param>
/// <param name="format">The image format for encoding (default: Png)</param>
/// <param name="quality">Quality level for the image; between 0 and 100 (default: 100)</param>
public static void SaveToFile(this SKImage image, string fileName,
SKEncodedImageFormat format = SKEncodedImageFormat.Png, int quality = 100)
{
// Ensure the output path exists
string fullPath = Path.GetFullPath(fileName);
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
// Actually save to the file
using (var stream = File.Create(fullPath))
using (var data = image.Encode(format, quality))
data.SaveTo(stream);
}
/// <summary>
/// Save a surface to a file
/// </summary>
/// <param name="surface">The surface to save</param>
/// <param name="fileName">The path to the output file</param>
/// <param name="format">The image format for encoding (default: Png)</param>
/// <param name="quality">Quality level for the image; between 0 and 100 (default: 100)</param>
public static void SaveToFile(this SKSurface surface, string fileName,
SKEncodedImageFormat format = SKEncodedImageFormat.Png, int quality = 100)
{
using (var image = surface.Snapshot())
SaveToFile(image, fileName, format, quality);
}
#endregion
#region Operating system
/// <summary>
/// Determine if the operating system is Windows
/// </summary>
/// <returns>If the operating system is Windows</returns>
public static bool IsWindows => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
/// <summary>
/// The supported SKColorType for 32-bit RGBA (OS dependent)
/// </summary>
public static SKColorType RGBA => IsWindows ? SKColorType.Bgra8888 : SKColorType.Rgba8888;
#endregion
#region Path helpers
/// <summary>
/// Change a forward-slash path into an OS-specific path
/// </summary>
/// <param name="path">Forward-slash path for conversion</param>
/// <returns>The operating specific path</returns>
public static string ToOS(this string path)
{
return path.Replace('/', Path.DirectorySeparatorChar);
}
#endregion
#region Resource helpers
/// <summary>
/// Load a font from a file
/// </summary>
/// <param name="fileName">The path to the input file</param>
/// <returns>The loaded font</returns>
public static SKTypeface LoadFont(string fileName)
{
using (var stream = File.OpenRead(fileName))
using (var data = SKData.Create(stream))
return SKTypeface.FromData(data);
}
/// <summary>
/// Load an image from a file
/// </summary>
/// <param name="fileName">The path to the input file</param>
/// <returns>The resulting image</returns>
public static SKImage LoadImage(string fileName)
{
using (var stream = File.OpenRead(fileName))
using (var data = SKData.Create(stream))
return SKImage.FromEncodedData(data);
}
#endregion
#region String helpers
/// <summary>
/// Parse a string either as raw pixel length, or as a percentage
/// </summary>
/// <param name="text">String to parse</param>
/// <param name="ruler">The length to scale against</param>
/// <returns>The distance</returns>
public static float ParseAsDistance(this string text, float ruler)
{
if (text.EndsWith("px"))
return int.Parse(text.Substring(0, text.Length - 2));
return float.Parse(text) * ruler;
}
/// <summary>
/// Remove the BOM if present in a string
/// </summary>
/// <param name="text">Original UTF8 string</param>
/// <returns>Corrected result</returns>
public static string FixBOM(this string text)
{
if (string.IsNullOrEmpty(text))
return text;
byte[] bytes = Encoding.UTF8.GetBytes(text);
if (bytes.Length >= 3 && bytes[0] == 239 && bytes[1] == 187 && bytes[2] == 191)
return text.Substring(3);
return text;
}
/// <summary>
/// Split a string of text into lines
/// </summary>
/// <param name="text">Full text string</param>
/// <returns>An array of lines</returns>
public static string[] GetLines(this string text)
{
if (string.IsNullOrEmpty(text))
return new string[] { };
return text.Replace("\r", "").Split('\n');
}
#endregion
}
}