summaryrefslogtreecommitdiff
path: root/font/font.c
blob: 2d30c507ea9a3d4edd4ae10bde5005edaf66d44c (plain)
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
#include <font.h>

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

static size_t GetFileSize(FILE* file) {
  fseek(file, 0, SEEK_END);
  const size_t size = ftell(file);
  fseek(file, 0, SEEK_SET);
  return size;
}

FontAtlas* LoadFontAtlas(const char* path) {
  assert(path);

  FILE*      file  = NULL;
  FontAtlas* atlas = 0;

  if ((file = fopen(path, "rb")) == NULL) {
    goto cleanup;
  }

  const size_t size = GetFileSize(file);
  if (size == (size_t)-1) {
    goto cleanup;
  }

  atlas = calloc(1, size);
  if (!atlas) {
    goto cleanup;
  }

  if (fread(atlas, size, 1, file) != 1) {
    free(atlas);
    atlas = 0;
  }

cleanup:
  if (file != NULL) {
    fclose(file);
  }
  return atlas;
}