pcfread.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /* pcfread.c
  2. FreeType font driver for pcf fonts
  3. Copyright 2000-2001, 2002 by
  4. Francesco Zappa Nardelli
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. */
  21. #include <ft2build.h>
  22. #include FT_INTERNAL_DEBUG_H
  23. #include FT_INTERNAL_STREAM_H
  24. #include FT_INTERNAL_OBJECTS_H
  25. #include "pcf.h"
  26. #include "pcfdriver.h"
  27. #include "pcferror.h"
  28. /*************************************************************************/
  29. /* */
  30. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  31. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  32. /* messages during execution. */
  33. /* */
  34. #undef FT_COMPONENT
  35. #define FT_COMPONENT trace_pcfread
  36. #if defined( FT_DEBUG_LEVEL_TRACE )
  37. static const char* tableNames[] =
  38. {
  39. "prop", "accl", "mtrcs", "bmps", "imtrcs",
  40. "enc", "swidth", "names", "accel"
  41. };
  42. #endif
  43. static
  44. const FT_Frame_Field pcf_toc_header[] =
  45. {
  46. #undef FT_STRUCTURE
  47. #define FT_STRUCTURE PCF_TocRec
  48. FT_FRAME_START( 8 ),
  49. FT_FRAME_ULONG_LE( version ),
  50. FT_FRAME_ULONG_LE( count ),
  51. FT_FRAME_END
  52. };
  53. static
  54. const FT_Frame_Field pcf_table_header[] =
  55. {
  56. #undef FT_STRUCTURE
  57. #define FT_STRUCTURE PCF_TableRec
  58. FT_FRAME_START( 16 ),
  59. FT_FRAME_ULONG_LE( type ),
  60. FT_FRAME_ULONG_LE( format ),
  61. FT_FRAME_ULONG_LE( size ),
  62. FT_FRAME_ULONG_LE( offset ),
  63. FT_FRAME_END
  64. };
  65. static FT_Error
  66. pcf_read_TOC( FT_Stream stream,
  67. PCF_Face face )
  68. {
  69. FT_Error error;
  70. PCF_Toc toc = &face->toc;
  71. PCF_Table tables;
  72. FT_Memory memory = FT_FACE(face)->memory;
  73. FT_UInt n;
  74. if ( FT_STREAM_SEEK ( 0 ) ||
  75. FT_STREAM_READ_FIELDS ( pcf_toc_header, toc ) )
  76. return PCF_Err_Cannot_Open_Resource;
  77. if ( toc->version != PCF_FILE_VERSION )
  78. return PCF_Err_Invalid_File_Format;
  79. if ( FT_NEW_ARRAY( face->toc.tables, toc->count ) )
  80. return PCF_Err_Out_Of_Memory;
  81. tables = face->toc.tables;
  82. for ( n = 0; n < toc->count; n++ )
  83. {
  84. if ( FT_STREAM_READ_FIELDS( pcf_table_header, tables ) )
  85. goto Exit;
  86. tables++;
  87. }
  88. #if defined( FT_DEBUG_LEVEL_TRACE )
  89. {
  90. FT_UInt i, j;
  91. const char* name = "?";
  92. FT_TRACE4(( "Tables count: %ld\n", face->toc.count ));
  93. tables = face->toc.tables;
  94. for ( i = 0; i < toc->count; i++ )
  95. {
  96. for( j = 0; j < sizeof ( tableNames ) / sizeof ( tableNames[0] ); j++ )
  97. if ( tables[i].type == (FT_UInt)( 1 << j ) )
  98. name = tableNames[j];
  99. FT_TRACE4(( "Table %d: type=%-6s format=0x%04lX "
  100. "size=0x%06lX (%8ld) offset=0x%04lX\n",
  101. i, name,
  102. tables[i].format,
  103. tables[i].size, tables[i].size,
  104. tables[i].offset ));
  105. }
  106. }
  107. #endif
  108. return PCF_Err_Ok;
  109. Exit:
  110. FT_FREE( face->toc.tables );
  111. return error;
  112. }
  113. static
  114. const FT_Frame_Field pcf_metric_header[] =
  115. {
  116. #undef FT_STRUCTURE
  117. #define FT_STRUCTURE PCF_MetricRec
  118. FT_FRAME_START( 12 ),
  119. FT_FRAME_SHORT_LE( leftSideBearing ),
  120. FT_FRAME_SHORT_LE( rightSideBearing ),
  121. FT_FRAME_SHORT_LE( characterWidth ),
  122. FT_FRAME_SHORT_LE( ascent ),
  123. FT_FRAME_SHORT_LE( descent ),
  124. FT_FRAME_SHORT_LE( attributes ),
  125. FT_FRAME_END
  126. };
  127. static
  128. const FT_Frame_Field pcf_metric_msb_header[] =
  129. {
  130. #undef FT_STRUCTURE
  131. #define FT_STRUCTURE PCF_MetricRec
  132. FT_FRAME_START( 12 ),
  133. FT_FRAME_SHORT( leftSideBearing ),
  134. FT_FRAME_SHORT( rightSideBearing ),
  135. FT_FRAME_SHORT( characterWidth ),
  136. FT_FRAME_SHORT( ascent ),
  137. FT_FRAME_SHORT( descent ),
  138. FT_FRAME_SHORT( attributes ),
  139. FT_FRAME_END
  140. };
  141. static
  142. const FT_Frame_Field pcf_compressed_metric_header[] =
  143. {
  144. #undef FT_STRUCTURE
  145. #define FT_STRUCTURE PCF_Compressed_MetricRec
  146. FT_FRAME_START( 5 ),
  147. FT_FRAME_BYTE( leftSideBearing ),
  148. FT_FRAME_BYTE( rightSideBearing ),
  149. FT_FRAME_BYTE( characterWidth ),
  150. FT_FRAME_BYTE( ascent ),
  151. FT_FRAME_BYTE( descent ),
  152. FT_FRAME_END
  153. };
  154. static FT_Error
  155. pcf_get_metric( FT_Stream stream,
  156. FT_ULong format,
  157. PCF_Metric metric )
  158. {
  159. FT_Error error = PCF_Err_Ok;
  160. if ( PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT ) )
  161. {
  162. const FT_Frame_Field* fields;
  163. /* parsing normal metrics */
  164. fields = PCF_BYTE_ORDER( format ) == MSBFirst
  165. ? pcf_metric_msb_header
  166. : pcf_metric_header;
  167. /* the following sets 'error' but doesn't return in case of failure */
  168. (void)FT_STREAM_READ_FIELDS( fields, metric );
  169. }
  170. else
  171. {
  172. PCF_Compressed_MetricRec compr;
  173. /* parsing compressed metrics */
  174. if ( FT_STREAM_READ_FIELDS( pcf_compressed_metric_header, &compr ) )
  175. goto Exit;
  176. metric->leftSideBearing = (FT_Short)( compr.leftSideBearing - 0x80 );
  177. metric->rightSideBearing = (FT_Short)( compr.rightSideBearing - 0x80 );
  178. metric->characterWidth = (FT_Short)( compr.characterWidth - 0x80 );
  179. metric->ascent = (FT_Short)( compr.ascent - 0x80 );
  180. metric->descent = (FT_Short)( compr.descent - 0x80 );
  181. metric->attributes = 0;
  182. }
  183. Exit:
  184. return error;
  185. }
  186. static FT_Error
  187. pcf_seek_to_table_type( FT_Stream stream,
  188. PCF_Table tables,
  189. FT_Int ntables,
  190. FT_ULong type,
  191. FT_ULong *aformat,
  192. FT_ULong *asize )
  193. {
  194. FT_Error error = 0;
  195. FT_Int i;
  196. for ( i = 0; i < ntables; i++ )
  197. if ( tables[i].type == type )
  198. {
  199. if ( stream->pos > tables[i].offset )
  200. return PCF_Err_Invalid_Stream_Skip;
  201. if ( FT_STREAM_SKIP( tables[i].offset - stream->pos ) )
  202. return PCF_Err_Invalid_Stream_Skip;
  203. *asize = tables[i].size; /* unused - to be removed */
  204. *aformat = tables[i].format;
  205. return PCF_Err_Ok;
  206. }
  207. return PCF_Err_Invalid_File_Format;
  208. }
  209. static FT_Bool
  210. pcf_has_table_type( PCF_Table tables,
  211. FT_Int ntables,
  212. FT_ULong type )
  213. {
  214. FT_Int i;
  215. for ( i = 0; i < ntables; i++ )
  216. if ( tables[i].type == type )
  217. return TRUE;
  218. return FALSE;
  219. }
  220. static
  221. const FT_Frame_Field pcf_property_header[] =
  222. {
  223. #undef FT_STRUCTURE
  224. #define FT_STRUCTURE PCF_ParsePropertyRec
  225. FT_FRAME_START( 9 ),
  226. FT_FRAME_LONG_LE( name ),
  227. FT_FRAME_BYTE ( isString ),
  228. FT_FRAME_LONG_LE( value ),
  229. FT_FRAME_END
  230. };
  231. static
  232. const FT_Frame_Field pcf_property_msb_header[] =
  233. {
  234. #undef FT_STRUCTURE
  235. #define FT_STRUCTURE PCF_ParsePropertyRec
  236. FT_FRAME_START( 9 ),
  237. FT_FRAME_LONG( name ),
  238. FT_FRAME_BYTE( isString ),
  239. FT_FRAME_LONG( value ),
  240. FT_FRAME_END
  241. };
  242. static PCF_Property
  243. pcf_find_property( PCF_Face face,
  244. const FT_String* prop )
  245. {
  246. PCF_Property properties = face->properties;
  247. FT_Bool found = 0;
  248. int i;
  249. for ( i = 0 ; i < face->nprops && !found; i++ )
  250. {
  251. if ( !ft_strcmp( properties[i].name, prop ) )
  252. found = 1;
  253. }
  254. if ( found )
  255. return properties + i - 1;
  256. else
  257. return NULL;
  258. }
  259. static FT_Error
  260. pcf_get_properties( FT_Stream stream,
  261. PCF_Face face )
  262. {
  263. PCF_ParseProperty props = 0;
  264. PCF_Property properties = 0;
  265. FT_Int nprops, i;
  266. FT_ULong format, size;
  267. FT_Error error;
  268. FT_Memory memory = FT_FACE(face)->memory;
  269. FT_ULong string_size;
  270. FT_String* strings = 0;
  271. error = pcf_seek_to_table_type( stream,
  272. face->toc.tables,
  273. face->toc.count,
  274. PCF_PROPERTIES,
  275. &format,
  276. &size );
  277. if ( error )
  278. goto Bail;
  279. if ( FT_READ_ULONG_LE( format ) )
  280. goto Bail;
  281. FT_TRACE4(( "get_prop: format = %ld\n", format ));
  282. if ( !PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT ) )
  283. goto Bail;
  284. if ( PCF_BYTE_ORDER( format ) == MSBFirst )
  285. (void)FT_READ_ULONG( nprops );
  286. else
  287. (void)FT_READ_ULONG_LE( nprops );
  288. if ( error )
  289. goto Bail;
  290. FT_TRACE4(( "get_prop: nprop = %d\n", nprops ));
  291. if ( FT_NEW_ARRAY( props, nprops ) )
  292. goto Bail;
  293. for ( i = 0; i < nprops; i++ )
  294. {
  295. if ( PCF_BYTE_ORDER( format ) == MSBFirst )
  296. {
  297. if ( FT_STREAM_READ_FIELDS( pcf_property_msb_header, props + i ) )
  298. goto Bail;
  299. }
  300. else
  301. {
  302. if ( FT_STREAM_READ_FIELDS( pcf_property_header, props + i ) )
  303. goto Bail;
  304. }
  305. }
  306. /* pad the property array */
  307. /* */
  308. /* clever here - nprops is the same as the number of odd-units read, */
  309. /* as only isStringProp are odd length (Keith Packard) */
  310. /* */
  311. if ( nprops & 3 )
  312. {
  313. i = 4 - ( nprops & 3 );
  314. FT_Stream_Skip( stream, i );
  315. }
  316. if ( PCF_BYTE_ORDER( format ) == MSBFirst )
  317. (void)FT_READ_ULONG( string_size );
  318. else
  319. (void)FT_READ_ULONG_LE( string_size );
  320. if ( error )
  321. goto Bail;
  322. FT_TRACE4(( "get_prop: string_size = %ld\n", string_size ));
  323. if ( FT_NEW_ARRAY( strings, string_size ) )
  324. goto Bail;
  325. error = FT_Stream_Read( stream, (FT_Byte*)strings, string_size );
  326. if ( error )
  327. goto Bail;
  328. if ( FT_NEW_ARRAY( properties, nprops ) )
  329. goto Bail;
  330. for ( i = 0; i < nprops; i++ )
  331. {
  332. /* XXX: make atom */
  333. if ( FT_NEW_ARRAY( properties[i].name,
  334. ft_strlen( strings + props[i].name ) + 1 ) )
  335. goto Bail;
  336. ft_strcpy( properties[i].name,strings + props[i].name );
  337. properties[i].isString = props[i].isString;
  338. if ( props[i].isString )
  339. {
  340. if ( FT_NEW_ARRAY( properties[i].value.atom,
  341. ft_strlen( strings + props[i].value ) + 1 ) )
  342. goto Bail;
  343. ft_strcpy( properties[i].value.atom, strings + props[i].value );
  344. }
  345. else
  346. properties[i].value.integer = props[i].value;
  347. }
  348. face->properties = properties;
  349. face->nprops = nprops;
  350. FT_FREE( props );
  351. FT_FREE( strings );
  352. return PCF_Err_Ok;
  353. Bail:
  354. FT_FREE( props );
  355. FT_FREE( strings );
  356. return error;
  357. }
  358. static FT_Error
  359. pcf_get_metrics( FT_Stream stream,
  360. PCF_Face face )
  361. {
  362. FT_Error error = PCF_Err_Ok;
  363. FT_Memory memory = FT_FACE(face)->memory;
  364. FT_ULong format = 0;
  365. FT_ULong size = 0;
  366. PCF_Metric metrics = 0;
  367. int i;
  368. int nmetrics = -1;
  369. error = pcf_seek_to_table_type( stream,
  370. face->toc.tables,
  371. face->toc.count,
  372. PCF_METRICS,
  373. &format,
  374. &size );
  375. if ( error )
  376. return error;
  377. error = FT_READ_ULONG_LE( format );
  378. if ( !PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT ) &&
  379. !PCF_FORMAT_MATCH( format, PCF_COMPRESSED_METRICS ) )
  380. return PCF_Err_Invalid_File_Format;
  381. if ( PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT ) )
  382. {
  383. if ( PCF_BYTE_ORDER( format ) == MSBFirst )
  384. (void)FT_READ_ULONG( nmetrics );
  385. else
  386. (void)FT_READ_ULONG_LE( nmetrics );
  387. }
  388. else
  389. {
  390. if ( PCF_BYTE_ORDER( format ) == MSBFirst )
  391. (void)FT_READ_USHORT( nmetrics );
  392. else
  393. (void)FT_READ_USHORT_LE( nmetrics );
  394. }
  395. if ( error || nmetrics == -1 )
  396. return PCF_Err_Invalid_File_Format;
  397. face->nmetrics = nmetrics;
  398. if ( FT_NEW_ARRAY( face->metrics, nmetrics ) )
  399. return PCF_Err_Out_Of_Memory;
  400. metrics = face->metrics;
  401. for ( i = 0; i < nmetrics; i++ )
  402. {
  403. pcf_get_metric( stream, format, metrics + i );
  404. metrics[i].bits = 0;
  405. FT_TRACE4(( "%d : width=%d, "
  406. "lsb=%d, rsb=%d, ascent=%d, descent=%d, swidth=%d\n",
  407. i,
  408. ( metrics + i )->characterWidth,
  409. ( metrics + i )->leftSideBearing,
  410. ( metrics + i )->rightSideBearing,
  411. ( metrics + i )->ascent,
  412. ( metrics + i )->descent,
  413. ( metrics + i )->attributes ));
  414. if ( error )
  415. break;
  416. }
  417. if ( error )
  418. FT_FREE( face->metrics );
  419. return error;
  420. }
  421. static FT_Error
  422. pcf_get_bitmaps( FT_Stream stream,
  423. PCF_Face face )
  424. {
  425. FT_Error error = PCF_Err_Ok;
  426. FT_Memory memory = FT_FACE(face)->memory;
  427. FT_Long* offsets;
  428. FT_Long bitmapSizes[GLYPHPADOPTIONS];
  429. FT_ULong format, size;
  430. int nbitmaps, i, sizebitmaps = 0;
  431. char* bitmaps;
  432. error = pcf_seek_to_table_type( stream,
  433. face->toc.tables,
  434. face->toc.count,
  435. PCF_BITMAPS,
  436. &format,
  437. &size );
  438. if ( error )
  439. return error;
  440. error = FT_Stream_EnterFrame( stream, 8 );
  441. if ( error )
  442. return error;
  443. format = FT_GET_ULONG_LE();
  444. if ( PCF_BYTE_ORDER( format ) == MSBFirst )
  445. nbitmaps = FT_GET_ULONG();
  446. else
  447. nbitmaps = FT_GET_ULONG_LE();
  448. FT_Stream_ExitFrame( stream );
  449. if ( !PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT ) )
  450. return PCF_Err_Invalid_File_Format;
  451. if ( nbitmaps != face->nmetrics )
  452. return PCF_Err_Invalid_File_Format;
  453. if ( FT_NEW_ARRAY( offsets, nbitmaps ) )
  454. return error;
  455. for ( i = 0; i < nbitmaps; i++ )
  456. {
  457. if ( PCF_BYTE_ORDER( format ) == MSBFirst )
  458. (void)FT_READ_LONG( offsets[i] );
  459. else
  460. (void)FT_READ_LONG_LE( offsets[i] );
  461. FT_TRACE4(( "bitmap %d is at offset %ld\n", i, offsets[i] ));
  462. }
  463. if ( error )
  464. goto Bail;
  465. for ( i = 0; i < GLYPHPADOPTIONS; i++ )
  466. {
  467. if ( PCF_BYTE_ORDER( format ) == MSBFirst )
  468. (void)FT_READ_LONG( bitmapSizes[i] );
  469. else
  470. (void)FT_READ_LONG_LE( bitmapSizes[i] );
  471. if ( error )
  472. goto Bail;
  473. sizebitmaps = bitmapSizes[PCF_GLYPH_PAD_INDEX( format )];
  474. FT_TRACE4(( "padding %d implies a size of %ld\n", i, bitmapSizes[i] ));
  475. }
  476. FT_TRACE4(( " %d bitmaps, padding index %ld\n",
  477. nbitmaps,
  478. PCF_GLYPH_PAD_INDEX( format ) ));
  479. FT_TRACE4(( "bitmap size = %d\n", sizebitmaps ));
  480. FT_UNUSED( sizebitmaps ); /* only used for debugging */
  481. for ( i = 0; i < nbitmaps; i++ )
  482. face->metrics[i].bits = stream->pos + offsets[i];
  483. face->bitmapsFormat = format;
  484. FT_FREE ( offsets );
  485. return error;
  486. Bail:
  487. FT_FREE ( offsets );
  488. FT_FREE ( bitmaps );
  489. return error;
  490. }
  491. static FT_Error
  492. pcf_get_encodings( FT_Stream stream,
  493. PCF_Face face )
  494. {
  495. FT_Error error = PCF_Err_Ok;
  496. FT_Memory memory = FT_FACE(face)->memory;
  497. FT_ULong format, size;
  498. int firstCol, lastCol;
  499. int firstRow, lastRow;
  500. int nencoding, encodingOffset;
  501. int i, j;
  502. PCF_Encoding tmpEncoding, encoding = 0;
  503. error = pcf_seek_to_table_type( stream,
  504. face->toc.tables,
  505. face->toc.count,
  506. PCF_BDF_ENCODINGS,
  507. &format,
  508. &size );
  509. if ( error )
  510. return error;
  511. error = FT_Stream_EnterFrame( stream, 14 );
  512. if ( error )
  513. return error;
  514. format = FT_GET_ULONG_LE();
  515. if ( PCF_BYTE_ORDER( format ) == MSBFirst )
  516. {
  517. firstCol = FT_GET_SHORT();
  518. lastCol = FT_GET_SHORT();
  519. firstRow = FT_GET_SHORT();
  520. lastRow = FT_GET_SHORT();
  521. face->defaultChar = FT_GET_SHORT();
  522. }
  523. else
  524. {
  525. firstCol = FT_GET_SHORT_LE();
  526. lastCol = FT_GET_SHORT_LE();
  527. firstRow = FT_GET_SHORT_LE();
  528. lastRow = FT_GET_SHORT_LE();
  529. face->defaultChar = FT_GET_SHORT_LE();
  530. }
  531. FT_Stream_ExitFrame( stream );
  532. if ( !PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT ) )
  533. return PCF_Err_Invalid_File_Format;
  534. FT_TRACE4(( "enc: firstCol %d, lastCol %d, firstRow %d, lastRow %d\n",
  535. firstCol, lastCol, firstRow, lastRow ));
  536. nencoding = ( lastCol - firstCol + 1 ) * ( lastRow - firstRow + 1 );
  537. if ( FT_NEW_ARRAY( tmpEncoding, nencoding ) )
  538. return PCF_Err_Out_Of_Memory;
  539. error = FT_Stream_EnterFrame( stream, 2 * nencoding );
  540. if ( error )
  541. goto Bail;
  542. for ( i = 0, j = 0 ; i < nencoding; i++ )
  543. {
  544. if ( PCF_BYTE_ORDER( format ) == MSBFirst )
  545. encodingOffset = FT_GET_SHORT();
  546. else
  547. encodingOffset = FT_GET_SHORT_LE();
  548. if ( encodingOffset != -1 )
  549. {
  550. tmpEncoding[j].enc = ( ( ( i / ( lastCol - firstCol + 1 ) ) +
  551. firstRow ) * 256 ) +
  552. ( ( i % ( lastCol - firstCol + 1 ) ) +
  553. firstCol );
  554. tmpEncoding[j].glyph = (FT_Short)encodingOffset;
  555. j++;
  556. }
  557. FT_TRACE4(( "enc n. %d ; Uni %ld ; Glyph %d\n",
  558. i, tmpEncoding[j - 1].enc, encodingOffset ));
  559. }
  560. FT_Stream_ExitFrame( stream );
  561. if ( FT_NEW_ARRAY( encoding, j ) )
  562. goto Bail;
  563. for ( i = 0; i < j; i++ )
  564. {
  565. encoding[i].enc = tmpEncoding[i].enc;
  566. encoding[i].glyph = tmpEncoding[i].glyph;
  567. }
  568. face->nencodings = j;
  569. face->encodings = encoding;
  570. FT_FREE( tmpEncoding );
  571. return error;
  572. Bail:
  573. FT_FREE( encoding );
  574. FT_FREE( tmpEncoding );
  575. return error;
  576. }
  577. static
  578. const FT_Frame_Field pcf_accel_header[] =
  579. {
  580. #undef FT_STRUCTURE
  581. #define FT_STRUCTURE PCF_AccelRec
  582. FT_FRAME_START( 20 ),
  583. FT_FRAME_BYTE ( noOverlap ),
  584. FT_FRAME_BYTE ( constantMetrics ),
  585. FT_FRAME_BYTE ( terminalFont ),
  586. FT_FRAME_BYTE ( constantWidth ),
  587. FT_FRAME_BYTE ( inkInside ),
  588. FT_FRAME_BYTE ( inkMetrics ),
  589. FT_FRAME_BYTE ( drawDirection ),
  590. FT_FRAME_SKIP_BYTES( 1 ),
  591. FT_FRAME_LONG_LE ( fontAscent ),
  592. FT_FRAME_LONG_LE ( fontDescent ),
  593. FT_FRAME_LONG_LE ( maxOverlap ),
  594. FT_FRAME_END
  595. };
  596. static
  597. const FT_Frame_Field pcf_accel_msb_header[] =
  598. {
  599. #undef FT_STRUCTURE
  600. #define FT_STRUCTURE PCF_AccelRec
  601. FT_FRAME_START( 20 ),
  602. FT_FRAME_BYTE ( noOverlap ),
  603. FT_FRAME_BYTE ( constantMetrics ),
  604. FT_FRAME_BYTE ( terminalFont ),
  605. FT_FRAME_BYTE ( constantWidth ),
  606. FT_FRAME_BYTE ( inkInside ),
  607. FT_FRAME_BYTE ( inkMetrics ),
  608. FT_FRAME_BYTE ( drawDirection ),
  609. FT_FRAME_SKIP_BYTES( 1 ),
  610. FT_FRAME_LONG ( fontAscent ),
  611. FT_FRAME_LONG ( fontDescent ),
  612. FT_FRAME_LONG ( maxOverlap ),
  613. FT_FRAME_END
  614. };
  615. static FT_Error
  616. pcf_get_accel( FT_Stream stream,
  617. PCF_Face face,
  618. FT_ULong type )
  619. {
  620. FT_ULong format, size;
  621. FT_Error error = PCF_Err_Ok;
  622. PCF_Accel accel = &face->accel;
  623. error = pcf_seek_to_table_type( stream,
  624. face->toc.tables,
  625. face->toc.count,
  626. type,
  627. &format,
  628. &size );
  629. if ( error )
  630. goto Bail;
  631. error = FT_READ_ULONG_LE( format );
  632. if ( !PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT ) &&
  633. !PCF_FORMAT_MATCH( format, PCF_ACCEL_W_INKBOUNDS ) )
  634. goto Bail;
  635. if ( PCF_BYTE_ORDER( format ) == MSBFirst )
  636. {
  637. if ( FT_STREAM_READ_FIELDS( pcf_accel_msb_header, accel ) )
  638. goto Bail;
  639. }
  640. else
  641. {
  642. if ( FT_STREAM_READ_FIELDS( pcf_accel_header, accel ) )
  643. goto Bail;
  644. }
  645. error = pcf_get_metric( stream,
  646. format & ( ~PCF_FORMAT_MASK ),
  647. &(accel->minbounds) );
  648. if ( error )
  649. goto Bail;
  650. error = pcf_get_metric( stream,
  651. format & ( ~PCF_FORMAT_MASK ),
  652. &(accel->maxbounds) );
  653. if ( error )
  654. goto Bail;
  655. if ( PCF_FORMAT_MATCH( format, PCF_ACCEL_W_INKBOUNDS ) )
  656. {
  657. error = pcf_get_metric( stream,
  658. format & ( ~PCF_FORMAT_MASK ),
  659. &(accel->ink_minbounds) );
  660. if ( error )
  661. goto Bail;
  662. error = pcf_get_metric( stream,
  663. format & ( ~PCF_FORMAT_MASK ),
  664. &(accel->ink_maxbounds) );
  665. if ( error )
  666. goto Bail;
  667. }
  668. else
  669. {
  670. accel->ink_minbounds = accel->minbounds; /* I'm not sure about this */
  671. accel->ink_maxbounds = accel->maxbounds;
  672. }
  673. return error;
  674. Bail:
  675. return error;
  676. }
  677. FT_LOCAL_DEF( FT_Error )
  678. pcf_load_font( FT_Stream stream,
  679. PCF_Face face )
  680. {
  681. FT_Error error = PCF_Err_Ok;
  682. FT_Memory memory = FT_FACE(face)->memory;
  683. FT_Bool hasBDFAccelerators;
  684. error = pcf_read_TOC( stream, face );
  685. if ( error )
  686. goto Exit;
  687. error = pcf_get_properties( stream, face );
  688. if ( error )
  689. goto Exit;
  690. /* Use the old accelerators if no BDF accelerators are in the file. */
  691. hasBDFAccelerators = pcf_has_table_type( face->toc.tables,
  692. face->toc.count,
  693. PCF_BDF_ACCELERATORS );
  694. if ( !hasBDFAccelerators )
  695. {
  696. error = pcf_get_accel( stream, face, PCF_ACCELERATORS );
  697. if ( error )
  698. goto Exit;
  699. }
  700. /* metrics */
  701. error = pcf_get_metrics( stream, face );
  702. if ( error )
  703. goto Exit;
  704. /* bitmaps */
  705. error = pcf_get_bitmaps( stream, face );
  706. if ( error )
  707. goto Exit;
  708. /* encodings */
  709. error = pcf_get_encodings( stream, face );
  710. if ( error )
  711. goto Exit;
  712. /* BDF style accelerators (i.e. bounds based on encoded glyphs) */
  713. if ( hasBDFAccelerators )
  714. {
  715. error = pcf_get_accel( stream, face, PCF_BDF_ACCELERATORS );
  716. if ( error )
  717. goto Exit;
  718. }
  719. /* XXX: TO DO: inkmetrics and glyph_names are missing */
  720. /* now construct the face object */
  721. {
  722. FT_Face root = FT_FACE( face );
  723. PCF_Property prop;
  724. int size_set = 0;
  725. root->num_faces = 1;
  726. root->face_index = 0;
  727. root->face_flags = FT_FACE_FLAG_FIXED_SIZES |
  728. FT_FACE_FLAG_HORIZONTAL |
  729. FT_FACE_FLAG_FAST_GLYPHS;
  730. if ( face->accel.constantWidth )
  731. root->face_flags |= FT_FACE_FLAG_FIXED_WIDTH;
  732. root->style_flags = 0;
  733. prop = pcf_find_property( face, "SLANT" );
  734. if ( prop != NULL )
  735. if ( prop->isString )
  736. if ( ( *(prop->value.atom) == 'O' ) ||
  737. ( *(prop->value.atom) == 'I' ) )
  738. root->style_flags |= FT_STYLE_FLAG_ITALIC;
  739. prop = pcf_find_property( face, "WEIGHT_NAME" );
  740. if ( prop != NULL )
  741. if ( prop->isString )
  742. if ( *(prop->value.atom) == 'B' )
  743. root->style_flags |= FT_STYLE_FLAG_BOLD;
  744. root->style_name = (char *)"Regular";
  745. if ( root->style_flags & FT_STYLE_FLAG_BOLD ) {
  746. if ( root->style_flags & FT_STYLE_FLAG_ITALIC )
  747. root->style_name = (char *)"Bold Italic";
  748. else
  749. root->style_name = (char *)"Bold";
  750. }
  751. else if ( root->style_flags & FT_STYLE_FLAG_ITALIC )
  752. root->style_name = (char *)"Italic";
  753. prop = pcf_find_property( face, "FAMILY_NAME" );
  754. if ( prop != NULL )
  755. {
  756. if ( prop->isString )
  757. {
  758. int l = ft_strlen( prop->value.atom ) + 1;
  759. if ( FT_NEW_ARRAY( root->family_name, l ) )
  760. goto Exit;
  761. ft_strcpy( root->family_name, prop->value.atom );
  762. }
  763. }
  764. else
  765. root->family_name = 0;
  766. root->num_glyphs = face->nmetrics;
  767. root->num_fixed_sizes = 1;
  768. if ( FT_NEW_ARRAY( root->available_sizes, 1 ) )
  769. goto Exit;
  770. prop = pcf_find_property( face, "PIXEL_SIZE" );
  771. if ( prop != NULL )
  772. {
  773. root->available_sizes->height =
  774. root->available_sizes->width = (FT_Short)( prop->value.integer );
  775. size_set = 1;
  776. }
  777. else
  778. {
  779. prop = pcf_find_property( face, "POINT_SIZE" );
  780. if ( prop != NULL )
  781. {
  782. PCF_Property xres, yres, avgw;
  783. xres = pcf_find_property( face, "RESOLUTION_X" );
  784. yres = pcf_find_property( face, "RESOLUTION_Y" );
  785. avgw = pcf_find_property( face, "AVERAGE_WIDTH" );
  786. if ( ( yres != NULL ) && ( xres != NULL ) )
  787. {
  788. root->available_sizes->height =
  789. (FT_Short)( prop->value.integer *
  790. yres->value.integer / 720 );
  791. root->available_sizes->width =
  792. (FT_Short)( prop->value.integer *
  793. xres->value.integer / 720 );
  794. size_set = 1;
  795. }
  796. }
  797. }
  798. if (size_set == 0 )
  799. {
  800. root->available_sizes->width = 12;
  801. root->available_sizes->height = 12;
  802. }
  803. /* set-up charset */
  804. {
  805. PCF_Property charset_registry = 0, charset_encoding = 0;
  806. charset_registry = pcf_find_property( face, "CHARSET_REGISTRY" );
  807. charset_encoding = pcf_find_property( face, "CHARSET_ENCODING" );
  808. if ( ( charset_registry != NULL ) &&
  809. ( charset_encoding != NULL ) )
  810. {
  811. if ( ( charset_registry->isString ) &&
  812. ( charset_encoding->isString ) )
  813. {
  814. if ( FT_NEW_ARRAY( face->charset_encoding,
  815. ft_strlen( charset_encoding->value.atom ) + 1 ) )
  816. goto Exit;
  817. if ( FT_NEW_ARRAY( face->charset_registry,
  818. ft_strlen( charset_registry->value.atom ) + 1 ) )
  819. goto Exit;
  820. ft_strcpy( face->charset_registry, charset_registry->value.atom );
  821. ft_strcpy( face->charset_encoding, charset_encoding->value.atom );
  822. }
  823. }
  824. }
  825. }
  826. Exit:
  827. if ( error )
  828. {
  829. /* this is done to respect the behaviour of the original */
  830. /* PCF font driver. */
  831. error = PCF_Err_Invalid_File_Format;
  832. }
  833. return error;
  834. }
  835. /* END */