1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 *
12 * TXT font file reader.
13 *
14 * See readme.txt for copyright information.
15 */
16 #include <stdio.h>
17 #include <string.h>
18 #include "allegro.h"
19 #include "allegro/internal/aintern.h"
20
21
22
23
24 /* load_txt_font:
25 * Loads a scripted font.
26 */
load_txt_font(AL_CONST char * filename,RGB * pal,void * param)27 FONT *load_txt_font(AL_CONST char *filename, RGB *pal, void *param)
28 {
29 char buf[1024], *font_str, *start_str = 0, *end_str = 0;
30 char font_filename[1024];
31 FONT *f, *f2, *f3, *f4;
32 PACKFILE *pack;
33 int begin, end, glyph_pos=32;
34
35 pack = pack_fopen(filename, F_READ);
36 if (!pack)
37 return NULL;
38
39 f = f2 = f3 = f4 = NULL;
40
41 while(pack_fgets(buf, sizeof(buf)-1, pack)) {
42 font_str = strtok(buf, " \t");
43 if (font_str)
44 start_str = strtok(0, " \t");
45 if (start_str)
46 end_str = strtok(0, " \t");
47
48 if (!font_str || !start_str) {
49 if (f)
50 destroy_font(f);
51 if (f2)
52 destroy_font(f2);
53
54 pack_fclose(pack);
55
56 return NULL;
57 }
58
59 if(font_str[0] == '-')
60 font_str[0] = '\0';
61
62 begin = strtol(start_str, 0, 0);
63
64 if (end_str)
65 end = strtol(end_str, 0, 0);
66 else
67 end = -1;
68
69 if(begin <= 0 || (end > 0 && end < begin)) {
70 if (f)
71 destroy_font(f);
72 if (f2)
73 destroy_font(f2);
74
75 pack_fclose(pack);
76
77 return NULL;
78 }
79
80 /* Load the font that needs to be merged with the current font */
81 if (font_str[0]) {
82 if (f2)
83 destroy_font(f2);
84 if (exists(font_str)) {
85 f2 = load_font(font_str, pal, param);
86 } else if (is_relative_filename(font_str)) {
87 replace_filename(font_filename, filename, font_str,
88 sizeof(font_filename));
89 f2 = load_font(font_filename, pal, param);
90 }
91 else {
92 f2 = NULL;
93 }
94 if (f2)
95 glyph_pos=get_font_range_begin(f2, -1);
96 }
97
98 if (!f2) {
99 if (f)
100 destroy_font(f);
101 pack_fclose(pack);
102 return NULL;
103 }
104
105 if (end == -1)
106 end = begin + get_font_range_end(f2,-1) - glyph_pos;
107
108 /* transpose the font to the range given in the .txt file */
109 f4=extract_font_range(f2,glyph_pos,glyph_pos + (end - begin));
110
111 if (f4 && (begin != glyph_pos)) {
112 transpose_font(f4, begin - glyph_pos);
113 }
114 glyph_pos += (end - begin) + 1;
115
116 /* FIXME: More efficient way than to repeatedely merge into a new font? */
117 if (f && f4) {
118 f3 = f;
119 f = merge_fonts(f4, f3);
120 destroy_font(f4);
121 destroy_font(f3);
122 }
123 else {
124 f = f4;
125 }
126 f3=f4=NULL;
127 }
128 if (f2)
129 destroy_font(f2);
130
131 pack_fclose(pack);
132 return f;
133 }
134