forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FixedSizeFont.cs
65 lines (57 loc) · 1.99 KB
/
FixedSizeFont.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
// 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.Max7219
{
/// <summary>
/// Implementation of a <see cref="IFont"/> that uses a common array for all characters.
/// The number of bytes per character is constant and zero values between the characters are trimmed.
/// </summary>
public class FixedSizeFont : IFont
{
private readonly byte[] _data;
private readonly int _bytesPerCharacter;
private readonly byte[] _space;
/// <summary>
/// Constructs FixedSizeFont instance
/// </summary>
/// <param name="bytesPerCharacter">number of bytes per character</param>
/// <param name="data">Font data</param>
/// <param name="spaceWidth">Space width</param>
public FixedSizeFont(int bytesPerCharacter, byte[] data, int spaceWidth = 3)
{
_data = data;
_bytesPerCharacter = bytesPerCharacter;
_space = new byte[spaceWidth];
}
/// <summary>
/// Get character information
/// </summary>
public ListByte this[char chr]
{
get
{
int start = chr * _bytesPerCharacter;
int end = start + _bytesPerCharacter;
if (end > _data.Length)
{
return new ListByte(_space); // character is not defined
}
if (chr == ' ')
{
return new ListByte(_space);
}
// trim the font
while (start < end && _data[start] == 0)
{
start++;
}
while (end > start && _data[end - 1] == 0)
{
end--;
}
return new ListByte(new SpanByte(_data, start, end - start).ToArray());
}
}
}
}