libpynq (release 5EWC0-2023 version 0.2.2 of 2023-09-24 22:22)
Loading...
Searching...
No Matches
fontx.c
Go to the documentation of this file.
1#include "fontx.h"
2#include <stdbool.h>
3#include <stdint.h>
4#include <stdio.h>
5#include <string.h>
6#include <sys/stat.h>
7#include <sys/unistd.h>
8
9#define FontxDebug 0
10
11void AddFontx(FontxFile *fx, const char *path) {
12 memset(fx, 0, sizeof(FontxFile));
13 fx->path = path;
14 fx->opened = false;
15}
16
17void InitFontx(FontxFile *fxs, const char *f0, const char *f1) {
18 AddFontx(&fxs[0], f0);
19 AddFontx(&fxs[1], f1);
20}
21
23 FILE *f;
24 if (!fx->opened) {
25 if (FontxDebug)
26 printf("[openFont]fx->path=[%s]\n", fx->path);
27 f = fopen(fx->path, "r");
28 if (FontxDebug)
29 printf("[openFont]fopen=%p\n", f);
30 if (f == NULL) {
31 fx->valid = false;
32 printf("Fontx:%s not found.\n", fx->path);
33 return fx->valid;
34 }
35 fx->opened = true;
36 fx->file = f;
37 char buf[18];
38 if (fread(buf, 1, sizeof(buf), fx->file) != sizeof(buf)) {
39 fx->valid = false;
40 printf("Fontx:%s not FONTX format.\n", fx->path);
41 fclose(fx->file);
42 return fx->valid;
43 }
44
45 if (FontxDebug) {
46 for (uint32_t i = 0; i < strlen(buf); i++) {
47 printf("buf[%d]=0x%x\n", i, buf[i]);
48 }
49 }
50 memcpy(fx->fxname, &buf[6], 8);
51 fx->w = buf[14];
52 fx->h = buf[15];
53 fx->is_ank = (buf[16] == 0);
54 fx->bc = buf[17];
55 fx->fsz = (fx->w + 7) / 8 * fx->h;
56 if (fx->fsz > FontxGlyphBufSize) {
57 printf("Fontx:%s is too big font size.\n", fx->path);
58 fx->valid = false;
59 fclose(fx->file);
60 return fx->valid;
61 }
62 fx->valid = true;
63 }
64 return fx->valid;
65}
66
68 if (fx->opened) {
69 fclose(fx->file);
70 fx->opened = false;
71 }
72}
73
74void DumpFontx(FontxFile *fxs) {
75 for (int i = 0; i < 2; i++) {
76 printf("fxs[%d]->path=%s\n", i, fxs[i].path);
77 printf("fxs[%d]->opened=%d\n", i, fxs[i].opened);
78 printf("fxs[%d]->fxname=%s\n", i, fxs[i].fxname);
79 printf("fxs[%d]->valid=%d\n", i, fxs[i].valid);
80 printf("fxs[%d]->is_ank=%d\n", i, fxs[i].is_ank);
81 printf("fxs[%d]->w=%d\n", i, fxs[i].w);
82 printf("fxs[%d]->h=%d\n", i, fxs[i].h);
83 printf("fxs[%d]->fsz=%d\n", i, fxs[i].fsz);
84 printf("fxs[%d]->bc=%d\n", i, fxs[i].bc);
85 }
86}
87
88uint8_t getFortWidth(FontxFile *fx) {
89 printf("fx->w=%d\n", fx->w);
90 return (fx->w);
91}
92
94 printf("fx->h=%d\n", fx->h);
95 return (fx->h);
96}
97
98bool GetFontx(FontxFile *fxs, uint8_t ascii, uint8_t *pGlyph, uint8_t *pw,
99 uint8_t *ph) {
100 int i;
101 uint32_t offset;
102
103 if (FontxDebug)
104 printf("[GetFontx]ascii=0x%x\n", ascii);
105 for (i = 0; i < 2; i++) {
106 if (!OpenFontx(&fxs[i]))
107 continue;
108 if (FontxDebug)
109 printf("[GetFontx]openFontxFile[%d] ok\n", i);
110
111 if (fxs[i].is_ank) {
112 if (FontxDebug)
113 printf("[GetFontx]fxs.is_ank fxs.fsz=%d\n", fxs[i].fsz);
114 offset = 17 + ascii * fxs[i].fsz;
115 if (FontxDebug)
116 printf("[GetFontx]offset=%d\n", offset);
117 if (fseek(fxs[i].file, offset, SEEK_SET)) {
118 printf("Fontx:seek(%u) failed.\n", offset);
119 return false;
120 }
121 if (fread(pGlyph, 1, fxs[i].fsz, fxs[i].file) != fxs[i].fsz) {
122 printf("Fontx:fread failed.\n");
123 return false;
124 }
125 if (pw)
126 *pw = fxs[i].w;
127 if (ph)
128 *ph = fxs[i].h;
129 return true;
130 }
131 }
132 return false;
133}
134
135void Font2Bitmap(uint8_t *fonts, uint8_t *line, uint8_t w, uint8_t h,
136 uint8_t inverse) {
137 int x, y;
138 for (y = 0; y < (h / 8); y++) {
139 for (x = 0; x < w; x++) {
140 line[y * 32 + x] = 0;
141 }
142 }
143
144 int mask = 7;
145 int fontp;
146 fontp = 0;
147 for (y = 0; y < h; y++) {
148 for (x = 0; x < w; x++) {
149 uint8_t d = fonts[fontp + x / 8];
150 uint8_t linep = (y / 8) * 32 + x;
151 if (d & (0x80 >> (x % 8)))
152 line[linep] = line[linep] + (1 << mask);
153 }
154 mask--;
155 if (mask < 0)
156 mask = 7;
157 fontp += (w + 7) / 8;
158 }
159
160 if (inverse) {
161 for (y = 0; y < (h / 8); y++) {
162 for (x = 0; x < w; x++) {
163 line[y * 32 + x] = RotateByte(line[y * 32 + x]);
164 }
165 }
166 }
167}
168
169void UnderlineBitmap(uint8_t *line, uint8_t w, uint8_t h) {
170 int x, y;
171 uint8_t wk;
172 for (y = 0; y < (h / 8); y++) {
173 for (x = 0; x < w; x++) {
174 wk = line[y * 32 + x];
175 if ((y + 1) == (h / 8))
176 line[y * 32 + x] = wk + 0x80;
177 }
178 }
179}
180
181void ReversBitmap(uint8_t *line, uint8_t w, uint8_t h) {
182 int x, y;
183 uint8_t wk;
184 for (y = 0; y < (h / 8); y++) {
185 for (x = 0; x < w; x++) {
186 wk = line[y * 32 + x];
187 line[y * 32 + x] = ~wk;
188 }
189 }
190}
191
192void ShowFont(uint8_t *fonts, uint8_t pw, uint8_t ph) {
193 int x, y, fpos;
194 printf("[ShowFont pw=%d ph=%d]\n", pw, ph);
195 fpos = 0;
196 for (y = 0; y < ph; y++) {
197 printf("%02d", y);
198 for (x = 0; x < pw; x++) {
199 if (fonts[fpos + x / 8] & (0x80 >> (x % 8))) {
200 printf("*");
201 } else {
202 printf(".");
203 }
204 }
205 printf("\n");
206 fpos = fpos + (pw + 7) / 8;
207 }
208 printf("\n");
209}
210
211void ShowBitmap(uint8_t *bitmap, uint8_t pw, uint8_t ph) {
212 int x, y, fpos;
213 printf("[ShowBitmap pw=%d ph=%d]\n", pw, ph);
214
215 fpos = 0;
216 for (y = 0; y < ph; y++) {
217 printf("%02d", y);
218 for (x = 0; x < pw; x++) {
219
220 if (bitmap[x + (y / 8) * 32] & (0x80 >> fpos)) {
221 printf("*");
222 } else {
223 printf(".");
224 }
225 }
226 printf("\n");
227 fpos++;
228 if (fpos > 7)
229 fpos = 0;
230 }
231 printf("\n");
232}
233
234uint8_t RotateByte(uint8_t ch1) {
235 uint8_t ch2 = 0;
236 int j;
237 for (j = 0; j < 8; j++) {
238 ch2 = (ch2 << 1) + (ch1 & 0x01);
239 ch1 = ch1 >> 1;
240 }
241 return ch2;
242}
#define FontxDebug
Definition fontx.c:9
void AddFontx(FontxFile *fx, const char *path)
Definition fontx.c:11
uint8_t getFortHeight(FontxFile *fx)
Definition fontx.c:93
uint8_t getFortWidth(FontxFile *fx)
Definition fontx.c:88
#define FontxGlyphBufSize
Definition fontx.h:3
void DumpFontx(FontxFile *fxs)
Dumps the font data stored in the FontxFile structure.
Definition fontx.c:74
void InitFontx(FontxFile *fxs, const char *f0, const char *f1)
Initializes the given FontxFile structure with the specified font files.
Definition fontx.c:17
void CloseFontx(FontxFile *fx)
Closes the font file.
Definition fontx.c:67
void ShowBitmap(uint8_t *bitmap, uint8_t pw, uint8_t ph)
Displays a bitmap on the screen.
Definition fontx.c:211
bool GetFontx(FontxFile *fxs, uint8_t ascii, uint8_t *pGlyph, uint8_t *pw, uint8_t *ph)
Gets the glyph data for the specified ASCII character.
Definition fontx.c:98
uint8_t RotateByte(uint8_t ch1)
Rotates a byte by 90 degrees.
Definition fontx.c:234
struct _IO_FILE FILE
Definition fontx.h:23
void ReversBitmap(uint8_t *line, uint8_t w, uint8_t h)
Reverses the bits in each byte of a bitmap.
Definition fontx.c:181
void ShowFont(uint8_t *fonts, uint8_t pw, uint8_t ph)
Displays a font on the screen.
Definition fontx.c:192
void Font2Bitmap(uint8_t *fonts, uint8_t *line, uint8_t w, uint8_t h, uint8_t inverse)
Converts a font data buffer into a bitmap.
Definition fontx.c:135
bool OpenFontx(FontxFile *fx)
Opens the font file and reads the font data into the FontxFile structure.
Definition fontx.c:22
void UnderlineBitmap(uint8_t *line, uint8_t w, uint8_t h)
Adds an underline to a bitmap.
Definition fontx.c:169
Struct representing a font file.
Definition fontx.h:28
uint8_t bc
Definition fontx.h:38
uint16_t fsz
Definition fontx.h:37
const char * path
Definition fontx.h:29
bool is_ank
Definition fontx.h:33
uint8_t w
Definition fontx.h:35
char fxname[10]
Definition fontx.h:30
bool opened
Definition fontx.h:31
FILE * file
Definition fontx.h:39
bool valid
Definition fontx.h:32
uint8_t h
Definition fontx.h:36