ftcsbits.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /***************************************************************************/
  2. /* */
  3. /* ftcsbits.c */
  4. /* */
  5. /* FreeType sbits manager (body). */
  6. /* */
  7. /* Copyright 2000-2001, 2002 by */
  8. /* David Turner, Robert Wilhelm, and Werner Lemberg. */
  9. /* */
  10. /* This file is part of the FreeType project, and may only be used, */
  11. /* modified, and distributed under the terms of the FreeType project */
  12. /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
  13. /* this file you indicate that you have read the license and */
  14. /* understand and accept it fully. */
  15. /* */
  16. /***************************************************************************/
  17. #include <ft2build.h>
  18. #include FT_CACHE_H
  19. #include FT_CACHE_SMALL_BITMAPS_H
  20. #include FT_CACHE_INTERNAL_GLYPH_H
  21. #include FT_INTERNAL_OBJECTS_H
  22. #include FT_INTERNAL_DEBUG_H
  23. #include FT_ERRORS_H
  24. #include "ftcerror.h"
  25. #define FTC_SBIT_ITEMS_PER_NODE 16
  26. typedef struct FTC_SBitNodeRec_* FTC_SBitNode;
  27. typedef struct FTC_SBitNodeRec_
  28. {
  29. FTC_GlyphNodeRec gnode;
  30. FTC_SBitRec sbits[FTC_SBIT_ITEMS_PER_NODE];
  31. } FTC_SBitNodeRec;
  32. #define FTC_SBIT_NODE( x ) ( (FTC_SBitNode)( x ) )
  33. typedef struct FTC_SBitQueryRec_
  34. {
  35. FTC_GlyphQueryRec gquery;
  36. FTC_ImageTypeRec type;
  37. } FTC_SBitQueryRec, *FTC_SBitQuery;
  38. #define FTC_SBIT_QUERY( x ) ( (FTC_SBitQuery)( x ) )
  39. typedef struct FTC_SBitFamilyRec_* FTC_SBitFamily;
  40. /* sbit family structure */
  41. typedef struct FTC_SBitFamilyRec_
  42. {
  43. FTC_GlyphFamilyRec gfam;
  44. FTC_ImageTypeRec type;
  45. } FTC_SBitFamilyRec;
  46. #define FTC_SBIT_FAMILY( x ) ( (FTC_SBitFamily)( x ) )
  47. #define FTC_SBIT_FAMILY_MEMORY( x ) FTC_GLYPH_FAMILY_MEMORY( &( x )->cset )
  48. /*************************************************************************/
  49. /*************************************************************************/
  50. /***** *****/
  51. /***** SBIT CACHE NODES *****/
  52. /***** *****/
  53. /*************************************************************************/
  54. /*************************************************************************/
  55. static FT_Error
  56. ftc_sbit_copy_bitmap( FTC_SBit sbit,
  57. FT_Bitmap* bitmap,
  58. FT_Memory memory )
  59. {
  60. FT_Error error;
  61. FT_Int pitch = bitmap->pitch;
  62. FT_ULong size;
  63. if ( pitch < 0 )
  64. pitch = -pitch;
  65. size = (FT_ULong)( pitch * bitmap->rows );
  66. if ( !FT_ALLOC( sbit->buffer, size ) )
  67. FT_MEM_COPY( sbit->buffer, bitmap->buffer, size );
  68. return error;
  69. }
  70. FT_CALLBACK_DEF( void )
  71. ftc_sbit_node_done( FTC_SBitNode snode,
  72. FTC_Cache cache )
  73. {
  74. FTC_SBit sbit = snode->sbits;
  75. FT_UInt count = FTC_GLYPH_NODE( snode )->item_count;
  76. FT_Memory memory = cache->memory;
  77. for ( ; count > 0; sbit++, count-- )
  78. FT_FREE( sbit->buffer );
  79. ftc_glyph_node_done( FTC_GLYPH_NODE( snode ), cache );
  80. }
  81. static FT_Error
  82. ftc_sbit_node_load( FTC_SBitNode snode,
  83. FTC_Manager manager,
  84. FTC_SBitFamily sfam,
  85. FT_UInt gindex,
  86. FT_ULong *asize )
  87. {
  88. FT_Error error;
  89. FTC_GlyphNode gnode = FTC_GLYPH_NODE( snode );
  90. FT_Memory memory;
  91. FT_Face face;
  92. FT_Size size;
  93. FTC_SBit sbit;
  94. if ( gindex < (FT_UInt)gnode->item_start ||
  95. gindex >= (FT_UInt)gnode->item_start + gnode->item_count )
  96. {
  97. FT_ERROR(( "ftc_sbit_node_load: invalid glyph index" ));
  98. return FTC_Err_Invalid_Argument;
  99. }
  100. memory = manager->library->memory;
  101. sbit = snode->sbits + ( gindex - gnode->item_start );
  102. error = FTC_Manager_Lookup_Size( manager, &sfam->type.font,
  103. &face, &size );
  104. if ( !error )
  105. {
  106. /* by default, indicates a `missing' glyph */
  107. sbit->buffer = 0;
  108. error = FT_Load_Glyph( face, gindex, sfam->type.flags | FT_LOAD_RENDER );
  109. if ( !error )
  110. {
  111. FT_Int temp;
  112. FT_GlyphSlot slot = face->glyph;
  113. FT_Bitmap* bitmap = &slot->bitmap;
  114. FT_Int xadvance, yadvance;
  115. /* check that our values fit into 8-bit containers! */
  116. /* If this is not the case, our bitmap is too large */
  117. /* and we will leave it as `missing' with sbit.buffer = 0 */
  118. #define CHECK_CHAR( d ) ( temp = (FT_Char)d, temp == d )
  119. #define CHECK_BYTE( d ) ( temp = (FT_Byte)d, temp == d )
  120. /* XXX: FIXME: add support for vertical layouts maybe */
  121. /* horizontal advance in pixels */
  122. xadvance = ( slot->metrics.horiAdvance + 32 ) >> 6;
  123. yadvance = ( slot->metrics.vertAdvance + 32 ) >> 6;
  124. if ( CHECK_BYTE( bitmap->rows ) &&
  125. CHECK_BYTE( bitmap->width ) &&
  126. CHECK_CHAR( bitmap->pitch ) &&
  127. CHECK_CHAR( slot->bitmap_left ) &&
  128. CHECK_CHAR( slot->bitmap_top ) &&
  129. CHECK_CHAR( xadvance ) &&
  130. CHECK_CHAR( yadvance ) )
  131. {
  132. sbit->width = (FT_Byte)bitmap->width;
  133. sbit->height = (FT_Byte)bitmap->rows;
  134. sbit->pitch = (FT_Char)bitmap->pitch;
  135. sbit->left = (FT_Char)slot->bitmap_left;
  136. sbit->top = (FT_Char)slot->bitmap_top;
  137. sbit->xadvance = (FT_Char)xadvance;
  138. sbit->yadvance = (FT_Char)yadvance;
  139. sbit->format = (FT_Byte)bitmap->pixel_mode;
  140. sbit->max_grays = (FT_Byte)(bitmap->num_grays - 1);
  141. #if 0 /* this doesn't work well with embedded bitmaps !! */
  142. /* grab the bitmap when possible - this is a hack! */
  143. if ( slot->flags & FT_GLYPH_OWN_BITMAP )
  144. {
  145. slot->flags &= ~FT_GLYPH_OWN_BITMAP;
  146. sbit->buffer = bitmap->buffer;
  147. }
  148. else
  149. #endif
  150. {
  151. /* copy the bitmap into a new buffer -- ignore error */
  152. error = ftc_sbit_copy_bitmap( sbit, bitmap, memory );
  153. }
  154. /* now, compute size */
  155. if ( asize )
  156. *asize = ABS( sbit->pitch ) * sbit->height;
  157. } /* glyph dimensions ok */
  158. } /* glyph loading successful */
  159. /* ignore the errors that might have occurred -- */
  160. /* we mark unloaded glyphs with `sbit.buffer == 0' */
  161. /* and 'width == 255', 'height == 0' */
  162. /* */
  163. if ( error )
  164. {
  165. sbit->width = 255;
  166. error = 0;
  167. /* sbit->buffer == NULL too! */
  168. }
  169. }
  170. return error;
  171. }
  172. FT_CALLBACK_DEF( FT_Error )
  173. ftc_sbit_node_init( FTC_SBitNode snode,
  174. FTC_GlyphQuery gquery,
  175. FTC_Cache cache )
  176. {
  177. FT_Error error;
  178. ftc_glyph_node_init( FTC_GLYPH_NODE( snode ),
  179. gquery->gindex,
  180. FTC_GLYPH_FAMILY( gquery->query.family ) );
  181. error = ftc_sbit_node_load( snode,
  182. cache->manager,
  183. FTC_SBIT_FAMILY( FTC_QUERY( gquery )->family ),
  184. gquery->gindex,
  185. NULL );
  186. if ( error )
  187. ftc_glyph_node_done( FTC_GLYPH_NODE( snode ), cache );
  188. return error;
  189. }
  190. FT_CALLBACK_DEF( FT_ULong )
  191. ftc_sbit_node_weight( FTC_SBitNode snode )
  192. {
  193. FTC_GlyphNode gnode = FTC_GLYPH_NODE( snode );
  194. FT_UInt count = gnode->item_count;
  195. FTC_SBit sbit = snode->sbits;
  196. FT_Int pitch;
  197. FT_ULong size;
  198. /* the node itself */
  199. size = sizeof ( *snode );
  200. /* the sbit records */
  201. size += FTC_GLYPH_NODE( snode )->item_count * sizeof ( FTC_SBitRec );
  202. for ( ; count > 0; count--, sbit++ )
  203. {
  204. if ( sbit->buffer )
  205. {
  206. pitch = sbit->pitch;
  207. if ( pitch < 0 )
  208. pitch = -pitch;
  209. /* add the size of a given glyph image */
  210. size += pitch * sbit->height;
  211. }
  212. }
  213. return size;
  214. }
  215. FT_CALLBACK_DEF( FT_Bool )
  216. ftc_sbit_node_compare( FTC_SBitNode snode,
  217. FTC_SBitQuery squery,
  218. FTC_Cache cache )
  219. {
  220. FTC_GlyphQuery gquery = FTC_GLYPH_QUERY( squery );
  221. FTC_GlyphNode gnode = FTC_GLYPH_NODE( snode );
  222. FT_Bool result;
  223. result = ftc_glyph_node_compare( gnode, gquery );
  224. if ( result )
  225. {
  226. /* check if we need to load the glyph bitmap now */
  227. FT_UInt gindex = gquery->gindex;
  228. FTC_SBit sbit = snode->sbits + ( gindex - gnode->item_start );
  229. if ( sbit->buffer == NULL && sbit->width != 255 )
  230. {
  231. FT_ULong size;
  232. /* yes, it's safe to ignore errors here */
  233. ftc_sbit_node_load( snode,
  234. cache->manager,
  235. FTC_SBIT_FAMILY( FTC_QUERY( squery )->family ),
  236. gindex,
  237. &size );
  238. cache->manager->cur_weight += size;
  239. }
  240. }
  241. return result;
  242. }
  243. /*************************************************************************/
  244. /*************************************************************************/
  245. /***** *****/
  246. /***** SBITS FAMILIES *****/
  247. /***** *****/
  248. /*************************************************************************/
  249. /*************************************************************************/
  250. FT_CALLBACK_DEF( FT_Error )
  251. ftc_sbit_family_init( FTC_SBitFamily sfam,
  252. FTC_SBitQuery squery,
  253. FTC_Cache cache )
  254. {
  255. FTC_Manager manager = cache->manager;
  256. FT_Error error;
  257. FT_Face face;
  258. sfam->type = squery->type;
  259. /* we need to compute "cquery.item_total" now */
  260. error = FTC_Manager_Lookup_Face( manager,
  261. squery->type.font.face_id,
  262. &face );
  263. if ( !error )
  264. {
  265. error = ftc_glyph_family_init( FTC_GLYPH_FAMILY( sfam ),
  266. FTC_IMAGE_TYPE_HASH( &sfam->type ),
  267. FTC_SBIT_ITEMS_PER_NODE,
  268. face->num_glyphs,
  269. FTC_GLYPH_QUERY( squery ),
  270. cache );
  271. }
  272. return error;
  273. }
  274. FT_CALLBACK_DEF( FT_Bool )
  275. ftc_sbit_family_compare( FTC_SBitFamily sfam,
  276. FTC_SBitQuery squery )
  277. {
  278. FT_Bool result;
  279. /* we need to set the "cquery.cset" field or our query for */
  280. /* faster glyph comparisons in ftc_sbit_node_compare */
  281. /* */
  282. result = FT_BOOL( FTC_IMAGE_TYPE_COMPARE( &sfam->type, &squery->type ) );
  283. if ( result )
  284. FTC_GLYPH_FAMILY_FOUND( sfam, squery );
  285. return result;
  286. }
  287. /*************************************************************************/
  288. /*************************************************************************/
  289. /***** *****/
  290. /***** SBITS CACHE *****/
  291. /***** *****/
  292. /*************************************************************************/
  293. /*************************************************************************/
  294. FT_CALLBACK_TABLE_DEF
  295. const FTC_Cache_ClassRec ftc_sbit_cache_class =
  296. {
  297. sizeof ( FTC_CacheRec ),
  298. (FTC_Cache_InitFunc) ftc_cache_init,
  299. (FTC_Cache_ClearFunc)ftc_cache_clear,
  300. (FTC_Cache_DoneFunc) ftc_cache_done,
  301. sizeof ( FTC_SBitFamilyRec ),
  302. (FTC_Family_InitFunc) ftc_sbit_family_init,
  303. (FTC_Family_CompareFunc)ftc_sbit_family_compare,
  304. (FTC_Family_DoneFunc) ftc_glyph_family_done,
  305. sizeof ( FTC_SBitNodeRec ),
  306. (FTC_Node_InitFunc) ftc_sbit_node_init,
  307. (FTC_Node_WeightFunc) ftc_sbit_node_weight,
  308. (FTC_Node_CompareFunc)ftc_sbit_node_compare,
  309. (FTC_Node_DoneFunc) ftc_sbit_node_done
  310. };
  311. /* documentation is in ftcsbits.h */
  312. FT_EXPORT_DEF( FT_Error )
  313. FTC_SBitCache_New( FTC_Manager manager,
  314. FTC_SBitCache *acache )
  315. {
  316. return FTC_Manager_Register_Cache( manager,
  317. &ftc_sbit_cache_class,
  318. (FTC_Cache*)acache );
  319. }
  320. /* documentation is in ftcsbits.h */
  321. #ifdef FTC_CACHE_USE_INLINE
  322. #define GEN_CACHE_FAMILY_COMPARE( f, q, c ) \
  323. ftc_sbit_family_compare( (FTC_SBitFamily)(f), (FTC_SBitQuery)(q) )
  324. #define GEN_CACHE_NODE_COMPARE( n, q, c ) \
  325. ftc_sbit_node_compare( (FTC_SBitNode)(n), (FTC_SBitQuery)(q), c )
  326. #define GEN_CACHE_LOOKUP ftc_sbit_cache_lookup
  327. #include "ftccache.i"
  328. #else /* !FTC_CACHE_USE_INLINE */
  329. #define ftc_sbit_cache_lookup ftc_cache_lookup
  330. #endif /* !FTC_CACHE_USE_INLINE */
  331. FT_EXPORT_DEF( FT_Error )
  332. FTC_SBitCache_Lookup( FTC_SBitCache cache,
  333. FTC_ImageType type,
  334. FT_UInt gindex,
  335. FTC_SBit *ansbit,
  336. FTC_Node *anode )
  337. {
  338. FT_Error error;
  339. FTC_SBitQueryRec squery;
  340. FTC_SBitNode node;
  341. /* other argument checks delayed to ftc_cache_lookup */
  342. if ( !ansbit )
  343. return FTC_Err_Invalid_Argument;
  344. *ansbit = NULL;
  345. if ( anode )
  346. *anode = NULL;
  347. squery.gquery.gindex = gindex;
  348. squery.type = *type;
  349. error = ftc_sbit_cache_lookup( FTC_CACHE( cache ),
  350. FTC_QUERY( &squery ),
  351. (FTC_Node*)&node );
  352. if ( !error )
  353. {
  354. *ansbit = node->sbits + ( gindex - FTC_GLYPH_NODE( node )->item_start );
  355. if ( anode )
  356. {
  357. *anode = FTC_NODE( node );
  358. FTC_NODE( node )->ref_count++;
  359. }
  360. }
  361. return error;
  362. }
  363. /* backwards-compatibility functions */
  364. FT_EXPORT_DEF( FT_Error )
  365. FTC_SBit_Cache_New( FTC_Manager manager,
  366. FTC_SBit_Cache *acache )
  367. {
  368. return FTC_SBitCache_New( manager, (FTC_SBitCache*)acache );
  369. }
  370. FT_EXPORT_DEF( FT_Error )
  371. FTC_SBit_Cache_Lookup( FTC_SBit_Cache cache,
  372. FTC_Image_Desc* desc,
  373. FT_UInt gindex,
  374. FTC_SBit *ansbit )
  375. {
  376. FTC_ImageTypeRec type0;
  377. if ( !desc )
  378. return FTC_Err_Invalid_Argument;
  379. type0.font = desc->font;
  380. type0.flags = 0;
  381. /* convert image type flags to load flags */
  382. {
  383. FT_UInt load_flags = FT_LOAD_DEFAULT;
  384. FT_UInt type = desc->image_type;
  385. /* determine load flags, depending on the font description's */
  386. /* image type */
  387. if ( ftc_image_format( type ) == ftc_image_format_bitmap )
  388. {
  389. if ( type & ftc_image_flag_monochrome )
  390. load_flags |= FT_LOAD_MONOCHROME;
  391. /* disable embedded bitmaps loading if necessary */
  392. if ( type & ftc_image_flag_no_sbits )
  393. load_flags |= FT_LOAD_NO_BITMAP;
  394. }
  395. else
  396. {
  397. /* we want an outline, don't load embedded bitmaps */
  398. load_flags |= FT_LOAD_NO_BITMAP;
  399. if ( type & ftc_image_flag_unscaled )
  400. load_flags |= FT_LOAD_NO_SCALE;
  401. }
  402. /* always render glyphs to bitmaps */
  403. load_flags |= FT_LOAD_RENDER;
  404. if ( type & ftc_image_flag_unhinted )
  405. load_flags |= FT_LOAD_NO_HINTING;
  406. if ( type & ftc_image_flag_autohinted )
  407. load_flags |= FT_LOAD_FORCE_AUTOHINT;
  408. type0.flags = load_flags;
  409. }
  410. return FTC_SBitCache_Lookup( (FTC_SBitCache)cache,
  411. &type0,
  412. gindex,
  413. ansbit,
  414. NULL );
  415. }
  416. /* END */