forked from TaraHoleInIt/tarablessd1306
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssd1306_font.c
executable file
·303 lines (234 loc) · 9.05 KB
/
ssd1306_font.c
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
/**
* Copyright (c) 2017-2018 Tara Keeling
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
#include "ssd1306.h"
#include "ssd1306_draw.h"
#include "ssd1306_font.h"
static int RoundUpFontHeight( const struct SSD1306_FontDef* Font ) {
int Height = Font->Height;
if ( ( Height % 8 ) != 0 ) {
return ( ( Height + 7 ) / 8 ) * 8;
}
return Height;
}
static const uint8_t* GetCharPtr( const struct SSD1306_FontDef* Font, char Character ) {
return &Font->FontData[ ( Character - Font->StartChar ) * ( ( Font->Width * ( RoundUpFontHeight( Font ) / 8 ) ) + 1 ) ];
}
void SSD1306_FontDrawChar( struct SSD1306_Device* DisplayHandle, char Character, int x, int y, int Color ) {
const uint8_t* GlyphData = NULL;
int GlyphColumnLen = 0;
int CharStartX = 0;
int CharStartY = 0;
int CharWidth = 0;
int CharHeight = 0;
int CharEndX = 0;
int CharEndY = 0;
int OffsetX = 0;
int OffsetY = 0;
int YByte = 0;
int YBit = 0;
int i = 0;
NullCheck( DisplayHandle, return );
NullCheck( DisplayHandle->Font, return );
NullCheck( ( GlyphData = GetCharPtr( DisplayHandle->Font, Character ) ), return );
if ( Character >= DisplayHandle->Font->StartChar || Character <= DisplayHandle->Font->EndChar ) {
/* The first byte in the glyph data is the width of the character in pixels, skip over */
GlyphData++;
GlyphColumnLen = RoundUpFontHeight( DisplayHandle->Font ) / 8;
CharWidth = SSD1306_FontGetCharWidth( DisplayHandle, Character );
CharHeight = SSD1306_FontGetHeight( DisplayHandle );
CharStartX = x;
CharStartY = y;
CharEndX = CharStartX + CharWidth;
CharEndY = CharStartY + CharHeight;
/* If the character is partially offscreen offset the end by
* distance between (coord) and 0.
*/
OffsetX = ( CharStartX < 0 ) ? abs( CharStartX ) : 0;
OffsetY = ( CharStartY < 0 ) ? abs( CharStartY ) : 0;
/* This skips into the proper column within the glyph data */
GlyphData+= ( OffsetX * GlyphColumnLen );
CharStartX+= OffsetX;
CharStartY+= OffsetY;
/* Do not attempt to draw if this character is entirely offscreen */
if ( CharEndX < 0 || CharStartX >= DisplayHandle->Width || CharEndY < 0 || CharStartY >= DisplayHandle->Height ) {
ClipDebug( x, y );
return;
}
/* Do not attempt to draw past the end of the screen */
CharEndX = ( CharEndX >= DisplayHandle->Width ) ? DisplayHandle->Width - 1 : CharEndX;
CharEndY = ( CharEndY >= DisplayHandle->Height ) ? DisplayHandle->Height - 1 : CharEndY;
for ( x = CharStartX; x < CharEndX; x++ ) {
for ( y = CharStartY, i = 0; y < CharEndY && i < CharHeight; y++, i++ ) {
YByte = ( i + OffsetY ) / 8;
YBit = ( i + OffsetY ) & 0x07;
if ( GlyphData[ YByte ] & BIT( YBit ) ) {
SSD1306_DrawPixel( DisplayHandle, x, y, Color );
}
}
GlyphData+= GlyphColumnLen;
}
}
}
bool SSD1306_SetFont( struct SSD1306_Device* Display, const struct SSD1306_FontDef* Font ) {
NullCheck( Display, return false );
NullCheck( Font, return false );
Display->FontForceProportional = false;
Display->FontForceMonospace = false;
Display->Font = Font;
return true;
}
void SSD1306_FontForceProportional( struct SSD1306_Device* Display, bool Force ) {
NullCheck( Display, return );
NullCheck( Display->Font, return );
Display->FontForceProportional = Force;
}
void SSD1306_FontForceMonospace( struct SSD1306_Device* Display, bool Force ) {
NullCheck( Display, return );
NullCheck( Display->Font, return );
Display->FontForceMonospace = Force;
}
int SSD1306_FontGetWidth( struct SSD1306_Device* Display ) {
NullCheck( Display, return 0 );
NullCheck( Display->Font, return 0 );
return Display->Font->Width;
}
int SSD1306_FontGetHeight( struct SSD1306_Device* Display ) {
NullCheck( Display, return 0 );
NullCheck( Display->Font, return 0 );
return Display->Font->Height;
}
int SSD1306_FontGetCharWidth( struct SSD1306_Device* Display, char Character ) {
const uint8_t* CharPtr = NULL;
int Width = 0;
NullCheck( Display, return 0 );
NullCheck( Display->Font, return 0 );
if ( Character >= Display->Font->StartChar && Character <= Display->Font->EndChar ) {
CharPtr = GetCharPtr( Display->Font, Character );
Width = ( Display->Font->Monospace == true ) ? Display->Font->Width : *CharPtr;
if ( Display->FontForceMonospace == true ) {
Width = Display->Font->Width;
}
if ( Display->FontForceProportional == true ) {
Width = *CharPtr;
}
}
return Width;
}
int SSD1306_FontGetMaxCharsPerRow( struct SSD1306_Device* Display ) {
NullCheck( Display, return 0 );
NullCheck( Display->Font, return 0 );
return Display->Width / Display->Font->Width;
}
int SSD1306_FontGetMaxCharsPerColumn( struct SSD1306_Device* Display ) {
NullCheck( Display, return 0 );
NullCheck( Display->Font, return 0 );
return Display->Height / Display->Font->Height;
}
int SSD1306_FontGetCharHeight( struct SSD1306_Device* Display ) {
NullCheck( Display, return 0 );
NullCheck( Display->Font, return 0 );
return Display->Font->Height;
}
int SSD1306_FontMeasureString( struct SSD1306_Device* Display, const char* Text ) {
int Width = 0;
int Len = 0;
NullCheck( Display, return 0 );
NullCheck( Display->Font, return 0 );
NullCheck( Text, return 0 );
for ( Len = strlen( Text ); Len >= 0; Len--, Text++ ) {
if ( *Text >= Display->Font->StartChar && *Text <= Display->Font->EndChar ) {
Width+= SSD1306_FontGetCharWidth( Display, *Text );
}
}
return Width;
}
void SSD1306_FontDrawString( struct SSD1306_Device* Display, int x, int y, const char* Text, int Color ) {
int Len = 0;
int i = 0;
NullCheck( Display, return );
NullCheck( Display->Font, return );
NullCheck( Text, return );
for ( Len = strlen( Text ), i = 0; i < Len; i++ ) {
SSD1306_FontDrawChar( Display, *Text, x, y, Color );
x+= SSD1306_FontGetCharWidth( Display, *Text );
Text++;
}
}
void SSD1306_FontDrawAnchoredString( struct SSD1306_Device* Display, TextAnchor Anchor, const char* Text, int Color ) {
int x = 0;
int y = 0;
NullCheck( Display, return );
NullCheck( Text, return );
SSD1306_FontGetAnchoredStringCoords( Display, &x, &y, Anchor, Text );
SSD1306_FontDrawString( Display, x, y, Text, Color );
}
void SSD1306_FontGetAnchoredStringCoords( struct SSD1306_Device* Display, int* OutX, int* OutY, TextAnchor Anchor, const char* Text ) {
int StringWidth = 0;
int StringHeight = 0;
NullCheck( Display, return );
NullCheck( OutX, return );
NullCheck( OutY, return );
NullCheck( Text, return );
StringWidth = SSD1306_FontMeasureString( Display, Text );
StringHeight = SSD1306_FontGetCharHeight( Display );
switch ( Anchor ) {
case TextAnchor_East: {
*OutY = ( Display->Height / 2 ) - ( StringHeight / 2 );
*OutX = ( Display->Width - StringWidth );
break;
}
case TextAnchor_West: {
*OutY = ( Display->Height / 2 ) - ( StringHeight / 2 );
*OutX = 0;
break;
}
case TextAnchor_North: {
*OutX = ( Display->Width / 2 ) - ( StringWidth / 2 );
*OutY = 0;
break;
}
case TextAnchor_South: {
*OutX = ( Display->Width / 2 ) - ( StringWidth / 2 );
*OutY = ( Display->Height - StringHeight );
break;
}
case TextAnchor_NorthEast: {
*OutX = ( Display->Width - StringWidth );
*OutY = 0;
break;
}
case TextAnchor_NorthWest: {
*OutY = 0;
*OutX = 0;
break;
}
case TextAnchor_SouthEast: {
*OutY = ( Display->Height - StringHeight );
*OutX = ( Display->Width - StringWidth );
break;
}
case TextAnchor_SouthWest: {
*OutY = ( Display->Height - StringHeight );
*OutX = 0;
break;
}
case TextAnchor_Center: {
*OutY = ( Display->Height / 2 ) - ( StringHeight / 2 );
*OutX = ( Display->Width / 2 ) - ( StringWidth / 2 );
break;
}
default: {
*OutX = 128;
*OutY = 64;
break;
}
};
}