PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/freetype2/src/otvalid/otvmod.c

https://bitbucket.org/soko/mozilla-central
C | 282 lines | 201 code | 52 blank | 29 comment | 42 complexity | 26da77838e2bf63265438f3c70733181 MD5 | raw file
Possible License(s): GPL-2.0, JSON, 0BSD, LGPL-3.0, AGPL-1.0, MIT, MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.1, Apache-2.0
  1. /***************************************************************************/
  2. /* */
  3. /* otvmod.c */
  4. /* */
  5. /* FreeType's OpenType validation module implementation (body). */
  6. /* */
  7. /* Copyright 2004, 2005, 2006, 2007, 2008 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_TRUETYPE_TABLES_H
  19. #include FT_TRUETYPE_TAGS_H
  20. #include FT_OPENTYPE_VALIDATE_H
  21. #include FT_INTERNAL_OBJECTS_H
  22. #include FT_SERVICE_OPENTYPE_VALIDATE_H
  23. #include "otvmod.h"
  24. #include "otvalid.h"
  25. #include "otvcommn.h"
  26. /*************************************************************************/
  27. /* */
  28. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  29. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  30. /* messages during execution. */
  31. /* */
  32. #undef FT_COMPONENT
  33. #define FT_COMPONENT trace_otvmodule
  34. static FT_Error
  35. otv_load_table( FT_Face face,
  36. FT_Tag tag,
  37. FT_Byte* volatile* table,
  38. FT_ULong* table_len )
  39. {
  40. FT_Error error;
  41. FT_Memory memory = FT_FACE_MEMORY( face );
  42. error = FT_Load_Sfnt_Table( face, tag, 0, NULL, table_len );
  43. if ( error == OTV_Err_Table_Missing )
  44. return OTV_Err_Ok;
  45. if ( error )
  46. goto Exit;
  47. if ( FT_ALLOC( *table, *table_len ) )
  48. goto Exit;
  49. error = FT_Load_Sfnt_Table( face, tag, 0, *table, table_len );
  50. Exit:
  51. return error;
  52. }
  53. static FT_Error
  54. otv_validate( FT_Face volatile face,
  55. FT_UInt ot_flags,
  56. FT_Bytes *ot_base,
  57. FT_Bytes *ot_gdef,
  58. FT_Bytes *ot_gpos,
  59. FT_Bytes *ot_gsub,
  60. FT_Bytes *ot_jstf )
  61. {
  62. FT_Error error = OTV_Err_Ok;
  63. FT_Byte* volatile base;
  64. FT_Byte* volatile gdef;
  65. FT_Byte* volatile gpos;
  66. FT_Byte* volatile gsub;
  67. FT_Byte* volatile jstf;
  68. FT_Byte* volatile math;
  69. FT_ULong len_base, len_gdef, len_gpos, len_gsub, len_jstf;
  70. FT_ULong len_math;
  71. FT_UInt num_glyphs = (FT_UInt)face->num_glyphs;
  72. FT_ValidatorRec volatile valid;
  73. base = gdef = gpos = gsub = jstf = math = NULL;
  74. len_base = len_gdef = len_gpos = len_gsub = len_jstf = len_math = 0;
  75. /*
  76. * XXX: OpenType tables cannot handle 32-bit glyph index,
  77. * although broken TrueType can have 32-bit glyph index.
  78. */
  79. if ( face->num_glyphs > 0xFFFFL )
  80. {
  81. FT_TRACE1(( "otv_validate: Invalid glyphs index (0x0000FFFF - 0x%08x) ",
  82. face->num_glyphs ));
  83. FT_TRACE1(( "are not handled by OpenType tables\n" ));
  84. num_glyphs = 0xFFFF;
  85. }
  86. /* load tables */
  87. if ( ot_flags & FT_VALIDATE_BASE )
  88. {
  89. error = otv_load_table( face, TTAG_BASE, &base, &len_base );
  90. if ( error )
  91. goto Exit;
  92. }
  93. if ( ot_flags & FT_VALIDATE_GDEF )
  94. {
  95. error = otv_load_table( face, TTAG_GDEF, &gdef, &len_gdef );
  96. if ( error )
  97. goto Exit;
  98. }
  99. if ( ot_flags & FT_VALIDATE_GPOS )
  100. {
  101. error = otv_load_table( face, TTAG_GPOS, &gpos, &len_gpos );
  102. if ( error )
  103. goto Exit;
  104. }
  105. if ( ot_flags & FT_VALIDATE_GSUB )
  106. {
  107. error = otv_load_table( face, TTAG_GSUB, &gsub, &len_gsub );
  108. if ( error )
  109. goto Exit;
  110. }
  111. if ( ot_flags & FT_VALIDATE_JSTF )
  112. {
  113. error = otv_load_table( face, TTAG_JSTF, &jstf, &len_jstf );
  114. if ( error )
  115. goto Exit;
  116. }
  117. if ( ot_flags & FT_VALIDATE_MATH )
  118. {
  119. error = otv_load_table( face, TTAG_MATH, &math, &len_math );
  120. if ( error )
  121. goto Exit;
  122. }
  123. /* validate tables */
  124. if ( base )
  125. {
  126. ft_validator_init( &valid, base, base + len_base, FT_VALIDATE_DEFAULT );
  127. if ( ft_setjmp( valid.jump_buffer ) == 0 )
  128. otv_BASE_validate( base, &valid );
  129. error = valid.error;
  130. if ( error )
  131. goto Exit;
  132. }
  133. if ( gpos )
  134. {
  135. ft_validator_init( &valid, gpos, gpos + len_gpos, FT_VALIDATE_DEFAULT );
  136. if ( ft_setjmp( valid.jump_buffer ) == 0 )
  137. otv_GPOS_validate( gpos, num_glyphs, &valid );
  138. error = valid.error;
  139. if ( error )
  140. goto Exit;
  141. }
  142. if ( gsub )
  143. {
  144. ft_validator_init( &valid, gsub, gsub + len_gsub, FT_VALIDATE_DEFAULT );
  145. if ( ft_setjmp( valid.jump_buffer ) == 0 )
  146. otv_GSUB_validate( gsub, num_glyphs, &valid );
  147. error = valid.error;
  148. if ( error )
  149. goto Exit;
  150. }
  151. if ( gdef )
  152. {
  153. ft_validator_init( &valid, gdef, gdef + len_gdef, FT_VALIDATE_DEFAULT );
  154. if ( ft_setjmp( valid.jump_buffer ) == 0 )
  155. otv_GDEF_validate( gdef, gsub, gpos, num_glyphs, &valid );
  156. error = valid.error;
  157. if ( error )
  158. goto Exit;
  159. }
  160. if ( jstf )
  161. {
  162. ft_validator_init( &valid, jstf, jstf + len_jstf, FT_VALIDATE_DEFAULT );
  163. if ( ft_setjmp( valid.jump_buffer ) == 0 )
  164. otv_JSTF_validate( jstf, gsub, gpos, num_glyphs, &valid );
  165. error = valid.error;
  166. if ( error )
  167. goto Exit;
  168. }
  169. if ( math )
  170. {
  171. ft_validator_init( &valid, math, math + len_math, FT_VALIDATE_DEFAULT );
  172. if ( ft_setjmp( valid.jump_buffer ) == 0 )
  173. otv_MATH_validate( math, num_glyphs, &valid );
  174. error = valid.error;
  175. if ( error )
  176. goto Exit;
  177. }
  178. *ot_base = (FT_Bytes)base;
  179. *ot_gdef = (FT_Bytes)gdef;
  180. *ot_gpos = (FT_Bytes)gpos;
  181. *ot_gsub = (FT_Bytes)gsub;
  182. *ot_jstf = (FT_Bytes)jstf;
  183. Exit:
  184. if ( error )
  185. {
  186. FT_Memory memory = FT_FACE_MEMORY( face );
  187. FT_FREE( base );
  188. FT_FREE( gdef );
  189. FT_FREE( gpos );
  190. FT_FREE( gsub );
  191. FT_FREE( jstf );
  192. }
  193. {
  194. FT_Memory memory = FT_FACE_MEMORY( face );
  195. FT_FREE( math ); /* Can't return this as API is frozen */
  196. }
  197. return error;
  198. }
  199. static
  200. const FT_Service_OTvalidateRec otvalid_interface =
  201. {
  202. otv_validate
  203. };
  204. static
  205. const FT_ServiceDescRec otvalid_services[] =
  206. {
  207. { FT_SERVICE_ID_OPENTYPE_VALIDATE, &otvalid_interface },
  208. { NULL, NULL }
  209. };
  210. static FT_Pointer
  211. otvalid_get_service( FT_Module module,
  212. const char* service_id )
  213. {
  214. FT_UNUSED( module );
  215. return ft_service_list_lookup( otvalid_services, service_id );
  216. }
  217. FT_CALLBACK_TABLE_DEF
  218. const FT_Module_Class otv_module_class =
  219. {
  220. 0,
  221. sizeof ( FT_ModuleRec ),
  222. "otvalid",
  223. 0x10000L,
  224. 0x20000L,
  225. 0, /* module-specific interface */
  226. (FT_Module_Constructor)0,
  227. (FT_Module_Destructor) 0,
  228. (FT_Module_Requester) otvalid_get_service
  229. };
  230. /* END */