Skip to content

Commit ba66b47

Browse files
committed
Implement a special extended charset mode for font loading
Will load 256-CHAR_OFFSET amount of glyphs instead of the regular 128-CHAR_OFFSET (extended ASCII, or whatever you may call it). Regular GUI fonts won't use this for now.
1 parent 9c421ff commit ba66b47

File tree

2 files changed

+66
-64
lines changed

2 files changed

+66
-64
lines changed

src/font.cc

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,16 @@ init_p_font(p_font *font, const char *font_path)
8585
FT_Open_Args args;
8686
FT_Stream stream;
8787
font->rw = SDL_RWFromFile(font_path, "rb");
88-
if (font->rw == NULL) {
88+
if (font->rw == NULL)
8989
tms_fatalf("Unable to open font file: %s", SDL_GetError());
90-
}
9190

9291
int position = SDL_RWtell(font->rw);
93-
if (position < 0) {
92+
if (position < 0)
9493
tms_fatalf("Unable to seek in font stream.");
95-
}
9694

9795
stream = (FT_Stream)malloc(sizeof(*stream));
98-
if (!stream) {
96+
if (!stream)
9997
tms_fatalf("Out of memory.");
100-
}
10198

10299
stream->read = RWread;
103100
stream->descriptor.pointer = font->rw;
@@ -111,14 +108,12 @@ init_p_font(p_font *font, const char *font_path)
111108

112109
error = FT_Open_Face(gui_spritesheet::ft, &args, 0, &font->face);
113110

114-
if (error) {
111+
if (error)
115112
tms_fatalf("Unable to open font: 0x%04X", error);
116-
}
117113

118114
error = FT_Set_Char_Size(font->face, 0, font->orig_height * 64, 0, 0);
119-
if (error) {
115+
if (error)
120116
tms_fatalf("Unable to set font size: 0x%04X", error);
121-
}
122117

123118
FT_Fixed scale = font->face->size->metrics.y_scale;
124119
font->ascent = FT_CEIL(FT_MulFix(font->face->ascender, scale));
@@ -128,10 +123,10 @@ init_p_font(p_font *font, const char *font_path)
128123

129124
if (!p_font::glyph_indices) {
130125
p_font::glyph_indices = new FT_UInt[128];
131-
for (int i=CHAR_OFFSET; i<128; ++i) {
126+
for (int i = CHAR_OFFSET; i < 128; ++i)
132127
p_font::glyph_indices[i] = FT_Get_Char_Index(font->face, i);
133-
}
134128
}
129+
font->glyph_indices_local = nullptr;
135130
}
136131

137132
p_font::p_font(const char *font_path, int height)
@@ -140,14 +135,17 @@ p_font::p_font(const char *font_path, int height)
140135
init_p_font(this, font_path);
141136
}
142137

