cffdrivr.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /***************************************************************************/
  2. /* */
  3. /* cffdrivr.c */
  4. /* */
  5. /* OpenType font driver implementation (body). */
  6. /* */
  7. /* Copyright 1996-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_FREETYPE_H
  19. #include FT_INTERNAL_DEBUG_H
  20. #include FT_INTERNAL_STREAM_H
  21. #include FT_INTERNAL_SFNT_H
  22. #include FT_TRUETYPE_IDS_H
  23. #include FT_INTERNAL_POSTSCRIPT_NAMES_H
  24. #include "cffdrivr.h"
  25. #include "cffgload.h"
  26. #include "cffload.h"
  27. #include "cfferrs.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_cffdriver
  36. /*************************************************************************/
  37. /*************************************************************************/
  38. /*************************************************************************/
  39. /**** ****/
  40. /**** ****/
  41. /**** F A C E S ****/
  42. /**** ****/
  43. /**** ****/
  44. /*************************************************************************/
  45. /*************************************************************************/
  46. /*************************************************************************/
  47. #undef PAIR_TAG
  48. #define PAIR_TAG( left, right ) ( ( (FT_ULong)left << 16 ) | \
  49. (FT_ULong)right )
  50. /*************************************************************************/
  51. /* */
  52. /* <Function> */
  53. /* Get_Kerning */
  54. /* */
  55. /* <Description> */
  56. /* A driver method used to return the kerning vector between two */
  57. /* glyphs of the same face. */
  58. /* */
  59. /* <Input> */
  60. /* face :: A handle to the source face object. */
  61. /* */
  62. /* left_glyph :: The index of the left glyph in the kern pair. */
  63. /* */
  64. /* right_glyph :: The index of the right glyph in the kern pair. */
  65. /* */
  66. /* <Output> */
  67. /* kerning :: The kerning vector. This is in font units for */
  68. /* scalable formats, and in pixels for fixed-sizes */
  69. /* formats. */
  70. /* */
  71. /* <Return> */
  72. /* FreeType error code. 0 means success. */
  73. /* */
  74. /* <Note> */
  75. /* Only horizontal layouts (left-to-right & right-to-left) are */
  76. /* supported by this function. Other layouts, or more sophisticated */
  77. /* kernings, are out of scope of this method (the basic driver */
  78. /* interface is meant to be simple). */
  79. /* */
  80. /* They can be implemented by format-specific interfaces. */
  81. /* */
  82. static FT_Error
  83. Get_Kerning( TT_Face face,
  84. FT_UInt left_glyph,
  85. FT_UInt right_glyph,
  86. FT_Vector* kerning )
  87. {
  88. TT_Kern0_Pair pair;
  89. if ( !face )
  90. return CFF_Err_Invalid_Face_Handle;
  91. kerning->x = 0;
  92. kerning->y = 0;
  93. if ( face->kern_pairs )
  94. {
  95. /* there are some kerning pairs in this font file! */
  96. FT_ULong search_tag = PAIR_TAG( left_glyph, right_glyph );
  97. FT_Long left, right;
  98. left = 0;
  99. right = face->num_kern_pairs - 1;
  100. while ( left <= right )
  101. {
  102. FT_Long middle = left + ( ( right - left ) >> 1 );
  103. FT_ULong cur_pair;
  104. pair = face->kern_pairs + middle;
  105. cur_pair = PAIR_TAG( pair->left, pair->right );
  106. if ( cur_pair == search_tag )
  107. goto Found;
  108. if ( cur_pair < search_tag )
  109. left = middle + 1;
  110. else
  111. right = middle - 1;
  112. }
  113. }
  114. Exit:
  115. return CFF_Err_Ok;
  116. Found:
  117. kerning->x = pair->value;
  118. goto Exit;
  119. }
  120. #undef PAIR_TAG
  121. /*************************************************************************/
  122. /* */
  123. /* <Function> */
  124. /* Load_Glyph */
  125. /* */
  126. /* <Description> */
  127. /* A driver method used to load a glyph within a given glyph slot. */
  128. /* */
  129. /* <Input> */
  130. /* slot :: A handle to the target slot object where the glyph */
  131. /* will be loaded. */
  132. /* */
  133. /* size :: A handle to the source face size at which the glyph */
  134. /* must be scaled, loaded, etc. */
  135. /* */
  136. /* glyph_index :: The index of the glyph in the font file. */
  137. /* */
  138. /* load_flags :: A flag indicating what to load for this glyph. The */
  139. /* FTLOAD_??? constants can be used to control the */
  140. /* glyph loading process (e.g., whether the outline */
  141. /* should be scaled, whether to load bitmaps or not, */
  142. /* whether to hint the outline, etc). */
  143. /* */
  144. /* <Return> */
  145. /* FreeType error code. 0 means success. */
  146. /* */
  147. static FT_Error
  148. Load_Glyph( CFF_GlyphSlot slot,
  149. CFF_Size size,
  150. FT_UShort glyph_index,
  151. FT_Int32 load_flags )
  152. {
  153. FT_Error error;
  154. if ( !slot )
  155. return CFF_Err_Invalid_Slot_Handle;
  156. /* check whether we want a scaled outline or bitmap */
  157. if ( !size )
  158. load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
  159. if ( load_flags & FT_LOAD_NO_SCALE )
  160. size = NULL;
  161. /* reset the size object if necessary */
  162. if ( size )
  163. {
  164. /* these two object must have the same parent */
  165. if ( size->face != slot->root.face )
  166. return CFF_Err_Invalid_Face_Handle;
  167. }
  168. /* now load the glyph outline if necessary */
  169. error = cff_slot_load( slot, size, glyph_index, load_flags );
  170. /* force drop-out mode to 2 - irrelevant now */
  171. /* slot->outline.dropout_mode = 2; */
  172. return error;
  173. }
  174. /*************************************************************************/
  175. /*************************************************************************/
  176. /*************************************************************************/
  177. /**** ****/
  178. /**** ****/
  179. /**** C H A R A C T E R M A P P I N G S ****/
  180. /**** ****/
  181. /**** ****/
  182. /*************************************************************************/
  183. /*************************************************************************/
  184. /*************************************************************************/
  185. static FT_Error
  186. cff_get_glyph_name( CFF_Face face,
  187. FT_UInt glyph_index,
  188. FT_Pointer buffer,
  189. FT_UInt buffer_max )
  190. {
  191. CFF_Font font = (CFF_Font)face->extra.data;
  192. FT_Memory memory = FT_FACE_MEMORY( face );
  193. FT_String* gname;
  194. FT_UShort sid;
  195. PSNames_Service psnames;
  196. FT_Error error;
  197. psnames = (PSNames_Service)FT_Get_Module_Interface(
  198. face->root.driver->root.library, "psnames" );
  199. if ( !psnames )
  200. {
  201. FT_ERROR(( "cff_get_glyph_name:" ));
  202. FT_ERROR(( " cannot open CFF & CEF fonts\n" ));
  203. FT_ERROR(( " " ));
  204. FT_ERROR(( " without the `PSNames' module\n" ));
  205. error = CFF_Err_Unknown_File_Format;
  206. goto Exit;
  207. }
  208. /* first, locate the sid in the charset table */
  209. sid = font->charset.sids[glyph_index];
  210. /* now, lookup the name itself */
  211. gname = cff_index_get_sid_string( &font->string_index, sid, psnames );
  212. if ( buffer_max > 0 )
  213. {
  214. FT_UInt len = (FT_UInt)ft_strlen( gname );
  215. if ( len >= buffer_max )
  216. len = buffer_max - 1;
  217. FT_MEM_COPY( buffer, gname, len );
  218. ((FT_Byte*)buffer)[len] = 0;
  219. }
  220. FT_FREE ( gname );
  221. error = CFF_Err_Ok;
  222. Exit:
  223. return error;
  224. }
  225. /*************************************************************************/
  226. /* */
  227. /* <Function> */
  228. /* cff_get_name_index */
  229. /* */
  230. /* <Description> */
  231. /* Uses the psnames module and the CFF font's charset to to return a */
  232. /* a given glyph name's glyph index. */
  233. /* */
  234. /* <Input> */
  235. /* face :: A handle to the source face object. */
  236. /* */
  237. /* glyph_name :: The glyph name. */
  238. /* */
  239. /* <Return> */
  240. /* Glyph index. 0 means `undefined character code'. */
  241. /* */
  242. static FT_UInt
  243. cff_get_name_index( CFF_Face face,
  244. FT_String* glyph_name )
  245. {
  246. CFF_Font cff;
  247. CFF_Charset charset;
  248. PSNames_Service psnames;
  249. FT_Memory memory = FT_FACE_MEMORY( face );
  250. FT_String* name;
  251. FT_UShort sid;
  252. FT_UInt i;
  253. FT_Int result;
  254. cff = (CFF_FontRec *)face->extra.data;
  255. charset = &cff->charset;
  256. psnames = (PSNames_Service)FT_Get_Module_Interface(
  257. face->root.driver->root.library, "psnames" );
  258. for ( i = 0; i < cff->num_glyphs; i++ )
  259. {
  260. sid = charset->sids[i];
  261. if ( sid > 390 )
  262. name = cff_index_get_name( &cff->string_index, sid - 391 );
  263. else
  264. name = (FT_String *)psnames->adobe_std_strings( sid );
  265. result = ft_strcmp( glyph_name, name );
  266. if ( sid > 390 )
  267. FT_FREE( name );
  268. if ( !result )
  269. return i;
  270. }
  271. return 0;
  272. }
  273. /*************************************************************************/
  274. /*************************************************************************/
  275. /*************************************************************************/
  276. /**** ****/
  277. /**** ****/
  278. /**** D R I V E R I N T E R F A C E ****/
  279. /**** ****/
  280. /**** ****/
  281. /*************************************************************************/
  282. /*************************************************************************/
  283. /*************************************************************************/
  284. static FT_Module_Interface
  285. cff_get_interface( CFF_Driver driver,
  286. const char* module_interface )
  287. {
  288. FT_Module sfnt;
  289. #ifndef FT_CONFIG_OPTION_NO_GLYPH_NAMES
  290. if ( ft_strcmp( (const char*)module_interface, "glyph_name" ) == 0 )
  291. return (FT_Module_Interface)cff_get_glyph_name;
  292. if ( ft_strcmp( (const char*)module_interface, "name_index" ) == 0 )
  293. return (FT_Module_Interface)cff_get_name_index;
  294. #endif
  295. /* we simply pass our request to the `sfnt' module */
  296. sfnt = FT_Get_Module( driver->root.root.library, "sfnt" );
  297. return sfnt ? sfnt->clazz->get_interface( sfnt, module_interface ) : 0;
  298. }
  299. /* The FT_DriverInterface structure is defined in ftdriver.h. */
  300. FT_CALLBACK_TABLE_DEF
  301. const FT_Driver_ClassRec cff_driver_class =
  302. {
  303. /* begin with the FT_Module_Class fields */
  304. {
  305. ft_module_font_driver |
  306. ft_module_driver_scalable |
  307. ft_module_driver_has_hinter,
  308. sizeof( CFF_DriverRec ),
  309. "cff",
  310. 0x10000L,
  311. 0x20000L,
  312. 0, /* module-specific interface */
  313. (FT_Module_Constructor)cff_driver_init,
  314. (FT_Module_Destructor) cff_driver_done,
  315. (FT_Module_Requester) cff_get_interface,
  316. },
  317. /* now the specific driver fields */
  318. sizeof( TT_FaceRec ),
  319. sizeof( FT_SizeRec ),
  320. sizeof( CFF_GlyphSlotRec ),
  321. (FT_Face_InitFunc) cff_face_init,
  322. (FT_Face_DoneFunc) cff_face_done,
  323. (FT_Size_InitFunc) cff_size_init,
  324. (FT_Size_DoneFunc) cff_size_done,
  325. (FT_Slot_InitFunc) cff_slot_init,
  326. (FT_Slot_DoneFunc) cff_slot_done,
  327. (FT_Size_ResetPointsFunc)cff_size_reset,
  328. (FT_Size_ResetPixelsFunc)cff_size_reset,
  329. (FT_Slot_LoadFunc) Load_Glyph,
  330. (FT_Face_GetKerningFunc) Get_Kerning,
  331. (FT_Face_AttachFunc) 0,
  332. (FT_Face_GetAdvancesFunc)0,
  333. };
  334. /* END */