Debian Codegraph @

src/bitmap/bdfread.c

raw
1 /************************************************************************
2 Copyright 1989 by Digital Equipment Corporation, Maynard, Massachusetts.
3
4 All Rights Reserved
5
6 Permission to use, copy, modify, and distribute this software and its
7 documentation for any purpose and without fee is hereby granted,
8 provided that the above copyright notice appear in all copies and that
9 both that copyright notice and this permission notice appear in
10 supporting documentation, and that the name of Digital not be
11 used in advertising or publicity pertaining to distribution of the
12 software without specific, written prior permission.
13
14 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
15 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
16 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
17 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
18 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
19 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20 SOFTWARE.
21
22 ************************************************************************/
23
24 /*
25
26 Copyright 1994, 1998 The Open Group
27
28 Permission to use, copy, modify, distribute, and sell this software and its
29 documentation for any purpose is hereby granted without fee, provided that
30 the above copyright notice appear in all copies and that both that
31 copyright notice and this permission notice appear in supporting
32 documentation.
33
34 The above copyright notice and this permission notice shall be included
35 in all copies or substantial portions of the Software.
36
37 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
38 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
39 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
40 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
41 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
42 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
43 OTHER DEALINGS IN THE SOFTWARE.
44
45 Except as contained in this notice, the name of The Open Group shall
46 not be used in advertising or otherwise to promote the sale, use or
47 other dealings in this Software without prior written authorization
48 from The Open Group.
49
50 */
51
52 #ifdef HAVE_CONFIG_H
53 #include <config.h>
54 #endif
55 #include "libxfontint.h"
56 #include "src/util/replace.h"
57
58 #include <ctype.h>
59 #include <X11/fonts/fntfilst.h>
60 #include <X11/fonts/fontutil.h>
61 /* use bitmap structure */
62 #include <X11/fonts/bitmap.h>
63 #include <X11/fonts/bdfint.h>
64
65 #if HAVE_STDINT_H
66 #include <stdint.h>
67 #else
68 # ifndef INT32_MAX
69 # define INT32_MAX 0x7fffffff
70 # endif
71 # ifndef INT16_MAX
72 # define INT16_MAX 0x7fff
73 # endif
74 # ifndef INT16_MIN
75 # define INT16_MIN (0 - 0x8000)
76 # endif
77 #endif
78
79 #define INDICES 256
80 #define MAXENCODING 0xFFFF
81 #define BDFLINELEN 1024
82 #define BDFLINESTR "%1023s" /* scanf specifier to read a BDFLINELEN string */
83
84 static Bool bdfPadToTerminal(FontPtr pFont);
85 extern int bdfFileLineNum;
86
87 /***====================================================================***/
88
89 static Bool
90 bdfReadBitmap(CharInfoPtr pCI, FontFilePtr file, int bit, int byte,
91 int glyph, int scan, CARD32 *sizes)
92 {
93 int widthBits,
94 widthBytes,
95 widthHexChars;
96 int height,
97 row;
98 int i,
99 inLineLen,
100 nextByte;
101 unsigned char *pInBits,
102 *picture,
103 *line = NULL;
104 unsigned char lineBuf[BDFLINELEN];
105
106 widthBits = GLYPHWIDTHPIXELS(pCI);
107 height = GLYPHHEIGHTPIXELS(pCI);
108
109 widthBytes = BYTES_PER_ROW(widthBits, glyph);
110 if (widthBytes * height > 0) {
111 picture = mallocarray(widthBytes, height);
112 if (!picture) {
113 bdfError("Couldn't allocate picture (%d*%d)\n", widthBytes, height);
114 goto BAILOUT;
115 }
116 } else
117 picture = NULL;
118 pCI->bits = (char *) picture;
119
120 if (sizes) {
121 for (i = 0; i < GLYPHPADOPTIONS; i++)
122 sizes[i] += BYTES_PER_ROW(widthBits, (1 << i)) * height;
123 }
124 nextByte = 0;
125 widthHexChars = BYTES_PER_ROW(widthBits, 1);
126
127 /* 5/31/89 (ef) -- hack, hack, hack. what *am* I supposed to do with */
128 /* 0 width characters? */
129
130 for (row = 0; row < height; row++) {
131 line = bdfGetLine(file, lineBuf, BDFLINELEN);
132 if (!line)
133 break;
134
135 if (widthBits == 0) {
136 if ((!line) || (bdfIsPrefix(line, "ENDCHAR")))
137 break;
138 else
139 continue;
140 }
141 pInBits = line;
142 inLineLen = strlen((char *) pInBits);
143
144 if (inLineLen & 1) {
145 bdfError("odd number of characters in hex encoding\n");
146 line[inLineLen++] = '0';
147 line[inLineLen] = '\0';
148 }
149 inLineLen >>= 1;
150 i = inLineLen;
151 if (i > widthHexChars)
152 i = widthHexChars;
153 for (; i > 0; i--, pInBits += 2)
154 picture[nextByte++] = bdfHexByte(pInBits);
155
156 /* pad if line is too short */
157 if (inLineLen < widthHexChars) {
158 for (i = widthHexChars - inLineLen; i > 0; i--)
159 picture[nextByte++] = 0;
160 } else {
161 unsigned char mask;
162
163 mask = 0xff << (8 - (widthBits & 0x7));
164 if (mask && picture[nextByte - 1] & ~mask) {
165 picture[nextByte - 1] &= mask;
166 }
167 }
168
169 if (widthBytes > widthHexChars) {
170 i = widthBytes - widthHexChars;
171 while (i-- > 0)
172 picture[nextByte++] = 0;
173 }
174 }
175
176 if ((line && (!bdfIsPrefix(line, "ENDCHAR"))) || (height == 0))
177 line = bdfGetLine(file, lineBuf, BDFLINELEN);
178
179 if ((!line) || (!bdfIsPrefix(line, "ENDCHAR"))) {
180 bdfError("missing 'ENDCHAR'\n");
181 goto BAILOUT;
182 }
183 if (nextByte != height * widthBytes) {
184 bdfError("bytes != rows * bytes_per_row (%d != %d * %d)\n",
185 nextByte, height, widthBytes);
186 goto BAILOUT;
187 }
188 if (picture != NULL) {
189 if (bit == LSBFirst)
190 BitOrderInvert(picture, nextByte);
191 if (bit != byte) {
192 if (scan == 2)
193 TwoByteSwap(picture, nextByte);
194 else if (scan == 4)
195 FourByteSwap(picture, nextByte);
196 }
197 }
198 return (TRUE);
199 BAILOUT:
200 if (picture)
201 free(picture);
202 pCI->bits = NULL;
203 return (FALSE);
204 }
205
206 /***====================================================================***/
207
208 static Bool
209 bdfSkipBitmap(FontFilePtr file, int height)
210 {
211 unsigned char *line;
212 int i = 0;
213 unsigned char lineBuf[BDFLINELEN];
214
215 do {
216 line = bdfGetLine(file, lineBuf, BDFLINELEN);
217 i++;
218 } while (line && !bdfIsPrefix(line, "ENDCHAR") && i <= height);
219
220 if (i > 1 && line && !bdfIsPrefix(line, "ENDCHAR")) {
221 bdfError("Error in bitmap, missing 'ENDCHAR'\n");
222 return (FALSE);
223 }
224 return (TRUE);
225 }
226
227 /***====================================================================***/
228
229 static void
230 bdfFreeFontBits(FontPtr pFont)
231 {
232 BitmapFontPtr bitmapFont;
233 BitmapExtraPtr bitmapExtra;
234 int i, nencoding;
235
236 bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
237 bitmapExtra = (BitmapExtraPtr) bitmapFont->bitmapExtra;
238 free(bitmapFont->ink_metrics);
239 if(bitmapFont->encoding) {
240 nencoding = (pFont->info.lastCol - pFont->info.firstCol + 1) *
241 (pFont->info.lastRow - pFont->info.firstRow + 1);
242 for(i=0; i<NUM_SEGMENTS(nencoding); i++)
243 free(bitmapFont->encoding[i]);
244 }
245 free(bitmapFont->encoding);
246 for (i = 0; i < bitmapFont->num_chars; i++)
247 free(bitmapFont->metrics[i].bits);
248 free(bitmapFont->metrics);
249 if (bitmapExtra)
250 {
251 free (bitmapExtra->glyphNames);
252 free (bitmapExtra->sWidths);
253 free (bitmapExtra);
254 }
255 free(pFont->info.props);
256 free(bitmapFont);
257 }
258
259
260 static Bool
261 bdfReadCharacters(FontFilePtr file, FontPtr pFont, bdfFileState *pState,
262 int bit, int byte, int glyph, int scan)
263 {
264 unsigned char *line;
265 register CharInfoPtr ci;
266 int i,
267 ndx,
268 nchars,
269 nignored;
270 unsigned int char_row, char_col;
271 int numEncodedGlyphs = 0;
272 CharInfoPtr *bdfEncoding[256];
273 BitmapFontPtr bitmapFont;
274 BitmapExtraPtr bitmapExtra;
275 CARD32 *bitmapsSizes;
276 unsigned char lineBuf[BDFLINELEN];
277 int nencoding;
278
279 bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
280 bitmapExtra = (BitmapExtraPtr) bitmapFont->bitmapExtra;
281
282 if (bitmapExtra) {
283 bitmapsSizes = bitmapExtra->bitmapsSizes;
284 for (i = 0; i < GLYPHPADOPTIONS; i++)
285 bitmapsSizes[i] = 0;
286 } else
287 bitmapsSizes = NULL;
288
289 bzero(bdfEncoding, sizeof(bdfEncoding));
290 bitmapFont->metrics = NULL;
291 ndx = 0;
292
293 line = bdfGetLine(file, lineBuf, BDFLINELEN);
294
295 if ((!line) || (sscanf((char *) line, "CHARS %d", &nchars) != 1)) {
296 bdfError("bad 'CHARS' in bdf file\n");
297 return (FALSE);
298 }
299 if (nchars < 1) {
300 bdfError("invalid number of CHARS in BDF file\n");
301 return (FALSE);
302 }
303 if (nchars > (signed) (INT32_MAX / sizeof(CharInfoRec))) {
304 bdfError("Couldn't allocate pCI (%d*%d)\n", nchars,
305 (int) sizeof(CharInfoRec));
306 goto BAILOUT;
307 }
308 ci = calloc(nchars, sizeof(CharInfoRec));
309 if (!ci) {
310 bdfError("Couldn't allocate pCI (%d*%d)\n", nchars,
311 (int) sizeof(CharInfoRec));
312 goto BAILOUT;
313 }
314 bitmapFont->metrics = ci;
315
316 if (bitmapExtra) {
317 bitmapExtra->glyphNames = mallocarray(nchars, sizeof(Atom));
318 if (!bitmapExtra->glyphNames) {
319 bdfError("Couldn't allocate glyphNames (%d*%d)\n",
320 nchars, (int) sizeof(Atom));
321 goto BAILOUT;
322 }
323 }
324 if (bitmapExtra) {
325 bitmapExtra->sWidths = mallocarray(nchars, sizeof(int));
326 if (!bitmapExtra->sWidths) {
327 bdfError("Couldn't allocate sWidth (%d *%d)\n",
328 nchars, (int) sizeof(int));
329 return FALSE;
330 }
331 }
332 line = bdfGetLine(file, lineBuf, BDFLINELEN);
333 pFont->info.firstRow = 256;
334 pFont->info.lastRow = 0;
335 pFont->info.firstCol = 256;
336 pFont->info.lastCol = 0;
337 nignored = 0;
338 for (ndx = 0; (ndx < nchars) && (line) && (bdfIsPrefix(line, "STARTCHAR"));) {
339 int t;
340 int wx; /* x component of width */
341 int wy; /* y component of width */
342 int bw; /* bounding-box width */
343 int bh; /* bounding-box height */
344 int bl; /* bounding-box left */
345 int bb; /* bounding-box bottom */
346 int enc,
347 enc2; /* encoding */
348 unsigned char *p; /* temp pointer into line */
349 char charName[100];
350 int ignore;
351
352 if (sscanf((char *) line, "STARTCHAR %99s", charName) != 1) {
353 bdfError("bad character name in BDF file\n");
354 goto BAILOUT; /* bottom of function, free and return error */
355 }
356 if (bitmapExtra)
357 bitmapExtra->glyphNames[ndx] = bdfForceMakeAtom(charName, NULL);
358
359 line = bdfGetLine(file, lineBuf, BDFLINELEN);
360 if (!line || (t = sscanf((char *) line, "ENCODING %d %d", &enc, &enc2)) < 1) {
361 bdfError("bad 'ENCODING' in BDF file\n");
362 goto BAILOUT;
363 }
364 if (enc < -1 || (t == 2 && enc2 < -1)) {
365 bdfError("bad ENCODING value");
366 goto BAILOUT;
367 }
368 if (t == 2 && enc == -1)
369 enc = enc2;
370 ignore = 0;
371 if (enc == -1) {
372 if (!bitmapExtra) {
373 nignored++;
374 ignore = 1;
375 }
376 } else if (enc > MAXENCODING) {
377 bdfError("char '%s' has encoding too large (%d)\n",
378 charName, enc);
379 } else {
380 char_row = (enc >> 8) & 0xFF;
381 char_col = enc & 0xFF;
382 if (char_row < pFont->info.firstRow)
383 pFont->info.firstRow = char_row;
384 if (char_row > pFont->info.lastRow)
385 pFont->info.lastRow = char_row;
386 if (char_col < pFont->info.firstCol)
387 pFont->info.firstCol = char_col;
388 if (char_col > pFont->info.lastCol)
389 pFont->info.lastCol = char_col;
390 if (bdfEncoding[char_row] == (CharInfoPtr *) NULL) {
391 bdfEncoding[char_row] = malloc(256 * sizeof(CharInfoPtr));
392 if (!bdfEncoding[char_row]) {
393 bdfError("Couldn't allocate row %d of encoding (%d*%d)\n",
394 char_row, INDICES, (int) sizeof(CharInfoPtr));
395 goto BAILOUT;
396 }
397 for (i = 0; i < 256; i++)
398 bdfEncoding[char_row][i] = (CharInfoPtr) NULL;
399 }
400 if (bdfEncoding[char_row] != NULL) {
401 bdfEncoding[char_row][char_col] = ci;
402 numEncodedGlyphs++;
403 }
404 }
405
406 line = bdfGetLine(file, lineBuf, BDFLINELEN);
407 if ((!line) || (sscanf((char *) line, "SWIDTH %d %d", &wx, &wy) != 2)) {
408 bdfError("bad 'SWIDTH'\n");
409 goto BAILOUT;
410 }
411 if (wy != 0) {
412 bdfError("SWIDTH y value must be zero\n");
413 goto BAILOUT;
414 }
415 if (bitmapExtra)
416 bitmapExtra->sWidths[ndx] = wx;
417
418 /* 5/31/89 (ef) -- we should be able to ditch the character and recover */
419 /* from all of these. */
420
421 line = bdfGetLine(file, lineBuf, BDFLINELEN);
422 if ((!line) || (sscanf((char *) line, "DWIDTH %d %d", &wx, &wy) != 2)) {
423 bdfError("bad 'DWIDTH'\n");
424 goto BAILOUT;
425 }
426 if (wy != 0) {
427 bdfError("DWIDTH y value must be zero\n");
428 goto BAILOUT;
429 }
430 /* xCharInfo metrics are stored as INT16 */
431 if ((wx < INT16_MIN) || (wx > INT16_MAX)) {
432 bdfError("character '%s' has out of range width, %d\n",
433 charName, wx);
434 goto BAILOUT;
435 }
436 line = bdfGetLine(file, lineBuf, BDFLINELEN);
437 if ((!line) || (sscanf((char *) line, "BBX %d %d %d %d", &bw, &bh, &bl, &bb) != 4)) {
438 bdfError("bad 'BBX'\n");
439 goto BAILOUT;
440 }
441 if ((bh < 0) || (bw < 0)) {
442 bdfError("character '%s' has a negative sized bitmap, %dx%d\n",
443 charName, bw, bh);
444 goto BAILOUT;
445 }
446 /* xCharInfo metrics are read as int, but stored as INT16 */
447 if ((bl > INT16_MAX) || (bl < INT16_MIN) ||
448 (bb > INT16_MAX) || (bb < INT16_MIN) ||
449 (bw > (INT16_MAX - bl)) || (bh > (INT16_MAX - bb))) {
450 bdfError("character '%s' has out of range metrics, %d %d %d %d\n",
451 charName, bl, (bl+bw), (bh+bb), -bb);
452 goto BAILOUT;
453 }
454 line = bdfGetLine(file, lineBuf, BDFLINELEN);
455 if ((line) && (bdfIsPrefix(line, "ATTRIBUTES"))) {
456 for (p = line + strlen("ATTRIBUTES ");
457 (*p == ' ') || (*p == '\t');
458 p++)
459 /* empty for loop */ ;
460 ci->metrics.attributes = (bdfHexByte(p) << 8) + bdfHexByte(p + 2);
461 line = bdfGetLine(file, lineBuf, BDFLINELEN);
462 } else
463 ci->metrics.attributes = 0;
464
465 if (!line || !bdfIsPrefix(line, "BITMAP")) {
466 bdfError("missing 'BITMAP'\n");
467 goto BAILOUT;
468 }
469 /* collect data for generated properties */
470 if ((strlen(charName) == 1)) {
471 if ((charName[0] >= '0') && (charName[0] <= '9')) {
472 pState->digitWidths += wx;
473 pState->digitCount++;
474 } else if (charName[0] == 'x') {
475 pState->exHeight = (bh + bb) <= 0 ? bh : bh + bb;
476 }
477 }
478 if (!ignore) {
479 ci->metrics.leftSideBearing = bl;
480 ci->metrics.rightSideBearing = bl + bw;
481 ci->metrics.ascent = bh + bb;
482 ci->metrics.descent = -bb;
483 ci->metrics.characterWidth = wx;
484 ci->bits = NULL;
485 if (!bdfReadBitmap(ci, file, bit, byte, glyph, scan, bitmapsSizes)) {
486 bdfError("could not read bitmap for character '%s'\n", charName);
487 goto BAILOUT;
488 }
489 ci++;
490 ndx++;
491 } else
492 bdfSkipBitmap(file, bh);
493
494 line = bdfGetLine(file, lineBuf, BDFLINELEN); /* get STARTCHAR or
495 * ENDFONT */
496 }
497
498 if (ndx + nignored != nchars) {
499 bdfError("%d too few characters\n", nchars - (ndx + nignored));
500 goto BAILOUT;
501 }
502 nchars = ndx;
503 bitmapFont->num_chars = nchars;
504 if ((line) && (bdfIsPrefix(line, "STARTCHAR"))) {
505 bdfError("more characters than specified\n");
506 goto BAILOUT;
507 }
508 if ((!line) || (!bdfIsPrefix(line, "ENDFONT"))) {
509 bdfError("missing 'ENDFONT'\n");
510 goto BAILOUT;
511 }
512 if (numEncodedGlyphs == 0)
513 bdfWarning("No characters with valid encodings\n");
514
515 nencoding = (pFont->info.lastRow - pFont->info.firstRow + 1) *
516 (pFont->info.lastCol - pFont->info.firstCol + 1);
517 bitmapFont->encoding = calloc(NUM_SEGMENTS(nencoding),sizeof(CharInfoPtr*));
518 if (!bitmapFont->encoding) {
519 bdfError("Couldn't allocate ppCI (%d,%d)\n",
520 NUM_SEGMENTS(nencoding),
521 (int) sizeof(CharInfoPtr*));
522 goto BAILOUT;
523 }
524 pFont->info.allExist = TRUE;
525 i = 0;
526 for (char_row = pFont->info.firstRow;
527 char_row <= pFont->info.lastRow;
528 char_row++) {
529 if (bdfEncoding[char_row] == (CharInfoPtr *) NULL) {
530 pFont->info.allExist = FALSE;
531 i += pFont->info.lastCol - pFont->info.firstCol + 1;
532 } else {
533 for (char_col = pFont->info.firstCol;
534 char_col <= pFont->info.lastCol;
535 char_col++) {
536 if (!bdfEncoding[char_row][char_col])
537 pFont->info.allExist = FALSE;
538 else {
539 if (!bitmapFont->encoding[SEGMENT_MAJOR(i)]) {
540 bitmapFont->encoding[SEGMENT_MAJOR(i)]=
541 calloc(BITMAP_FONT_SEGMENT_SIZE,
542 sizeof(CharInfoPtr));
543 if (!bitmapFont->encoding[SEGMENT_MAJOR(i)])
544 goto BAILOUT;
545 }
546 ACCESSENCODINGL(bitmapFont->encoding,i) =
547 bdfEncoding[char_row][char_col];
548 }
549 i++;
550 }
551 }
552 }
553 for (i = 0; i < 256; i++)
554 if (bdfEncoding[i])
555 free(bdfEncoding[i]);
556 return (TRUE);
557 BAILOUT:
558 for (i = 0; i < 256; i++)
559 if (bdfEncoding[i])
560 free(bdfEncoding[i]);
561 /* bdfFreeFontBits will clean up the rest */
562 return (FALSE);
563 }
564
565 /***====================================================================***/
566
567 static Bool
568 bdfReadHeader(FontFilePtr file, bdfFileState *pState)
569 {
570 unsigned char *line;
571 char namebuf[BDFLINELEN];
572 unsigned char lineBuf[BDFLINELEN];
573
574 line = bdfGetLine(file, lineBuf, BDFLINELEN);
575 if (!line ||
576 sscanf((char *) line, "STARTFONT " BDFLINESTR, namebuf) != 1 ||
577 !bdfStrEqual(namebuf, "2.1")) {
578 bdfError("bad 'STARTFONT'\n");
579 return (FALSE);
580 }
581 line = bdfGetLine(file, lineBuf, BDFLINELEN);
582 #if MAXFONTNAMELEN != 1024
583 # error "need to adjust sscanf length limit to be MAXFONTNAMELEN - 1"
584 #endif
585 if (!line ||
586 sscanf((char *) line, "FONT %1023[^\n]", pState->fontName) != 1) {
587 bdfError("bad 'FONT'\n");
588 return (FALSE);
589 }
590 line = bdfGetLine(file, lineBuf, BDFLINELEN);
591 if (!line || !bdfIsPrefix(line, "SIZE")) {
592 bdfError("missing 'SIZE'\n");
593 return (FALSE);
594 }
595 if (sscanf((char *) line, "SIZE %f%d%d", &pState->pointSize,
596 &pState->resolution_x, &pState->resolution_y) != 3) {
597 bdfError("bad 'SIZE'\n");
598 return (FALSE);
599 }
600 if (pState->pointSize < 1 ||
601 pState->resolution_x < 1 || pState->resolution_y < 1) {
602 bdfError("SIZE values must be > 0\n");
603 return (FALSE);
604 }
605 line = bdfGetLine(file, lineBuf, BDFLINELEN);
606 if (!line || !bdfIsPrefix(line, "FONTBOUNDINGBOX")) {
607 bdfError("missing 'FONTBOUNDINGBOX'\n");
608 return (FALSE);
609 }
610 return (TRUE);
611 }
612
613 /***====================================================================***/
614
615 static Bool
616 bdfReadProperties(FontFilePtr file, FontPtr pFont, bdfFileState *pState)
617 {
618 int nProps, props_left,
619 nextProp;
620 char *stringProps;
621 FontPropPtr props;
622 char namebuf[BDFLINELEN],
623 secondbuf[BDFLINELEN],
624 thirdbuf[BDFLINELEN];
625 unsigned char *line;
626 unsigned char lineBuf[BDFLINELEN];
627 BitmapFontPtr bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
628
629 line = bdfGetLine(file, lineBuf, BDFLINELEN);
630 if (!line || !bdfIsPrefix(line, "STARTPROPERTIES")) {
631 bdfError("missing 'STARTPROPERTIES'\n");
632 return (FALSE);
633 }
634 if ((sscanf((char *) line, "STARTPROPERTIES %d", &nProps) != 1) ||
635 (nProps <= 0) ||
636 (nProps > (signed) ((INT32_MAX / sizeof(FontPropRec)) - BDF_GENPROPS))) {
637 bdfError("bad 'STARTPROPERTIES'\n");
638 return (FALSE);
639 }
640 pFont->info.isStringProp = NULL;
641 pFont->info.props = NULL;
642 pFont->info.nprops = 0;
643
644 stringProps = mallocarray((nProps + BDF_GENPROPS), sizeof(char));
645 pFont->info.isStringProp = stringProps;
646 if (stringProps == NULL) {
647 bdfError("Couldn't allocate stringProps (%d*%d)\n",
648 (nProps + BDF_GENPROPS), (int) sizeof(Bool));
649 goto BAILOUT;
650 }
651 pFont->info.props = props = calloc(nProps + BDF_GENPROPS,
652 sizeof(FontPropRec));
653 if (props == NULL) {
654 bdfError("Couldn't allocate props (%d*%d)\n", nProps + BDF_GENPROPS,
655 (int) sizeof(FontPropRec));
656 goto BAILOUT;
657 }
658
659 nextProp = 0;
660 props_left = nProps;
661 while (props_left-- > 0) {
662 line = bdfGetLine(file, lineBuf, BDFLINELEN);
663 if (line == NULL || bdfIsPrefix(line, "ENDPROPERTIES")) {
664 bdfError("\"STARTPROPERTIES %d\" followed by only %d properties\n",
665 nProps, nProps - props_left - 1);
666 goto BAILOUT;
667 }
668 while (*line && isspace(*line))
669 line++;
670
671 switch (sscanf((char *) line,
672 BDFLINESTR BDFLINESTR BDFLINESTR,
673 namebuf, secondbuf, thirdbuf)) {
674 default:
675 bdfError("missing '%s' parameter value\n", namebuf);
676 goto BAILOUT;
677
678 case 2:
679 /*
680 * Possibilities include: valid quoted string with no white space
681 * valid integer value invalid value
682 */
683 if (secondbuf[0] == '"') {
684 stringProps[nextProp] = TRUE;
685 props[nextProp].value =
686 bdfGetPropertyValue((char *)line + strlen(namebuf) + 1);
687 if (!props[nextProp].value)
688 goto BAILOUT;
689 break;
690 } else if (bdfIsInteger(secondbuf)) {
691 stringProps[nextProp] = FALSE;
692 props[nextProp].value = atoi(secondbuf);
693 break;
694 } else {
695 bdfError("invalid '%s' parameter value\n", namebuf);
696 goto BAILOUT;
697 }
698
699 case 3:
700 /*
701 * Possibilities include: valid quoted string with some white space
702 * invalid value (reject even if second string is integer)
703 */
704 if (secondbuf[0] == '"') {
705 stringProps[nextProp] = TRUE;
706 props[nextProp].value =
707 bdfGetPropertyValue((char *)line + strlen(namebuf) + 1);
708 if (!props[nextProp].value)
709 goto BAILOUT;
710 break;
711 } else {
712 bdfError("invalid '%s' parameter value\n", namebuf);
713 goto BAILOUT;
714 }
715 }
716 props[nextProp].name = bdfForceMakeAtom(namebuf, NULL);
717 if (props[nextProp].name == None) {
718 bdfError("Empty property name.\n");
719 goto BAILOUT;
720 }
721 if (!bdfSpecialProperty(pFont, &props[nextProp],
722 stringProps[nextProp], pState))
723 nextProp++;
724 }
725
726 line = bdfGetLine(file, lineBuf, BDFLINELEN);
727 if (!line || !bdfIsPrefix(line, "ENDPROPERTIES")) {
728 bdfError("missing 'ENDPROPERTIES'\n");
729 goto BAILOUT;
730 }
731 if (!pState->haveFontAscent || !pState->haveFontDescent) {
732 bdfError("missing 'FONT_ASCENT' or 'FONT_DESCENT' properties\n");
733 goto BAILOUT;
734 }
735 if (bitmapFont->bitmapExtra) {
736 bitmapFont->bitmapExtra->info.fontAscent = pFont->info.fontAscent;
737 bitmapFont->bitmapExtra->info.fontDescent = pFont->info.fontDescent;
738 }
739 if (!pState->pointSizeProp) {
740 props[nextProp].name = bdfForceMakeAtom("POINT_SIZE", NULL);
741 props[nextProp].value = (INT32) (pState->pointSize * 10.0);
742 stringProps[nextProp] = FALSE;
743 pState->pointSizeProp = &props[nextProp];
744 nextProp++;
745 }
746 if (!pState->fontProp) {
747 props[nextProp].name = bdfForceMakeAtom("FONT", NULL);
748 props[nextProp].value = (INT32) bdfForceMakeAtom(pState->fontName, NULL);
749 stringProps[nextProp] = TRUE;
750 pState->fontProp = &props[nextProp];
751 nextProp++;
752 }
753 if (!pState->weightProp) {
754 props[nextProp].name = bdfForceMakeAtom("WEIGHT", NULL);
755 props[nextProp].value = -1; /* computed later */
756 stringProps[nextProp] = FALSE;
757 pState->weightProp = &props[nextProp];
758 nextProp++;
759 }
760 if (!pState->resolutionProp &&
761 pState->resolution_x == pState->resolution_y) {
762 props[nextProp].name = bdfForceMakeAtom("RESOLUTION", NULL);
763 props[nextProp].value = (INT32) ((pState->resolution_x * 100.0) / 72.27);
764 stringProps[nextProp] = FALSE;
765 pState->resolutionProp = &props[nextProp];
766 nextProp++;
767 }
768 if (!pState->resolutionXProp) {
769 props[nextProp].name = bdfForceMakeAtom("RESOLUTION_X", NULL);
770 props[nextProp].value = (INT32) pState->resolution_x;
771 stringProps[nextProp] = FALSE;
772 pState->resolutionProp = &props[nextProp];
773 nextProp++;
774 }
775 if (!pState->resolutionYProp) {
776 props[nextProp].name = bdfForceMakeAtom("RESOLUTION_Y", NULL);
777 props[nextProp].value = (INT32) pState->resolution_y;
778 stringProps[nextProp] = FALSE;
779 pState->resolutionProp = &props[nextProp];
780 nextProp++;
781 }
782 if (!pState->xHeightProp) {
783 props[nextProp].name = bdfForceMakeAtom("X_HEIGHT", NULL);
784 props[nextProp].value = -1; /* computed later */
785 stringProps[nextProp] = FALSE;
786 pState->xHeightProp = &props[nextProp];
787 nextProp++;
788 }
789 if (!pState->quadWidthProp) {
790 props[nextProp].name = bdfForceMakeAtom("QUAD_WIDTH", NULL);
791 props[nextProp].value = -1; /* computed later */
792 stringProps[nextProp] = FALSE;
793 pState->quadWidthProp = &props[nextProp];
794 nextProp++;
795 }
796 pFont->info.nprops = nextProp;
797 return (TRUE);
798 BAILOUT:
799 if (pFont->info.isStringProp) {
800 free(pFont->info.isStringProp);
801 pFont->info.isStringProp = NULL;
802 }
803 if (pFont->info.props) {
804 free(pFont->info.props);
805 pFont->info.props = NULL;
806 }
807 while (line && bdfIsPrefix(line, "ENDPROPERTIES"))
808 line = bdfGetLine(file, lineBuf, BDFLINELEN);
809 return (FALSE);
810 }
811
812 /***====================================================================***/
813
814 static void
815 bdfUnloadFont(FontPtr pFont)
816 {
817 bdfFreeFontBits (pFont);
818 DestroyFontRec(pFont);
819 }
820
821 int
822 bdfReadFont(FontPtr pFont, FontFilePtr file,
823 int bit, int byte, int glyph, int scan)
824 {
825 bdfFileState state;
826 xCharInfo *min,
827 *max;
828 BitmapFontPtr bitmapFont;
829
830 pFont->fontPrivate = 0;
831
832 bzero(&state, sizeof(bdfFileState));
833 bdfFileLineNum = 0;
834
835 if (!bdfReadHeader(file, &state))
836 goto BAILOUT;
837
838 bitmapFont = calloc(1, sizeof(BitmapFontRec));
839 if (!bitmapFont) {
840 bdfError("Couldn't allocate bitmapFontRec (%d)\n",
841 (int) sizeof(BitmapFontRec));
842 goto BAILOUT;
843 }
844
845 pFont->fontPrivate = (pointer) bitmapFont;
846 bitmapFont->metrics = 0;
847 bitmapFont->ink_metrics = 0;
848 bitmapFont->bitmaps = 0;
849 bitmapFont->encoding = 0;
850 bitmapFont->pDefault = NULL;
851
852 bitmapFont->bitmapExtra = calloc(1, sizeof(BitmapExtraRec));
853 if (!bitmapFont->bitmapExtra) {
854 bdfError("Couldn't allocate bitmapExtra (%d)\n",
855 (int) sizeof(BitmapExtraRec));
856 goto BAILOUT;
857 }
858
859 bitmapFont->bitmapExtra->glyphNames = 0;
860 bitmapFont->bitmapExtra->sWidths = 0;
861
862 if (!bdfReadProperties(file, pFont, &state))
863 goto BAILOUT;
864
865 if (!bdfReadCharacters(file, pFont, &state, bit, byte, glyph, scan))
866 goto BAILOUT;
867
868 if (state.haveDefaultCh) {
869 unsigned int r, c, cols;
870
871 r = pFont->info.defaultCh >> 8;
872 c = pFont->info.defaultCh & 0xFF;
873 if (pFont->info.firstRow <= r && r <= pFont->info.lastRow &&
874 pFont->info.firstCol <= c && c <= pFont->info.lastCol) {
875 cols = pFont->info.lastCol - pFont->info.firstCol + 1;
876 r = r - pFont->info.firstRow;
877 c = c - pFont->info.firstCol;
878 bitmapFont->pDefault = ACCESSENCODING(bitmapFont->encoding,
879 r * cols + c);
880 }
881 }
882 pFont->bit = bit;
883 pFont->byte = byte;
884 pFont->glyph = glyph;
885 pFont->scan = scan;
886 pFont->info.anamorphic = FALSE;
887 pFont->info.cachable = TRUE;
888 bitmapComputeFontBounds(pFont);
889 if (FontCouldBeTerminal(&pFont->info)) {
890 bdfPadToTerminal(pFont);
891 bitmapComputeFontBounds(pFont);
892 }
893 FontComputeInfoAccelerators(&pFont->info);
894 if (bitmapFont->bitmapExtra)
895 FontComputeInfoAccelerators(&bitmapFont->bitmapExtra->info);
896 if (pFont->info.constantMetrics) {
897 if (!bitmapAddInkMetrics(pFont)) {
898 bdfError("Failed to add bitmap ink metrics\n");
899 goto BAILOUT;
900 }
901 }
902 if (bitmapFont->bitmapExtra)
903 bitmapFont->bitmapExtra->info.inkMetrics = pFont->info.inkMetrics;
904
905 bitmapComputeFontInkBounds(pFont);
906 /* ComputeFontAccelerators (pFont); */
907
908 /* generate properties */
909 min = &pFont->info.ink_minbounds;
910 max = &pFont->info.ink_maxbounds;
911 if (state.xHeightProp && (state.xHeightProp->value == -1))
912 state.xHeightProp->value = state.exHeight ?
913 state.exHeight : min->ascent;
914
915 if (state.quadWidthProp && (state.quadWidthProp->value == -1))
916 state.quadWidthProp->value = state.digitCount ?
917 (INT32) (state.digitWidths / state.digitCount) :
918 (min->characterWidth + max->characterWidth) / 2;
919
920 if (state.weightProp && (state.weightProp->value == -1))
921 state.weightProp->value = bitmapComputeWeight(pFont);
922
923 pFont->get_glyphs = bitmapGetGlyphs;
924 pFont->get_metrics = bitmapGetMetrics;
925 pFont->unload_font = bdfUnloadFont;
926 pFont->unload_glyphs = NULL;
927 return Successful;
928 BAILOUT:
929 if (pFont->fontPrivate)
930 bdfFreeFontBits (pFont);
931 return AllocError;
932 }
933
934 int
935 bdfReadFontInfo(FontInfoPtr pFontInfo, FontFilePtr file)
936 {
937 FontRec font;
938 int ret;
939
940 bzero(&font, sizeof (FontRec));
941
942 ret = bdfReadFont(&font, file, MSBFirst, LSBFirst, 1, 1);
943 if (ret == Successful) {
944 *pFontInfo = font.info;
945 font.info.props = 0;
946 font.info.isStringProp = 0;
947 font.info.nprops = 0;
948 bdfFreeFontBits (&font);
949 }
950 return ret;
951 }
952
953 static Bool
954 bdfPadToTerminal(FontPtr pFont)
955 {
956 BitmapFontPtr bitmapFont;
957 BitmapExtraPtr bitmapExtra;
958 int i;
959 int new_size;
960 CharInfoRec new;
961 int w,
962 h;
963
964 bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
965
966 bzero(&new, sizeof(CharInfoRec));
967 new.metrics.ascent = pFont->info.fontAscent;
968 new.metrics.descent = pFont->info.fontDescent;
969 new.metrics.leftSideBearing = 0;
970 new.metrics.rightSideBearing = pFont->info.minbounds.characterWidth;
971 new.metrics.characterWidth = new.metrics.rightSideBearing;
972 new_size = BYTES_FOR_GLYPH(&new, pFont->glyph);
973
974 for (i = 0; i < bitmapFont->num_chars; i++) {
975 new.bits = malloc(new_size);
976 if (!new.bits) {
977 bdfError("Couldn't allocate bits (%d)\n", new_size);
978 return FALSE;
979 }
980 FontCharReshape(pFont, &bitmapFont->metrics[i], &new);
981 new.metrics.attributes = bitmapFont->metrics[i].metrics.attributes;
982 free(bitmapFont->metrics[i].bits);
983 bitmapFont->metrics[i] = new;
984 }
985 bitmapExtra = bitmapFont->bitmapExtra;
986 if (bitmapExtra) {
987 w = GLYPHWIDTHPIXELS(&new);
988 h = GLYPHHEIGHTPIXELS(&new);
989 for (i = 0; i < GLYPHPADOPTIONS; i++)
990 bitmapExtra->bitmapsSizes[i] = bitmapFont->num_chars *
991 (BYTES_PER_ROW(w, 1 << i) * h);
992 }
993 return TRUE;
994 }