sfdriver.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /***************************************************************************/
  2. /* */
  3. /* sfdriver.c */
  4. /* */
  5. /* High-level SFNT driver interface (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_INTERNAL_SFNT_H
  19. #include FT_INTERNAL_OBJECTS_H
  20. #include "sfdriver.h"
  21. #include "ttload.h"
  22. #include "ttcmap.h"
  23. #include "sfobjs.h"
  24. #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
  25. #include "ttsbit.h"
  26. #endif
  27. #ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES
  28. #include "ttpost.h"
  29. #endif
  30. static void*
  31. get_sfnt_table( TT_Face face,
  32. FT_Sfnt_Tag tag )
  33. {
  34. void* table;
  35. switch ( tag )
  36. {
  37. case ft_sfnt_head:
  38. table = &face->header;
  39. break;
  40. case ft_sfnt_hhea:
  41. table = &face->horizontal;
  42. break;
  43. case ft_sfnt_vhea:
  44. table = face->vertical_info ? &face->vertical : 0;
  45. break;
  46. case ft_sfnt_os2:
  47. table = face->os2.version == 0xFFFFU ? 0 : &face->os2;
  48. break;
  49. case ft_sfnt_post:
  50. table = &face->postscript;
  51. break;
  52. case ft_sfnt_maxp:
  53. table = &face->max_profile;
  54. break;
  55. case ft_sfnt_pclt:
  56. table = face->pclt.Version ? &face->pclt : 0;
  57. break;
  58. default:
  59. table = 0;
  60. }
  61. return table;
  62. }
  63. #ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES
  64. static FT_Error
  65. get_sfnt_glyph_name( TT_Face face,
  66. FT_UInt glyph_index,
  67. FT_Pointer buffer,
  68. FT_UInt buffer_max )
  69. {
  70. FT_String* gname;
  71. FT_Error error;
  72. error = tt_face_get_ps_name( face, glyph_index, &gname );
  73. if ( !error && buffer_max > 0 )
  74. {
  75. FT_UInt len = (FT_UInt)( ft_strlen( gname ) );
  76. if ( len >= buffer_max )
  77. len = buffer_max - 1;
  78. FT_MEM_COPY( buffer, gname, len );
  79. ((FT_Byte*)buffer)[len] = 0;
  80. }
  81. return error;
  82. }
  83. static const char*
  84. get_sfnt_postscript_name( TT_Face face )
  85. {
  86. FT_Int n, found_win, found_apple;
  87. const char* result = NULL;
  88. /* shouldn't happen, but just in case to avoid memory leaks */
  89. if ( face->root.internal->postscript_name )
  90. return face->root.internal->postscript_name;
  91. /* scan the name table to see whether we have a Postscript name here, */
  92. /* either in Macintosh or Windows platform encodings */
  93. found_win = -1;
  94. found_apple = -1;
  95. for ( n = 0; n < face->num_names; n++ )
  96. {
  97. TT_NameEntryRec* name = face->name_table.names + n;
  98. if ( name->nameID == 6 && name->stringLength > 0 )
  99. {
  100. if ( name->platformID == 3 &&
  101. name->encodingID == 1 &&
  102. name->languageID == 0x409 )
  103. found_win = n;
  104. if ( name->platformID == 1 &&
  105. name->encodingID == 0 &&
  106. name->languageID == 0 )
  107. found_apple = n;
  108. }
  109. }
  110. if ( found_win != -1 )
  111. {
  112. FT_Memory memory = face->root.memory;
  113. TT_NameEntryRec* name = face->name_table.names + found_win;
  114. FT_UInt len = name->stringLength / 2;
  115. FT_Error error;
  116. if ( !FT_ALLOC( result, name->stringLength + 1 ) )
  117. {
  118. FT_Stream stream = face->name_table.stream;
  119. FT_String* r = (FT_String*)result;
  120. FT_Byte* p = (FT_Byte*)name->string;
  121. if ( FT_STREAM_SEEK( name->stringOffset ) ||
  122. FT_FRAME_ENTER( name->stringLength ) )
  123. {
  124. FT_FREE( result );
  125. name->stringLength = 0;
  126. name->stringOffset = 0;
  127. FT_FREE( name->string );
  128. goto Exit;
  129. }
  130. p = (FT_Byte*)stream->cursor;
  131. for ( ; len > 0; len--, p += 2 )
  132. {
  133. if ( p[0] == 0 && p[1] >= 32 && p[1] < 128 )
  134. *r++ = p[1];
  135. }
  136. *r = '\0';
  137. FT_FRAME_EXIT();
  138. }
  139. goto Exit;
  140. }
  141. if ( found_apple != -1 )
  142. {
  143. FT_Memory memory = face->root.memory;
  144. TT_NameEntryRec* name = face->name_table.names + found_apple;
  145. FT_UInt len = name->stringLength;
  146. FT_Error error;
  147. if ( !FT_ALLOC( result, len + 1 ) )
  148. {
  149. FT_Stream stream = face->name_table.stream;
  150. if ( FT_STREAM_SEEK( name->stringOffset ) ||
  151. FT_STREAM_READ( result, len ) )
  152. {
  153. name->stringOffset = 0;
  154. name->stringLength = 0;
  155. FT_FREE( name->string );
  156. FT_FREE( result );
  157. goto Exit;
  158. }
  159. ((char*)result)[len] = '\0';
  160. }
  161. }
  162. Exit:
  163. face->root.internal->postscript_name = result;
  164. return result;
  165. }
  166. #endif /* TT_CONFIG_OPTION_POSTSCRIPT_NAMES */
  167. FT_CALLBACK_DEF( FT_Module_Interface )
  168. sfnt_get_interface( FT_Module module,
  169. const char* module_interface )
  170. {
  171. FT_UNUSED( module );
  172. if ( ft_strcmp( module_interface, "get_sfnt" ) == 0 )
  173. return (FT_Module_Interface)get_sfnt_table;
  174. #ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES
  175. if ( ft_strcmp( module_interface, "glyph_name" ) == 0 )
  176. return (FT_Module_Interface)get_sfnt_glyph_name;
  177. #endif
  178. if ( ft_strcmp( module_interface, "postscript_name" ) == 0 )
  179. return (FT_Module_Interface)get_sfnt_postscript_name;
  180. return 0;
  181. }
  182. static
  183. const SFNT_Interface sfnt_interface =
  184. {
  185. tt_face_goto_table,
  186. sfnt_init_face,
  187. sfnt_load_face,
  188. sfnt_done_face,
  189. sfnt_get_interface,
  190. tt_face_load_any,
  191. tt_face_load_sfnt_header,
  192. tt_face_load_directory,
  193. tt_face_load_header,
  194. tt_face_load_metrics_header,
  195. tt_face_load_cmap,
  196. tt_face_load_max_profile,
  197. tt_face_load_os2,
  198. tt_face_load_postscript,
  199. tt_face_load_names,
  200. tt_face_free_names,
  201. tt_face_load_hdmx,
  202. tt_face_free_hdmx,
  203. tt_face_load_kern,
  204. tt_face_load_gasp,
  205. tt_face_load_pclt,
  206. #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
  207. /* see `ttload.h' */
  208. tt_face_load_bitmap_header,
  209. /* see `ttsbit.h' */
  210. tt_face_set_sbit_strike,
  211. tt_face_load_sbit_strikes,
  212. tt_face_load_sbit_image,
  213. tt_face_free_sbit_strikes,
  214. #else /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
  215. 0,
  216. 0,
  217. 0,
  218. 0,
  219. 0,
  220. #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
  221. #ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES
  222. /* see `ttpost.h' */
  223. tt_face_get_ps_name,
  224. tt_face_free_ps_names,
  225. #else /* TT_CONFIG_OPTION_POSTSCRIPT_NAMES */
  226. 0,
  227. 0,
  228. #endif /* TT_CONFIG_OPTION_POSTSCRIPT_NAMES */
  229. /* see `ttcmap.h' */
  230. tt_face_load_charmap,
  231. tt_face_free_charmap,
  232. };
  233. FT_CALLBACK_TABLE_DEF
  234. const FT_Module_Class sfnt_module_class =
  235. {
  236. 0, /* not a font driver or renderer */
  237. sizeof( FT_ModuleRec ),
  238. "sfnt", /* driver name */
  239. 0x10000L, /* driver version 1.0 */
  240. 0x20000L, /* driver requires FreeType 2.0 or higher */
  241. (const void*)&sfnt_interface, /* module specific interface */
  242. (FT_Module_Constructor)0,
  243. (FT_Module_Destructor) 0,
  244. (FT_Module_Requester) sfnt_get_interface
  245. };
  246. /* END */