pcfdriver.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /* pcfdriver.c
  2. FreeType font driver for pcf files
  3. Copyright (C) 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 FT_GZIP_H
  26. #include FT_ERRORS_H
  27. #include "pcf.h"
  28. #include "pcfdriver.h"
  29. #include "pcfutil.h"
  30. #include "pcferror.h"
  31. #undef FT_COMPONENT
  32. #define FT_COMPONENT trace_pcfread
  33. typedef struct PCF_CMapRec_
  34. {
  35. FT_CMapRec cmap;
  36. FT_UInt num_encodings;
  37. PCF_Encoding encodings;
  38. } PCF_CMapRec, *PCF_CMap;
  39. FT_CALLBACK_DEF( FT_Error )
  40. pcf_cmap_init( PCF_CMap cmap )
  41. {
  42. PCF_Face face = (PCF_Face)FT_CMAP_FACE( cmap );
  43. cmap->num_encodings = (FT_UInt)face->nencodings;
  44. cmap->encodings = face->encodings;
  45. return FT_Err_Ok;
  46. }
  47. FT_CALLBACK_DEF( void )
  48. pcf_cmap_done( PCF_CMap cmap )
  49. {
  50. cmap->encodings = NULL;
  51. cmap->num_encodings = 0;
  52. }
  53. FT_CALLBACK_DEF( FT_UInt )
  54. pcf_cmap_char_index( PCF_CMap cmap,
  55. FT_UInt32 charcode )
  56. {
  57. PCF_Encoding encodings = cmap->encodings;
  58. FT_UInt min, max, mid;
  59. FT_UInt result = 0;
  60. min = 0;
  61. max = cmap->num_encodings;
  62. while ( min < max )
  63. {
  64. FT_UInt32 code;
  65. mid = ( min + max ) >> 1;
  66. code = encodings[mid].enc;
  67. if ( charcode == code )
  68. {
  69. result = encodings[mid].glyph + 1;
  70. break;
  71. }
  72. if ( charcode < code )
  73. max = mid;
  74. else
  75. min = mid + 1;
  76. }
  77. return result;
  78. }
  79. FT_CALLBACK_DEF( FT_UInt )
  80. pcf_cmap_char_next( PCF_CMap cmap,
  81. FT_UInt32 *acharcode )
  82. {
  83. PCF_Encoding encodings = cmap->encodings;
  84. FT_UInt min, max, mid;
  85. FT_UInt32 charcode = *acharcode + 1;
  86. FT_UInt result = 0;
  87. min = 0;
  88. max = cmap->num_encodings;
  89. while ( min < max )
  90. {
  91. FT_UInt32 code;
  92. mid = ( min + max ) >> 1;
  93. code = encodings[mid].enc;
  94. if ( charcode == code )
  95. {
  96. result = encodings[mid].glyph + 1;
  97. goto Exit;
  98. }
  99. if ( charcode < code )
  100. max = mid;
  101. else
  102. min = mid + 1;
  103. }
  104. charcode = 0;
  105. if ( min < cmap->num_encodings )
  106. {
  107. charcode = encodings[min].enc;
  108. result = encodings[min].glyph + 1;
  109. }
  110. Exit:
  111. *acharcode = charcode;
  112. return result;
  113. }
  114. FT_CALLBACK_TABLE_DEF const FT_CMap_ClassRec pcf_cmap_class =
  115. {
  116. sizeof( PCF_CMapRec ),
  117. (FT_CMap_InitFunc) pcf_cmap_init,
  118. (FT_CMap_DoneFunc) pcf_cmap_done,
  119. (FT_CMap_CharIndexFunc)pcf_cmap_char_index,
  120. (FT_CMap_CharNextFunc) pcf_cmap_char_next
  121. };
  122. /*************************************************************************/
  123. /* */
  124. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  125. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  126. /* messages during execution. */
  127. /* */
  128. #undef FT_COMPONENT
  129. #define FT_COMPONENT trace_pcfdriver
  130. FT_CALLBACK_DEF( FT_Error )
  131. PCF_Face_Done( PCF_Face face )
  132. {
  133. FT_Memory memory = FT_FACE_MEMORY( face );
  134. FT_FREE( face->encodings );
  135. FT_FREE( face->metrics );
  136. /* free properties */
  137. {
  138. PCF_Property prop = face->properties;
  139. FT_Int i;
  140. for ( i = 0; i < face->nprops; i++ )
  141. {
  142. prop = &face->properties[i];
  143. FT_FREE( prop->name );
  144. if ( prop->isString )
  145. FT_FREE( prop->value );
  146. }
  147. FT_FREE( face->properties );
  148. }
  149. FT_FREE( face->toc.tables );
  150. FT_FREE( face->root.family_name );
  151. FT_FREE( face->root.available_sizes );
  152. FT_FREE( face->charset_encoding );
  153. FT_FREE( face->charset_registry );
  154. FT_TRACE4(( "PCF_Face_Done: done face\n" ));
  155. /* close gzip stream if any */
  156. if ( face->root.stream == &face->gzip_stream )
  157. {
  158. FT_Stream_Close( &face->gzip_stream );
  159. face->root.stream = face->gzip_source;
  160. }
  161. return PCF_Err_Ok;
  162. }
  163. FT_CALLBACK_DEF( FT_Error )
  164. PCF_Face_Init( FT_Stream stream,
  165. PCF_Face face,
  166. FT_Int face_index,
  167. FT_Int num_params,
  168. FT_Parameter* params )
  169. {
  170. FT_Error error = PCF_Err_Ok;
  171. FT_UNUSED( num_params );
  172. FT_UNUSED( params );
  173. FT_UNUSED( face_index );
  174. error = pcf_load_font( stream, face );
  175. if ( error )
  176. {
  177. FT_Error error2;
  178. /* this didn't work, try gzip support !! */
  179. error2 = FT_Stream_OpenGzip( &face->gzip_stream, stream );
  180. if ( error2 == FT_Err_Unimplemented_Feature )
  181. goto Fail;
  182. error = error2;
  183. if ( error )
  184. goto Fail;
  185. face->gzip_source = stream;
  186. face->root.stream = &face->gzip_stream;
  187. stream = face->root.stream;
  188. error = pcf_load_font( stream, face );
  189. if ( error )
  190. goto Fail;
  191. }
  192. /* set-up charmap */
  193. {
  194. FT_String *charset_registry, *charset_encoding;
  195. FT_Bool unicode_charmap = 0;
  196. charset_registry = face->charset_registry;
  197. charset_encoding = face->charset_encoding;
  198. if ( ( charset_registry != NULL ) &&
  199. ( charset_encoding != NULL ) )
  200. {
  201. if ( !ft_strcmp( face->charset_registry, "ISO10646" ) ||
  202. ( !ft_strcmp( face->charset_registry, "ISO8859" ) &&
  203. !ft_strcmp( face->charset_encoding, "1" ) ) )
  204. unicode_charmap = 1;
  205. }
  206. {
  207. FT_CharMapRec charmap;
  208. charmap.face = FT_FACE( face );
  209. charmap.encoding = FT_ENCODING_NONE;
  210. charmap.platform_id = 0;
  211. charmap.encoding_id = 0;
  212. if ( unicode_charmap )
  213. {
  214. charmap.encoding = FT_ENCODING_UNICODE;
  215. charmap.platform_id = 3;
  216. charmap.encoding_id = 1;
  217. }
  218. error = FT_CMap_New( &pcf_cmap_class, NULL, &charmap, NULL );
  219. #if 0
  220. /* Select default charmap */
  221. if (face->root.num_charmaps)
  222. face->root.charmap = face->root.charmaps[0];
  223. #endif
  224. }
  225. }
  226. Exit:
  227. return error;
  228. Fail:
  229. FT_TRACE2(( "[not a valid PCF file]\n" ));
  230. error = PCF_Err_Unknown_File_Format; /* error */
  231. goto Exit;
  232. }
  233. static FT_Error
  234. PCF_Set_Pixel_Size( FT_Size size )
  235. {
  236. PCF_Face face = (PCF_Face)FT_SIZE_FACE( size );
  237. FT_TRACE4(( "rec %d - pres %d\n", size->metrics.y_ppem,
  238. face->root.available_sizes->height ));
  239. if ( size->metrics.y_ppem == face->root.available_sizes->height )
  240. {
  241. size->metrics.ascender = face->accel.fontAscent << 6;
  242. size->metrics.descender = face->accel.fontDescent * (-64);
  243. #if 0
  244. size->metrics.height = face->accel.maxbounds.ascent << 6;
  245. #endif
  246. size->metrics.height = size->metrics.ascender -
  247. size->metrics.descender;
  248. size->metrics.max_advance = face->accel.maxbounds.characterWidth << 6;
  249. return PCF_Err_Ok;
  250. }
  251. else
  252. {
  253. FT_TRACE4(( "size WRONG\n" ));
  254. return PCF_Err_Invalid_Pixel_Size;
  255. }
  256. }
  257. static FT_Error
  258. PCF_Glyph_Load( FT_GlyphSlot slot,
  259. FT_Size size,
  260. FT_UInt glyph_index,
  261. FT_Int32 load_flags )
  262. {
  263. PCF_Face face = (PCF_Face)FT_SIZE_FACE( size );
  264. FT_Stream stream = face->root.stream;
  265. FT_Error error = PCF_Err_Ok;
  266. FT_Memory memory = FT_FACE( face )->memory;
  267. FT_Bitmap* bitmap = &slot->bitmap;
  268. PCF_Metric metric;
  269. int bytes;
  270. FT_UNUSED( load_flags );
  271. FT_TRACE4(( "load_glyph %d ---", glyph_index ));
  272. if ( !face )
  273. {
  274. error = PCF_Err_Invalid_Argument;
  275. goto Exit;
  276. }
  277. if ( glyph_index > 0 )
  278. glyph_index--;
  279. metric = face->metrics + glyph_index;
  280. bitmap->rows = metric->ascent + metric->descent;
  281. bitmap->width = metric->rightSideBearing - metric->leftSideBearing;
  282. bitmap->num_grays = 1;
  283. bitmap->pixel_mode = FT_PIXEL_MODE_MONO;
  284. FT_TRACE6(( "BIT_ORDER %d ; BYTE_ORDER %d ; GLYPH_PAD %d\n",
  285. PCF_BIT_ORDER( face->bitmapsFormat ),
  286. PCF_BYTE_ORDER( face->bitmapsFormat ),
  287. PCF_GLYPH_PAD( face->bitmapsFormat ) ));
  288. switch ( PCF_GLYPH_PAD( face->bitmapsFormat ) )
  289. {
  290. case 1:
  291. bitmap->pitch = ( bitmap->width + 7 ) >> 3;
  292. break;
  293. case 2:
  294. bitmap->pitch = ( ( bitmap->width + 15 ) >> 4 ) << 1;
  295. break;
  296. case 4:
  297. bitmap->pitch = ( ( bitmap->width + 31 ) >> 5 ) << 2;
  298. break;
  299. case 8:
  300. bitmap->pitch = ( ( bitmap->width + 63 ) >> 6 ) << 3;
  301. break;
  302. default:
  303. return PCF_Err_Invalid_File_Format;
  304. }
  305. /* XXX: to do: are there cases that need repadding the bitmap? */
  306. bytes = bitmap->pitch * bitmap->rows;
  307. if ( FT_ALLOC( bitmap->buffer, bytes ) )
  308. goto Exit;
  309. if ( FT_STREAM_SEEK( metric->bits ) ||
  310. FT_STREAM_READ( bitmap->buffer, bytes ) )
  311. goto Exit;
  312. if ( PCF_BIT_ORDER( face->bitmapsFormat ) != MSBFirst )
  313. BitOrderInvert( bitmap->buffer, bytes );
  314. if ( ( PCF_BYTE_ORDER( face->bitmapsFormat ) !=
  315. PCF_BIT_ORDER( face->bitmapsFormat ) ) )
  316. {
  317. switch ( PCF_SCAN_UNIT( face->bitmapsFormat ) )
  318. {
  319. case 1:
  320. break;
  321. case 2:
  322. TwoByteSwap( bitmap->buffer, bytes );
  323. break;
  324. case 4:
  325. FourByteSwap( bitmap->buffer, bytes );
  326. break;
  327. }
  328. }
  329. slot->bitmap_left = metric->leftSideBearing;
  330. slot->bitmap_top = metric->ascent;
  331. slot->metrics.horiAdvance = metric->characterWidth << 6 ;
  332. slot->metrics.horiBearingX = metric->leftSideBearing << 6 ;
  333. slot->metrics.horiBearingY = metric->ascent << 6 ;
  334. slot->metrics.width = ( metric->rightSideBearing -
  335. metric->leftSideBearing ) << 6;
  336. slot->metrics.height = bitmap->rows << 6;
  337. slot->linearHoriAdvance = (FT_Fixed)bitmap->width << 16;
  338. slot->format = FT_GLYPH_FORMAT_BITMAP;
  339. slot->flags = FT_GLYPH_OWN_BITMAP;
  340. FT_TRACE4(( " --- ok\n" ));
  341. Exit:
  342. return error;
  343. }
  344. FT_CALLBACK_TABLE_DEF
  345. const FT_Driver_ClassRec pcf_driver_class =
  346. {
  347. {
  348. ft_module_font_driver,
  349. sizeof ( FT_DriverRec ),
  350. "pcf",
  351. 0x10000L,
  352. 0x20000L,
  353. 0,
  354. (FT_Module_Constructor)0,
  355. (FT_Module_Destructor) 0,
  356. (FT_Module_Requester) 0
  357. },
  358. sizeof( PCF_FaceRec ),
  359. sizeof( FT_SizeRec ),
  360. sizeof( FT_GlyphSlotRec ),
  361. (FT_Face_InitFunc) PCF_Face_Init,
  362. (FT_Face_DoneFunc) PCF_Face_Done,
  363. (FT_Size_InitFunc) 0,
  364. (FT_Size_DoneFunc) 0,
  365. (FT_Slot_InitFunc) 0,
  366. (FT_Slot_DoneFunc) 0,
  367. (FT_Size_ResetPointsFunc) PCF_Set_Pixel_Size,
  368. (FT_Size_ResetPixelsFunc) PCF_Set_Pixel_Size,
  369. (FT_Slot_LoadFunc) PCF_Glyph_Load,
  370. (FT_Face_GetKerningFunc) 0,
  371. (FT_Face_AttachFunc) 0,
  372. (FT_Face_GetAdvancesFunc) 0
  373. };
  374. /* END */