summaryrefslogtreecommitdiff
path: root/src/user/libfont/fonts.c
blob: 1692992ce218f2375da070afa2203551fcd02726 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <libfont/fonts.h>
#include <knob/format.h>
#include <knob/block.h>
#include <knob/heap.h>
#include <knob/file.h>

#include "filist.h"
#include "bdf.h"

#define FONT_PATH "fonts/"
#define FONT_PATH_L 6

struct font_loader_t {
  const char *ext;
  bool (*func)(const char *, struct font_info *);
} font_loaders[] = {
  { .ext = ".bdf",
    .func = try_load_bdf
  },
  { .ext = ""
  }
};

struct font_info *get_font(const char *name) {
  struct font_info *font = find_entry(name);
  if (font)
    return font;
  font = new_entry(name);
  if (!font)
    return 0;//out of memory?

  const uint32_t name_len = strlen(name);
  for (struct font_loader_t *i = font_loaders; i->ext[0]; ++i) {
    char *buf = get_block(FONT_PATH_L + name_len + strlen(i->ext) + 1);
    blockcpy(buf, FONT_PATH, FONT_PATH_L);
    blockcpy(buf + FONT_PATH_L, name, name_len);
    strcpy(buf + FONT_PATH_L + name_len, i->ext);
    struct file *f = open_file(buf);
    if (!f) {
      free_block(buf);
      continue;
    }
    syslogf("[libfont] Loading %s%s...", name, i->ext);
    if (i->func(buf, font)) {
      close_file(f);
      free_block(buf);
      syslogf("[libfont] Loaded %s%s.", name, i->ext);
      return font;
    }
    close_file(f);
    free_block(buf);
    syslogf("[libfont] Failed to load %s%s.", name, i->ext);
  }
  del_last();
  return 0;
}

void put_char(const struct font_info *font, char ch, uint8_t *pb_ptr, uint32_t pb_pitch, uint8_t bg, uint8_t fg) {
//char *const msg = format("put_char(font = 0x%x, ch = '%c', pb_ptr = 0x%x, pb_pitch = %u, bg = 0x%2x, fg = 0x%2x);", font, ch, pb_ptr, pb_pitch, bg, fg);
//_system_log(msg);
//free_block(msg);
  const bool *const bitmap = font->bitmaps[(uint8_t)ch] ? font->bitmaps[(uint8_t)ch] : font->bitmaps[0];
  for (uint32_t y = 0; y < font->char_height; ++y)
    for (uint32_t x = 0; x < font->char_width; ++x)
      pb_ptr[y * pb_pitch + x] = bitmap[y * font->char_width + x] ? fg : bg;
}