summaryrefslogtreecommitdiff
path: root/src/font.c
blob: 999c3dada338ab2bef797e784a09456d5c8536c7 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "font.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#define FILE_NAME_SIZE 300

void free_sprite_font(sprite_font* font) {
  free(font->characters);
  SDL_DestroyTexture(font->spritesheet);

  memset(font, 0, sizeof(sprite_font));
}

int get_string_parameter(const char* line, const char* parameter, const char** start) {
  *start = strstr(line, parameter);
  if ((*start) == NULL)
    return -1;

  *start += strlen(parameter);
  if ((*start)[0] != '\"') {
    return -1;
  }

  int length = 0;
  while ((*start)[length+1] != '\0' && (*start)[length+1] != '\"') length++;

  (*start)++;
  return length;
}

char parse_character_tag(const char* character_tag) {
  if (strncmp(character_tag, "space", 5) == 0)
    return ' ';

  return character_tag[0];
}

bool get_int_parameter(const char* line, const char* parameter, int* value) {
  const char* start = strstr(line, parameter);
  if (start == NULL) {
    return false;
  }
  start += strlen(parameter);

  int c = 0;
  bool negative = false;

  if (start[0] == '-') {
    negative = true;
    start++;
  }

  for(int i = 0; start[i] >= '0' && start[i] <= '9'; i++) {
    c *= 10; c += start[i] - '0';
  }

  *value = negative ? -c : c;
  return true;
}

bool load_sprite_font(const char* filename, sprite_font* font) {
  if (font->characters || font->spritesheet) {
    SDL_Log("Trying to reallocate font causing memory leak, exitting!");
    exit(EXIT_FAILURE);
  }

  FILE* f = fopen(filename, "r");
  if (f == NULL) {
    SDL_Log("Failed to open sprite font file %s, error: %s, exitting!",
            filename,
            strerror(errno)
            );
    exit(EXIT_FAILURE);
  }

  char line[PARSER_LINE_SIZE];
  if (fgets(line, sizeof(line), f) == NULL ||
      strncmp(line, "info", 4) != 0) {
    SDL_Log("File %s is not a valid font file, exitting!",
            filename);
    fclose(f);
    exit(EXIT_FAILURE);
  }

  char font_name[FONT_NAME_SIZE];
  const char* font_name_start;
  int font_name_length =
    SDL_min(get_string_parameter(line, "face=", &font_name_start),
            FONT_NAME_SIZE-1
            );

  strncpy(font_name, font_name_start, font_name_length);
  font_name[font_name_length] = '\0';

  int char_size = 0, char_count = 0;
  font_char* characters_unarranged = NULL;

  while (fgets(line, PARSER_LINE_SIZE, f) != NULL) {

    if (strncmp(line, "chars", 5) == 0) {
      int total_char_count;

      if(!get_int_parameter(line, "count=", &total_char_count))
        continue;

      char_size = total_char_count;
      characters_unarranged =
        realloc(characters_unarranged, char_size * sizeof(font_char));

      if (characters_unarranged == NULL) {
        SDL_Log("Failed to allocate space while loading font %s, error %s, exitting!",
                filename,
                strerror(errno));
        exit(EXIT_FAILURE);
      }
    }

    else if (strncmp(line, "char", 4) == 0) {
      font_char new_font_char;

      int val;

      if (!get_int_parameter(line, "x=", &val))
        continue;
      new_font_char.x = val;

      if (!get_int_parameter(line, "y=", &val))
        continue;
      new_font_char.y = val;

      if (!get_int_parameter(line, "width=", &val))
        continue;
      new_font_char.width = val;

      if (!get_int_parameter(line, "height=", &val))
        continue;
      new_font_char.height = val;

      if (!get_int_parameter(line, "xoffset=", &val))
        continue;
      new_font_char.x_offset = val;

      if (!get_int_parameter(line, "yoffset=", &val))
        continue;
      new_font_char.y_offset = val;

      if (!get_int_parameter(line, "xadvance=", &val))
        continue;
      new_font_char.x_advance = val;

      const char* character_tag;
      if (get_string_parameter(line, "letter=", &character_tag) <= 0)
        continue;
      new_font_char.character = parse_character_tag(character_tag);

      if (char_count >= char_size) {
        char_size = char_count+1;
        characters_unarranged =
          realloc(characters_unarranged, char_size * sizeof(font_char));
      }

      if (characters_unarranged == NULL) {
        SDL_Log("Failed to allocate space while loading font %s, error %s, exitting!",
                filename,
                strerror(errno));
        exit(EXIT_FAILURE);
      }

      characters_unarranged[char_count] = new_font_char;
      char_count++;
    }
  }

  fclose(f);
  return true;
}