cidparse.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /***************************************************************************/
  2. /* */
  3. /* cidparse.c */
  4. /* */
  5. /* CID-keyed Type1 parser (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_DEBUG_H
  19. #include FT_INTERNAL_CALC_H
  20. #include FT_INTERNAL_OBJECTS_H
  21. #include FT_INTERNAL_STREAM_H
  22. #include "cidparse.h"
  23. #include "ciderrs.h"
  24. /*************************************************************************/
  25. /* */
  26. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  27. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  28. /* messages during execution. */
  29. /* */
  30. #undef FT_COMPONENT
  31. #define FT_COMPONENT trace_cidparse
  32. /*************************************************************************/
  33. /*************************************************************************/
  34. /*************************************************************************/
  35. /***** *****/
  36. /***** INPUT STREAM PARSER *****/
  37. /***** *****/
  38. /*************************************************************************/
  39. /*************************************************************************/
  40. /*************************************************************************/
  41. FT_LOCAL_DEF( FT_Error )
  42. cid_parser_new( CID_Parser* parser,
  43. FT_Stream stream,
  44. FT_Memory memory,
  45. PSAux_Service psaux )
  46. {
  47. FT_Error error;
  48. FT_ULong base_offset, offset, ps_len;
  49. FT_Byte buffer[256 + 10];
  50. FT_Int buff_len;
  51. FT_MEM_ZERO( parser, sizeof ( *parser ) );
  52. psaux->ps_parser_funcs->init( &parser->root, 0, 0, memory );
  53. parser->stream = stream;
  54. base_offset = FT_STREAM_POS();
  55. /* first of all, check the font format in the header */
  56. if ( FT_FRAME_ENTER( 31 ) )
  57. goto Exit;
  58. if ( ft_strncmp( (char *)stream->cursor,
  59. "%!PS-Adobe-3.0 Resource-CIDFont", 31 ) )
  60. {
  61. FT_TRACE2(( "[not a valid CID-keyed font]\n" ));
  62. error = CID_Err_Unknown_File_Format;
  63. }
  64. FT_FRAME_EXIT();
  65. if ( error )
  66. goto Exit;
  67. /* now, read the rest of the file, until we find a `StartData' */
  68. buff_len = 256;
  69. for (;;)
  70. {
  71. FT_Byte *p, *limit = buffer + 256;
  72. FT_ULong top_position;
  73. /* fill input buffer */
  74. buff_len -= 256;
  75. if ( buff_len > 0 )
  76. FT_MEM_MOVE( buffer, limit, buff_len );
  77. p = buffer + buff_len;
  78. if ( FT_STREAM_READ( p, 256 + 10 - buff_len ) )
  79. goto Exit;
  80. top_position = FT_STREAM_POS() - buff_len;
  81. buff_len = 256 + 10;
  82. /* look for `StartData' */
  83. for ( p = buffer; p < limit; p++ )
  84. {
  85. if ( p[0] == 'S' && ft_strncmp( (char*)p, "StartData", 9 ) == 0 )
  86. {
  87. /* save offset of binary data after `StartData' */
  88. offset = (FT_ULong)( top_position - ( limit - p ) + 10 );
  89. goto Found;
  90. }
  91. }
  92. }
  93. Found:
  94. /* we have found the start of the binary data. We will now */
  95. /* rewind and extract the frame of corresponding to the Postscript */
  96. /* section */
  97. ps_len = offset - base_offset;
  98. if ( FT_STREAM_SEEK( base_offset ) ||
  99. FT_FRAME_EXTRACT( ps_len, parser->postscript ) )
  100. goto Exit;
  101. parser->data_offset = offset;
  102. parser->postscript_len = ps_len;
  103. parser->root.base = parser->postscript;
  104. parser->root.cursor = parser->postscript;
  105. parser->root.limit = parser->root.cursor + ps_len;
  106. parser->num_dict = -1;
  107. Exit:
  108. return error;
  109. }
  110. FT_LOCAL_DEF( void )
  111. cid_parser_done( CID_Parser* parser )
  112. {
  113. /* always free the private dictionary */
  114. if ( parser->postscript )
  115. {
  116. FT_Stream stream = parser->stream;
  117. FT_FRAME_RELEASE( parser->postscript );
  118. }
  119. parser->root.funcs.done( &parser->root );
  120. }
  121. /* END */