cffcmap.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /***************************************************************************/
  2. /* */
  3. /* cffcmap.c */
  4. /* */
  5. /* CFF character mapping table (cmap) support (body). */
  6. /* */
  7. /* Copyright 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 "cffcmap.h"
  18. #include "cffload.h"
  19. /*************************************************************************/
  20. /*************************************************************************/
  21. /***** *****/
  22. /***** CFF STANDARD (AND EXPERT) ENCODING CMAPS *****/
  23. /***** *****/
  24. /*************************************************************************/
  25. /*************************************************************************/
  26. FT_CALLBACK_DEF( FT_Error )
  27. cff_cmap_encoding_init( CFF_CMapStd cmap )
  28. {
  29. TT_Face face = (TT_Face)FT_CMAP_FACE( cmap );
  30. CFF_Font cff = (CFF_Font)face->extra.data;
  31. CFF_Encoding encoding = &cff->encoding;
  32. cmap->count = encoding->count;
  33. cmap->gids = encoding->codes;
  34. return 0;
  35. }
  36. FT_CALLBACK_DEF( void )
  37. cff_cmap_encoding_done( CFF_CMapStd cmap )
  38. {
  39. cmap->count = 0;
  40. cmap->gids = NULL;
  41. }
  42. FT_CALLBACK_DEF( FT_UInt )
  43. cff_cmap_encoding_char_index( CFF_CMapStd cmap,
  44. FT_UInt32 char_code )
  45. {
  46. FT_UInt result = 0;
  47. if ( char_code < cmap->count )
  48. result = cmap->gids[char_code];
  49. return result;
  50. }
  51. FT_CALLBACK_DEF( FT_UInt )
  52. cff_cmap_encoding_char_next( CFF_CMapStd cmap,
  53. FT_UInt32 *pchar_code )
  54. {
  55. FT_UInt result = 0;
  56. FT_UInt32 char_code = *pchar_code;
  57. *pchar_code = 0;
  58. if ( char_code < cmap->count )
  59. {
  60. FT_UInt code = (FT_UInt)(char_code + 1);
  61. for (;;)
  62. {
  63. if ( code >= cmap->count )
  64. break;
  65. result = cmap->gids[code];
  66. if ( result != 0 )
  67. {
  68. *pchar_code = code;
  69. break;
  70. }
  71. code++;
  72. }
  73. }
  74. return result;
  75. }
  76. FT_CALLBACK_TABLE_DEF const FT_CMap_ClassRec
  77. cff_cmap_encoding_class_rec =
  78. {
  79. sizeof ( CFF_CMapStdRec ),
  80. (FT_CMap_InitFunc) cff_cmap_encoding_init,
  81. (FT_CMap_DoneFunc) cff_cmap_encoding_done,
  82. (FT_CMap_CharIndexFunc)cff_cmap_encoding_char_index,
  83. (FT_CMap_CharNextFunc) cff_cmap_encoding_char_next
  84. };
  85. /*************************************************************************/
  86. /*************************************************************************/
  87. /***** *****/
  88. /***** CFF SYNTHETIC UNICODE ENCODING CMAP *****/
  89. /***** *****/
  90. /*************************************************************************/
  91. /*************************************************************************/
  92. FT_CALLBACK_DEF( FT_Int )
  93. cff_cmap_uni_pair_compare( const void* pair1,
  94. const void* pair2 )
  95. {
  96. FT_UInt32 u1 = ((CFF_CMapUniPair)pair1)->unicode;
  97. FT_UInt32 u2 = ((CFF_CMapUniPair)pair2)->unicode;
  98. if ( u1 < u2 )
  99. return -1;
  100. if ( u1 > u2 )
  101. return +1;
  102. return 0;
  103. }
  104. FT_CALLBACK_DEF( FT_Error )
  105. cff_cmap_unicode_init( CFF_CMapUnicode cmap )
  106. {
  107. FT_Error error;
  108. FT_UInt count;
  109. TT_Face face = (TT_Face)FT_CMAP_FACE( cmap );
  110. FT_Memory memory = FT_FACE_MEMORY( face );
  111. CFF_Font cff = (CFF_Font)face->extra.data;
  112. CFF_Charset charset = &cff->charset;
  113. PSNames_Service psnames = (PSNames_Service)cff->psnames;
  114. cmap->num_pairs = 0;
  115. cmap->pairs = NULL;
  116. count = (FT_UInt)face->root.num_glyphs;
  117. if ( !FT_NEW_ARRAY( cmap->pairs, count ) )
  118. {
  119. FT_UInt n, new_count;
  120. CFF_CMapUniPair pair;
  121. FT_UInt32 uni_code;
  122. pair = cmap->pairs;
  123. for ( n = 0; n < count; n++ )
  124. {
  125. FT_UInt sid = charset->sids[n];
  126. const char* gname;
  127. gname = cff_index_get_sid_string( &cff->string_index, sid, psnames );
  128. /* build unsorted pair table by matching glyph names */
  129. if ( gname )
  130. {
  131. uni_code = psnames->unicode_value( gname );
  132. if ( uni_code != 0 )
  133. {
  134. pair->unicode = uni_code;
  135. pair->gindex = n;
  136. pair++;
  137. }
  138. FT_FREE( gname );
  139. }
  140. }
  141. new_count = (FT_UInt)( pair - cmap->pairs );
  142. if ( new_count == 0 )
  143. {
  144. /* there are no unicode characters in here! */
  145. FT_FREE( cmap->pairs );
  146. error = FT_Err_Invalid_Argument;
  147. }
  148. else
  149. {
  150. /* re-allocate if the new array is much smaller than the original */
  151. /* one */
  152. if ( new_count != count && new_count < count / 2 )
  153. {
  154. (void)FT_RENEW_ARRAY( cmap->pairs, count, new_count );
  155. error = 0;
  156. }
  157. /* sort the pairs table to allow efficient binary searches */
  158. ft_qsort( cmap->pairs,
  159. new_count,
  160. sizeof ( CFF_CMapUniPairRec ),
  161. cff_cmap_uni_pair_compare );
  162. cmap->num_pairs = new_count;
  163. }
  164. }
  165. return error;
  166. }
  167. FT_CALLBACK_DEF( void )
  168. cff_cmap_unicode_done( CFF_CMapUnicode cmap )
  169. {
  170. FT_Face face = FT_CMAP_FACE( cmap );
  171. FT_Memory memory = FT_FACE_MEMORY( face );
  172. FT_FREE( cmap->pairs );
  173. cmap->num_pairs = 0;
  174. }
  175. FT_CALLBACK_DEF( FT_UInt )
  176. cff_cmap_unicode_char_index( CFF_CMapUnicode cmap,
  177. FT_UInt32 char_code )
  178. {
  179. FT_UInt min = 0;
  180. FT_UInt max = cmap->num_pairs;
  181. FT_UInt mid;
  182. CFF_CMapUniPair pair;
  183. while ( min < max )
  184. {
  185. mid = min + ( max - min ) / 2;
  186. pair = cmap->pairs + mid;
  187. if ( pair->unicode == char_code )
  188. return pair->gindex;
  189. if ( pair->unicode < char_code )
  190. min = mid + 1;
  191. else
  192. max = mid;
  193. }
  194. return 0;
  195. }
  196. FT_CALLBACK_DEF( FT_UInt )
  197. cff_cmap_unicode_char_next( CFF_CMapUnicode cmap,
  198. FT_UInt32 *pchar_code )
  199. {
  200. FT_UInt result = 0;
  201. FT_UInt32 char_code = *pchar_code + 1;
  202. Restart:
  203. {
  204. FT_UInt min = 0;
  205. FT_UInt max = cmap->num_pairs;
  206. FT_UInt mid;
  207. CFF_CMapUniPair pair;
  208. while ( min < max )
  209. {
  210. mid = min + ( ( max - min ) >> 1 );
  211. pair = cmap->pairs + mid;
  212. if ( pair->unicode == char_code )
  213. {
  214. result = pair->gindex;
  215. if ( result != 0 )
  216. goto Exit;
  217. char_code++;
  218. goto Restart;
  219. }
  220. if ( pair->unicode < char_code )
  221. min = mid+1;
  222. else
  223. max = mid;
  224. }
  225. /* we didn't find it, but we have a pair just above it */
  226. char_code = 0;
  227. if ( min < cmap->num_pairs )
  228. {
  229. pair = cmap->pairs + min;
  230. result = pair->gindex;
  231. if ( result != 0 )
  232. char_code = pair->unicode;
  233. }
  234. }
  235. Exit:
  236. *pchar_code = char_code;
  237. return result;
  238. }
  239. FT_CALLBACK_TABLE_DEF const FT_CMap_ClassRec
  240. cff_cmap_unicode_class_rec =
  241. {
  242. sizeof ( CFF_CMapUnicodeRec ),
  243. (FT_CMap_InitFunc) cff_cmap_unicode_init,
  244. (FT_CMap_DoneFunc) cff_cmap_unicode_done,
  245. (FT_CMap_CharIndexFunc)cff_cmap_unicode_char_index,
  246. (FT_CMap_CharNextFunc) cff_cmap_unicode_char_next
  247. };
  248. /* END */