otlparse.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "otlparse.h"
  2. #include "otlutils.h"
  3. static void
  4. otl_string_ensure( OTL_String string,
  5. OTL_UInt count,
  6. OTL_Parser parser )
  7. {
  8. count += string->length;
  9. if ( count > string->capacity )
  10. {
  11. OTL_UInt old_count = string->capacity;
  12. OTL_UInt new_count = old_count;
  13. OTL_Memory memory = parser->memory;
  14. while ( new_count < count )
  15. new_count += (new_count >> 1) + 16;
  16. if ( OTL_MEM_RENEW_ARRAY( string->glyphs, old_count, new_count ) )
  17. otl_parser_error( parser, OTL_Parse_Err_Memory );
  18. string->capacity = new_count;
  19. }
  20. }
  21. #define OTL_STRING_ENSURE(str,count,parser) \
  22. OTL_BEGIN_STMNT \
  23. if ( (str)->length + (count) > (str)>capacity ) \
  24. otl_string_ensure( str, count, parser ); \
  25. OTL_END_STMNT
  26. OTL_LOCALDEF( OTL_UInt )
  27. otl_parser_get_gindex( OTL_Parser parser )
  28. {
  29. OTL_String in = parser->str_in;
  30. if ( in->cursor >= in->num_glyphs )
  31. otl_parser_error( parser, OTL_Err_Parser_Internal );
  32. return in->str[ in->cursor ].gindex;
  33. }
  34. OTL_LOCALDEF( void )
  35. otl_parser_error( OTL_Parser parser,
  36. OTL_ParseError error; )
  37. {
  38. parser->error = error;
  39. otl_longjmp( parser->jump_buffer, 1 );
  40. }
  41. #define OTL_PARSER_UNCOVERED(x) otl_parser_error( x, OTL_Parse_Err_UncoveredGlyph );
  42. OTL_LOCAL( void )
  43. otl_parser_check_property( OTL_Parser parser,
  44. OTL_UInt gindex,
  45. OTL_UInt flags,
  46. OTL_UInt *aproperty );
  47. OTL_LOCALDEF( void )
  48. otl_parser_replace_1( OTL_Parser parser,
  49. OTL_UInt gindex )
  50. {
  51. OTL_String in = parser->str_in;
  52. OTL_String out = parser->str_out;
  53. OTL_StringGlyph glyph, in_glyph;
  54. /* sanity check */
  55. if ( in == NULL ||
  56. out == NULL ||
  57. in->length == 0 ||
  58. in->cursor >= in->length )
  59. {
  60. /* report as internal error, since these should */
  61. /* never happen !! */
  62. otl_parser_error( parser, OTL_Err_Parse_Internal );
  63. }
  64. OTL_STRING_ENSURE( out, 1, parser );
  65. glyph = out->glyphs + out->length;
  66. in_glyph = in->glyphs + in->cursor;
  67. glyph->gindex = gindex;
  68. glyph->property = in_glyph->property;
  69. glyph->lig_component = in_glyph->lig_component;
  70. glyph->lig_id = in_glyph->lig_id;
  71. out->length += 1;
  72. out->cursor = out->length;
  73. in->cursor += 1;
  74. }
  75. OTL_LOCALDEF( void )
  76. otl_parser_replace_n( OTL_Parser parser,
  77. OTL_UInt count,
  78. OTL_Bytes indices )
  79. {
  80. OTL_UInt lig_component, lig_id, property;
  81. OTL_String in = parser->str_in;
  82. OTL_String out = parser->str_out;
  83. OTL_StringGlyph glyph, in_glyph;
  84. OTL_Bytes p = indices;
  85. /* sanity check */
  86. if ( in == NULL ||
  87. out == NULL ||
  88. in->length == 0 ||
  89. in->cursor >= in->length )
  90. {
  91. /* report as internal error, since these should */
  92. /* never happen !! */
  93. otl_parser_error( parser, OTL_Err_Parse_Internal );
  94. }
  95. OTL_STRING_ENSURE( out, count, parser );
  96. glyph = out->glyphs + out->length;
  97. in_glyph = in->glyphs + in->cursor;
  98. glyph->gindex = gindex;
  99. lig_component = in_glyph->lig_component;
  100. lig_id = in_glyph->lid_id;
  101. property = in_glyph->property;
  102. for ( ; count > 0; count-- )
  103. {
  104. glyph->gindex = OTL_NEXT_USHORT(p);
  105. glyph->property = property;
  106. glyph->lig_component = lig_component;
  107. glyph->lig_id = lig_id;
  108. out->length ++;
  109. }
  110. out->cursor = out->length;
  111. in->cursor += 1;
  112. }