fbpad: pass NULL instead of 0 to wait_pid()
[fbpad.git] / font.c
blob16f8408e438796a83c339af1d5ae7c53403b5f6a
1 #include <fcntl.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <sys/mman.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include "config.h"
9 #include "font.h"
10 #include "util.h"
12 static int fd;
13 static char *tf;
14 static int rows;
15 static int cols;
16 static int n;
17 static int *glyphs;
18 static unsigned char *data;
20 static void xerror(char *msg)
22 perror(msg);
23 exit(1);
26 static size_t file_size(int fd)
28 struct stat st;
29 if (!fstat(fd, &st))
30 return st.st_size;
31 return 0;
34 struct tf_header {
35 char sig[8];
36 int ver;
37 int n;
38 int rows, cols;
41 void font_init(void)
43 struct tf_header *head;
44 fd = open(TINYFONT, O_RDONLY);
45 if (fd == -1)
46 xerror("can't open font");
47 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
48 tf = mmap(NULL, file_size(fd), PROT_READ, MAP_SHARED, fd, 0);
49 if (tf == MAP_FAILED)
50 xerror("can't mmap font file");
51 head = (struct tf_header *) tf;
52 n = head->n;
53 rows = head->rows;
54 cols = head->cols;
55 glyphs = (int *) (tf + sizeof(*head));
56 data = (unsigned char *) (glyphs + n);
59 static int find_glyph(int c)
61 int l = 0;
62 int h = n;
63 while (l < h) {
64 int m = (l + h) / 2;
65 if (glyphs[m] == c)
66 return m;
67 if (c < glyphs[m])
68 h = m;
69 else
70 l = m + 1;
72 return -1;
75 unsigned char *font_bitmap(int c)
77 int i = find_glyph(c);
78 return i >= 0 ? &data[i * rows * cols] : NULL;
81 void font_free(void)
83 munmap(tf, file_size(fd));
84 close(fd);
87 int font_rows(void)
89 return rows;
92 int font_cols(void)
94 return cols;