File: font.c

package info (click to toggle)
toppler 1.3-3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,868 kB
  • sloc: cpp: 8,979; ansic: 2,349; makefile: 402; sh: 24
file content (297 lines) | stat: -rw-r--r-- 9,049 bytes parent folder | download | duplicates (2)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <stdio.h>

#include <SDL.h>
#include <SDL_image.h>

/* creates a font.dat file out of specially formatted image files
 * there must be 2 files: one containing the color for each pixel
 * belonging to a letter, one containing a mast having the alpha value
 * for each pixel
 *
 * the colors files must contain additional information about the size
 * of each letter in form of specially places pixels. the color of the
 * marker pixels is defines by the pixel in the top left corner of the image
 * the best is, if you have a look at the example image
 */

int ypos, xpos;
Uint8 fontheight = 1;
Uint8 markercolor;
FILE *outp;

Uint8 get_color(SDL_Surface *s, int x, int y) {
  return ((Uint8*)s->pixels)[y*s->pitch+x];
}

Uint8 get_alpha(SDL_Surface *s, int x, int y) {
  return s->format->palette->colors[((Uint8*)s->pixels)[y*s->pitch+x]].r;
}

void write_palette(FILE *out, SDL_Surface *s) {
  int i;
  Uint8 c = s->format->palette->ncolors - 1;

  fwrite(&c, 1, 1, out);

  for (i = 0; i < s->format->palette->ncolors; i++) {
    fwrite(&s->format->palette->colors[i].r, 1, 1, out);
    fwrite(&s->format->palette->colors[i].g, 1, 1, out);
    fwrite(&s->format->palette->colors[i].b, 1, 1, out);
  }
}

void get(SDL_Surface *colors, SDL_Surface * mask, unsigned short c)
{
  // xpos und ypos zeigen auf oberes linkes umrandungs pixel */
  // hoehe ist bekannt : 19
  // breite ist zu suchen

  int height = fontheight;
  unsigned char width = 0;
  int i, x, y;
  Uint8 b;

  while (get_color(colors, xpos + width + 1, ypos + 1) != markercolor)
    width++;

  i = 0;

  fwrite(&c, 1, 2, outp);
  fwrite(&width, 1, 1, outp);

  if (c < 0x100)
    printf(" get %c(%i) at pos (%i;%i)\n", c, c, xpos, ypos);
  else
    printf(" get  (%i) at pos (%i;%i)\n", c, xpos, ypos);

  for (y = 0; y < height; y++)
    for (x = 0; x < width; x++) {
      b = get_color(colors, xpos + 1 + x, ypos + 1 + y);
      fwrite(&b, 1, 1, outp);
      b = get_alpha(mask, xpos + 1 + x, ypos + 1 + y);
      fwrite(&b, 1, 1, outp);
    }

  xpos += (1 + width);

  while (get_color(colors, xpos + 1, ypos + 1) == markercolor) xpos++;

  if (get_color(colors, xpos + 1, ypos) != markercolor) {
    ypos += 1 + height;
    xpos = 0;
  }
}

