PatchworkOS
Loading...
Searching...
No Matches
font.c
Go to the documentation of this file.
1#include "internal.h"
2
3#include <stdlib.h>
4#include <string.h>
5#include <sys/io.h>
6
8{
9 mtx_lock(&disp->mutex);
10 font_t* temp = disp->defaultFont;
11 mtx_unlock(&disp->mutex);
12 return temp;
13}
14
15#include <stdio.h>
16
17font_t* font_new(display_t* disp, const char* family, const char* weight, uint64_t size)
18{
20 if (strcmp(family, "default") == 0)
21 {
22 family = theme->defaultFont;
23 }
24
25 fd_t file = openf("%s/%s-%s%d.grf", theme->fontsDir, family, weight, size);
26 if (file == ERR)
27 {
28 return NULL;
29 }
30
31 uint64_t fileSize = seek(file, 0, SEEK_END);
32 seek(file, 0, SEEK_SET);
33
34 if (fileSize <= sizeof(font_t))
35 {
36 close(file);
37 return NULL;
38 }
39
40 font_t* font = malloc(sizeof(font_t) - sizeof(grf_t) + fileSize);
41 if (font == NULL)
42 {
43 close(file);
44 return NULL;
45 }
46
47 grf_t grf;
48 if (read(file, &font->grf, fileSize) != fileSize)
49 {
50 free(font);
51 close(file);
52 return NULL;
53 }
54
55 if (font->grf.magic != GRF_MAGIC)
56 {
57 free(font);
58 close(file);
59 return NULL;
60 }
61
62 for (uint64_t i = 0; i < 256; i++)
63 {
64 if (font->grf.glyphOffsets[i] != GRF_NONE && font->grf.glyphOffsets[i] >= fileSize)
65 {
66 free(font);
67 close(file);
68 return NULL;
69 }
70 }
71
72 for (uint64_t i = 0; i < 256; i++)
73 {
74 if (font->grf.kernOffsets[i] != GRF_NONE && font->grf.kernOffsets[i] >= fileSize)
75 {
76 free(font);
77 close(file);
78 return NULL;
79 }
80 }
81
82 close(file);
83 font->disp = disp;
84 list_entry_init(&font->entry);
85 mtx_lock(&disp->mutex);
86 list_push(&disp->fonts, &font->entry);
87 mtx_unlock(&disp->mutex);
88 return font;
89}
90
91void font_free(font_t* font)
92{
93 mtx_lock(&font->disp->mutex);
94 list_remove(&font->disp->fonts, &font->entry);
95 mtx_unlock(&font->disp->mutex);
96 free(font);
97}
98
99int16_t font_kerning_offset(const font_t* font, char firstChar, char secondChar)
100{
101 if (font == NULL)
102 {
103 return 0;
104 }
105
106 uint32_t offset = font->grf.kernOffsets[(uint8_t)firstChar];
107 if (offset == GRF_NONE)
108 {
109 return 0;
110 }
111
112 grf_kern_block_t* block = (grf_kern_block_t*)(&font->grf.buffer[offset]);
113
114 for (uint16_t i = 0; i < block->amount; i++)
115 {
116 if (block->entries[i].secondChar == (uint8_t)secondChar)
117 {
118 return block->entries[i].offsetX;
119 }
120
121 if (block->entries[i].secondChar > (uint8_t)secondChar)
122 {
123 break;
124 }
125 }
126
127 return 0;
128}
129
130uint64_t font_width(const font_t* font, const char* string, uint64_t length)
131{
132 if (string == NULL || length == 0)
133 {
134 return 0;
135 }
136
137 uint64_t width = 0;
138
139 for (uint64_t i = 0; i < length; ++i)
140 {
141 uint32_t offset = font->grf.glyphOffsets[(uint8_t)string[i]];
142
143 if (offset != GRF_NONE)
144 {
145 grf_glyph_t* glyph = (grf_glyph_t*)(&font->grf.buffer[offset]);
146 width += glyph->advanceX;
147
148 if (i != length - 1)
149 {
150 width += font_kerning_offset(font, string[i], string[i + 1]);
151 }
152 }
153 }
154
155 return width;
156}
157
159{
160 return font->grf.height;
161}
#define SEEK_SET
Definition SEEK.h:4
#define SEEK_END
Definition SEEK.h:6
uint64_t font_width(const font_t *font, const char *string, uint64_t length)
Definition font.c:130
int16_t font_kerning_offset(const font_t *font, char firstChar, char secondChar)
Definition font.c:99
void font_free(font_t *font)
Definition font.c:91
font_t * font_new(display_t *disp, const char *family, const char *weight, uint64_t size)
Definition font.c:17
uint64_t font_height(const font_t *font)
Definition font.c:158
font_t * font_default(display_t *disp)
Definition font.c:7
#define GRF_MAGIC
Definition grf.h:24
#define GRF_NONE
Definition grf.h:25
theme_t * theme_global_get(void)
Get the global theme.
Definition theme.c:97
uint64_t close(fd_t fd)
System call for closing files.
Definition close.c:9
uint64_t seek(fd_t fd, int64_t offset, seek_origin_t origin)
System call for changing the file offset.
Definition seek.c:9
fd_t openf(const char *_RESTRICT format,...)
Wrapper for opening files with a formatted path.
Definition openf.c:9
uint64_t read(fd_t fd, void *buffer, uint64_t count)
System call for reading from files.
Definition read.c:9
static void list_remove(list_t *list, list_entry_t *entry)
Removes a list entry from its current list.
Definition list.h:317
static void list_push(list_t *list, list_entry_t *entry)
Pushes an entry to the end of the list.
Definition list.h:345
static void list_entry_init(list_entry_t *entry)
Initializes a list entry.
Definition list.h:184
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ fd_t
A file descriptor.
Definition fd_t.h:12
static dentry_t * file
Definition log_file.c:17
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
__UINT16_TYPE__ uint16_t
Definition stdint.h:13
__INT16_TYPE__ int16_t
Definition stdint.h:12
_PUBLIC void * malloc(size_t size)
Definition malloc.c:5
_PUBLIC void free(void *ptr)
Definition free.c:11
_PUBLIC int strcmp(const char *s1, const char *s2)
Definition strcmp.c:3
Opaque display structure.
Definition internal.h:61
mtx_t mutex
Definition internal.h:73
font_t * defaultFont
Definition internal.h:72
list_t fonts
Definition internal.h:70
display_t * disp
Definition internal.h:18
list_entry_t entry
Definition internal.h:17
grf_t grf
Definition internal.h:19
int16_t advanceX
Definition grf.h:46
grf_kern_entry_t entries[]
Definition grf.h:63
uint16_t amount
Definition grf.h:62
int16_t offsetX
Definition grf.h:56
uint8_t secondChar
Definition grf.h:55
Definition grf.h:29
uint8_t buffer[]
Definition grf.h:38
uint32_t magic
Definition grf.h:30
int16_t height
Definition grf.h:33
uint32_t glyphOffsets[256]
Definition grf.h:34
uint32_t kernOffsets[256]
Definition grf.h:36
Theme structure.
Definition theme.h:71
char defaultFont[MAX_PATH]
Definition theme.h:80
char fontsDir[MAX_PATH]
Definition theme.h:78
static theme_t theme
Definition theme.c:12
_PUBLIC int mtx_lock(mtx_t *mtx)
Definition mtx_lock.c:11
_PUBLIC int mtx_unlock(mtx_t *mtx)
Definition mtx_unlock.c:10