SayoriOS  0.3.3
ttf_font.c
1 //
2 // Created by ndraey on 27.01.24.
3 //
4 
5 #include "ttf_font.h"
6 
7 #define STB_TRUETYPE_IMPLEMENTATION
8 #include "3rdparty/stb_truetype.h"
9 
10 #include "lib/stdio.h"
11 #include "io/screen.h"
12 #include "io/ports.h"
13 
14 void ttf_init(ttf_font_t* font, const char* path) {
15  FILE* fp = fopen(path, "rb");
16 
17  if(!fp) {
18  return;
19  }
20 
21  fseek(fp, 0, SEEK_END);
22 
23  size_t filesize = ftell(fp);
24 
25  fseek(fp, 0, SEEK_SET);
26 
27  font->data = kcalloc(filesize, 1);
28  fread(fp, filesize, 1, font->data);
29 
30  stbtt_InitFont(&font->info, font->data, 0);
31 
32  stbtt_GetFontVMetrics(&font->info, &font->ascent, &font->descent, &font->linegap);
33 
34  font->user_size = 32;
35 
36  qemu_log("ASCENT: %d; DESCENT: %d", font->ascent, font->descent);
37 
38  fclose(fp);
39 }
40 
41 void ttf_draw_char(ttf_font_t *font, char *buffer, int bwidth, int bheight, int sx, int sy, int character) {
42  // TODO: Remove hardcoded values
43 
44  float scale = stbtt_ScaleForPixelHeight(&font->info, font->user_size);
45 
46  int ax;
47  int lsb;
48 
49  stbtt_GetCodepointHMetrics(&font->info, character, &ax, &lsb);
50 
51  int c_x1, c_y1, c_x2, c_y2;
52  stbtt_GetCodepointBitmapBox(&font->info, character, scale, scale, &c_x1, &c_y1, &c_x2, &c_y2);
53  qemu_log("==> C_X1: %d, C_Y1: %d, C_X2: %d, C_Y2: %d", c_x1, c_y1, c_x2, c_y2);
54 
55  char* bitmap = calloc(100 * 100, 1);
56 
57  // TODO: Draw character at correct position
58 
59  stbtt_MakeCodepointBitmap(&font->info, bitmap, c_x2 - c_x1, font->user_size,
60  font->user_size, scale, scale, character);
61 
62  for(int y = 0; y < font->user_size; y++) {
63  for(int x = 0; x < c_x2; x++) {
64  if(bitmap[y * font->user_size + x] != 0) {
65  set_pixel(sx + x, sy + y + ((font->user_size / 2) + 5 /* idk */ + c_y1), 0xFFFFFF);
66  }
67  }
68  }
69 
70  kfree(bitmap);
71 }
72 
73 
74 void ttf_draw_string(ttf_font_t* font, char* buffer, int bwidth, int bheight, int sx, int sy, const char* string) {
75  // TODO: Remove hardcoded values
76 
77  while(*string) {
78  float scale = stbtt_ScaleForPixelHeight(&font->info, font->user_size);
79 
80  int ax;
81  int lsb;
82 
83  stbtt_GetCodepointHMetrics(&font->info, *string, &ax, &lsb);
84 
85 // int c_x1, c_y1, c_x2, c_y2;
86 // stbtt_GetCodepointBitmapBox(&font->info, *string, scale, scale, &c_x1, &c_y1, &c_x2, &c_y2);
87 
88  qemu_log("AX: %d; LSB: %d", ax, lsb);
89 
90 // ttf_draw_char(font, buffer, bwidth, bheight, sx, sy, *string);
91  ttf_draw_char(font, buffer, bwidth, bheight, sx, sy, *string);
92 
93 
94  sx += (int)(scale * ax);
95 
96  string++;
97  }
98 }
99 
100 void ttf_destroy(ttf_font_t* font) {
101  free(font->data);
102 }
size_t filesize(const char *Path)
[FileIO] Возвращает размер указанного файла
Definition: fileio.c:67
ssize_t fseek(FILE *stream, ssize_t offset, uint8_t whence)
Установка позиции в потоке данных относительно текущей позиции
Definition: stdio.c:315
void fclose(FILE *stream)
Закончить работу с файлом
Definition: stdio.c:213
FILE * fopen(const char *filename, const char *_mode)
Открывает файл
Definition: stdio.c:166
int ftell(FILE *stream)
Текущая позиция считывания в файле
Definition: stdio.c:287
int fread(FILE *stream, size_t count, size_t size, void *buffer)
Чтение файла
Definition: stdio.c:250
Структура файла. Требуется для работы с VFS.
Definition: stdio.h:21