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
|
/* BOGL - Ben's Own Graphics Library.
Written by Ben Pfaff <pfaffben@debian.org>.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA. */
#define _GNU_SOURCE 1
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bogl.h"
#include "boglP.h"
#include "bogl-font.h"
#define INDEX_BITS 8
#define INDEX_MASK ((1<<INDEX_BITS)-1)
struct bogl_glyph
{
unsigned long char_width; /* ((wc & ~index_mask) | width) */
struct bogl_glyph *next;
u_int32_t *content; /* (height * ((width + 31) / 32)) values */
};
/* Returns nonzero iff LINE begins with PREFIX. */
static inline int
matches_prefix (const char *line, const char *prefix)
{
return !strncmp (line, prefix, strlen (prefix));
}
/* Reads a .bdf font file in FILENAME into a struct bogl_font, which
is returned. On failure, returns NULL, in which case an error
message can be retrieved using bogl_error(). */
struct bogl_font *
bogl_read_bdf (char *filename)
{
char *font_name;
int font_height;
struct bogl_glyph *font_glyphs[1<<INDEX_BITS];
/* Line buffer, current buffer size, and line number in input file. */
char *line;
size_t line_size;
int ln = 0;
/* Number of characters in font, theoretically, and the number that
we've seen. These can be different at the end because there may
be named characters in the font that aren't encoded into
character set. */
int n_chars;
int char_index;
/* Font ascent and descent in pixels. */
int ascent;
int descent;
/* Character to substitute when a character lacks a glyph of its
own. Normally the space character. */
int default_char;
/* .bdf file. */
FILE *file;
/* Read a line from FILE. Returns nonzero if successful, reports an
error message using bogl_fail() if not. Strips trailing
whitespace from input line. */
int read_line (void)
{
ssize_t len;
ln++;
len = getline (&line, &line_size, file);
if (len == -1)
{
if (ferror (file))
return bogl_fail ("reading %s: %s", filename, strerror (errno));
else
return bogl_fail ("%s:%d: unexpected end of file", filename, ln);
}
while (len > 0 && isspace ((unsigned char) line[len - 1]))
line[--len] = 0;
return 1;
}
/* Attempt to malloc NBYTES bytes. Sets a BOGL error message on
failure. Returns the result of the malloc() operation in any
case. */
void *
smalloc (size_t nbytes)
{
void *p = malloc (nbytes);
if (p == NULL)
bogl_fail ("%s:%d: virtual memory exhausted", filename, ln);
return p;
}
/* Parse header information. Returns nonzero if successful, reports
an error message using bogl_fail() if not.
On success will always: set ascent, descent, default_char,
font_height, and initialize font_glyphs. */
int read_bdf_header (void)
{
int bbx = 0, bby = 0, bbw = 0, bbh = 0;
ascent = descent = 0;
default_char = ' ';
for (;;)
{
if (!read_line ())
return 0;
if (matches_prefix (line, "FONT_ASCENT "))
ascent = atoi (line + strlen ("FONT_ASCENT "));
else if (matches_prefix (line, "FONT_DESCENT"))
descent = atoi (line + strlen ("FONT_DESCENT"));
else if (matches_prefix (line, "DEFAULT_CHAR "))
default_char = atoi (line + strlen ("DEFAULT_CHAR "));
else if (matches_prefix (line, "FONTBOUNDINGBOX "))
sscanf (line + strlen ("FONTBOUNDINGBOX "), "%d %d %d %d",
&bbw, &bbh, &bbx, &bby);
else if (matches_prefix (line, "CHARS "))
break;
}
n_chars = atoi (line + strlen ("CHARS "));
if (n_chars < 1)
return bogl_fail ("%s:%d: font contains no characters", filename, ln);
/* Adjust ascent and descent based on bounding box. */
if (-bby > descent)
descent = -bby;
if (bby + bbh > ascent)
ascent = bby + bbh;
font_height = ascent + descent;
if (font_height <= 0)
return bogl_fail ("%s:%d: font ascent (%d) + descent (%d) must be "
"positive", filename, ln, ascent, descent);
return 1;
}
/* Parse a character definition. Returns nonzero if successful,
reports an error message using bogl_fail() if not.
Sets the character data into font->content, font->offset, and
font->width as appropriate. Updates char_index. */
int read_character (void)
{
int encoding = -1;
int width = INT_MIN;
int bbx = 0, bby = 0, bbw = 0, bbh = 0;
/* Read everything for this character up to the bitmap. */
for (;;)
{
if (!read_line ())
return 0;
if (matches_prefix (line, "ENCODING "))
encoding = atoi (line + strlen ("ENCODING "));
else if (matches_prefix (line, "DWIDTH "))
width = atoi (line + strlen ("DWIDTH "));
else if (matches_prefix (line, "BBX "))
sscanf (line + strlen ("BBX "), "%d %d %d %d",
&bbw, &bbh, &bbx, &bby);
else if (matches_prefix (line, "BITMAP"))
break;
}
/* Adjust width based on bounding box. */
if (width == INT_MIN)
return bogl_fail ("%s:%d: character width not specified", filename, ln);
if (bbx < 0)
{
width -= bbx;
bbx = 0;
}
if (bbx + bbw > width)
width = bbx + bbw;
/* Put the character's encoding into the font table. */
if (encoding != -1 && width <= INDEX_MASK)
{
u_int32_t *content, *bm;
int i;
struct bogl_glyph **t;
t = &font_glyphs[encoding & INDEX_MASK];
while (*t &&
(((*t)->char_width & ~INDEX_MASK) != (encoding & ~INDEX_MASK)))
t = &((*t)->next);
if (*t)
return bogl_fail ("%s:%d: duplicate entry for character");
*t = smalloc (sizeof (struct bogl_glyph));
if (*t == NULL)
return 0;
content = smalloc (sizeof (u_int32_t) *
font_height * ((width + 31)/32));
if (content == NULL)
{
free (*t);
*t = NULL;
return 0;
}
memset(content, 0,
sizeof (u_int32_t) * font_height * ((width + 31)/32));
(*t)->char_width = (encoding & ~INDEX_MASK) | width;
(*t)->next = NULL;
(*t)->content = content;
/* Read the glyph bitmap. */
/* FIXME: This won't work for glyphs wider than 32 pixels. */
bm = content;
for (i = 0; ; i++) {
int row;
if (!read_line ())
return 0;
if (matches_prefix (line, "ENDCHAR"))
break;
if (encoding == -1)
continue;
row = font_height - descent - bby - bbh + i;
if (row < 0 || row >= font_height)
continue;
bm[row] = strtoul (line, NULL, 16) << (32 - 4 * strlen (line) - bbx);
}
}
/* Advance to next glyph. */
if (encoding != -1)
char_index++;
return 1;
}
void free_font_glyphs (void)
{
int i;
struct bogl_glyph *t, *tnext;
for (i = 0; i < (1<<INDEX_BITS); i++)
for (t = font_glyphs[i]; t != NULL; t = tnext)
{
tnext = t->next;
free (t);
}
}
{
int i;
for (i = 0; i < (1<<INDEX_BITS); i++)
font_glyphs[i] = NULL;
}
/* Open the file. */
file = fopen (filename, "r");
if (file == NULL)
{
bogl_fail ("opening %s: %s\n", filename, strerror (errno));
return NULL;
}
/* Make the font name based on the filename. This is probably not
the best thing to do, but it seems to work okay for now. */
{
unsigned char *cp;
font_name = strdup (filename);
if (font_name == NULL)
{
bogl_fail ("virtual memory exhausted");
goto lossage;
}
cp = (unsigned char *) strstr (font_name, ".bdf");
if (cp)
*cp = 0;
for (cp = (unsigned char *) font_name; *cp; cp++)
if (!isalnum (*cp))
*cp = '_';
}
line = NULL;
line_size = 0;
char_index = 0;
/* Read the header. */
if (!read_bdf_header ())
goto lossage;
/* Read all the glyphs. */
{
for (;;)
{
if (!read_line ())
goto lossage;
if (matches_prefix (line, "STARTCHAR "))
{
#if 0
if (char_index >= n_chars)
{
bogl_fail ("%s:%d: font contains more characters than "
"declared", filename, ln);
goto lossage;
}
#endif
if (!read_character ())
goto lossage;
}
else if (matches_prefix (line, "ENDFONT"))
break;
}
/* Make sure that we found at least one encoded character. */
if (!char_index)
{
bogl_fail ("%s:%d: font contains no encoded characters", filename, ln);
goto lossage;
}
}
/* Build the bogl_font structure. */
{
struct bogl_font *font = NULL;
int *offset = NULL;
int *index = NULL;
u_int32_t *content = NULL;
int index_size = 0, indexp = 0;
int content_size = 0, contentp = 0;
int i, j;
for (i = 0; i < (1<<INDEX_BITS); i++)
{
struct bogl_glyph *t = font_glyphs[i];
if (t != NULL)
{
for (; t != NULL; t = t->next)
{
index_size += 2;
content_size +=
font_height * (((t->char_width & INDEX_MASK) + 31) / 32);
}
++index_size;
}
}
font = smalloc (sizeof (struct bogl_font));
offset = smalloc (sizeof (int) * (1<<INDEX_BITS));
index = smalloc (sizeof (int) * index_size);
content = smalloc (sizeof (u_int32_t) * content_size);
if (font == NULL || offset == NULL || index == NULL || content == NULL)
{
free(font), free(offset), free(index), free(content);
goto lossage;
}
for (i = 0; i < (1<<INDEX_BITS); i++)
{
struct bogl_glyph *t = font_glyphs[i];
if (t == NULL)
offset[i] = index_size - 1;
else
{
offset[i] = indexp;
for (; t != NULL; t = t->next)
{
int n = font_height *
(((t->char_width & INDEX_MASK) + 31) / 32);
index[indexp++] = t->char_width;
index[indexp++] = contentp;
for (j = 0; j < n; j++)
content[contentp++] = t->content[j];
}
index[indexp++] = 0;
}
}
#if 0
if (indexp != index_size || contentp != content_size)
{
bogl_fail ("Internal error");
return NULL;
}
#endif
font->name = font_name;
font->height = font_height;
font->index_mask = INDEX_MASK;
font->index = index;
font->offset = offset;
font->content = content;
font->default_char = default_char;
/* Clean up. */
free_font_glyphs ();
fclose (file);
free (line);
return font;
}
lossage:
/* Come here on error. */
free_font_glyphs ();
free (line);
fclose (file);
return NULL;
}
/* Free FONT. */
void
bogl_free_font (struct bogl_font *font)
{
free (font->name);
free (font->offset);
free (font->content);
free (font);
}
|