5f0fd3b4e0759e04869c8b876c893bf9f838d87d
[vg.git] / vg_build_font.h
1 #include "vg_font.h"
2
3 #define STB_IMAGE_IMPLEMENTATION
4 #include "submodules/stb/stb_image.h"
5
6 void vg_build_font_face_run( vg_font_face *face,
7 char first, char last, i16 x, i16 y )
8 {
9 u32 count = (u32)last - (u32)first;
10 for( u32 i=0; i<=count; i ++ )
11 {
12 u32 index = (u32)first + i;
13 face->map[index].x = x+(i*face->cw);
14 face->map[index].y = y;
15 }
16 }
17
18 void vg_build_write_font_face( FILE *fp, vg_font_face *face )
19 {
20 fprintf( fp, "vg_font_face %s = {\n", face->name );
21 fprintf( fp, " .name=\"%s\",\n", face->name );
22 fprintf( fp, " .cw=%hd,.ch=%hd,\n", face->cw,face->ch );
23 fprintf( fp, " .sx=%hd,.sy=%hd,\n", face->sx,face->sy );
24 fprintf( fp, " .baseline=%hd,\n", face->baseline );
25 fprintf( fp, " .map={\n" );
26
27 u32 chars = 0;
28
29 for( u32 i=0; i<256; i ++ )
30 {
31 if( face->map[i].x || face->map[i].y )
32 {
33 chars += fprintf( fp, "[%u]={%hd,%hd},",
34 i, face->map[i].x, face->map[i].y );
35
36 if( chars > 80 )
37 {
38 fprintf( fp, "\n" );
39 chars = 0;
40 }
41 }
42 }
43
44 fprintf( fp, "\n}};\n\n" );
45 }
46
47 void vg_build_font_sheet( FILE *fp, char *name, const char *source )
48 {
49 int x,y,n;
50 unsigned char *data = stbi_load( source, &x, &y, &n, 4 );
51
52 if( !data )
53 {
54 vg_error( "Couldn't open source file\n" );
55 return;
56 }
57
58 fprintf( fp, "vg_font_sheet %s = {\n", name );
59 fprintf( fp, " .w=%d, .h=%d,\n", x,y );
60 fprintf( fp, " .bitmap={\n" );
61
62 u32 pixel_max = x*y;
63 u32 pixel = 0, chars = 0;
64 while( pixel_max )
65 {
66 u32 buff = 0;
67 for( int b = 31; b >= 0; b-- )
68 {
69 buff |= data[pixel*4]>128?0x1<<b:0;
70 pixel++;
71
72 if( pixel >= pixel_max )
73 {
74 pixel_max = 0;
75 break;
76 }
77 }
78
79 chars += fprintf( fp, "%#x,", buff );
80 if( chars > 80 )
81 {
82 fprintf( fp, "\n" );
83 chars = 0;
84 }
85 }
86
87 fprintf( fp, "\n}};\n" );
88 free( data );
89 }
90
91 void vg_build_default_font(void)
92 {
93 FILE *fp = fopen( "vg/vg_default_font.gc", "w" );
94 vg_build_font_sheet( fp, "vg_default_font_sheet",
95 "vg/src/fonts/vg_font_thin_3.png" );
96 vg_font_face small =
97 {
98 .name = "vgf_default_small",
99 .cw=8, .ch=14,
100 .sx=8, .sy=14,
101 .baseline = 4
102 };
103 vg_build_font_face_run( &small, '!', '/', 8, 0 );
104 vg_build_font_face_run( &small, '[', '`', 128,0 );
105 vg_build_font_face_run( &small, '{', '~', 176,0 );
106 vg_build_font_face_run( &small, ':', '@', 208,0 );
107 vg_build_font_face_run( &small, 'A', 'Z', 0, 14 );
108 vg_build_font_face_run( &small, 'a', 'z', 0, 28 );
109 vg_build_font_face_run( &small, '0', '9', 208,14 );
110 vg_build_write_font_face( fp, &small );
111
112 vg_font_face large =
113 {
114 .name = "vgf_default_large",
115 .cw=12, .ch=21,
116 .sx=12, .sy=21,
117 .baseline=6,
118 };
119 vg_build_font_face_run( &large, '!', '/', 12, 56 );
120 vg_build_font_face_run( &large, '[', '`', 192,56 );
121 vg_build_font_face_run( &large, '{', '~', 264,56 );
122 vg_build_font_face_run( &large, ':', '@', 324,56 );
123 vg_build_font_face_run( &large, 'A', 'Z', 0, 77 );
124 vg_build_font_face_run( &large, 'a', 'z', 0, 98 );
125 vg_build_font_face_run( &large, '0', '9', 312,77 );
126 vg_build_write_font_face( fp, &large );
127
128 vg_font_face title =
129 {
130 .name = "vgf_default_title",
131 .cw=24, .ch=42,
132 .sx=24, .sy=42,
133 .baseline=12,
134 };
135 vg_build_font_face_run( &title, '!', '/', 24, 140 );
136 vg_build_font_face_run( &title, '[', '_', 384,140 );
137 vg_build_font_face_run( &title, '`', '`', 0, 182 );
138 vg_build_font_face_run( &title, '{', '~', 24, 182 );
139 vg_build_font_face_run( &title, ':', '@', 120,182 );
140 vg_build_font_face_run( &title, 'A', 'U', 0, 224 );
141 vg_build_font_face_run( &title, 'V', 'Z', 0, 308 );
142 vg_build_font_face_run( &title, 'a', 'u', 0, 266 );
143 vg_build_font_face_run( &title, 'v', 'z', 0, 350 );
144 vg_build_font_face_run( &title, '0', '9', 120,308 );
145 vg_build_write_font_face( fp, &title );
146
147 fclose( fp );
148 }