[go: up one dir, main page]

File: text.c

package info (click to toggle)
svgalib 1%3A1.4.3-33
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,012 kB
  • sloc: ansic: 60,381; makefile: 1,138; asm: 630; sh: 86; perl: 54; pascal: 49
file content (328 lines) | stat: -rw-r--r-- 6,609 bytes parent folder | download | duplicates (3)
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
/* Framebuffer Graphics Libary for Linux, Copyright 1993 Harm Hanemaayer */
/* text.c       Text writing and fonts */


#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <vga.h>
#include "inlstring.h"		/* include inline string operations */

#include "vgagl.h"
#include "def.h"



/* Text/font functions */

static int font_width = 8;
static int font_height = 8;
static char *font_address;
static int font_charactersize = 64;
static int font_writemode = WRITEMODE_OVERWRITE;
static int compressed_font_bg = 0;
static int compressed_font_fg = 15;

static void writecompressed(int x, int y, int n, unsigned char *s);


void gl_colorfont(int fw, int fh, int fg, void *_dp)
{
    uchar *dp = _dp;
    int i;
    i = fw * fh * 256;
    switch (BYTESPERPIXEL) {
    case 1:
	while (i > 0) {
	    if (*dp)
		*dp = fg;
	    dp++;
	    i--;
	}
	break;
    case 2:
	while (i > 0) {
	    if (*(ushort *) dp)
		*(ushort *) dp = fg;
	    dp += 2;
	    i--;
	}
	break;
    case 3:
	while (i > 0) {
	    if (*(ushort *) dp || *(dp + 2)) {
		*(ushort *) dp = fg;
		*(dp + 2) = fg >> 16;
	    }
	    dp += 3;
	    i--;
	}
	break;
    case 4:
	while (i > 0) {
	    if (*(int *) dp)
		*(int *) dp = fg;
	    dp += 4;
	    i--;
	}
	break;

    }
}

void gl_setfont(int fw, int fh, void *font)
{
    font_width = fw;
    font_height = fh;
    font_charactersize = font_width * font_height * BYTESPERPIXEL;
    font_address = font;
}

void gl_setwritemode(int m)
{
    font_writemode = m;
}

void gl_writen(int x, int y, int n, char *s)
{
/* clipping in putbox */
    int i;
    if (font_writemode & FONT_COMPRESSED) {
	writecompressed(x, y, n, s);
	return;
    }
    if (!(font_writemode & WRITEMODE_MASKED)) {
        for (i=0; i<n; i++) {
	    gl_putbox(x + i * font_width, y, font_width,
		      font_height, font_address +
		      (unsigned char) s[i] * font_charactersize);
	}
    } else {			/* masked write */
        for (i=0; i<n; i++) {
	    gl_putboxmask(x + i * font_width, y, font_width,
			  font_height, font_address +
			  (unsigned char) s[i] * font_charactersize);
	}
    }
}

void gl_write(int x, int y, char *s)
{
    int n;
    for (n=0; s[n]; n++);
    gl_writen(x, y, n, s);
}

#ifndef SVGA_AOUT

static int gl_nprintf(int sx, int sy, size_t bufs, const char *fmt, va_list args)
{
    unsigned char *buf;
    static int x = 0, y = 0, x_start = 0;
    int n;

    buf = alloca(bufs);
    n = vsnprintf(buf, bufs, fmt, args);
    if (n < 0)
	return n; /* buffer did not suffice, return and retry */

    if ((sx >= 0) && (sy >= 0)) {
	x = x_start = sx;
	y = sy;
    }
	
    for (; *buf; buf++)
	switch (*buf) {
	case '\a':		/* badly implemented */
	    fputc('\a', stdout);
	    fflush(stdout);
	    break;
	case '\b':
	    x -= font_width;
	    if (x < x_start) {
		x = WIDTH + (x_start % font_width);
		while(x + font_width > WIDTH)
		    x -= font_width;
		if (y >= font_height)
		    y -= font_height;
	    }
	    break;
	case '\n':
	  newline:
	    y += font_height;
	    if (y + font_height > HEIGHT)
		y %= font_height;
	case '\r':
	    x = x_start;
	    break;
	case '\t':
	    x += ((TEXT_TABSIZE - ((x - x_start) / font_width) % TEXT_TABSIZE) * font_width);
	    goto chk_wrap;
	    break;
	case '\v':
	    y += font_height;
	    if (y + font_height > HEIGHT)
		y %= font_height;
	    break;
	default:
	    gl_writen(x, y, 1, buf);
	    x += font_width;
	  chk_wrap:
	    if (x + font_width > WIDTH)
		goto newline;
	}
    return n;
}

