forked from espressif/esp32-arduino-lib-builder
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathfb_gfx.c
167 lines (153 loc) · 4.85 KB
/
fb_gfx.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
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "stdint.h"
#include "stdarg.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "fb_gfx.h"
typedef struct { // Data stored PER GLYPH
uint16_t bitmapOffset; // Pointer into GFXfont->bitmap
uint8_t width, height; // Bitmap dimensions in pixels
uint8_t xAdvance; // Distance to advance cursor (x axis)
int8_t xOffset, yOffset; // Dist from cursor pos to UL corner
} GFXglyph;
typedef struct { // Data stored for FONT AS A WHOLE:
uint8_t *bitmap; // Glyph bitmaps, concatenated
GFXglyph *glyph; // Glyph array
uint8_t first, last; // ASCII extents
uint8_t yAdvance; // Newline distance (y axis)
uint8_t yOffset; // Y offset of the font zero line (y axis)
} GFXfont;
#include "FreeMonoBold12pt7b.h"//14x24
#define gfxFont ((GFXfont*)(&FreeMonoBold12pt7b))
void fb_gfx_fillRect(fb_data_t *fb, int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color)
{
int32_t line_step = (fb->width - w) * fb->bytes_per_pixel;
uint8_t *data = fb->data + ((x + (y * fb->width)) * fb->bytes_per_pixel);
uint8_t c0 = color >> 16;
uint8_t c1 = color >> 8;
uint8_t c2 = color;
for (int i=0; i<h; i++){
for (int j=0; j<w; j++){
if(fb->bytes_per_pixel == 2){
data[0] = c1;
data[1] = c2;
} else if(fb->bytes_per_pixel == 1){
data[0] = c2;
} else {
data[0] = c0;
data[1] = c1;
data[2] = c2;
}
data+=fb->bytes_per_pixel;
}
data += line_step;
}
}
void fb_gfx_drawFastHLine(fb_data_t *fb, int32_t x, int32_t y, int32_t w, uint32_t color)
{
fb_gfx_fillRect(fb, x, y, w, 1, color);
}
void fb_gfx_drawFastVLine(fb_data_t *fb, int32_t x, int32_t y, int32_t h, uint32_t color)
{
fb_gfx_fillRect(fb, x, y, 1, h, color);
}
uint8_t fb_gfx_putc(fb_data_t *fb, int32_t x, int32_t y, uint32_t color, unsigned char c)
{
uint16_t line_width;
uint8_t xa = 0, bit = 0, bits = 0, xx, yy;
uint8_t *bitmap;
GFXglyph *glyph;
if ((c < 32) || (c < gfxFont->first) || (c > gfxFont->last)) {
return xa;
}
c -= gfxFont->first;
glyph = &(gfxFont->glyph[c]);
bitmap = gfxFont->bitmap + glyph->bitmapOffset;
xa = glyph->xAdvance;
x += glyph->xOffset;
y += glyph->yOffset;
y += gfxFont->yOffset;
line_width = 0;
for(yy=0; yy<glyph->height; yy++) {
for(xx=0; xx<glyph->width; xx++) {
if(bit == 0) {
bits = *bitmap++;
bit = 0x80;
}
if(bits & bit) {
line_width++;
} else if (line_width) {
fb_gfx_drawFastHLine(fb, x+xx-line_width, y+yy, line_width, color);
line_width=0;
}
bit >>= 1;
}
if (line_width) {
fb_gfx_drawFastHLine(fb, x+xx-line_width, y+yy, line_width, color);
line_width=0;
}
}
return xa;
}
uint32_t fb_gfx_print(fb_data_t *fb, int32_t x, int32_t y, uint32_t color, const char * str)
{
uint32_t l = 0;
int xc = x, yc = y, lc = fb->width - gfxFont->glyph[0].xAdvance;
uint8_t fh = gfxFont->yAdvance;
char c = *str++;
while(c){
if(c != '\r'){
if(c == '\n'){
yc += fh;
xc = x;
} else {
if(xc > lc){
yc += fh;
xc = x;
}
xc += fb_gfx_putc(fb, xc, yc, color, c);
}
}
l++;
c = *str++;
}
return l;
}
uint32_t fb_gfx_printf(fb_data_t *fb, int32_t x, int32_t y, uint32_t color, const char *format, ...)
{
char loc_buf[64];
char * temp = loc_buf;
int len;
va_list arg;
va_list copy;
va_start(arg, format);
va_copy(copy, arg);
len = vsnprintf(loc_buf, sizeof(loc_buf), format, arg);
va_end(copy);
if(len >= sizeof(loc_buf)){
temp = (char*)malloc(len+1);
if(temp == NULL) {
return 0;
}
}
vsnprintf(temp, len+1, format, arg);
va_end(arg);
fb_gfx_print(fb, x, y, color, temp);
if(len > 64){
free(temp);
}
return len;
}