summaryrefslogtreecommitdiff
path: root/source/saw/_gen.c
blob: 8618f8dd03c965e1581aa52146a50f729532dba6 (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
#if 0
SRC=${0##*/}
BIN=${SRC%.*}
gcc -fsanitize=undefined,address,leak -o $BIN $SRC && ./$BIN && rm $BIN
exit
#endif

//  ================================================================
//
//      Saw code generation
//
//  ================================================================

#include "../kit/types.h"

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

enum {
  MAX_LENGTH = 200,
};

i64 int_len(u32 x) {
  i64 len = 0;

  do {
    x /= 10;
    ++len;
  } while (x > 0);

  return len;
}

i64 print_bytes(FILE *out, FILE *in) {
  i64 size = 0, line_len = MAX_LENGTH;

  while (!feof(in)) {
    u32 x = 0;

    i64 n = fread(&x, 1, sizeof x, in);
    if (n <= 0)
      break;

    i64 len = int_len(x);

    line_len += len + 2;

    if (line_len >= MAX_LENGTH) {
      fprintf(out, "\n ");
      line_len = 3 + len;
    }

    fprintf(out, " %u,", x);

    size += n;
  }

  return size;
}

int main(int argc, char **argv) {
  FILE *out = fopen("fonts.inl.h", "wb");
  assert(out != NULL);

  fprintf(out, "//  "
               "====================================================="
               "===========\n");
  fprintf(out, "//\n");
  fprintf(out, "//      Saw generated code\n");
  fprintf(out, "//\n");
  fprintf(out, "//  "
               "====================================================="
               "===========\n\n");

  fprintf(out, "#ifndef SAW_FONTS_INL_H\n");
  fprintf(out, "#define SAW_FONTS_INL_H\n\n");
  fprintf(out, "#include \"../kit/types.h\"\n\n");

  //  Write Domitian Roman
  //
  {
    FILE *in = fopen("fonts/domitian_roman.ttf", "rb");
    assert(in != NULL);

    fprintf(out, "static u32 saw_ttf_text[] = {");

    i64 n = print_bytes(out, in);

    fprintf(out, "\n};\n\n");
    fprintf(out, "enum { SAW_TTF_TEXT_SIZE = %lld, };\n\n", n);

    fclose(in);
  }

  //  Write Font Awesome
  //
  {
    FILE *in = fopen("fonts/font_awesome_6_free_solid_900.ttf", "rb");
    assert(in != NULL);

    fprintf(out, "static u32 saw_ttf_icons[] = {");

    i64 n = print_bytes(out, in);

    fprintf(out, "\n};\n\n");
    fprintf(out, "enum { SAW_TTF_ICONS_SIZE = %lld, };\n\n", n);

    fclose(in);
  }

  fprintf(out, "#endif\n");
  fclose(out);

  return 0;
}