File: sdltext.c

package info (click to toggle)
tworld 1.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 6,620 kB
  • ctags: 1,407
  • sloc: ansic: 12,442; perl: 2,465; makefile: 191; sh: 16
file content (601 lines) | stat: -rw-r--r-- 14,365 bytes parent folder | download | duplicates (4)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/* sdltext.c: Font-rendering functions for SDL.
 *
 * Copyright (C) 2001-2006 by Brian Raiter, under the GNU General Public
 * License. No warranty. See COPYING for details.
 */

#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<ctype.h>
#include	"SDL.h"
#include	"sdlgen.h"
#include	"../err.h"

/* Accept a bitmap as an 8-bit SDL surface and from it extract the
 * glyphs of a font. (See the documentation included in the Tile World
 * distribution for specifics regarding the bitmap layout.)
 */
static int makefontfromsurface(fontinfo *pf, SDL_Surface *surface)
{
    char		brk[267];
    unsigned char      *p;
    unsigned char      *dest;
    Uint8		foregnd, bkgnd;
    int			pitch, wsum;
    int			count, ch;
    int			x, y, x0, y0, w;

    if (surface->format->BytesPerPixel != 1)
	return FALSE;

    if (SDL_MUSTLOCK(surface))
	SDL_LockSurface(surface);

    pitch = surface->pitch;
    p = surface->pixels;
    foregnd = p[0];
    bkgnd = p[pitch];
    for (y = 1, p += pitch ; y < surface->h && *p == bkgnd ; ++y, p += pitch) ;
    pf->h = y - 1;

    wsum = 0;
    ch = 32;
    memset(pf->w, 0, sizeof pf->w);
    memset(brk, 0, sizeof brk);
    for (y = 0 ; y + pf->h < surface->h && ch < 256 ; y += pf->h + 1) {
	p = surface->pixels;
	p += y * pitch;
	x0 = 1;
	for (x = 1 ; x < surface->w ; ++x) {
	    if (p[x] == bkgnd)
		continue;
	    w = x - x0;
	    x0 = x + 1;
	    pf->w[ch] = w;
	    wsum += w;
	    ++ch;
	    if (ch == 127)
		ch = 144;
	    else if (ch == 154)
		ch = 160;
	    else if (ch == 256)
		break;
	}
	brk[ch] = 1;
    }

    count = ch;
    if (!(pf->memory = calloc(wsum, pf->h)))
	memerrexit();

    x0 = 1;
    y0 = 1;
    dest = pf->memory;
    for (ch = 0 ; ch < 256 ; ++ch) {
	pf->bits[ch] = dest;
	if (pf->w[ch] == 0)
	    continue;
	if (brk[ch]) {
	    x0 = 1;
	    y0 += pf->h + 1;
	}
	p = surface->pixels;
	p += y0 * pitch + x0;
	for (y = 0 ; y < pf->h ; ++y, p += pitch)
	    for (x = 0 ; x < pf->w[ch] ; ++x, ++dest)
		*dest = p[x] == bkgnd ? 0 : p[x] == foregnd ? 2 : 1;
	x0 += pf->w[ch] + 1;
    }

    if (SDL_MUSTLOCK(surface))
	SDL_UnlockSurface(surface);

    return TRUE;
}

/* Given a text and a maximum horizontal space to occupy, return
 * the amount of vertial space needed to render the entire text with
 * word-wrapping.
 */
static int measuremltext(unsigned char const *text, int len, int maxwidth)
{
    int	brk, w, h, n;

    if (len < 0)
	len = strlen((char const*)text);
    h = 0;
    brk = 0;
    for (n = 0, w = 0 ; n < len ; ++n) {
	w += sdlg.font.w[text[n]];
	if (isspace(text[n])) {
	    brk = w;
	} else if (w > maxwidth) {
	    h += sdlg.font.h;
	    if (brk) {
		w -= brk;
		brk = 0;
	    } else {
		w = sdlg.font.w[text[n]];
		brk = 0;
	    }
	}
    }
    if (w)
	h += sdlg.font.h;
    return h;
}

/*
 * Render a single line of pixels of the given text to a locked
 * surface at scanline. w specifies the total number of pixels to
 * render. (Any pixels remaining after the last glyph has been
 * rendered are set to the background color.) y specifies the vertical
 * coordinate of the line to render relative to the font glyphs. A
 * separate function is supplied for each possible surface depth.
 */

static void *drawtextscanline8(Uint8 *scanline, int w, int y, Uint32 *clr,
			       unsigned char const *text, int len)
{
    unsigned char const	       *glyph;
    int				n, x;

    for (n = 0 ; n < len ; ++n) {
	glyph = sdlg.font.bits[text[n]];
	glyph += y * sdlg.font.w[text[n]];
	for (x = 0 ; w && x < sdlg.font.w[text[n]] ; ++x, --w)
	    scanline[x] = (Uint8)clr[glyph[x]];
	scanline += x;
    }
    while (w--)
	*scanline++ = (Uint8)clr[0];
    return scanline;
}

