readpcf.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: readpcf.c /main/5 1996/06/05 16:42:29 ageorge $ */
  24. /*
  25. * (c) Copyright 1995 FUJITSU LIMITED
  26. * This is source code modified by FUJITSU LIMITED under the Joint
  27. * Development Agreement for the CDEnext PST.
  28. * This is unpublished proprietary source code of FUJITSU LIMITED
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <errno.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <sys/mman.h>
  37. #include <X11/Intrinsic.h>
  38. #include "FaLib.h"
  39. #include "falfont.h"
  40. #ifndef GLYPHPADOPTIONS
  41. #define GLYPHPADOPTIONS 4
  42. #endif
  43. static CARD32 getLSB32();
  44. static int getINT32();
  45. static int getINT16();
  46. static Bool seekToType();
  47. static void getMetric();
  48. static Bool getAccel();
  49. int falInitReadPcf();
  50. static void ByteSwap();
  51. static void repadBits();
  52. int falPcfGlyph();
  53. void falGetPcfGSize();
  54. int falInitReadPcfProp() ;
  55. int falInitReadSnfProp() ;
  56. int falReadGpfProp() ;
  57. extern void BitOrderInvert() ;
  58. extern void set_errfile_str() ;
  59. static CARD32
  60. getLSB32(unsigned char *p)
  61. {
  62. CARD32 c;
  63. c = *p++;
  64. c |= (CARD32)(*p++) << 8;
  65. c |= (CARD32)(*p++) << 16;
  66. c |= (CARD32)(*p) << 24;
  67. return c;
  68. }
  69. static int
  70. getINT32(unsigned char *p, CARD32 format)
  71. {
  72. CARD32 c;
  73. if (PCF_BYTE_ORDER(format) == MSBFirst) {
  74. c = (CARD32)(*p++) << 24;
  75. c |= (CARD32)(*p++) << 16;
  76. c |= (CARD32)(*p++) << 8;
  77. c |= (CARD32)(*p);
  78. } else {
  79. c = (CARD32)(*p++);
  80. c |= (CARD32)(*p++) << 8;
  81. c |= (CARD32)(*p++) << 16;
  82. c |= (CARD32)(*p) << 24;
  83. }
  84. return (int)c;
  85. }
  86. static int
  87. getINT16(unsigned char *p, CARD32 format)
  88. {
  89. CARD32 c;
  90. if (PCF_BYTE_ORDER(format) == MSBFirst) {
  91. c = (CARD32)(*p++) << 8;
  92. c |= (CARD32)(*p);
  93. } else {
  94. c = (CARD32)(*p++);
  95. c |= (CARD32)(*p) << 8;
  96. }
  97. return (int)c;
  98. }
  99. static Bool
  100. seekToType(
  101. PCFTablePtr tables,
  102. int ntables,
  103. CARD32 type,
  104. CARD32 *formatp,
  105. CARD32 *sizep,
  106. CARD32 *offsetp)
  107. {
  108. int i;
  109. for ( i = 0; i < ntables; i++) {
  110. if ( getLSB32( (unsigned char *)&tables[i].type ) == type) {
  111. if ( formatp)
  112. *formatp = getLSB32( (unsigned char *)&tables[i].format);
  113. if ( sizep)
  114. *sizep = getLSB32( (unsigned char *)&tables[i].size);
  115. if ( offsetp)
  116. *offsetp = getLSB32( (unsigned char *)&tables[i].offset);
  117. return(TRUE);
  118. }
  119. }
  120. return(FALSE);
  121. }
  122. static void
  123. getMetric(caddr_t buf, CARD32 format, xCharInfo *metric)
  124. {
  125. metric->leftSideBearing = getINT16( (unsigned char *)buf, (CARD32)format);
  126. buf += 2;
  127. metric->rightSideBearing = getINT16( (unsigned char *)buf, (CARD32)format);
  128. buf += 2;
  129. metric->characterWidth = getINT16( (unsigned char *)buf, (CARD32)format);
  130. buf += 2;
  131. metric->ascent = getINT16( (unsigned char *)buf, (CARD32)format);
  132. buf += 2;
  133. metric->descent = getINT16( (unsigned char *)buf, (CARD32)format);
  134. buf += 2;
  135. metric->attributes = getINT16( (unsigned char *)buf, (CARD32)format);
  136. buf += 2;
  137. }
  138. static Bool
  139. getAccel(
  140. FontInfoPtr pFontInfo,
  141. xCharInfo *maxink,
  142. caddr_t buf_top,
  143. PCFTablePtr tables,
  144. int ntables,
  145. CARD32 type)
  146. {
  147. CARD32 format;
  148. CARD32 offset;
  149. caddr_t buffer;
  150. if ( !seekToType( tables, ntables, (CARD32)type, &format, (CARD32 *)NULL, &offset))
  151. return FALSE;
  152. buffer = buf_top + offset;
  153. format = getLSB32( (unsigned char *)buffer );
  154. buffer += 4;
  155. if (!PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT) &&
  156. !PCF_FORMAT_MATCH(format, PCF_ACCEL_W_INKBOUNDS)) {
  157. return FALSE;
  158. }
  159. pFontInfo->noOverlap = *buffer++;
  160. pFontInfo->constantMetrics = *buffer++;
  161. pFontInfo->terminalFont = *buffer++;
  162. pFontInfo->constantWidth = *buffer++;
  163. pFontInfo->inkInside = *buffer++;
  164. pFontInfo->inkMetrics = *buffer++;
  165. pFontInfo->drawDirection = *buffer++;
  166. /* pFontInfo->anamorphic = FALSE; */
  167. /* natural alignment */ buffer++;
  168. pFontInfo->fontAscent = getINT32( (unsigned char *)buffer, (CARD32)format);
  169. buffer += 4;
  170. pFontInfo->fontDescent = getINT32( (unsigned char *)buffer, (CARD32)format);
  171. buffer +=4;
  172. /* pFontInfo->maxOverlap = getINT32( (unsigned char *)buffer, (CARD32)format); */ buffer += 4;
  173. getMetric(buffer, format, &pFontInfo->minbounds);
  174. buffer += 12;
  175. getMetric(buffer, format, &pFontInfo->maxbounds);
  176. buffer += 12;
  177. if (PCF_FORMAT_MATCH(format, PCF_ACCEL_W_INKBOUNDS)) {
  178. buffer += 12;
  179. getMetric( buffer, format, maxink);
  180. } else {
  181. *maxink = pFontInfo->maxbounds;
  182. }
  183. return TRUE;
  184. }
  185. int
  186. falInitReadPcf(struct pcf_inf *pcfinf, caddr_t buftop)
  187. {
  188. CARD32 format;
  189. CARD32 offset;
  190. CARD32 *bitmapSizes;
  191. xCharInfo maxink;
  192. caddr_t buffp;
  193. if ( getLSB32( (unsigned char *)buftop ) != PCF_FILE_VERSION)
  194. return -1;
  195. pcfinf->ntables = getLSB32( (unsigned char *)(buftop + 4) );
  196. pcfinf->tables = (PCFTablePtr)(buftop + 8);
  197. if ( !getAccel( &pcfinf->info, &maxink, buftop, pcfinf->tables, pcfinf->ntables,
  198. (CARD32)PCF_BDF_ACCELERATORS))
  199. if ( !getAccel( &pcfinf->info, &maxink, buftop, pcfinf->tables, pcfinf->ntables,
  200. (CARD32)PCF_ACCELERATORS))
  201. return -1;
  202. pcfinf->org_bounds = pcfinf->info.maxbounds;
  203. if ( !seekToType( pcfinf->tables, pcfinf->ntables, (CARD32)PCF_BITMAPS,
  204. &format, (CARD32 *)NULL, &offset))
  205. return -1;
  206. buffp = buftop + offset;
  207. format = getLSB32( (unsigned char *)buffp );
  208. buffp += 4;
  209. if (!PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT))
  210. return -1;
  211. pcfinf->nbitmaps = getINT32( (unsigned char *)buffp, (CARD32)format);
  212. buffp += 4;
  213. pcfinf->offsets = (CARD32 *)buffp;
  214. buffp += sizeof( *pcfinf->offsets) * pcfinf->nbitmaps;
  215. bitmapSizes = (CARD32 *)buffp;
  216. pcfinf->sizebitmaps = getINT32( (unsigned char *)&bitmapSizes[PCF_GLYPH_PAD_INDEX(format)], (CARD32)format);
  217. pcfinf->bmp_fmt = format;
  218. buffp += sizeof( *bitmapSizes) * GLYPHPADOPTIONS;
  219. pcfinf->bitmaps = buffp;
  220. buffp += pcfinf->sizebitmaps;
  221. if ( !seekToType( pcfinf->tables, pcfinf->ntables, (CARD32)PCF_BDF_ENCODINGS,
  222. &format, (CARD32 *)NULL, &offset))
  223. return -1;
  224. buffp = buftop + offset;
  225. format = getLSB32( (unsigned char *)buffp );
  226. buffp += 4;
  227. if (!PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT))
  228. return -1;
  229. pcfinf->info.firstCol = getINT16( (unsigned char *)buffp, (CARD32)format);
  230. buffp += 2;
  231. pcfinf->info.lastCol = getINT16( (unsigned char *)buffp, (CARD32)format);
  232. buffp += 2;
  233. pcfinf->info.firstRow = getINT16( (unsigned char *)buffp, (CARD32)format);
  234. buffp += 2;
  235. pcfinf->info.lastRow = getINT16( (unsigned char *)buffp, (CARD32)format);
  236. buffp += 2;
  237. /*
  238. pcfinf->info.defaultCh = getINT16( (unsigned char *)buffp, (CARD32)format); buffp += 2;
  239. */
  240. pcfinf->info.defaultCh = getINT16( (unsigned char *)buffp, (CARD32)format);
  241. buffp += 2;
  242. pcfinf->info.allExist = FALSE;
  243. pcfinf->enc_fmt = format;
  244. pcfinf->encodingOffsets = (CARD16 *)buffp;
  245. return 0;
  246. }
  247. static void
  248. ByteSwap(char *p, int scan)
  249. {
  250. char w;
  251. switch( scan) {
  252. case 1:
  253. break;
  254. case 2:
  255. w = *p;
  256. *p = *(p + 1);
  257. *(p + 1) = w;
  258. break;
  259. case 4:
  260. w = *p;
  261. *p = *(p + 3);
  262. *(p + 3) = w;
  263. w = *(p + 1);
  264. *(p + 1) = *(p + 2);
  265. *(p + 2) = w;
  266. break;
  267. }
  268. }
  269. static void
  270. repadBits(char *src, CARD32 format, int width, int height, char *dest)
  271. {
  272. int bit, byte, glyph, scan;
  273. int src_bytewidth, dest_bytewidth;
  274. char work[8];
  275. int i, j;
  276. bit = PCF_BIT_ORDER( format);
  277. byte = PCF_BYTE_ORDER( format);
  278. glyph = PCF_GLYPH_PAD( format);
  279. scan = PCF_SCAN_UNIT( format);
  280. src_bytewidth = (( width + ( 8 * glyph ) - 1)/( 8 * glyph)) * glyph;
  281. dest_bytewidth = ( width + 7) /8;
  282. for ( i = 0; i < height; i++, src += src_bytewidth,
  283. dest += dest_bytewidth) {
  284. for ( j = 0; j < src_bytewidth; j += scan) {
  285. memcpy( work, src + j, scan);
  286. if ( bit == LSBFirst)
  287. BitOrderInvert( work, scan );
  288. if ( byte == LSBFirst)
  289. ByteSwap( work, scan);
  290. if (( j + scan) >= dest_bytewidth) {
  291. memcpy( dest + j, work, dest_bytewidth - j);
  292. break;
  293. }
  294. memcpy( dest + j, work, scan);
  295. }
  296. }
  297. }
  298. int
  299. falPcfGlyph(char *glyph, Oak_FontInf *finf, int code)
  300. {
  301. int encode;
  302. int inner_code;
  303. char *bitmap;
  304. int encodingOffset;
  305. int codeRow, codeCol;
  306. int bytewidth;
  307. int bmp_adj, ptn_adj;
  308. int adj_hi;
  309. int cpy_height;
  310. int bmp_height;
  311. if ( !glyph){
  312. fal_utyerrno = FAL_ERR_PARM ;
  313. return FAL_ERROR ;
  314. }
  315. inner_code = code;
  316. codeCol = inner_code & 0xff;
  317. codeRow = (inner_code >> 8) & 0xff;
  318. /* code check */
  319. if (
  320. ((code < finf->start) || (code > finf->end))||
  321. ((codeCol < finf->pFinf->firstCol)||(codeCol > finf->pFinf->lastCol))||
  322. ((codeRow < finf->pFinf->firstRow)||(codeRow > finf->pFinf->lastRow))
  323. ) {
  324. fal_utyexists = 1;
  325. return(-1);
  326. }
  327. encode = (codeRow - finf->pFinf->firstRow) * ( finf->pFinf->lastCol - finf->pFinf->firstCol + 1);
  328. encode += codeCol - finf->pFinf->firstCol;
  329. encodingOffset = getINT16( (unsigned char *)(finf->pcfinf.encodingOffsets + encode), finf->pcfinf.enc_fmt);
  330. if (encodingOffset == 0xFFFF) {
  331. fal_utyexists = 1;
  332. return(-1);
  333. }
  334. fal_utyexists = 0;
  335. bitmap = finf->pcfinf.bitmaps + getINT32( (unsigned char *)(finf->pcfinf.offsets + encodingOffset), finf->pcfinf.bmp_fmt);
  336. bmp_height = finf->pFinf->maxbounds.ascent
  337. + finf->pFinf->maxbounds.descent;
  338. if (( adj_hi = finf->pFinf->maxbounds.ascent
  339. - finf->pcfinf.org_bounds.ascent) > 0) {
  340. bytewidth = 8 * PCF_GLYPH_PAD( finf->pcfinf.bmp_fmt);
  341. bytewidth = (( finf->width + bytewidth - 1)/ bytewidth ) * PCF_GLYPH_PAD( finf->pcfinf.bmp_fmt);
  342. bmp_adj = bytewidth * adj_hi;
  343. ptn_adj = 0;
  344. if (( cpy_height = bmp_height - adj_hi) > finf->height)
  345. cpy_height = finf->height;
  346. } else if ( adj_hi < 0) {
  347. adj_hi *= -1;
  348. bytewidth = ( finf->width + 7) / 8;
  349. bmp_adj = 0;
  350. ptn_adj = bytewidth * adj_hi;
  351. if (( cpy_height = finf->height - adj_hi) > bmp_height)
  352. cpy_height = bmp_height;
  353. } else {
  354. bmp_adj = 0;
  355. ptn_adj = 0;
  356. cpy_height = finf->height;
  357. }
  358. repadBits( bitmap + bmp_adj , finf->pcfinf.bmp_fmt, finf->width, cpy_height, glyph + ptn_adj);
  359. return(0);
  360. }
  361. void
  362. falGetPcfGSize(
  363. struct pcf_inf *pcfinf,
  364. unsigned int *widthp,
  365. unsigned int *heightp)
  366. {
  367. unsigned int w, h;
  368. w = pcfinf->org_bounds.rightSideBearing
  369. - pcfinf->org_bounds.leftSideBearing;
  370. h = pcfinf->org_bounds.ascent + pcfinf->org_bounds.descent;
  371. *widthp = w;
  372. *heightp = h;
  373. }
  374. /********************************************************
  375. *
  376. * functions for collect GPF file properties
  377. *
  378. *******************************************************/
  379. #include <X11/fonts/fontstruct.h>
  380. static char *getPcfFontProp();
  381. static char *getSnfFontProp();
  382. /*
  383. * read properties and get font style and
  384. * letter size
  385. */
  386. int
  387. falReadFontProp(
  388. char *file, /* name of font file */
  389. int protect_key_data,
  390. FalFontData *databuff,
  391. int islock)
  392. {
  393. Oak_FontInf finf;
  394. int fd ;
  395. char *buf;
  396. char *openfontfile;
  397. struct stat st;
  398. int rtn ;
  399. /* initialezation */
  400. openfontfile = file ;
  401. /* read a condition of a fontfile */
  402. if ( stat( openfontfile, &st ) < 0 ) {
  403. set_errfile_str( fal_err_file, openfontfile ) ;
  404. fal_utyerror = _FAL_STAT_ER;
  405. fal_utyderror = errno;
  406. fal_utyerrno = FAL_ERR_STAT ;
  407. return FAL_ERROR;
  408. }
  409. if ( st.st_size < sizeof( FontInfoRec ) ) {
  410. fal_utyerror = _FAL_FONT_ER;
  411. fal_utyderror = 0;
  412. fal_utyerrno = FAL_ERR_FONT ;
  413. return FAL_ERROR;
  414. }
  415. /* open a fontfile */
  416. if ( (fd = open( openfontfile, ((islock)? O_RDONLY : O_RDWR) )) < 0 ) {
  417. switch( errno ) {
  418. case EACCES :
  419. return _FAL_TRY_NEXT ;
  420. default :
  421. set_errfile_str( fal_err_file, openfontfile ) ;
  422. fal_utyerror = _FAL_OPEN_ER;
  423. fal_utyderror = errno;
  424. fal_utyerrno = FAL_ERR_FNT_OPN ;
  425. return FAL_ERROR;
  426. }
  427. }
  428. #if defined( SVR4 )
  429. buf = (char *)mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
  430. if ( buf != (char *)-1 ) {
  431. /* if "mmap" is normal end */
  432. close( fd );
  433. finf.ismmap = TRUE;
  434. finf.fsize = st.st_size;
  435. } else {
  436. /* if "mmap" is abnormal end , try "read()" */
  437. finf.ismmap = FALSE;
  438. if ( !(buf = (char *)malloc(st.st_size)) ) {
  439. fal_utyerror = _FAL_MALOC_ER;
  440. fal_utyderror = 0;
  441. fal_utyerrno = FAL_ERR_MALLOC ;
  442. close( fd );
  443. return FAL_ERROR;
  444. }
  445. if ( read(fd, buf, st.st_size) != st.st_size ) {
  446. set_errfile_str( fal_err_file, openfontfile ) ;
  447. fal_utyerror = _FAL_READ_ER;
  448. fal_utyderror = errno;
  449. fal_utyerrno = FAL_ERR_FNT_RD ;
  450. free( buf );
  451. close( fd );
  452. return FAL_ERROR;
  453. }
  454. }
  455. #else
  456. finf.ismmap = FALSE;
  457. if ( !(buf = (char *)malloc( st.st_size )) ) {
  458. fal_utyerror = _FAL_MALOC_ER;
  459. fal_utyderror = 0;
  460. fal_utyerrno = FAL_ERR_MALLOC ;
  461. close( fd );
  462. return FAL_ERROR;
  463. }
  464. if ( read(fd, buf, st.st_size) != st.st_size ) {
  465. set_errfile_str( fal_err_file, openfontfile ) ;
  466. fal_utyerror = _FAL_READ_ER;
  467. fal_utyderror = errno;
  468. fal_utyerrno = FAL_ERR_FNT_RD ;
  469. free( buf );
  470. close( fd );
  471. return FAL_ERROR;
  472. }
  473. #endif
  474. finf.fname = openfontfile ;
  475. finf.buf = buf ;
  476. /*
  477. * open GPF font file
  478. */
  479. rtn = falReadGpfProp( FAL_UPDATE_FONTINFO, &finf, protect_key_data, databuff ) ;
  480. #if defined( SVR4 )
  481. if ( finf.ismmap == TRUE ) {
  482. munmap( buf, finf.fsize );
  483. } else {
  484. free(buf);
  485. close(fd);
  486. }
  487. #else
  488. free( buf );
  489. close( fd );
  490. #endif
  491. return( rtn ) ;
  492. }
  493. /*
  494. * get properties of GPF format file
  495. */
  496. int
  497. falReadGpfProp(
  498. int updflg,
  499. Oak_FontInf *finf,
  500. int protect_key_data,
  501. FalFontData *databuff)
  502. {
  503. char *openfontfile;
  504. int rtn ;
  505. /* initialize */
  506. openfontfile = finf->fname ;
  507. if( updflg == FAL_UPDATE_FONTINFO ) {
  508. /* case of a PCF format font */
  509. if ( strcmp( FILE_SUFFIX( openfontfile ), PCFSUFFIX ) == 0 ) {
  510. finf->isFef = FALSE;
  511. finf->isPcf = TRUE;
  512. }
  513. else /* case of a SNF format font */
  514. {
  515. FAL_READ_SNF_HEADER( finf, protect_key_data, fal_utyerror, fal_utyderror ) ;
  516. }
  517. } /* updflg */
  518. /*
  519. * get font properties
  520. */
  521. if ( finf->isFef ){
  522. /* read SNF format property */
  523. if ( rtn = falInitReadSnfProp( finf, (caddr_t)finf->pFinf, databuff ) ) {
  524. if( rtn == _FAL_TRY_NEXT ){
  525. return( rtn ) ;
  526. }
  527. fal_utyerror = _FAL_FONT_ER;
  528. fal_utyderror = 0;
  529. return FAL_ERROR;
  530. }
  531. } else if ( finf->isPcf ) {
  532. /* read PCF format property */
  533. if ( rtn = falInitReadPcfProp( updflg, finf, databuff ) ) {
  534. if( rtn == _FAL_TRY_NEXT ){
  535. return( rtn ) ;
  536. }
  537. fal_utyerror = _FAL_FONT_ER;
  538. fal_utyderror = 0;
  539. return FAL_ERROR;
  540. }
  541. if( updflg == FAL_UPDATE_FONTINFO ) {
  542. finf->pFinf = &finf->pcfinf.info;
  543. finf->pCinf = NULL;
  544. finf->pGlyphs = NULL;
  545. }
  546. } else {
  547. fal_utyerror = _FAL_FONT_ER;
  548. fal_utyderror = 0;
  549. fal_utyerrno = FAL_ERR_FONT ;
  550. return FAL_ERROR;
  551. }
  552. return(0);
  553. }
  554. /*
  555. * get properties of PCF format file
  556. */
  557. int
  558. falInitReadPcfProp(int updflg, Oak_FontInf *finf, FalFontData *databuff)
  559. {
  560. struct pcf_inf *pcfinf;
  561. caddr_t buftop;
  562. xCharInfo maxink;
  563. caddr_t buffp;
  564. int lb, rb, as, ds ;
  565. pcfinf = &finf->pcfinf ;
  566. buftop = finf->buf ;
  567. if ( getLSB32( (unsigned char *)buftop ) != PCF_FILE_VERSION)
  568. return _FAL_TRY_NEXT ;
  569. if( updflg == FAL_UPDATE_FONTINFO ) {
  570. pcfinf->ntables = getLSB32( (unsigned char *)(buftop + 4) );
  571. pcfinf->tables = (PCFTablePtr)(buftop + 8);
  572. if ( !getAccel( &pcfinf->info, &maxink, buftop, pcfinf->tables,
  573. pcfinf->ntables, (CARD32)PCF_BDF_ACCELERATORS)) {
  574. if ( !getAccel( &pcfinf->info, &maxink, buftop, pcfinf->tables,
  575. pcfinf->ntables, (CARD32)PCF_ACCELERATORS)) {
  576. fal_utyerrno = FAL_ERR_FONT ;
  577. return -1;
  578. }
  579. }
  580. }
  581. /*
  582. * read property
  583. */
  584. if( updflg == FAL_UPDATE_FONTINFO ) {
  585. pcfinf->org_bounds = pcfinf->info.maxbounds;
  586. }
  587. lb = pcfinf->org_bounds.leftSideBearing ;
  588. rb = pcfinf->org_bounds.rightSideBearing ;
  589. as = pcfinf->org_bounds.ascent ;
  590. ds = pcfinf->org_bounds.descent ;
  591. databuff->size.w = rb - lb ;
  592. databuff->size.h = as + ds ;
  593. /*
  594. * read property "FONT"
  595. */
  596. if ( buffp = getPcfFontProp( buftop, pcfinf->tables,
  597. pcfinf->ntables, "FONT" )) {
  598. if( (databuff->xlfdname = (char *)strdup( buffp )) == (char *)NULL ){
  599. fal_utyerrno = FAL_ERR_MALLOC ;
  600. return -1;
  601. }
  602. }else{
  603. set_errfile_str( fal_err_file, finf->fname ) ;
  604. fal_utyerrno = FAL_ERR_PROP_FONT ;
  605. return( _FAL_TRY_NEXT ) ;
  606. }
  607. /*
  608. * read property "FAMILY_NAME"
  609. */
  610. if ( buffp = getPcfFontProp( buftop, pcfinf->tables,
  611. pcfinf->ntables, "FAMILY_NAME")) {
  612. if( (databuff->style.name = (char *)strdup( buffp )) == NULL ){
  613. fal_utyerrno = FAL_ERR_MALLOC ;
  614. return -1;
  615. }
  616. }else{
  617. set_errfile_str( fal_err_file, finf->fname ) ;
  618. fal_utyerrno = FAL_ERR_PROP_FNAME ;
  619. return( _FAL_TRY_NEXT ) ;
  620. }
  621. return 0;
  622. }
  623. static char *
  624. getPcfFontProp(caddr_t buftop, PCFTablePtr tables, int ntables, char *propname)
  625. {
  626. caddr_t buffer;
  627. int name_ofs;
  628. int i ;
  629. int nprops;
  630. char *propstr ;
  631. CARD32 format, offset;
  632. if ( !seekToType( tables, ntables, (CARD32)PCF_PROPERTIES, &format, (CARD32 *)NULL, &offset))
  633. return NULL;
  634. buffer = buftop + offset;
  635. format = getLSB32( (unsigned char *)buffer );
  636. buffer += 4;
  637. if ( !PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT))
  638. return NULL;
  639. nprops = getINT32( (unsigned char *)buffer, (CARD32)format);
  640. buffer += 4;
  641. propstr = buffer + (4 * ((nprops * 9 + 3) / 4)) + 4;
  642. for ( i=0; i < nprops ; i++ ) {
  643. name_ofs = getINT32( (unsigned char *)buffer, (CARD32)format ) ;
  644. buffer += 4 ; /* name */
  645. if ( strcmp( propstr + name_ofs, propname) == 0) {
  646. if ( *buffer) { /* isStringProp */
  647. return( propstr + getINT32( (unsigned char *)(buffer + 1), (CARD32)format) );
  648. }else{
  649. return((char *)(intptr_t)getINT32( (unsigned char *)(buffer + 1), (CARD32)format) );
  650. }
  651. }
  652. buffer += 5 ; /* isStringProp + value */
  653. }
  654. return( NULL);
  655. }
  656. /*
  657. * get properties of SNF format file
  658. */
  659. int
  660. falInitReadSnfProp(
  661. Oak_FontInf *finf, /* pointer to the infomation structure */
  662. caddr_t buftop, /* font file */
  663. FalFontData *databuff)
  664. {
  665. caddr_t stprop ;
  666. int lb, rb, as, ds ;
  667. char *propptr ;
  668. char *fnt = NULL, *fam ;
  669. int nprops, bitmapSize, num_chars ;
  670. /* initialize pointer */
  671. nprops = finf->pFinf->nprops ;
  672. num_chars = ( finf->pFinf->lastRow - finf->pFinf->firstRow + 1 ) *
  673. ( finf->pFinf->lastCol - finf->pFinf->firstCol + 1 ) ;
  674. stprop = buftop ;
  675. stprop += sizeof(FontInfoRec) ;
  676. stprop += num_chars * sizeof(CharInfoRec) ;
  677. /*
  678. * read property "FONTBOUNDINGBOX"
  679. */
  680. lb = finf->pFinf->maxbounds.leftSideBearing ;
  681. rb = finf->pFinf->maxbounds.rightSideBearing ;
  682. as = finf->pFinf->maxbounds.ascent ;
  683. ds = finf->pFinf->maxbounds.descent ;
  684. /*
  685. * read property "FONT"
  686. */
  687. if ( propptr = getSnfFontProp( stprop, nprops, "FONT" )) {
  688. if( (fnt = (char *)strdup( propptr )) == NULL ){
  689. fal_utyerrno = FAL_ERR_MALLOC ;
  690. free(fnt);
  691. return -1;
  692. }
  693. }else{
  694. set_errfile_str( fal_err_file, finf->fname ) ;
  695. fal_utyerrno = FAL_ERR_PROP_FONT ;
  696. return( _FAL_TRY_NEXT ) ;
  697. }
  698. /*
  699. * read property "FAMILY_NAME"
  700. */
  701. if ( propptr = getSnfFontProp( stprop, nprops, "FAMILY_NAME")) {
  702. if( (fam = (char *)strdup( propptr )) == NULL ){
  703. fal_utyerrno = FAL_ERR_MALLOC ;
  704. free(fnt);
  705. return -1;
  706. }
  707. }else{
  708. set_errfile_str( fal_err_file, finf->fname ) ;
  709. fal_utyerrno = FAL_ERR_PROP_FNAME ;
  710. free(fnt);
  711. return( _FAL_TRY_NEXT ) ;
  712. }
  713. /*
  714. * set data buffer
  715. */
  716. databuff->size.w = rb - lb ;
  717. databuff->size.h = as + ds ;
  718. databuff->xlfdname = fnt ;
  719. databuff->style.name = fam ;
  720. return 0;
  721. }
  722. static char *
  723. getSnfFontProp(caddr_t buftop, int nprops, char *propname)
  724. {
  725. caddr_t buffer;
  726. int name_ofs;
  727. int i ;
  728. char *propstr ;
  729. FontPropRec *ProcRec ;
  730. ProcRec = (FontPropRec *)buftop ;
  731. buffer = (char *)buftop ;
  732. propstr = buffer + nprops * sizeof(FontPropRec) ;
  733. for ( i=0; i < nprops ; i++, ProcRec++ ) {
  734. name_ofs = ProcRec->name ;
  735. if( strcmp( propstr + name_ofs, propname ) == 0 ){
  736. return( (char *) (intptr_t) ProcRec->value ) ;
  737. }
  738. }
  739. return( NULL);
  740. }
  741. /***********************< end of readpcf.c >********************/