#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;
}