static void *drawtextscanline16(Uint16 *scanline, int w, int y, Uint32 *clr,
				unsigned char const *text, int len)
{
    unsigned char const	       *glyph;
    int				n, x;

    for (n = 0 ; n < len ; ++n) {
	glyph = sdlg.font.bits[text[n]];
	glyph += y * sdlg.font.w[text[n]];
	for (x = 0 ; w && x < sdlg.font.w[text[n]] ; ++x, --w)
	    scanline[x] = (Uint16)clr[glyph[x]];
	scanline += x;
    }
    while (w--)
	*scanline++ = (Uint16)clr[0];
    return scanline;
}

static void *drawtextscanline24(Uint8 *scanline, int w, int y, Uint32 *clr,
				unsigned char const *text, int len)
{
    unsigned char const	       *glyph;
    Uint32			c;
    int				n, x;

    for (n = 0 ; n < len ; ++n) {
	glyph = sdlg.font.bits[text[n]];
	glyph += y * sdlg.font.w[text[n]];
	for (x = 0 ; w && x < sdlg.font.w[text[n]] ; ++x, --w) {
	    c = clr[glyph[x]];
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
	    *scanline++ = (Uint8)(c >> 16);
	    *scanline++ = (Uint8)(c >> 8);
	    *scanline++ = (Uint8)c;
#else
	    *scanline++ = (Uint8)c;
	    *scanline++ = (Uint8)(c >> 8);
	    *scanline++ = (Uint8)(c >> 16);
#endif
	}
    }
    c = clr[0];
    while (w--) {
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
	*scanline++ = (Uint8)(c >> 16);
	*scanline++ = (Uint8)(c >> 8);
	*scanline++ = (Uint8)c;
#else
	*scanline++ = (Uint8)c;
	*scanline++ = (Uint8)(c >> 8);
	*scanline++ = (Uint8)(c >> 16);
#endif
    }
    return scanline;
}

static void *drawtextscanline32(Uint32 *scanline, int w, int y, Uint32 *clr,
				unsigned char const *text, int len)
{
    unsigned char const	       *glyph;
    int				n, x;

    for (n = 0 ; n < len ; ++n) {
	glyph = sdlg.font.bits[text[n]];
	glyph += y * sdlg.font.w[text[n]];
	for (x = 0 ; w && x < sdlg.font.w[text[n]] ; ++x, --w)
	    scanline[x] = clr[glyph[x]];
	scanline += x;
    }
    while (w--)
	*scanline++ = clr[0];
    return scanline;
}

/*
 * The main font-rendering functions.
 */

/* Draw a single line of text to the screen at the position given by
 * rect. The bitflags in the final argument control the placement of
 * text within rect and what colors to use.
 */
static void drawtext(SDL_Rect *rect, unsigned char const *text,
		     int len, int flags)
{
    Uint32     *clr;
    void       *p;
    void       *q;
    int		l, r;
    int		pitch, bpp, n, w, y;

    if (len < 0)
	len = text ? strlen((char const*)text) : 0;

    w = 0;
    for (n = 0 ; n < len ; ++n)
	w += sdlg.font.w[text[n]];
    if (flags & PT_CALCSIZE) {
	rect->h = sdlg.font.h;
	rect->w = w;
	return;
    }
    if (w >= rect->w) {
	w = rect->w;
	l = r = 0;
    } else if (flags & PT_RIGHT) {
	l = rect->w - w;
	r = 0;
    } else if (flags & PT_CENTER) {
	l = (rect->w - w) / 2;
	r = (rect->w - w) - l;
    } else {
	l = 0;
	r = rect->w - w;
    }

    if (flags & PT_DIM)
	clr = sdlg.dimtextclr.c;
    else if (flags & PT_HILIGHT)
	clr = sdlg.hilightclr.c;
    else
	clr = sdlg.textclr.c;

    pitch = sdlg.screen->pitch;
    bpp = sdlg.screen->format->BytesPerPixel;
    p = (unsigned char*)sdlg.screen->pixels + rect->y * pitch + rect->x * bpp;
    for (y = 0 ; y < sdlg.font.h && y < rect->h ; ++y) {
	switch (bpp) {
	  case 1:
	    q = drawtextscanline8(p, l, y, clr, NULL, 0);
	    q = drawtextscanline8(q, w, y, clr, text, len);
	    q = drawtextscanline8(q, r, y, clr, NULL, 0);
	    break;
	  case 2:
	    q = drawtextscanline16(p, l, y, clr, NULL, 0);
	    q = drawtextscanline16(q, w, y, clr, text, len);
	    q = drawtextscanline16(q, r, y, clr, NULL, 0);
	    break;
	  case 3:
	    q = drawtextscanline24(p, l, y, clr, NULL, 0);
	    q = drawtextscanline24(q, w, y, clr, text, len);
	    q = drawtextscanline24(q, r, y, clr, NULL, 0);
	    break;
	  case 4:
	    q = drawtextscanline32(p, l, y, clr, NULL, 0);
	    q = drawtextscanline32(q, w, y, clr, text, len);
	    q = drawtextscanline32(q, r, y, clr, NULL, 0);
	    break;
	}
	p = (unsigned char*)p + pitch;
    }

    if (flags & PT_UPDATERECT) {
	rect->y += y;
	rect->h -= y;
    }
}