int main() {

  SDL_Surface *colors = IMG_LoadPNG_RW(SDL_RWFromFile("font_colors.png", "rb"));
  SDL_Surface *mask = IMG_LoadPNG_RW(SDL_RWFromFile("font_mask.png", "rb"));

  markercolor = get_color(colors, 0, 0);

  outp = fopen("font.dat", "wb");

  write_palette(outp, colors);

  /* ok first lets fins out the pixel height of the fonts, I
   assume that all the rows have the same hight, so the first is ok */

  while (get_color(colors, 1, 1+fontheight) != markercolor)
    fontheight++;

  fwrite(&fontheight, 1, 1, outp);

  /* and not lets get the pixels */
  xpos = ypos = 0;

  get(colors, mask, '0');
  get(colors, mask, '1');
  get(colors, mask, '2');
  get(colors, mask, '3');
  get(colors, mask, '4');
  get(colors, mask, '5');
  get(colors, mask, '6');
  get(colors, mask, '7');
  get(colors, mask, '8');
  get(colors, mask, '9');
  get(colors, mask, 'A');
  get(colors, mask, 'B');
  get(colors, mask, 'C');
  get(colors, mask, 'D');
  get(colors, mask, 'E');
  get(colors, mask, 'F');
  get(colors, mask, 'G');
  get(colors, mask, 'H');
  get(colors, mask, 'I');
  get(colors, mask, 'J');
  get(colors, mask, 'K');
  get(colors, mask, '.');
  get(colors, mask, 'M');
  get(colors, mask, 'N');
  get(colors, mask, 'O');
  get(colors, mask, 'P');
  get(colors, mask, 'Q');
  get(colors, mask, 'R');
  get(colors, mask, 'S');
  get(colors, mask, 'T');
  get(colors, mask, 'U');
  get(colors, mask, 'V');
  get(colors, mask, 'W');
  get(colors, mask, 'X');
  get(colors, mask, 'Y');
  get(colors, mask, 'Z');
  get(colors, mask, 'a');
  get(colors, mask, 'b');
  get(colors, mask, 'c');
  get(colors, mask, 'd');
  get(colors, mask, 'e');
  get(colors, mask, 'f');
  get(colors, mask, 'h');
  get(colors, mask, 'i');
  get(colors, mask, 'j');
  get(colors, mask, 'k');
  get(colors, mask, 'l');
  get(colors, mask, 'm');
  get(colors, mask, 'n');
  get(colors, mask, 'o');
  get(colors, mask, 'p');
  get(colors, mask, 'q');
  get(colors, mask, 'r');
  get(colors, mask, 's');
  get(colors, mask, 't');
  get(colors, mask, 'u');
  get(colors, mask, 'v');
  get(colors, mask, 'w');
  get(colors, mask, 'x');
  get(colors, mask, 'y');
  get(colors, mask, 'z');
  get(colors, mask, 'L');
  get(colors, mask, ',');
  get(colors, mask, '?');
  get(colors, mask, '+');
  get(colors, mask, '*');
  get(colors, mask, 'g');
  get(colors, mask, ':');
  get(colors, mask, ';');
  get(colors, mask, '(');
  get(colors, mask, ')');
  get(colors, mask, '!');
  get(colors, mask, '/');
  get(colors, mask, '%');
  get(colors, mask, '&');
  get(colors, mask, '\'');
  get(colors, mask, '"');
  get(colors, mask, '\x01'); /* fonttoppler */
  get(colors, mask, '\x02'); /* fontpoint */
  get(colors, mask, '\x03'); /* empty tagbox */
  get(colors, mask, '\x04'); /* taged tagbox */
  get(colors, mask, '\x05'); /* pointer up */
  get(colors, mask, '\x06'); /* pointer right */
  get(colors, mask, '\x07'); /* pointer down */
  get(colors, mask, '\x08'); /* pointer left */
  get(colors, mask, '-');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');

  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');

  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');
  get(colors, mask, (unsigned char)'');

  get(colors, mask, 0x108);  // ^C
  get(colors, mask, 0x109);  // ^c
  get(colors, mask, 0x11c);  // ^G
  get(colors, mask, 0x11d);  // ^g
  get(colors, mask, 0x124);  // ^H
  get(colors, mask, 0x125);  // ^h
  get(colors, mask, 0x134);  // ^J
  get(colors, mask, 0x135);  // ^j
  get(colors, mask, 0x15c);  // ^S
  get(colors, mask, 0x15d);  // ^s
  get(colors, mask, 0x168);  // ~U
  get(colors, mask, 0x169);  // ~u
  get(colors, mask, 0xC5);   // A
  get(colors, mask, 0xE5);   // a
  get(colors, mask, 0x153);   // oe 

  get(colors, mask, 0x152);   // OE 
  get(colors, mask, 0xC3);   // ~A 
  get(colors, mask, 0xE3);   // ~a 
  get(colors, mask, 0xC6);   // AE 
  get(colors, mask, 0xE6);   // ae 
  get(colors, mask, 0xCB);   // "E 
  get(colors, mask, 0xEB);   // "e 
  get(colors, mask, 0xCF);   // "I 
  get(colors, mask, 0xEF);   // "i 
  get(colors, mask, 0xD0);   // Thorn
  get(colors, mask, 0xF0);   // thorn
  get(colors, mask, 0xD5);   // ~O 
  get(colors, mask, 0xF5);   // ~o 
  get(colors, mask, 0xD8);   // /O 
  get(colors, mask, 0xF8);   // /o 
  get(colors, mask, 0xD9);   // `U 
  get(colors, mask, 0xF9);   // `u 
  get(colors, mask, 0xDA);   // u 
  get(colors, mask, 0xFA);   // u 
  get(colors, mask, 0xDD);   // Y 
  get(colors, mask, 0xFD);   // y 
  get(colors, mask, 0xDE);   // |
  get(colors, mask, 0xFE);   // |
  get(colors, mask, 0xFF);   // "y
  
  get(colors, mask, 0x11A);  // inv^ E
  get(colors, mask, 0x11B);  // inv^ e
  get(colors, mask, 0x160);  // inv^ S
  get(colors, mask, 0x161);  // inv^ s
  get(colors, mask, 0x10C);  // inv^ C
  get(colors, mask, 0x10D);  // inv^ c
  get(colors, mask, 0x158);  // inv^ R
  get(colors, mask, 0x159);  // inv^ r
  get(colors, mask, 0x16E);  // U
  get(colors, mask, 0x16F);  // u
  get(colors, mask, 0x102);  // A with lower halve circle above
  get(colors, mask, 0x103);  // a with lower halve circle above
  get(colors, mask, 0x21A);  // T with comma below
  get(colors, mask, 0x21B);  // t with comma below
  get(colors, mask, 0x218);  // S with comma below
  get(colors, mask, 0x219);  // s with comma below
  get(colors, mask, 0x17D);  // inv^ Z
  get(colors, mask, 0x17E);  // inv^ z

  fclose(outp);
}