143-
p_font::p_font(struct tms_atlas *atlas, const char *font_path, int height)
138+
139+
p_font::p_font(struct tms_atlas *atlas, const char *font_path, int height, bool extended_charset)
144140
: orig_height(height)
145141
{
146142
init_p_font(this, font_path);
147-
148143
FT_Error error;
149144

150-
//FT_Set_Pixel_Sizes(this->face, 0, this->height);
145+
this->glyph_indices_local = nullptr;
146+
this->extended = extended_charset;
147+
148+
// rest of atlas-loading code (duplicate of other ctor)
151149
FT_GlyphSlot slot = this->face->glyph;
152150
FT_Glyph_Metrics *metrics;
153151
FT_Glyph ft_glyph;
@@ -160,29 +158,33 @@ p_font::p_font(struct tms_atlas *atlas, const char *font_path, int height)
160158
int ft_glyph_top = 0;
161159
int ft_glyph_left = 0;
162160

163-
for (int i=CHAR_OFFSET; i<128; ++i) {
161+
if (extended_charset) {
162+
this->glyph_indices_local = new FT_UInt[256];
163+
for (int i = CHAR_OFFSET; i < 256; ++i) {
164+
tms_infof("Getting glyph index for char code %d", i);
165+
this->glyph_indices_local[i] = FT_Get_Char_Index(this->face, i);
166+
}
167+
}
168+
169+
for (int i = CHAR_OFFSET; i < (extended_charset ? 256 : 128); ++i) {
164170
cur_glyph = this->get_glyph(i);
165171

166-
if (FT_Load_Glyph(face, glyph_indices[i], FT_LOAD_NO_BITMAP)) {
172+
FT_UInt idx = this->glyph_indices_local ? this->glyph_indices_local[i] : glyph_indices[i];
173+
174+
if (FT_Load_Glyph(face, idx, FT_LOAD_NO_BITMAP))
167175
tms_fatalf("Loading character %c failed.", i);
168-
continue;
169-
}
170176

171177
/* Render outline */
172178
FT_Stroker stroker;
173179
FT_BitmapGlyph ft_bitmap_glyph;
174180
error = FT_Stroker_New(gui_spritesheet::ft, &stroker);
175181

176-
if (error) {
182+
if (error)
177183
tms_fatalf("Error creating stroker");
178-
continue;
179-
}
180184

181185
error = FT_Get_Glyph(this->face->glyph, &ft_glyph);
182-
if (error) {
186+
if (error)
183187
tms_fatalf("Error getting glyph");
184-
continue;
185-
}
186188

187189
FT_Stroker_Set(stroker,
188190
this->height * OUTLINE_MODIFIER,
@@ -191,16 +193,12 @@ p_font::p_font(struct tms_atlas *atlas, const char *font_path, int height)
191193
0);
192194

193195
error = FT_Glyph_Stroke(&ft_glyph, stroker, 1);
194-
if (error) {
196+
if (error)
195197
tms_fatalf("Error Applying stroke to glyph");
196-
continue;
197-
}
198198

199199
error = FT_Glyph_To_Bitmap(&ft_glyph, FT_RENDER_MODE_NORMAL, 0, 1);
200-
if (error) {
200+
if (error)
201201
tms_fatalf("Error doing glyph to bitmap");
202-
continue;
203-
}
204202

205203
slot = this->face->glyph;
206204
metrics = &slot->metrics;
@@ -225,10 +223,8 @@ p_font::p_font(struct tms_atlas *atlas, const char *font_path, int height)
225223
memcpy(cur_glyph->m_outline_buf, ft_bitmap.buffer, ft_bitmap_width*ft_bitmap_rows);
226224

227225
/* Render bitmap */
228-
if (FT_Load_Glyph(face, glyph_indices[i], FT_LOAD_RENDER)) {
226+
if (FT_Load_Glyph(face, idx, FT_LOAD_RENDER))
229227
tms_fatalf("Loading character %c failed.", i);
230-
continue;
231-
}
232228

233229
slot = this->face->glyph;
234230
metrics = &slot->metrics;
@@ -247,20 +243,18 @@ p_font::p_font(struct tms_atlas *atlas, const char *font_path, int height)
247243
cur_glyph->bl = ft_glyph_left;
248244
cur_glyph->bt = ft_glyph_top;
249245

250-
if (FT_IS_SCALABLE(this->face)) {
251-
cur_glyph->minx = FT_FLOOR(metrics->horiBearingX);
252-
cur_glyph->maxx = cur_glyph->minx + FT_CEIL(metrics->width);
253-
cur_glyph->maxy = FT_FLOOR(metrics->horiBearingY);
254-
cur_glyph->miny = cur_glyph->maxy - FT_CEIL(metrics->height);
255-
cur_glyph->yoffset = this->ascent - cur_glyph->maxy;
256-
cur_glyph->advance = FT_CEIL(metrics->horiAdvance);
257-
cur_glyph->ax = cur_glyph->advance;
258-
//cur_glyph->ay = cur_glyph->miny;
259-
cur_glyph->index = glyph_indices[i];
260-
//tms_fatalf("is scalable");
261-
} else {
262-
tms_fatalf("is not scalable");
263-
}
246+
if (!FT_IS_SCALABLE(this->face))
247+
tms_fatalf("font is not scalable!");
248+
249+
cur_glyph->minx = FT_FLOOR(metrics->horiBearingX);
250+
cur_glyph->maxx = cur_glyph->minx + FT_CEIL(metrics->width);
251+
cur_glyph->maxy = FT_FLOOR(metrics->horiBearingY);
252+
cur_glyph->miny = cur_glyph->maxy - FT_CEIL(metrics->height);
253+
cur_glyph->yoffset = this->ascent - cur_glyph->maxy;
254+
cur_glyph->advance = FT_CEIL(metrics->horiAdvance);
255+
cur_glyph->ax = cur_glyph->advance;
256+
//cur_glyph->ay = cur_glyph->miny;
257+
cur_glyph->index = idx;
264258

265259
cur_glyph->m_sprite_buf = (unsigned char*)malloc(ft_bitmap_width*ft_bitmap_rows);
266260
memcpy(cur_glyph->m_sprite_buf, ft_bitmap.buffer, ft_bitmap_width*ft_bitmap_rows);
@@ -276,39 +270,45 @@ p_font::p_font(struct tms_atlas *atlas, const char *font_path, int height)
276270
}
277271
}
278272

273+
p_font::p_font(struct tms_atlas *atlas, const char *font_path, int height)
274+
: orig_height(height)
275+
{
276+
// call the other ctor
277+
p_font(atlas, font_path, height, false);
278+
}
279+
279280
p_font::~p_font()
280281
{
281-
if (this->rw) {
282+
if (this->rw)
282283
this->rw->close(rw);
283-
}
284284

285-
//FT_Done_Face(this->face);
285+
if (this->glyph_indices_local) {
286+
delete [] this->glyph_indices_local;
287+
this->glyph_indices_local = nullptr;
288+
}
286289
}
287290

288291
static struct glyph* nl_glyph = 0;
289292

290293
struct glyph*
291294
p_font::get_glyph(int c)
292295
{
293-
if (c >= CHAR_OFFSET && c <= 128) {
296+
if (c >= CHAR_OFFSET && c <= (extended ? 256 : 128)) {
294297
return &this->glyphs[c-CHAR_OFFSET];
295298
}
296299

297300
switch (c) {
298-
case '\n':
299-
{
300-
if (!nl_glyph) {
301-
nl_glyph = (struct glyph*)calloc(1, sizeof(struct glyph));
302-
nl_glyph->newline = true;
303-
}
304-
305-
return nl_glyph;
301+
case '\n': {
302+
if (!nl_glyph) {
303+
nl_glyph = (struct glyph*)calloc(1, sizeof(struct glyph));
304+
nl_glyph->newline = true;
306305
}
307-
break;
306+
307+
return nl_glyph;
308+
} break;
308309
}
309310

310311
return 0;
311-
312312
}
313313

314314
void

src/font.hh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ class p_font
6969
public:
7070
int orig_height;
7171

72-
glyph glyphs[128-CHAR_OFFSET];
72+
glyph glyphs[256-CHAR_OFFSET];
73+
bool extended;
7374
FT_Face face;
7475
SDL_RWops *rw;
75-
76+
FT_UInt *glyph_indices_local;
7677
p_font(const char *font_path, int height);
7778
p_font(struct tms_atlas *atlas, const char *font_path, int height);
79+
p_font(struct tms_atlas *atlas, const char *font_path, int height, bool extended_charset);
7880
~p_font();
7981

8082
int height;

0 commit comments

Comments
 (0)