/src/freetype/src/cid/cidparse.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 225 lines · 134 code · 45 blank · 46 comment · 34 complexity · 06f31cb48d6297aa8d03dba03fd7fcf2 MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* cidparse.c */
  4. /* */
  5. /* CID-keyed Type1 parser (body). */
  6. /* */
  7. /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 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_OBJECTS_H
  20. #include FT_INTERNAL_STREAM_H
  21. #include "cidparse.h"
  22. #include "ciderrs.h"
  23. /*************************************************************************/
  24. /* */
  25. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  26. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  27. /* messages during execution. */
  28. /* */
  29. #undef FT_COMPONENT
  30. #define FT_COMPONENT trace_cidparse
  31. /*************************************************************************/
  32. /*************************************************************************/
  33. /*************************************************************************/
  34. /***** *****/
  35. /***** INPUT STREAM PARSER *****/
  36. /***** *****/
  37. /*************************************************************************/
  38. /*************************************************************************/
  39. /*************************************************************************/
  40. FT_LOCAL_DEF( FT_Error )
  41. cid_parser_new( CID_Parser* parser,
  42. FT_Stream stream,
  43. FT_Memory memory,
  44. PSAux_Service psaux )
  45. {
  46. FT_Error error;
  47. FT_ULong base_offset, offset, ps_len;
  48. FT_Byte *cur, *limit;
  49. FT_Byte *arg1, *arg2;
  50. FT_MEM_ZERO( parser, sizeof ( *parser ) );
  51. psaux->ps_parser_funcs->init( &parser->root, 0, 0, memory );
  52. parser->stream = stream;
  53. base_offset = FT_STREAM_POS();
  54. /* first of all, check the font format in the header */
  55. if ( FT_FRAME_ENTER( 31 ) )
  56. goto Exit;
  57. if ( ft_strncmp( (char *)stream->cursor,
  58. "%!PS-Adobe-3.0 Resource-CIDFont", 31 ) )
  59. {
  60. FT_TRACE2(( " not a CID-keyed font\n" ));
  61. error = CID_Err_Unknown_File_Format;
  62. }
  63. FT_FRAME_EXIT();
  64. if ( error )
  65. goto Exit;
  66. Again:
  67. /* now, read the rest of the file until we find */
  68. /* `StartData' or `/sfnts' */
  69. {
  70. FT_Byte buffer[256 + 10];
  71. FT_Long read_len = 256 + 10; /* same as signed FT_Stream->size */
  72. FT_Byte* p = buffer;
  73. for ( offset = FT_STREAM_POS(); ; offset += 256 )
  74. {
  75. FT_Long stream_len; /* same as signed FT_Stream->size */
  76. stream_len = stream->size - FT_STREAM_POS();
  77. if ( stream_len == 0 )
  78. {
  79. FT_TRACE2(( "cid_parser_new: no `StartData' keyword found\n" ));
  80. error = CID_Err_Invalid_File_Format;
  81. goto Exit;
  82. }
  83. read_len = FT_MIN( read_len, stream_len );
  84. if ( FT_STREAM_READ( p, read_len ) )
  85. goto Exit;
  86. if ( read_len < 256 )
  87. p[read_len] = '\0';
  88. limit = p + read_len - 10;
  89. for ( p = buffer; p < limit; p++ )
  90. {
  91. if ( p[0] == 'S' && ft_strncmp( (char*)p, "StartData", 9 ) == 0 )
  92. {
  93. /* save offset of binary data after `StartData' */
  94. offset += p - buffer + 10;
  95. goto Found;
  96. }
  97. else if ( p[1] == 's' && ft_strncmp( (char*)p, "/sfnts", 6 ) == 0 )
  98. {
  99. offset += p - buffer + 7;
  100. goto Found;
  101. }
  102. }
  103. FT_MEM_MOVE( buffer, p, 10 );
  104. read_len = 256;
  105. p = buffer + 10;
  106. }
  107. }
  108. Found:
  109. /* We have found the start of the binary data or the `/sfnts' token. */
  110. /* Now rewind and extract the frame corresponding to this PostScript */
  111. /* section. */
  112. ps_len = offset - base_offset;
  113. if ( FT_STREAM_SEEK( base_offset ) ||
  114. FT_FRAME_EXTRACT( ps_len, parser->postscript ) )
  115. goto Exit;
  116. parser->data_offset = offset;
  117. parser->postscript_len = ps_len;
  118. parser->root.base = parser->postscript;
  119. parser->root.cursor = parser->postscript;
  120. parser->root.limit = parser->root.cursor + ps_len;
  121. parser->num_dict = -1;
  122. /* Finally, we check whether `StartData' or `/sfnts' was real -- */
  123. /* it could be in a comment or string. We also get the arguments */
  124. /* of `StartData' to find out whether the data is represented in */
  125. /* binary or hex format. */
  126. arg1 = parser->root.cursor;
  127. cid_parser_skip_PS_token( parser );
  128. cid_parser_skip_spaces ( parser );
  129. arg2 = parser->root.cursor;
  130. cid_parser_skip_PS_token( parser );
  131. cid_parser_skip_spaces ( parser );
  132. limit = parser->root.limit;
  133. cur = parser->root.cursor;
  134. while ( cur < limit )
  135. {
  136. if ( parser->root.error )
  137. {
  138. error = parser->root.error;
  139. goto Exit;
  140. }
  141. if ( cur[0] == 'S' && ft_strncmp( (char*)cur, "StartData", 9 ) == 0 )
  142. {
  143. if ( ft_strncmp( (char*)arg1, "(Hex)", 5 ) == 0 )
  144. parser->binary_length = ft_atol( (const char *)arg2 );
  145. limit = parser->root.limit;
  146. cur = parser->root.cursor;
  147. goto Exit;
  148. }
  149. else if ( cur[1] == 's' && ft_strncmp( (char*)cur, "/sfnts", 6 ) == 0 )
  150. {
  151. FT_TRACE2(( "cid_parser_new: cannot handle Type 11 fonts\n" ));
  152. error = CID_Err_Unknown_File_Format;
  153. goto Exit;
  154. }
  155. cid_parser_skip_PS_token( parser );
  156. cid_parser_skip_spaces ( parser );
  157. arg1 = arg2;
  158. arg2 = cur;
  159. cur = parser->root.cursor;
  160. }
  161. /* we haven't found the correct `StartData'; go back and continue */
  162. /* searching */
  163. FT_FRAME_RELEASE( parser->postscript );
  164. if ( !FT_STREAM_SEEK( offset ) )
  165. goto Again;
  166. Exit:
  167. return error;
  168. }
  169. FT_LOCAL_DEF( void )
  170. cid_parser_done( CID_Parser* parser )
  171. {
  172. /* always free the private dictionary */
  173. if ( parser->postscript )
  174. {
  175. FT_Stream stream = parser->stream;
  176. FT_FRAME_RELEASE( parser->postscript );
  177. }
  178. parser->root.funcs.done( &parser->root );
  179. }
  180. /* END */