int gl_printf(int x, int y, const char *fmt, ...)
{
    size_t bufs = BUFSIZ;
    int result;
    va_list args;

    va_start(args, fmt);

    /* Loop until buffer size suffices */
    do {
	result = gl_nprintf(x, y, bufs, fmt, args);
	bufs <<= 1;
    } while(result < 0);
	
    va_end(args);
    return result;
}

#endif
void gl_expandfont(int fw, int fh, int fg, void *_f1, void *_f2)
{
/* Convert bit-per-pixel font to byte(s)-per-pixel font */
    uchar *f1 = _f1;
    uchar *f2 = _f2;
    int i, x, y, b = 0;		/* keep gcc happy with b = 0 - MW */

    for (i = 0; i < 256; i++) {
	for (y = 0; y < fh; y++)
	    for (x = 0; x < fw; x++) {
		if (x % 8 == 0)
		    b = *f1++;
		if (b & (128 >> (x % 8)))	/* pixel */
		    switch (BYTESPERPIXEL) {
		    case 1:
			*f2 = fg;
			f2++;
			break;
		    case 2:
			*(ushort *) f2 = fg;
			f2 += 2;
			break;
		    case 3:
			*(ushort *) f2 = fg;
			*(f2 + 2) = fg >> 16;
			f2 += 3;
			break;
		    case 4:
			*(uint *) f2 = fg;
			f2 += 4;
		} else		/* no pixel */
		    switch (BYTESPERPIXEL) {
		    case 1:
			*f2 = 0;
			f2++;
			break;
		    case 2:
			*(ushort *) f2 = 0;
			f2 += 2;
			break;
		    case 3:
			*(ushort *) f2 = 0;
			*(f2 + 2) = 0;
			f2 += 3;
			break;
		    case 4:
			*(uint *) f2 = 0;
			f2 += 4;
		    }
	    }
    }
}

static void expandcharacter(int bg, int fg, int c, unsigned char *bitmap)
{
    int x, y;
    unsigned char *font;
    int b = 0;			/* keep gcc happy with b = 0 - MW */

    font = font_address + c * (font_height * ((font_width + 7) / 8));

    for (y = 0; y < font_height; y++)
	for (x = 0; x < font_width; x++) {
	    if (x % 8 == 0)
		b = *font++;
	    if (b & (128 >> (x % 8)))	/* pixel */
		switch (BYTESPERPIXEL) {
		case 1:
		    *bitmap = fg;
		    bitmap++;
		    break;
		case 2:
		    *(ushort *) bitmap = fg;
		    bitmap += 2;
		    break;
		case 3:
		    *(ushort *) bitmap = fg;
		    *(bitmap + 2) = fg >> 16;
		    bitmap += 3;
		    break;
		case 4:
		    *(uint *) bitmap = fg;
		    bitmap += 4;
	    } else		/* background pixel */
		switch (BYTESPERPIXEL) {
		case 1:
		    *bitmap = bg;
		    bitmap++;
		    break;
		case 2:
		    *(ushort *) bitmap = bg;
		    bitmap += 2;
		    break;
		case 3:
		    *(ushort *) bitmap = bg;
		    *(bitmap + 2) = bg >> 16;
		    bitmap += 3;
		    break;
		case 4:
		    *(uint *) bitmap = bg;
		    bitmap += 4;
		}
	}
}

/* Write using compressed font. */

static void writecompressed(int x, int y, int n, unsigned char *s)
{
    unsigned char *bitmap;
    int i;
    bitmap = alloca(font_width * font_height * BYTESPERPIXEL);
    if (!(font_writemode & WRITEMODE_MASKED)) {
        for (i=0; i<n; i++) {
	    expandcharacter(compressed_font_bg,
			    compressed_font_fg, s[i], bitmap);
	    gl_putbox(x + i * font_width, y, font_width,
		      font_height, bitmap);
	}
    } else {			/* masked write */
        for (i=0; i<n; i++) {
	    expandcharacter(0, compressed_font_fg, s[i], bitmap);
	    gl_putboxmask(x + i * font_width, y, font_width,
			  font_height, bitmap);
	}
    }
}

void gl_setfontcolors(int bg, int fg)
{
    compressed_font_bg = bg;
    compressed_font_fg = fg;
}