bdfdrivr.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /* bdfdrivr.c
  2. FreeType font driver for bdf files
  3. Copyright (C) 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 "bdf.h"
  26. #include "bdfdrivr.h"
  27. #include "bdferror.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_bdfdriver
  36. typedef struct BDF_CMapRec_
  37. {
  38. FT_CMapRec cmap;
  39. FT_UInt num_encodings;
  40. BDF_encoding_el* encodings;
  41. } BDF_CMapRec, *BDF_CMap;
  42. FT_CALLBACK_DEF( FT_Error )
  43. bdf_cmap_init( BDF_CMap cmap )
  44. {
  45. BDF_Face face = (BDF_Face)FT_CMAP_FACE( cmap );
  46. cmap->num_encodings = face->bdffont->glyphs_used;
  47. cmap->encodings = face->en_table;
  48. return FT_Err_Ok;
  49. }
  50. FT_CALLBACK_DEF( void )
  51. bdf_cmap_done( BDF_CMap cmap )
  52. {
  53. cmap->encodings = NULL;
  54. cmap->num_encodings = 0;
  55. }
  56. FT_CALLBACK_DEF( FT_UInt )
  57. bdf_cmap_char_index( BDF_CMap cmap,
  58. FT_UInt32 charcode )
  59. {
  60. BDF_encoding_el* encodings = cmap->encodings;
  61. FT_UInt min, max, mid;
  62. FT_UInt result = 0;
  63. min = 0;
  64. max = cmap->num_encodings;
  65. while ( min < max )
  66. {
  67. FT_UInt32 code;
  68. mid = ( min + max ) >> 1;
  69. code = encodings[mid].enc;
  70. if ( charcode == code )
  71. {
  72. result = encodings[mid].glyph + 1;
  73. break;
  74. }
  75. if ( charcode < code )
  76. max = mid;
  77. else
  78. min = mid + 1;
  79. }
  80. return result;
  81. }
  82. FT_CALLBACK_DEF( FT_UInt )
  83. bdf_cmap_char_next( BDF_CMap cmap,
  84. FT_UInt32 *acharcode )
  85. {
  86. BDF_encoding_el* encodings = cmap->encodings;
  87. FT_UInt min, max, mid;
  88. FT_UInt32 charcode = *acharcode + 1;
  89. FT_UInt result = 0;
  90. min = 0;
  91. max = cmap->num_encodings;
  92. while ( min < max )
  93. {
  94. FT_UInt32 code;
  95. mid = ( min + max ) >> 1;
  96. code = encodings[mid].enc;
  97. if ( charcode == code )
  98. {
  99. result = encodings[mid].glyph + 1;
  100. goto Exit;
  101. }
  102. if ( charcode < code )
  103. max = mid;
  104. else
  105. min = mid + 1;
  106. }
  107. charcode = 0;
  108. if ( min < cmap->num_encodings )
  109. {
  110. charcode = encodings[min].enc;
  111. result = encodings[min].glyph + 1;
  112. }
  113. Exit:
  114. *acharcode = charcode;
  115. return result;
  116. }
  117. FT_CALLBACK_TABLE_DEF const FT_CMap_ClassRec bdf_cmap_class =
  118. {
  119. sizeof( BDF_CMapRec ),
  120. (FT_CMap_InitFunc) bdf_cmap_init,
  121. (FT_CMap_DoneFunc) bdf_cmap_done,
  122. (FT_CMap_CharIndexFunc)bdf_cmap_char_index,
  123. (FT_CMap_CharNextFunc) bdf_cmap_char_next
  124. };
  125. FT_CALLBACK_DEF( FT_Error )
  126. BDF_Face_Done( BDF_Face face )
  127. {
  128. FT_Memory memory = FT_FACE_MEMORY( face );
  129. bdf_free_font( face->bdffont );
  130. FT_FREE( face->en_table );
  131. FT_FREE( face->charset_encoding );
  132. FT_FREE( face->charset_registry );
  133. FT_FREE( face->root.family_name );
  134. FT_FREE( face->root.available_sizes );
  135. FT_FREE( face->bdffont );
  136. FT_TRACE4(( "BDF_Face_Done: done face\n" ));
  137. return BDF_Err_Ok;
  138. }
  139. FT_CALLBACK_DEF( FT_Error )
  140. BDF_Face_Init( FT_Stream stream,
  141. BDF_Face face,
  142. FT_Int face_index,
  143. FT_Int num_params,
  144. FT_Parameter* params )
  145. {
  146. FT_Error error = BDF_Err_Ok;
  147. FT_Memory memory = FT_FACE_MEMORY( face );
  148. bdf_font_t* font;
  149. bdf_options_t options;
  150. FT_UNUSED( num_params );
  151. FT_UNUSED( params );
  152. FT_UNUSED( face_index );
  153. if ( FT_STREAM_SEEK( 0 ) )
  154. goto Exit;
  155. options.correct_metrics = 1; /* FZ XXX: options semantics */
  156. options.keep_unencoded = 1;
  157. options.keep_comments = 0;
  158. options.font_spacing = BDF_PROPORTIONAL;
  159. error = bdf_load_font( stream, memory, &options, &font );
  160. if ( error == BDF_Err_Missing_Startfont_Field )
  161. {
  162. FT_TRACE2(( "[not a valid BDF file]\n" ));
  163. goto Fail;
  164. }
  165. else if ( error )
  166. goto Exit;
  167. /* we have a bdf font: let's construct the face object */
  168. face->bdffont = font;
  169. {
  170. FT_Face root = FT_FACE( face );
  171. bdf_property_t* prop = NULL;
  172. FT_TRACE4(( "number of glyphs: %d (%d)\n",
  173. font->glyphs_size,
  174. font->glyphs_used ));
  175. FT_TRACE4(( "number of unencoded glyphs: %d (%d)\n",
  176. font->unencoded_size,
  177. font->unencoded_used ));
  178. root->num_faces = 1;
  179. root->face_index = 0;
  180. root->face_flags = FT_FACE_FLAG_FIXED_SIZES |
  181. FT_FACE_FLAG_HORIZONTAL |
  182. FT_FACE_FLAG_FAST_GLYPHS;
  183. prop = bdf_get_font_property( font, (char *)"SPACING" );
  184. if ( prop != NULL )
  185. if ( prop->format == BDF_ATOM )
  186. if ( prop->value.atom != NULL )
  187. if ( ( *(prop->value.atom) == 'M' ) ||
  188. ( *(prop->value.atom) == 'C' ) )
  189. root->face_flags |= FT_FACE_FLAG_FIXED_WIDTH;
  190. /* FZ XXX: TO DO: FT_FACE_FLAGS_VERTICAL */
  191. /* FZ XXX: I need a font to implement this */
  192. root->style_flags = 0;
  193. prop = bdf_get_font_property( font, (char *)"SLANT" );
  194. if ( prop != NULL )
  195. if ( prop->format == BDF_ATOM )
  196. if ( prop->value.atom != NULL )
  197. if ( ( *(prop->value.atom) == 'O' ) ||
  198. ( *(prop->value.atom) == 'I' ) )
  199. root->style_flags |= FT_STYLE_FLAG_ITALIC;
  200. prop = bdf_get_font_property( font, (char *)"WEIGHT_NAME" );
  201. if ( prop != NULL )
  202. if ( prop->format == BDF_ATOM )
  203. if ( prop->value.atom != NULL )
  204. if ( *(prop->value.atom) == 'B' )
  205. root->style_flags |= FT_STYLE_FLAG_BOLD;
  206. prop = bdf_get_font_property( font, (char *)"FAMILY_NAME" );
  207. if ( ( prop != NULL ) && ( prop->value.atom != NULL ) )
  208. {
  209. int l = ft_strlen( prop->value.atom ) + 1;
  210. if ( FT_NEW_ARRAY( root->family_name, l ) )
  211. goto Exit;
  212. ft_strcpy( root->family_name, prop->value.atom );
  213. }
  214. else
  215. root->family_name = 0;
  216. root->style_name = (char *)"Regular";
  217. if ( root->style_flags & FT_STYLE_FLAG_BOLD )
  218. {
  219. if ( root->style_flags & FT_STYLE_FLAG_ITALIC )
  220. root->style_name = (char *)"Bold Italic";
  221. else
  222. root->style_name = (char *)"Bold";
  223. }
  224. else if ( root->style_flags & FT_STYLE_FLAG_ITALIC )
  225. root->style_name = (char *)"Italic";
  226. root->num_glyphs = font->glyphs_size; /* unencoded included */
  227. root->num_fixed_sizes = 1;
  228. if ( FT_NEW_ARRAY( root->available_sizes, 1 ) )
  229. goto Exit;
  230. prop = bdf_get_font_property( font, (char *)"AVERAGE_WIDTH" );
  231. if ( ( prop != NULL ) && ( prop->value.int32 >= 10 ) )
  232. root->available_sizes->width = (short)( prop->value.int32 / 10 );
  233. prop = bdf_get_font_property( font, (char *)"PIXEL_SIZE" );
  234. if ( prop != NULL )
  235. root->available_sizes->height = (short) prop->value.int32;
  236. else
  237. {
  238. prop = bdf_get_font_property( font, (char *)"POINT_SIZE" );
  239. if ( prop != NULL )
  240. {
  241. bdf_property_t *yres;
  242. yres = bdf_get_font_property( font, (char *)"RESOLUTION_Y" );
  243. if ( yres != NULL )
  244. {
  245. FT_TRACE4(( "POINT_SIZE: %d RESOLUTION_Y: %d\n",
  246. prop->value.int32, yres->value.int32 ));
  247. root->available_sizes->height =
  248. (FT_Short)( prop->value.int32 * yres->value.int32 / 720 );
  249. }
  250. }
  251. }
  252. if ( root->available_sizes->width == 0 )
  253. {
  254. if ( root->available_sizes->height == 0 )
  255. {
  256. /* some fonts have broken SIZE declaration (jiskan24.bdf) */
  257. FT_ERROR(( "BDF_Face_Init: reading size\n" ));
  258. root->available_sizes->width = (FT_Short)font->point_size;
  259. }
  260. else
  261. root->available_sizes->width = root->available_sizes->height;
  262. }
  263. if ( root->available_sizes->height == 0 )
  264. root->available_sizes->height = root->available_sizes->width;
  265. /* encoding table */
  266. {
  267. bdf_glyph_t* cur = font->glyphs;
  268. unsigned long n;
  269. if ( FT_NEW_ARRAY( face->en_table, font->glyphs_size ) )
  270. goto Exit;
  271. for ( n = 0; n < font->glyphs_size; n++ )
  272. {
  273. (face->en_table[n]).enc = cur[n].encoding;
  274. FT_TRACE4(( "idx %d, val 0x%lX\n", n, cur[n].encoding ));
  275. (face->en_table[n]).glyph = (FT_Short)n;
  276. }
  277. }
  278. /* charmaps */
  279. {
  280. bdf_property_t *charset_registry = 0, *charset_encoding = 0;
  281. FT_Bool unicode_charmap = 0;
  282. charset_registry =
  283. bdf_get_font_property( font, (char *)"CHARSET_REGISTRY" );
  284. charset_encoding =
  285. bdf_get_font_property( font, (char *)"CHARSET_ENCODING" );
  286. if ( ( charset_registry != NULL ) && ( charset_encoding != NULL ) )
  287. {
  288. if ( ( charset_registry->format == BDF_ATOM ) &&
  289. ( charset_encoding->format == BDF_ATOM ) &&
  290. ( charset_registry->value.atom != NULL ) &&
  291. ( charset_encoding->value.atom != NULL ) )
  292. {
  293. if ( FT_NEW_ARRAY( face->charset_encoding,
  294. strlen( charset_encoding->value.atom ) + 1 ) )
  295. goto Exit;
  296. if ( FT_NEW_ARRAY( face->charset_registry,
  297. strlen( charset_registry->value.atom ) + 1 ) )
  298. goto Exit;
  299. ft_strcpy( face->charset_registry, charset_registry->value.atom );
  300. ft_strcpy( face->charset_encoding, charset_encoding->value.atom );
  301. if ( !ft_strcmp( face->charset_registry, "ISO10646" ) ||
  302. ( !ft_strcmp( face->charset_registry, "ISO8859" ) &&
  303. !ft_strcmp( face->charset_encoding, "1" ) ) )
  304. unicode_charmap = 1;
  305. {
  306. FT_CharMapRec charmap;
  307. charmap.face = FT_FACE( face );
  308. charmap.encoding = FT_ENCODING_NONE;
  309. charmap.platform_id = 0;
  310. charmap.encoding_id = 0;
  311. if ( unicode_charmap )
  312. {
  313. charmap.encoding = FT_ENCODING_UNICODE;
  314. charmap.platform_id = 3;
  315. charmap.encoding_id = 1;
  316. }
  317. error = FT_CMap_New( &bdf_cmap_class, NULL, &charmap, NULL );
  318. #if 0
  319. /* Select default charmap */
  320. if (root->num_charmaps)
  321. root->charmap = root->charmaps[0];
  322. #endif
  323. }
  324. goto Exit;
  325. }
  326. }
  327. /* otherwise assume Adobe standard encoding */
  328. {
  329. FT_CharMapRec charmap;
  330. charmap.face = FT_FACE( face );
  331. charmap.encoding = FT_ENCODING_ADOBE_STANDARD;
  332. charmap.platform_id = 7;
  333. charmap.encoding_id = 0;
  334. error = FT_CMap_New( &bdf_cmap_class, NULL, &charmap, NULL );
  335. /* Select default charmap */
  336. if (root->num_charmaps)
  337. root->charmap = root->charmaps[0];
  338. }
  339. }
  340. }
  341. Exit:
  342. return error;
  343. Fail:
  344. BDF_Face_Done( face );
  345. return BDF_Err_Unknown_File_Format;
  346. }
  347. static FT_Error
  348. BDF_Set_Pixel_Size( FT_Size size )
  349. {
  350. BDF_Face face = (BDF_Face)FT_SIZE_FACE( size );
  351. FT_Face root = FT_FACE( face );
  352. FT_TRACE4(( "rec %d - pres %d\n",
  353. size->metrics.y_ppem, root->available_sizes->height ));
  354. if ( size->metrics.y_ppem == root->available_sizes->height )
  355. {
  356. size->metrics.ascender = face->bdffont->bbx.ascent << 6;
  357. size->metrics.descender = face->bdffont->bbx.descent * ( -64 );
  358. size->metrics.height = face->bdffont->bbx.height << 6;
  359. return BDF_Err_Ok;
  360. }
  361. else
  362. return BDF_Err_Invalid_Pixel_Size;
  363. }
  364. static FT_Error
  365. BDF_Glyph_Load( FT_GlyphSlot slot,
  366. FT_Size size,
  367. FT_UInt glyph_index,
  368. FT_Int32 load_flags )
  369. {
  370. BDF_Face face = (BDF_Face)FT_SIZE_FACE( size );
  371. FT_Error error = BDF_Err_Ok;
  372. FT_Bitmap* bitmap = &slot->bitmap;
  373. bdf_glyph_t glyph;
  374. int bpp = face->bdffont->bpp;
  375. int i, j, count;
  376. unsigned char *p, *pp;
  377. FT_Memory memory = face->bdffont->memory;
  378. FT_UNUSED( load_flags );
  379. if ( !face )
  380. {
  381. error = BDF_Err_Invalid_Argument;
  382. goto Exit;
  383. }
  384. if ( glyph_index > 0 )
  385. glyph_index--;
  386. /* slot, bitmap => freetype, glyph => bdflib */
  387. glyph = face->bdffont->glyphs[glyph_index];
  388. bitmap->rows = glyph.bbx.height;
  389. bitmap->width = glyph.bbx.width;
  390. if ( bpp == 1 )
  391. {
  392. bitmap->pixel_mode = FT_PIXEL_MODE_MONO;
  393. bitmap->pitch = glyph.bpr;
  394. if ( FT_NEW_ARRAY( bitmap->buffer, glyph.bytes ) )
  395. goto Exit;
  396. FT_MEM_COPY( bitmap->buffer, glyph.bitmap, glyph.bytes );
  397. }
  398. else
  399. {
  400. /* blow up pixmap to have 8 bits per pixel */
  401. bitmap->pixel_mode = FT_PIXEL_MODE_GRAY;
  402. bitmap->pitch = bitmap->width;
  403. if ( FT_NEW_ARRAY( bitmap->buffer, bitmap->rows * bitmap->pitch ) )
  404. goto Exit;
  405. switch ( bpp )
  406. {
  407. case 2:
  408. bitmap->num_grays = 4;
  409. count = 0;
  410. p = glyph.bitmap;
  411. for ( i = 0; i < bitmap->rows; i++ )
  412. {
  413. pp = p;
  414. /* get the full bytes */
  415. for ( j = 0; j < ( bitmap->width >> 2 ); j++ )
  416. {
  417. bitmap->buffer[count++] = (FT_Byte)( ( *pp & 0xC0 ) >> 6 );
  418. bitmap->buffer[count++] = (FT_Byte)( ( *pp & 0x30 ) >> 4 );
  419. bitmap->buffer[count++] = (FT_Byte)( ( *pp & 0x0C ) >> 2 );
  420. bitmap->buffer[count++] = (FT_Byte)( *pp & 0x03 );
  421. pp++;
  422. }
  423. /* get remaining pixels (if any) */
  424. switch ( bitmap->width & 3 )
  425. {
  426. case 3:
  427. bitmap->buffer[count++] = (FT_Byte)( ( *pp & 0xC0 ) >> 6 );
  428. /* fall through */
  429. case 2:
  430. bitmap->buffer[count++] = (FT_Byte)( ( *pp & 0x30 ) >> 4 );
  431. /* fall through */
  432. case 1:
  433. bitmap->buffer[count++] = (FT_Byte)( ( *pp & 0x0C ) >> 2 );
  434. /* fall through */
  435. case 0:
  436. break;
  437. }
  438. p += glyph.bpr;
  439. }
  440. break;
  441. case 4:
  442. bitmap->num_grays = 16;
  443. count = 0;
  444. p = glyph.bitmap;
  445. for ( i = 0; i < bitmap->rows; i++ )
  446. {
  447. pp = p;
  448. /* get the full bytes */
  449. for ( j = 0; j < ( bitmap->width >> 1 ); j++ )
  450. {
  451. bitmap->buffer[count++] = (FT_Byte)( ( *pp & 0xF0 ) >> 4 );
  452. bitmap->buffer[count++] = (FT_Byte)( *pp & 0x0F );
  453. pp++;
  454. }
  455. /* get remaining pixel (if any) */
  456. switch ( bitmap->width & 1 )
  457. {
  458. case 1:
  459. bitmap->buffer[count++] = (FT_Byte)( ( *pp & 0xF0 ) >> 4 );
  460. /* fall through */
  461. case 0:
  462. break;
  463. }
  464. p += glyph.bpr;
  465. }
  466. break;
  467. case 8:
  468. bitmap->num_grays = 256;
  469. FT_MEM_COPY( bitmap->buffer, glyph.bitmap,
  470. bitmap->rows * bitmap->pitch );
  471. break;
  472. }
  473. }
  474. slot->bitmap_left = 0;
  475. slot->bitmap_top = glyph.bbx.ascent;
  476. /* FZ XXX: TODO: vertical metrics */
  477. slot->metrics.horiAdvance = glyph.dwidth << 6;
  478. slot->metrics.horiBearingX = glyph.bbx.x_offset << 6;
  479. slot->metrics.horiBearingY = ( glyph.bbx.y_offset +
  480. glyph.bbx.height ) << 6;
  481. slot->metrics.width = bitmap->width << 6;
  482. slot->metrics.height = bitmap->rows << 6;
  483. slot->linearHoriAdvance = (FT_Fixed)glyph.dwidth << 16;
  484. slot->format = FT_GLYPH_FORMAT_BITMAP;
  485. slot->flags = FT_GLYPH_OWN_BITMAP;
  486. Exit:
  487. return error;
  488. }
  489. FT_CALLBACK_TABLE_DEF
  490. const FT_Driver_ClassRec bdf_driver_class =
  491. {
  492. {
  493. ft_module_font_driver,
  494. sizeof ( FT_DriverRec ),
  495. "bdf",
  496. 0x10000L,
  497. 0x20000L,
  498. 0,
  499. (FT_Module_Constructor)0,
  500. (FT_Module_Destructor) 0,
  501. (FT_Module_Requester) 0
  502. },
  503. sizeof ( BDF_FaceRec ),
  504. sizeof ( FT_SizeRec ),
  505. sizeof ( FT_GlyphSlotRec ),
  506. (FT_Face_InitFunc) BDF_Face_Init,
  507. (FT_Face_DoneFunc) BDF_Face_Done,
  508. (FT_Size_InitFunc) 0,
  509. (FT_Size_DoneFunc) 0,
  510. (FT_Slot_InitFunc) 0,
  511. (FT_Slot_DoneFunc) 0,
  512. (FT_Size_ResetPointsFunc) BDF_Set_Pixel_Size,
  513. (FT_Size_ResetPixelsFunc) BDF_Set_Pixel_Size,
  514. (FT_Slot_LoadFunc) BDF_Glyph_Load,
  515. (FT_Face_GetKerningFunc) 0,
  516. (FT_Face_AttachFunc) 0,
  517. (FT_Face_GetAdvancesFunc) 0
  518. };
  519. /* END */