/src/freetype/src/truetype/ttdriver.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 502 lines · 273 code · 105 blank · 124 comment · 27 complexity · f4b985b912fe931368ca05c943260dfa MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* ttdriver.c */
  4. /* */
  5. /* TrueType font driver implementation (body). */
  6. /* */
  7. /* Copyright 1996-2012 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_DEBUG_H
  19. #include FT_INTERNAL_STREAM_H
  20. #include FT_INTERNAL_SFNT_H
  21. #include FT_SERVICE_XFREE86_NAME_H
  22. #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
  23. #include FT_MULTIPLE_MASTERS_H
  24. #include FT_SERVICE_MULTIPLE_MASTERS_H
  25. #endif
  26. #include FT_SERVICE_TRUETYPE_ENGINE_H
  27. #include FT_SERVICE_TRUETYPE_GLYF_H
  28. #include "ttdriver.h"
  29. #include "ttgload.h"
  30. #include "ttpload.h"
  31. #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
  32. #include "ttgxvar.h"
  33. #endif
  34. #include "tterrors.h"
  35. #include "ttpic.h"
  36. /*************************************************************************/
  37. /* */
  38. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  39. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  40. /* messages during execution. */
  41. /* */
  42. #undef FT_COMPONENT
  43. #define FT_COMPONENT trace_ttdriver
  44. /*************************************************************************/
  45. /*************************************************************************/
  46. /*************************************************************************/
  47. /**** ****/
  48. /**** ****/
  49. /**** F A C E S ****/
  50. /**** ****/
  51. /**** ****/
  52. /*************************************************************************/
  53. /*************************************************************************/
  54. /*************************************************************************/
  55. #undef PAIR_TAG
  56. #define PAIR_TAG( left, right ) ( ( (FT_ULong)left << 16 ) | \
  57. (FT_ULong)right )
  58. /*************************************************************************/
  59. /* */
  60. /* <Function> */
  61. /* tt_get_kerning */
  62. /* */
  63. /* <Description> */
  64. /* A driver method used to return the kerning vector between two */
  65. /* glyphs of the same face. */
  66. /* */
  67. /* <Input> */
  68. /* face :: A handle to the source face object. */
  69. /* */
  70. /* left_glyph :: The index of the left glyph in the kern pair. */
  71. /* */
  72. /* right_glyph :: The index of the right glyph in the kern pair. */
  73. /* */
  74. /* <Output> */
  75. /* kerning :: The kerning vector. This is in font units for */
  76. /* scalable formats, and in pixels for fixed-sizes */
  77. /* formats. */
  78. /* */
  79. /* <Return> */
  80. /* FreeType error code. 0 means success. */
  81. /* */
  82. /* <Note> */
  83. /* Only horizontal layouts (left-to-right & right-to-left) are */
  84. /* supported by this function. Other layouts, or more sophisticated */
  85. /* kernings, are out of scope of this method (the basic driver */
  86. /* interface is meant to be simple). */
  87. /* */
  88. /* They can be implemented by format-specific interfaces. */
  89. /* */
  90. static FT_Error
  91. tt_get_kerning( FT_Face ttface, /* TT_Face */
  92. FT_UInt left_glyph,
  93. FT_UInt right_glyph,
  94. FT_Vector* kerning )
  95. {
  96. TT_Face face = (TT_Face)ttface;
  97. SFNT_Service sfnt = (SFNT_Service)face->sfnt;
  98. kerning->x = 0;
  99. kerning->y = 0;
  100. if ( sfnt )
  101. kerning->x = sfnt->get_kerning( face, left_glyph, right_glyph );
  102. return 0;
  103. }
  104. #undef PAIR_TAG
  105. static FT_Error
  106. tt_get_advances( FT_Face ttface,
  107. FT_UInt start,
  108. FT_UInt count,
  109. FT_Int32 flags,
  110. FT_Fixed *advances )
  111. {
  112. FT_UInt nn;
  113. TT_Face face = (TT_Face) ttface;
  114. /* XXX: TODO: check for sbits */
  115. if ( flags & FT_LOAD_VERTICAL_LAYOUT )
  116. {
  117. for ( nn = 0; nn < count; nn++ )
  118. {
  119. FT_Short tsb;
  120. FT_UShort ah;
  121. TT_Get_VMetrics( face, start + nn, &tsb, &ah );
  122. advances[nn] = ah;
  123. }
  124. }
  125. else
  126. {
  127. for ( nn = 0; nn < count; nn++ )
  128. {
  129. FT_Short lsb;
  130. FT_UShort aw;
  131. TT_Get_HMetrics( face, start + nn, &lsb, &aw );
  132. advances[nn] = aw;
  133. }
  134. }
  135. return TT_Err_Ok;
  136. }
  137. /*************************************************************************/
  138. /*************************************************************************/
  139. /*************************************************************************/
  140. /**** ****/
  141. /**** ****/
  142. /**** S I Z E S ****/
  143. /**** ****/
  144. /**** ****/
  145. /*************************************************************************/
  146. /*************************************************************************/
  147. /*************************************************************************/
  148. #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
  149. static FT_Error
  150. tt_size_select( FT_Size size,
  151. FT_ULong strike_index )
  152. {
  153. TT_Face ttface = (TT_Face)size->face;
  154. TT_Size ttsize = (TT_Size)size;
  155. FT_Error error = TT_Err_Ok;
  156. ttsize->strike_index = strike_index;
  157. if ( FT_IS_SCALABLE( size->face ) )
  158. {
  159. /* use the scaled metrics, even when tt_size_reset fails */
  160. FT_Select_Metrics( size->face, strike_index );
  161. tt_size_reset( ttsize );
  162. }
  163. else
  164. {
  165. SFNT_Service sfnt = (SFNT_Service) ttface->sfnt;
  166. FT_Size_Metrics* metrics = &size->metrics;
  167. error = sfnt->load_strike_metrics( ttface, strike_index, metrics );
  168. if ( error )
  169. ttsize->strike_index = 0xFFFFFFFFUL;
  170. }
  171. return error;
  172. }
  173. #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
  174. static FT_Error
  175. tt_size_request( FT_Size size,
  176. FT_Size_Request req )
  177. {
  178. TT_Size ttsize = (TT_Size)size;
  179. FT_Error error = TT_Err_Ok;
  180. #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
  181. if ( FT_HAS_FIXED_SIZES( size->face ) )
  182. {
  183. TT_Face ttface = (TT_Face)size->face;
  184. SFNT_Service sfnt = (SFNT_Service) ttface->sfnt;
  185. FT_ULong strike_index;
  186. error = sfnt->set_sbit_strike( ttface, req, &strike_index );
  187. if ( error )
  188. ttsize->strike_index = 0xFFFFFFFFUL;
  189. else
  190. return tt_size_select( size, strike_index );
  191. }
  192. #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
  193. FT_Request_Metrics( size->face, req );
  194. if ( FT_IS_SCALABLE( size->face ) )
  195. {
  196. error = tt_size_reset( ttsize );
  197. ttsize->root.metrics = ttsize->metrics;
  198. }
  199. return error;
  200. }
  201. /*************************************************************************/
  202. /* */
  203. /* <Function> */
  204. /* tt_glyph_load */
  205. /* */
  206. /* <Description> */
  207. /* A driver method used to load a glyph within a given glyph slot. */
  208. /* */
  209. /* <Input> */
  210. /* slot :: A handle to the target slot object where the glyph */
  211. /* will be loaded. */
  212. /* */
  213. /* size :: A handle to the source face size at which the glyph */
  214. /* must be scaled, loaded, etc. */
  215. /* */
  216. /* glyph_index :: The index of the glyph in the font file. */
  217. /* */
  218. /* load_flags :: A flag indicating what to load for this glyph. The */
  219. /* FT_LOAD_XXX constants can be used to control the */
  220. /* glyph loading process (e.g., whether the outline */
  221. /* should be scaled, whether to load bitmaps or not, */
  222. /* whether to hint the outline, etc). */
  223. /* */
  224. /* <Return> */
  225. /* FreeType error code. 0 means success. */
  226. /* */
  227. static FT_Error
  228. tt_glyph_load( FT_GlyphSlot ttslot, /* TT_GlyphSlot */
  229. FT_Size ttsize, /* TT_Size */
  230. FT_UInt glyph_index,
  231. FT_Int32 load_flags )
  232. {
  233. TT_GlyphSlot slot = (TT_GlyphSlot)ttslot;
  234. TT_Size size = (TT_Size)ttsize;
  235. FT_Face face = ttslot->face;
  236. FT_Error error;
  237. if ( !slot )
  238. return TT_Err_Invalid_Slot_Handle;
  239. if ( !size )
  240. return TT_Err_Invalid_Size_Handle;
  241. if ( !face )
  242. return TT_Err_Invalid_Argument;
  243. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  244. if ( glyph_index >= (FT_UInt)face->num_glyphs &&
  245. !face->internal->incremental_interface )
  246. #else
  247. if ( glyph_index >= (FT_UInt)face->num_glyphs )
  248. #endif
  249. return TT_Err_Invalid_Argument;
  250. if ( load_flags & FT_LOAD_NO_HINTING )
  251. {
  252. /* both FT_LOAD_NO_HINTING and FT_LOAD_NO_AUTOHINT */
  253. /* are necessary to disable hinting for tricky fonts */
  254. if ( FT_IS_TRICKY( face ) )
  255. load_flags &= ~FT_LOAD_NO_HINTING;
  256. if ( load_flags & FT_LOAD_NO_AUTOHINT )
  257. load_flags |= FT_LOAD_NO_HINTING;
  258. }
  259. if ( load_flags & ( FT_LOAD_NO_RECURSE | FT_LOAD_NO_SCALE ) )
  260. {
  261. load_flags |= FT_LOAD_NO_BITMAP | FT_LOAD_NO_SCALE;
  262. if ( !FT_IS_TRICKY( face ) )
  263. load_flags |= FT_LOAD_NO_HINTING;
  264. }
  265. /* now load the glyph outline if necessary */
  266. error = TT_Load_Glyph( size, slot, glyph_index, load_flags );
  267. /* force drop-out mode to 2 - irrelevant now */
  268. /* slot->outline.dropout_mode = 2; */
  269. return error;
  270. }
  271. /*************************************************************************/
  272. /*************************************************************************/
  273. /*************************************************************************/
  274. /**** ****/
  275. /**** ****/
  276. /**** D R I V E R I N T E R F A C E ****/
  277. /**** ****/
  278. /**** ****/
  279. /*************************************************************************/
  280. /*************************************************************************/
  281. /*************************************************************************/
  282. #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
  283. FT_DEFINE_SERVICE_MULTIMASTERSREC(tt_service_gx_multi_masters,
  284. (FT_Get_MM_Func) NULL,
  285. (FT_Set_MM_Design_Func) NULL,
  286. (FT_Set_MM_Blend_Func) TT_Set_MM_Blend,
  287. (FT_Get_MM_Var_Func) TT_Get_MM_Var,
  288. (FT_Set_Var_Design_Func)TT_Set_Var_Design
  289. )
  290. #endif
  291. static const FT_Service_TrueTypeEngineRec tt_service_truetype_engine =
  292. {
  293. #ifdef TT_USE_BYTECODE_INTERPRETER
  294. #ifdef TT_CONFIG_OPTION_UNPATENTED_HINTING
  295. FT_TRUETYPE_ENGINE_TYPE_UNPATENTED
  296. #else
  297. FT_TRUETYPE_ENGINE_TYPE_PATENTED
  298. #endif
  299. #else /* !TT_USE_BYTECODE_INTERPRETER */
  300. FT_TRUETYPE_ENGINE_TYPE_NONE
  301. #endif /* TT_USE_BYTECODE_INTERPRETER */
  302. };
  303. FT_DEFINE_SERVICE_TTGLYFREC(tt_service_truetype_glyf,
  304. (TT_Glyf_GetLocationFunc)tt_face_get_location
  305. )
  306. #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
  307. FT_DEFINE_SERVICEDESCREC4(tt_services,
  308. FT_SERVICE_ID_XF86_NAME, FT_XF86_FORMAT_TRUETYPE,
  309. FT_SERVICE_ID_MULTI_MASTERS, &FT_TT_SERVICE_GX_MULTI_MASTERS_GET,
  310. FT_SERVICE_ID_TRUETYPE_ENGINE, &tt_service_truetype_engine,
  311. FT_SERVICE_ID_TT_GLYF, &FT_TT_SERVICE_TRUETYPE_GLYF_GET
  312. )
  313. #else
  314. FT_DEFINE_SERVICEDESCREC3(tt_services,
  315. FT_SERVICE_ID_XF86_NAME, FT_XF86_FORMAT_TRUETYPE,
  316. FT_SERVICE_ID_TRUETYPE_ENGINE, &tt_service_truetype_engine,
  317. FT_SERVICE_ID_TT_GLYF, &FT_TT_SERVICE_TRUETYPE_GLYF_GET
  318. )
  319. #endif
  320. FT_CALLBACK_DEF( FT_Module_Interface )
  321. tt_get_interface( FT_Module driver, /* TT_Driver */
  322. const char* tt_interface )
  323. {
  324. FT_Library library;
  325. FT_Module_Interface result;
  326. FT_Module sfntd;
  327. SFNT_Service sfnt;
  328. /* FT_TT_SERVICES_GET derefers `library' in PIC mode */
  329. #ifdef FT_CONFIG_OPTION_PIC
  330. if ( !driver )
  331. return NULL;
  332. library = driver->library;
  333. if ( !library )
  334. return NULL;
  335. #endif
  336. result = ft_service_list_lookup( FT_TT_SERVICES_GET, tt_interface );
  337. if ( result != NULL )
  338. return result;
  339. #ifndef FT_CONFIG_OPTION_PIC
  340. if ( !driver )
  341. return NULL;
  342. library = driver->library;
  343. if ( !library )
  344. return NULL;
  345. #endif
  346. /* only return the default interface from the SFNT module */
  347. sfntd = FT_Get_Module( library, "sfnt" );
  348. if ( sfntd )
  349. {
  350. sfnt = (SFNT_Service)( sfntd->clazz->module_interface );
  351. if ( sfnt )
  352. return sfnt->get_interface( driver, tt_interface );
  353. }
  354. return 0;
  355. }
  356. /* The FT_DriverInterface structure is defined in ftdriver.h. */
  357. #ifdef TT_USE_BYTECODE_INTERPRETER
  358. #define TT_HINTER_FLAG FT_MODULE_DRIVER_HAS_HINTER
  359. #else
  360. #define TT_HINTER_FLAG 0
  361. #endif
  362. #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
  363. #define TT_SIZE_SELECT tt_size_select
  364. #else
  365. #define TT_SIZE_SELECT 0
  366. #endif
  367. FT_DEFINE_DRIVER( tt_driver_class,
  368. FT_MODULE_FONT_DRIVER |
  369. FT_MODULE_DRIVER_SCALABLE |
  370. TT_HINTER_FLAG,
  371. sizeof ( TT_DriverRec ),
  372. "truetype", /* driver name */
  373. 0x10000L, /* driver version == 1.0 */
  374. 0x20000L, /* driver requires FreeType 2.0 or above */
  375. (void*)0, /* driver specific interface */
  376. tt_driver_init,
  377. tt_driver_done,
  378. tt_get_interface,
  379. sizeof ( TT_FaceRec ),
  380. sizeof ( TT_SizeRec ),
  381. sizeof ( FT_GlyphSlotRec ),
  382. tt_face_init,
  383. tt_face_done,
  384. tt_size_init,
  385. tt_size_done,
  386. tt_slot_init,
  387. 0, /* FT_Slot_DoneFunc */
  388. ft_stub_set_char_sizes, /* FT_CONFIG_OPTION_OLD_INTERNALS */
  389. ft_stub_set_pixel_sizes, /* FT_CONFIG_OPTION_OLD_INTERNALS */
  390. tt_glyph_load,
  391. tt_get_kerning,
  392. 0, /* FT_Face_AttachFunc */
  393. tt_get_advances,
  394. tt_size_request,
  395. TT_SIZE_SELECT
  396. )
  397. /* END */