PageRenderTime 52ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/nx-3.5.0/nx-X11/lib/font/bitmap/bdfread.c

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