/* Draw one or more lines of text to the screen at the position given by
 * rect. The text is broken up on whitespace whenever possible.
 */
static void drawmultilinetext(SDL_Rect *rect, unsigned char const *text,
			      int len, int flags)
{
    SDL_Rect	area;
    int		index, brkw, brkn;
    int		w, n;

    if (flags & PT_CALCSIZE) {
	rect->h = measuremltext(text, len, rect->w);
	return;
    }

    if (len < 0)
	len = strlen((char const*)text);

    area = *rect;
    brkw = brkn = 0;
    index = 0;
    for (n = 0, w = 0 ; n < len ; ++n) {
	w += sdlg.font.w[text[n]];
	if (isspace(text[n])) {
	    brkn = n;
	    brkw = w;
	} else if (w > rect->w) {
	    if (brkw) {
		drawtext(&area, text + index, brkn - index,
				 flags | PT_UPDATERECT);
		index = brkn + 1;
		w -= brkw;
	    } else {
		drawtext(&area, text + index, n - index,
				 flags | PT_UPDATERECT);
		index = n;
		w = sdlg.font.w[text[n]];
	    }
	    brkw = 0;
	}
    }
    if (w)
	drawtext(&area, text + index, len - index, flags | PT_UPDATERECT);
    if (flags & PT_UPDATERECT) {
	*rect = area;
    } else {
	while (area.h)
	    drawtext(&area, NULL, 0, PT_UPDATERECT);
    }
}

/*
 * The exported functions.
 */

/* Render a string of text.
 */
static void _puttext(SDL_Rect *rect, char const *text, int len, int flags)
{
    if (!sdlg.font.h)
	die("no font available! (how did I get this far?)");

    if (len < 0)
	len = text ? strlen(text) : 0;

    if (SDL_MUSTLOCK(sdlg.screen))
	SDL_LockSurface(sdlg.screen);

    if (flags & PT_MULTILINE)
	drawmultilinetext(rect, (unsigned char const*)text, len, flags);
    else
	drawtext(rect, (unsigned char const*)text, len, flags);

    if (SDL_MUSTLOCK(sdlg.screen))
	SDL_UnlockSurface(sdlg.screen);
}

/* Lay out the columns of the given table so that the entire table
 * fits within area (horizontally; no attempt is made to make it fit
 * vertically). Return an array of rectangles, one per column. This
 * function is essentially the same algorithm used within printtable()
 * in tworld.c
 */
