config.h: rename SCREENSHOT to SCRSHOT
[fbpad.git] / font.c
blobc0acadc7d3cf8191d30a11b0d708ed5cf5d1fd14
1 #include <fcntl.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include "config.h"
7 #include "font.h"
8 #include "util.h"
10 static int fd;
11 static int rows;
12 static int cols;
13 static int n;
14 static int *glyphs;
17 * tinyfont format:
19 * sig[8] "tinyfont"
20 * ver 0
21 * n number of glyphs
22 * rows glyph rows
23 * cols glyph cols
25 * glyphs[n] unicode character numbers (int)
26 * bitmaps[n] character bitmaps (char[rows * cols])
28 struct tinyfont {
29 char sig[8];
30 int ver;
31 int n;
32 int rows, cols;
35 int font_init(void)
37 struct tinyfont head;
38 fd = open(TINYFONT, O_RDONLY);
39 if (fd == -1)
40 return 1;
41 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
42 if (read(fd, &head, sizeof(head)) != sizeof(head))
43 return 1;
44 n = head.n;
45 rows = head.rows;
46 cols = head.cols;
47 glyphs = malloc(n * sizeof(int));
48 if (read(fd, glyphs, n * sizeof(int)) != n * sizeof(int))
49 return 1;
50 return 0;
53 static int find_glyph(int c)
55 int l = 0;
56 int h = n;
57 while (l < h) {
58 int m = (l + h) / 2;
59 if (glyphs[m] == c)
60 return m;
61 if (c < glyphs[m])
62 h = m;
63 else
64 l = m + 1;
66 return -1;
69 int font_bitmap(void *dst, int c)
71 int i = find_glyph(c);
72 if (i < 0)
73 return 1;
74 lseek(fd, sizeof(struct tinyfont) + n * sizeof(int) + i * rows * cols, 0);
75 read(fd, dst, rows * cols);
76 return 0;
79 void font_free(void)
81 free(glyphs);
82 close(fd);
85 int font_rows(void)
87 return rows;
90 int font_cols(void)
92 return cols;