/src/freetype/src/pcf/pcfdrivr.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 717 lines · 489 code · 168 blank · 60 comment · 66 complexity · 59344d8203373d2cb0f4d3344661b61b MD5 · raw file

  1. /* pcfdrivr.c
  2. FreeType font driver for pcf files
  3. Copyright (C) 2000-2004, 2006-2011 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 FT_GZIP_H
  26. #include FT_LZW_H
  27. #include FT_BZIP2_H
  28. #include FT_ERRORS_H
  29. #include FT_BDF_H
  30. #include FT_TRUETYPE_IDS_H
  31. #include "pcf.h"
  32. #include "pcfdrivr.h"
  33. #include "pcfread.h"
  34. #include "pcferror.h"
  35. #include "pcfutil.h"
  36. #undef FT_COMPONENT
  37. #define FT_COMPONENT trace_pcfread
  38. #include FT_SERVICE_BDF_H
  39. #include FT_SERVICE_XFREE86_NAME_H
  40. /*************************************************************************/
  41. /* */
  42. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  43. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  44. /* messages during execution. */
  45. /* */
  46. #undef FT_COMPONENT
  47. #define FT_COMPONENT trace_pcfdriver
  48. typedef struct PCF_CMapRec_
  49. {
  50. FT_CMapRec root;
  51. FT_UInt num_encodings;
  52. PCF_Encoding encodings;
  53. } PCF_CMapRec, *PCF_CMap;
  54. FT_CALLBACK_DEF( FT_Error )
  55. pcf_cmap_init( FT_CMap pcfcmap, /* PCF_CMap */
  56. FT_Pointer init_data )
  57. {
  58. PCF_CMap cmap = (PCF_CMap)pcfcmap;
  59. PCF_Face face = (PCF_Face)FT_CMAP_FACE( pcfcmap );
  60. FT_UNUSED( init_data );
  61. cmap->num_encodings = (FT_UInt)face->nencodings;
  62. cmap->encodings = face->encodings;
  63. return PCF_Err_Ok;
  64. }
  65. FT_CALLBACK_DEF( void )
  66. pcf_cmap_done( FT_CMap pcfcmap ) /* PCF_CMap */
  67. {
  68. PCF_CMap cmap = (PCF_CMap)pcfcmap;
  69. cmap->encodings = NULL;
  70. cmap->num_encodings = 0;
  71. }
  72. FT_CALLBACK_DEF( FT_UInt )
  73. pcf_cmap_char_index( FT_CMap pcfcmap, /* PCF_CMap */
  74. FT_UInt32 charcode )
  75. {
  76. PCF_CMap cmap = (PCF_CMap)pcfcmap;
  77. PCF_Encoding encodings = cmap->encodings;
  78. FT_UInt min, max, mid;
  79. FT_UInt result = 0;
  80. min = 0;
  81. max = cmap->num_encodings;
  82. while ( min < max )
  83. {
  84. FT_ULong code;
  85. mid = ( min + max ) >> 1;
  86. code = encodings[mid].enc;
  87. if ( charcode == code )
  88. {
  89. result = encodings[mid].glyph + 1;
  90. break;
  91. }
  92. if ( charcode < code )
  93. max = mid;
  94. else
  95. min = mid + 1;
  96. }
  97. return result;
  98. }
  99. FT_CALLBACK_DEF( FT_UInt )
  100. pcf_cmap_char_next( FT_CMap pcfcmap, /* PCF_CMap */
  101. FT_UInt32 *acharcode )
  102. {
  103. PCF_CMap cmap = (PCF_CMap)pcfcmap;
  104. PCF_Encoding encodings = cmap->encodings;
  105. FT_UInt min, max, mid;
  106. FT_ULong charcode = *acharcode + 1;
  107. FT_UInt result = 0;
  108. min = 0;
  109. max = cmap->num_encodings;
  110. while ( min < max )
  111. {
  112. FT_ULong code;
  113. mid = ( min + max ) >> 1;
  114. code = encodings[mid].enc;
  115. if ( charcode == code )
  116. {
  117. result = encodings[mid].glyph + 1;
  118. goto Exit;
  119. }
  120. if ( charcode < code )
  121. max = mid;
  122. else
  123. min = mid + 1;
  124. }
  125. charcode = 0;
  126. if ( min < cmap->num_encodings )
  127. {
  128. charcode = encodings[min].enc;
  129. result = encodings[min].glyph + 1;
  130. }
  131. Exit:
  132. if ( charcode > 0xFFFFFFFFUL )
  133. {
  134. FT_TRACE1(( "pcf_cmap_char_next: charcode 0x%x > 32bit API" ));
  135. *acharcode = 0;
  136. /* XXX: result should be changed to indicate an overflow error */
  137. }
  138. else
  139. *acharcode = (FT_UInt32)charcode;
  140. return result;
  141. }
  142. FT_CALLBACK_TABLE_DEF
  143. const FT_CMap_ClassRec pcf_cmap_class =
  144. {
  145. sizeof ( PCF_CMapRec ),
  146. pcf_cmap_init,
  147. pcf_cmap_done,
  148. pcf_cmap_char_index,
  149. pcf_cmap_char_next,
  150. NULL, NULL, NULL, NULL, NULL
  151. };
  152. FT_CALLBACK_DEF( void )
  153. PCF_Face_Done( FT_Face pcfface ) /* PCF_Face */
  154. {
  155. PCF_Face face = (PCF_Face)pcfface;
  156. FT_Memory memory;
  157. if ( !face )
  158. return;
  159. memory = FT_FACE_MEMORY( face );
  160. FT_FREE( face->encodings );
  161. FT_FREE( face->metrics );
  162. /* free properties */
  163. {
  164. PCF_Property prop;
  165. FT_Int i;
  166. if ( face->properties )
  167. {
  168. for ( i = 0; i < face->nprops; i++ )
  169. {
  170. prop = &face->properties[i];
  171. if ( prop )
  172. {
  173. FT_FREE( prop->name );
  174. if ( prop->isString )
  175. FT_FREE( prop->value.atom );
  176. }
  177. }
  178. }
  179. FT_FREE( face->properties );
  180. }
  181. FT_FREE( face->toc.tables );
  182. FT_FREE( pcfface->family_name );
  183. FT_FREE( pcfface->style_name );
  184. FT_FREE( pcfface->available_sizes );
  185. FT_FREE( face->charset_encoding );
  186. FT_FREE( face->charset_registry );
  187. /* close compressed stream if any */
  188. if ( pcfface->stream == &face->comp_stream )
  189. {
  190. FT_Stream_Close( &face->comp_stream );
  191. pcfface->stream = face->comp_source;
  192. }
  193. }
  194. FT_CALLBACK_DEF( FT_Error )
  195. PCF_Face_Init( FT_Stream stream,
  196. FT_Face pcfface, /* PCF_Face */
  197. FT_Int face_index,
  198. FT_Int num_params,
  199. FT_Parameter* params )
  200. {
  201. PCF_Face face = (PCF_Face)pcfface;
  202. FT_Error error = PCF_Err_Ok;
  203. FT_UNUSED( num_params );
  204. FT_UNUSED( params );
  205. FT_UNUSED( face_index );
  206. FT_TRACE2(( "PCF driver\n" ));
  207. error = pcf_load_font( stream, face );
  208. if ( error )
  209. {
  210. PCF_Face_Done( pcfface );
  211. #if defined( FT_CONFIG_OPTION_USE_ZLIB ) || \
  212. defined( FT_CONFIG_OPTION_USE_LZW ) || \
  213. defined( FT_CONFIG_OPTION_USE_BZIP2 )
  214. #ifdef FT_CONFIG_OPTION_USE_ZLIB
  215. {
  216. FT_Error error2;
  217. /* this didn't work, try gzip support! */
  218. error2 = FT_Stream_OpenGzip( &face->comp_stream, stream );
  219. if ( FT_ERROR_BASE( error2 ) == FT_Err_Unimplemented_Feature )
  220. goto Fail;
  221. error = error2;
  222. }
  223. #endif /* FT_CONFIG_OPTION_USE_ZLIB */
  224. #ifdef FT_CONFIG_OPTION_USE_LZW
  225. if ( error )
  226. {
  227. FT_Error error3;
  228. /* this didn't work, try LZW support! */
  229. error3 = FT_Stream_OpenLZW( &face->comp_stream, stream );
  230. if ( FT_ERROR_BASE( error3 ) == FT_Err_Unimplemented_Feature )
  231. goto Fail;
  232. error = error3;
  233. }
  234. #endif /* FT_CONFIG_OPTION_USE_LZW */
  235. #ifdef FT_CONFIG_OPTION_USE_BZIP2
  236. if ( error )
  237. {
  238. FT_Error error4;
  239. /* this didn't work, try Bzip2 support! */
  240. error4 = FT_Stream_OpenBzip2( &face->comp_stream, stream );
  241. if ( FT_ERROR_BASE( error4 ) == FT_Err_Unimplemented_Feature )
  242. goto Fail;
  243. error = error4;
  244. }
  245. #endif /* FT_CONFIG_OPTION_USE_BZIP2 */
  246. if ( error )
  247. goto Fail;
  248. face->comp_source = stream;
  249. pcfface->stream = &face->comp_stream;
  250. stream = pcfface->stream;
  251. error = pcf_load_font( stream, face );
  252. if ( error )
  253. goto Fail;
  254. #else /* !(FT_CONFIG_OPTION_USE_ZLIB ||
  255. FT_CONFIG_OPTION_USE_LZW ||
  256. FT_CONFIG_OPTION_USE_BZIP2) */
  257. goto Fail;
  258. #endif
  259. }
  260. /* set up charmap */
  261. {
  262. FT_String *charset_registry = face->charset_registry;
  263. FT_String *charset_encoding = face->charset_encoding;
  264. FT_Bool unicode_charmap = 0;
  265. if ( charset_registry && charset_encoding )
  266. {
  267. char* s = charset_registry;
  268. /* Uh, oh, compare first letters manually to avoid dependency
  269. on locales. */
  270. if ( ( s[0] == 'i' || s[0] == 'I' ) &&
  271. ( s[1] == 's' || s[1] == 'S' ) &&
  272. ( s[2] == 'o' || s[2] == 'O' ) )
  273. {
  274. s += 3;
  275. if ( !ft_strcmp( s, "10646" ) ||
  276. ( !ft_strcmp( s, "8859" ) &&
  277. !ft_strcmp( face->charset_encoding, "1" ) ) )
  278. unicode_charmap = 1;
  279. }
  280. }
  281. {
  282. FT_CharMapRec charmap;
  283. charmap.face = FT_FACE( face );
  284. charmap.encoding = FT_ENCODING_NONE;
  285. /* initial platform/encoding should indicate unset status? */
  286. charmap.platform_id = TT_PLATFORM_APPLE_UNICODE;
  287. charmap.encoding_id = TT_APPLE_ID_DEFAULT;
  288. if ( unicode_charmap )
  289. {
  290. charmap.encoding = FT_ENCODING_UNICODE;
  291. charmap.platform_id = TT_PLATFORM_MICROSOFT;
  292. charmap.encoding_id = TT_MS_ID_UNICODE_CS;
  293. }
  294. error = FT_CMap_New( &pcf_cmap_class, NULL, &charmap, NULL );
  295. #if 0
  296. /* Select default charmap */
  297. if ( pcfface->num_charmaps )
  298. pcfface->charmap = pcfface->charmaps[0];
  299. #endif
  300. }
  301. }
  302. Exit:
  303. return error;
  304. Fail:
  305. FT_TRACE2(( " not a PCF file\n" ));
  306. PCF_Face_Done( pcfface );
  307. error = PCF_Err_Unknown_File_Format; /* error */
  308. goto Exit;
  309. }
  310. FT_CALLBACK_DEF( FT_Error )
  311. PCF_Size_Select( FT_Size size,
  312. FT_ULong strike_index )
  313. {
  314. PCF_Accel accel = &( (PCF_Face)size->face )->accel;
  315. FT_Select_Metrics( size->face, strike_index );
  316. size->metrics.ascender = accel->fontAscent << 6;
  317. size->metrics.descender = -accel->fontDescent << 6;
  318. size->metrics.max_advance = accel->maxbounds.characterWidth << 6;
  319. return PCF_Err_Ok;
  320. }
  321. FT_CALLBACK_DEF( FT_Error )
  322. PCF_Size_Request( FT_Size size,
  323. FT_Size_Request req )
  324. {
  325. PCF_Face face = (PCF_Face)size->face;
  326. FT_Bitmap_Size* bsize = size->face->available_sizes;
  327. FT_Error error = PCF_Err_Invalid_Pixel_Size;
  328. FT_Long height;
  329. height = FT_REQUEST_HEIGHT( req );
  330. height = ( height + 32 ) >> 6;
  331. switch ( req->type )
  332. {
  333. case FT_SIZE_REQUEST_TYPE_NOMINAL:
  334. if ( height == ( ( bsize->y_ppem + 32 ) >> 6 ) )
  335. error = PCF_Err_Ok;
  336. break;
  337. case FT_SIZE_REQUEST_TYPE_REAL_DIM:
  338. if ( height == ( face->accel.fontAscent +
  339. face->accel.fontDescent ) )
  340. error = PCF_Err_Ok;
  341. break;
  342. default:
  343. error = PCF_Err_Unimplemented_Feature;
  344. break;
  345. }
  346. if ( error )
  347. return error;
  348. else
  349. return PCF_Size_Select( size, 0 );
  350. }
  351. FT_CALLBACK_DEF( FT_Error )
  352. PCF_Glyph_Load( FT_GlyphSlot slot,
  353. FT_Size size,
  354. FT_UInt glyph_index,
  355. FT_Int32 load_flags )
  356. {
  357. PCF_Face face = (PCF_Face)FT_SIZE_FACE( size );
  358. FT_Stream stream;
  359. FT_Error error = PCF_Err_Ok;
  360. FT_Bitmap* bitmap = &slot->bitmap;
  361. PCF_Metric metric;
  362. FT_Offset bytes;
  363. FT_UNUSED( load_flags );
  364. FT_TRACE4(( "load_glyph %d ---", glyph_index ));
  365. if ( !face || glyph_index >= (FT_UInt)face->root.num_glyphs )
  366. {
  367. error = PCF_Err_Invalid_Argument;
  368. goto Exit;
  369. }
  370. stream = face->root.stream;
  371. if ( glyph_index > 0 )
  372. glyph_index--;
  373. metric = face->metrics + glyph_index;
  374. bitmap->rows = metric->ascent + metric->descent;
  375. bitmap->width = metric->rightSideBearing - metric->leftSideBearing;
  376. bitmap->num_grays = 1;
  377. bitmap->pixel_mode = FT_PIXEL_MODE_MONO;
  378. FT_TRACE6(( "BIT_ORDER %d ; BYTE_ORDER %d ; GLYPH_PAD %d\n",
  379. PCF_BIT_ORDER( face->bitmapsFormat ),
  380. PCF_BYTE_ORDER( face->bitmapsFormat ),
  381. PCF_GLYPH_PAD( face->bitmapsFormat ) ));
  382. switch ( PCF_GLYPH_PAD( face->bitmapsFormat ) )
  383. {
  384. case 1:
  385. bitmap->pitch = ( bitmap->width + 7 ) >> 3;
  386. break;
  387. case 2:
  388. bitmap->pitch = ( ( bitmap->width + 15 ) >> 4 ) << 1;
  389. break;
  390. case 4:
  391. bitmap->pitch = ( ( bitmap->width + 31 ) >> 5 ) << 2;
  392. break;
  393. case 8:
  394. bitmap->pitch = ( ( bitmap->width + 63 ) >> 6 ) << 3;
  395. break;
  396. default:
  397. return PCF_Err_Invalid_File_Format;
  398. }
  399. /* XXX: to do: are there cases that need repadding the bitmap? */
  400. bytes = bitmap->pitch * bitmap->rows;
  401. error = ft_glyphslot_alloc_bitmap( slot, bytes );
  402. if ( error )
  403. goto Exit;
  404. if ( FT_STREAM_SEEK( metric->bits ) ||
  405. FT_STREAM_READ( bitmap->buffer, bytes ) )
  406. goto Exit;
  407. if ( PCF_BIT_ORDER( face->bitmapsFormat ) != MSBFirst )
  408. BitOrderInvert( bitmap->buffer, bytes );
  409. if ( ( PCF_BYTE_ORDER( face->bitmapsFormat ) !=
  410. PCF_BIT_ORDER( face->bitmapsFormat ) ) )
  411. {
  412. switch ( PCF_SCAN_UNIT( face->bitmapsFormat ) )
  413. {
  414. case 1:
  415. break;
  416. case 2:
  417. TwoByteSwap( bitmap->buffer, bytes );
  418. break;
  419. case 4:
  420. FourByteSwap( bitmap->buffer, bytes );
  421. break;
  422. }
  423. }
  424. slot->format = FT_GLYPH_FORMAT_BITMAP;
  425. slot->bitmap_left = metric->leftSideBearing;
  426. slot->bitmap_top = metric->ascent;
  427. slot->metrics.horiAdvance = metric->characterWidth << 6;
  428. slot->metrics.horiBearingX = metric->leftSideBearing << 6;
  429. slot->metrics.horiBearingY = metric->ascent << 6;
  430. slot->metrics.width = ( metric->rightSideBearing -
  431. metric->leftSideBearing ) << 6;
  432. slot->metrics.height = bitmap->rows << 6;
  433. ft_synthesize_vertical_metrics( &slot->metrics,
  434. ( face->accel.fontAscent +
  435. face->accel.fontDescent ) << 6 );
  436. FT_TRACE4(( " --- ok\n" ));
  437. Exit:
  438. return error;
  439. }
  440. /*
  441. *
  442. * BDF SERVICE
  443. *
  444. */
  445. static FT_Error
  446. pcf_get_bdf_property( PCF_Face face,
  447. const char* prop_name,
  448. BDF_PropertyRec *aproperty )
  449. {
  450. PCF_Property prop;
  451. prop = pcf_find_property( face, prop_name );
  452. if ( prop != NULL )
  453. {
  454. if ( prop->isString )
  455. {
  456. aproperty->type = BDF_PROPERTY_TYPE_ATOM;
  457. aproperty->u.atom = prop->value.atom;
  458. }
  459. else
  460. {
  461. if ( prop->value.l > 0x7FFFFFFFL || prop->value.l < ( -1 - 0x7FFFFFFFL ) )
  462. {
  463. FT_TRACE1(( "pcf_get_bdf_property: " ));
  464. FT_TRACE1(( "too large integer 0x%x is truncated\n" ));
  465. }
  466. /* Apparently, the PCF driver loads all properties as signed integers!
  467. * This really doesn't seem to be a problem, because this is
  468. * sufficient for any meaningful values.
  469. */
  470. aproperty->type = BDF_PROPERTY_TYPE_INTEGER;
  471. aproperty->u.integer = (FT_Int32)prop->value.l;
  472. }
  473. return 0;
  474. }
  475. return PCF_Err_Invalid_Argument;
  476. }
  477. static FT_Error
  478. pcf_get_charset_id( PCF_Face face,
  479. const char* *acharset_encoding,
  480. const char* *acharset_registry )
  481. {
  482. *acharset_encoding = face->charset_encoding;
  483. *acharset_registry = face->charset_registry;
  484. return 0;
  485. }
  486. static const FT_Service_BDFRec pcf_service_bdf =
  487. {
  488. (FT_BDF_GetCharsetIdFunc)pcf_get_charset_id,
  489. (FT_BDF_GetPropertyFunc) pcf_get_bdf_property
  490. };
  491. /*
  492. *
  493. * SERVICE LIST
  494. *
  495. */
  496. static const FT_ServiceDescRec pcf_services[] =
  497. {
  498. { FT_SERVICE_ID_BDF, &pcf_service_bdf },
  499. { FT_SERVICE_ID_XF86_NAME, FT_XF86_FORMAT_PCF },
  500. { NULL, NULL }
  501. };
  502. FT_CALLBACK_DEF( FT_Module_Interface )
  503. pcf_driver_requester( FT_Module module,
  504. const char* name )
  505. {
  506. FT_UNUSED( module );
  507. return ft_service_list_lookup( pcf_services, name );
  508. }
  509. FT_CALLBACK_TABLE_DEF
  510. const FT_Driver_ClassRec pcf_driver_class =
  511. {
  512. {
  513. FT_MODULE_FONT_DRIVER |
  514. FT_MODULE_DRIVER_NO_OUTLINES,
  515. sizeof ( FT_DriverRec ),
  516. "pcf",
  517. 0x10000L,
  518. 0x20000L,
  519. 0,
  520. 0, /* FT_Module_Constructor */
  521. 0, /* FT_Module_Destructor */
  522. pcf_driver_requester
  523. },
  524. sizeof ( PCF_FaceRec ),
  525. sizeof ( FT_SizeRec ),
  526. sizeof ( FT_GlyphSlotRec ),
  527. PCF_Face_Init,
  528. PCF_Face_Done,
  529. 0, /* FT_Size_InitFunc */
  530. 0, /* FT_Size_DoneFunc */
  531. 0, /* FT_Slot_InitFunc */
  532. 0, /* FT_Slot_DoneFunc */
  533. #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
  534. ft_stub_set_char_sizes,
  535. ft_stub_set_pixel_sizes,
  536. #endif
  537. PCF_Glyph_Load,
  538. 0, /* FT_Face_GetKerningFunc */
  539. 0, /* FT_Face_AttachFunc */
  540. 0, /* FT_Face_GetAdvancesFunc */
  541. PCF_Size_Request,
  542. PCF_Size_Select
  543. };
  544. /* END */