static SDL_Rect *_measuretable(SDL_Rect const *area, tablespec const *table)
{
    SDL_Rect		       *colsizes;
    unsigned char const	       *p;
    int				sep, mlindex, mlwidth, diff;
    int				i, j, n, i0, c, w, x;

    if (!(colsizes = malloc(table->cols * sizeof *colsizes)))
	memerrexit();
    for (i = 0 ; i < table->cols ; ++i) {
	colsizes[i].x = 0;
	colsizes[i].y = area->y;
	colsizes[i].w = 0;
	colsizes[i].h = area->h;
    }

    mlindex = -1;
    mlwidth = 0;
    n = 0;
    for (j = 0 ; j < table->rows ; ++j) {
	for (i = 0 ; i < table->cols ; ++n) {
	    c = table->items[n][0] - '0';
	    if (c == 1) {
		w = 0;
		p = (unsigned char const*)table->items[n];
		for (p += 2 ; *p ; ++p)
		    w += sdlg.font.w[*p];
		if (table->items[n][1] == '!') {
		    if (w > mlwidth || mlindex != i)
			mlwidth = w;
		    mlindex = i;
		} else {
		    if (w > colsizes[i].w)
			colsizes[i].w = w;
		}
	    }
	    i += c;
	}
    }

    sep = sdlg.font.w[' '] * table->sep;
    w = -sep;
    for (i = 0 ; i < table->cols ; ++i)
	w += colsizes[i].w + sep;
    diff = area->w - w;
    if (diff < 0 && table->collapse >= 0) {
	w = -diff;
	if (colsizes[table->collapse].w < w)
	    w = colsizes[table->collapse].w - sdlg.font.w[' '];
	colsizes[table->collapse].w -= w;
	diff += w;
    }

    if (diff > 0) {
	n = 0;
	for (j = 0 ; j < table->rows && diff > 0 ; ++j) {
	    for (i = 0 ; i < table->cols ; ++n) {
		c = table->items[n][0] - '0';
		if (c > 1 && table->items[n][1] != '!') {
		    w = sep;
		    p = (unsigned char const*)table->items[n];
		    for (p += 2 ; *p ; ++p)
			w += sdlg.font.w[*p];
		    for (i0 = i ; i0 < i + c ; ++i0)
			w -= colsizes[i0].w + sep;
		    if (w > 0) {
			if (table->collapse >= i && table->collapse < i + c)
			    i0 = table->collapse;
			else if (mlindex >= i && mlindex < i + c)
			    i0 = mlindex;
			else
			    i0 = i + c - 1;
			if (w > diff)
			    w = diff;
			colsizes[i0].w += w;
			diff -= w;
			if (diff == 0)
			    break;
		    }
		}
		i += c;
	    }
	}
    }
    if (diff > 0 && mlindex >= 0 && colsizes[mlindex].w < mlwidth) {
	mlwidth -= colsizes[mlindex].w;
	w = mlwidth < diff ? mlwidth : diff;
	colsizes[mlindex].w += w;
	diff -= w;
    }

    x = 0;
    for (i = 0 ; i < table->cols && x < area->w ; ++i) {
	colsizes[i].x = area->x + x;
	x += colsizes[i].w + sep;
	if (x >= area->w)
	    colsizes[i].w = area->x + area->w - colsizes[i].x;
    }
    for ( ; i < table->cols ; ++i) {
	colsizes[i].x = area->x + area->w;
	colsizes[i].w = 0;
    }

    return colsizes;
}

/* Render a single row of a table to the screen, using cols to locate
 * the entries in the individual columns.
 */
static int _drawtablerow(tablespec const *table, SDL_Rect *cols,
			 int *row, int flags)
{
    SDL_Rect			rect;
    unsigned char const	       *p;
    int				c, f, n, i, y;

    if (!cols) {
	for (i = 0 ; i < table->cols ; i += table->items[(*row)++][0] - '0') ;
	return TRUE;
    }

    if (SDL_MUSTLOCK(sdlg.screen))
	SDL_LockSurface(sdlg.screen);

    y = cols[0].y;
    n = *row;
    for (i = 0 ; i < table->cols ; ++n) {
	p = (unsigned char const*)table->items[n];
	c = p[0] - '0';
	rect = cols[i];
	i += c;
	if (c > 1)
	    rect.w = cols[i - 1].x + cols[i - 1].w - rect.x;
	f = flags | PT_UPDATERECT;
	if (p[1] == '+')
	    f |= PT_RIGHT;
	else if (p[1] == '.')
	    f |= PT_CENTER;
	if (p[1] == '!')
	    drawmultilinetext(&rect, p + 2, -1, f);
	else
	    drawtext(&rect, p + 2, -1, f);
	if (rect.y > y)
	    y = rect.y;
    }

    if (SDL_MUSTLOCK(sdlg.screen))
	SDL_UnlockSurface(sdlg.screen);

    *row = n;
    for (i = 0 ; i < table->cols ; ++i) {
	cols[i].h -= y - cols[i].y;
	cols[i].y = y;
    }

    return TRUE;
}

/* Free the resources associated with a font.
 */
void freefont(void)
{
    if (sdlg.font.h) {
	free(sdlg.font.memory);
	sdlg.font.memory = NULL;
	sdlg.font.h = 0;
    }
}

/* Load the font contained in the given bitmap file. Error messages
 * will be displayed if complain is TRUE. The return value is TRUE if
 * the font was successfully retrieved.
 */
int loadfontfromfile(char const *filename, int complain)
{
    SDL_Surface	       *bmp;
    fontinfo		font;

    bmp = SDL_LoadBMP(filename);
    if (!bmp) {
	if (complain)
	    errmsg(filename, "can't load font bitmap: %s", SDL_GetError());
	return FALSE;
    }
    if (!makefontfromsurface(&font, bmp)) {
	if (complain)
	    errmsg(filename, "invalid font file");
	return FALSE;
    }
    SDL_FreeSurface(bmp);
    freefont();
    sdlg.font = font;
    return TRUE;
}

/* Initialize the module.
 */
int _sdltextinitialize(void)
{
    sdlg.font.h = 0;
    sdlg.puttextfunc = _puttext;
    sdlg.measuretablefunc = _measuretable;
    sdlg.drawtablerowfunc = _drawtablerow;
    return TRUE;
}