-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathFontHelper.cs
207 lines (191 loc) · 6.82 KB
/
FontHelper.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
namespace Iot.Device.Display
{
/// <summary>
/// Provides segment mappings for hexadecimal digits and certain ASCII characters
/// </summary>
public static class FontHelper
{
#region Private members
/// <summary>
/// Hexadecimal digit (0..F) fonts
/// </summary>
private static readonly Font[] s_hexDigits =
{
Font.Digit_0,
Font.Digit_1,
Font.Digit_2,
Font.Digit_3,
Font.Digit_4,
Font.Digit_5,
Font.Digit_6,
Font.Digit_7,
Font.Digit_8,
Font.Digit_9,
Font.Digit_A,
Font.Digit_B,
Font.Digit_C,
Font.Digit_D,
Font.Digit_E,
Font.Digit_F
};
/// <summary>
/// Upper case letter fonts
/// </summary>
private static readonly Font[] s_upperCaseLetters =
{
Font.Letter_A,
Font.Letter_B,
Font.Letter_C,
Font.Letter_D,
Font.Letter_E,
Font.Letter_F,
Font.Letter_G,
Font.Letter_H,
Font.Letter_I,
Font.Letter_J,
Font.Whitespace, // letter K is not supported
Font.Letter_L,
Font.Whitespace, // letter M is not supported
Font.Letter_N,
Font.Letter_O,
Font.Letter_P,
Font.Whitespace, // letter Q is not supported
Font.Letter_R,
Font.Letter_S,
Font.Whitespace, // letter T is not supported
Font.Letter_U,
Font.Whitespace, // letter V is not supported
Font.Whitespace, // letter W is not supported
Font.Whitespace, // letter X is not supported
Font.Letter_Y,
Font.Letter_Z
};
/// <summary>
/// Lower case letter fonts
/// </summary>
private static readonly Font[] s_lowerCaseLetters =
{
Font.Letter_a,
Font.Letter_b,
Font.Letter_c,
Font.Letter_d,
Font.Letter_e,
Font.Letter_f,
Font.Letter_g,
Font.Letter_h,
Font.Letter_i,
Font.Letter_j,
Font.Whitespace, // letter k is not supported
Font.Letter_l,
Font.Whitespace, // letter m is not supported
Font.Letter_n,
Font.Letter_o,
Font.Letter_p,
Font.Whitespace, // letter q is not supported
Font.Letter_r,
Font.Letter_s,
Font.Letter_t,
Font.Letter_u,
Font.Whitespace, // letter v is not supported
Font.Whitespace, // letter w is not supported
Font.Whitespace, // letter x is not supported
Font.Letter_y,
Font.Letter_z
};
#endregion
#region Public members
#region Constants
/// <summary>
/// Used to mask upper 4 bits of a byte for a single hexadecimal value
/// </summary>
public const byte HexadecimalMask = 0b0000_1111;
#endregion
/// <summary>
/// Convert byte value hexadecimal digit (0..F) to corresponding font bits
/// </summary>
/// <param name="digit">hexadecimal digit (0..F)</param>
/// <returns>corresponding font</returns>
public static Font GetHexDigit(byte digit) => s_hexDigits[digit & HexadecimalMask];
/// <summary>
/// Converts a span of bytes to their corresponding hexadecimal digits' font representation
/// </summary>
/// <param name="digits">list of hexadecimal digits (will be converted in place!)</param>
public static void ConvertHexDigits(Span<byte> digits)
{
for (int i = 0, l = digits.Length; i < l; i++)
{
digits[i] = (byte)GetHexDigit(digits[i]);
}
}
/// <summary>
/// Converts a span of bytes to their corresponding hexadecimal digits' font representation
/// </summary>
/// <param name="digits">list of hexadecimal digits (will be converted in place!)</param>
/// <returns>list of corresponding digit fonts</returns>
public static Font[] GetHexDigits(ReadOnlySpan<byte> digits)
{
var fonts = new Font[digits.Length];
if (digits.Length > 0)
{
for (int i = 0, l = fonts.Length; i < l; i++)
{
fonts[i] = GetHexDigit(digits[i]);
}
}
return fonts;
}
/// <summary>
/// Convert character value to corresponding font segments
/// </summary>
/// <param name="value">input character</param>
/// <returns>corresponding font bits</returns>
public static Font GetCharacter(char value) => value switch
{
_ when value >= 'A' && value <= 'Z' => s_upperCaseLetters[value - 'A'],
_ when value >= 'a' && value <= 'z' => s_lowerCaseLetters[value - 'a'],
_ when value >= '0' && value <= '9' => s_hexDigits[value - '0'],
'-' => Font.Symbol_Minus,
'=' => Font.Symbol_Equals,
'_' => Font.Symbol_Underscore,
'|' => Font.Symbol_Pipe,
'°' => Font.Symbol_Degree,
'[' => Font.Symbol_LeftSquareBracket,
']' => Font.Symbol_RightSquareBracket,
_ => Font.Whitespace
};
/// <summary>
/// Convert a string of characters to corresponding fonts
/// </summary>
/// <param name="input">input string</param>
/// <param name="output">list of corresponding character fonts</param>
public static void ConvertString(ReadOnlySpan<char> input, Span<Font> output)
{
if (input.Length != output.Length)
{
throw new InvalidOperationException($"{nameof(input)} and {nameof(output)} length must be the same");
}
for (int i = 0, l = input.Length; i < l; i++)
{
output[i] = GetCharacter(input[i]);
}
}
/// <summary>
/// Convert a string of characters to corresponding font segments
/// </summary>
/// <param name="input">input string</param>
/// <returns>list of corresponding character fonts</returns>
public static Font[] GetString(string input)
{
var fonts = new Font[input.Length];
if (fonts.Length > 0)
{
ConvertString(input.AsSpan(), fonts.AsSpan());
}
return fonts;
}
#endregion
}
}