Behdad Esfahbod | 9f8da38 | 2006-03-31 12:28:09 +0000 | [diff] [blame] | 1 | /* harfbuzz-buffer.c: Buffer of glyphs for substitution/positioning |
| 2 | * |
| 3 | * Copyright 2004 Red Hat Software |
| 4 | * |
| 5 | * Portions Copyright 1996-2000 by |
| 6 | * David Turner, Robert Wilhelm, and Werner Lemberg. |
| 7 | */ |
| 8 | |
| 9 | #include "harfbuzz-impl.h" |
| 10 | #include "harfbuzz-buffer.h" |
| 11 | #include "harfbuzz-gsub-private.h" |
| 12 | #include "harfbuzz-gpos-private.h" |
| 13 | |
| 14 | static FT_Error |
| 15 | hb_buffer_ensure( HB_Buffer buffer, |
| 16 | FT_ULong size ) |
| 17 | { |
| 18 | FT_Memory memory = buffer->memory; |
| 19 | FT_ULong new_allocated = buffer->allocated; |
| 20 | |
| 21 | if (size > new_allocated) |
| 22 | { |
| 23 | FT_Error error; |
| 24 | |
| 25 | while (size > new_allocated) |
| 26 | new_allocated += (new_allocated >> 1) + 8; |
| 27 | |
| 28 | if ( REALLOC_ARRAY( buffer->in_string, buffer->allocated, new_allocated, HB_GlyphItemRec ) ) |
| 29 | return error; |
| 30 | if ( REALLOC_ARRAY( buffer->out_string, buffer->allocated, new_allocated, HB_GlyphItemRec ) ) |
| 31 | return error; |
| 32 | if ( REALLOC_ARRAY( buffer->positions, buffer->allocated, new_allocated, HB_PositionRec ) ) |
| 33 | return error; |
| 34 | |
| 35 | buffer->allocated = new_allocated; |
| 36 | } |
| 37 | |
| 38 | return FT_Err_Ok; |
| 39 | } |
| 40 | |
| 41 | FT_Error |
| 42 | hb_buffer_new( FT_Memory memory, |
| 43 | HB_Buffer *buffer ) |
| 44 | { |
| 45 | FT_Error error; |
| 46 | |
| 47 | if ( ALLOC( *buffer, sizeof( HB_BufferRec ) ) ) |
| 48 | return error; |
| 49 | |
| 50 | (*buffer)->memory = memory; |
| 51 | (*buffer)->in_length = 0; |
| 52 | (*buffer)->out_length = 0; |
| 53 | (*buffer)->allocated = 0; |
| 54 | (*buffer)->in_pos = 0; |
| 55 | (*buffer)->out_pos = 0; |
| 56 | |
| 57 | (*buffer)->in_string = NULL; |
| 58 | (*buffer)->out_string = NULL; |
| 59 | (*buffer)->positions = NULL; |
| 60 | (*buffer)->max_ligID = 0; |
| 61 | |
| 62 | return FT_Err_Ok; |
| 63 | } |
| 64 | |
| 65 | FT_Error |
| 66 | hb_buffer_swap( HB_Buffer buffer ) |
| 67 | { |
| 68 | HB_GlyphItem tmp_string; |
| 69 | |
| 70 | tmp_string = buffer->in_string; |
| 71 | buffer->in_string = buffer->out_string; |
| 72 | buffer->out_string = tmp_string; |
| 73 | |
| 74 | buffer->in_length = buffer->out_length; |
| 75 | buffer->out_length = 0; |
| 76 | |
| 77 | buffer->in_pos = 0; |
| 78 | buffer->out_pos = 0; |
| 79 | |
| 80 | return FT_Err_Ok; |
| 81 | } |
| 82 | |
| 83 | FT_Error |
| 84 | hb_buffer_free( HB_Buffer buffer ) |
| 85 | { |
| 86 | FT_Memory memory = buffer->memory; |
| 87 | |
| 88 | FREE( buffer->in_string ); |
| 89 | FREE( buffer->out_string ); |
| 90 | FREE( buffer->positions ); |
| 91 | FREE( buffer ); |
| 92 | |
| 93 | return FT_Err_Ok; |
| 94 | } |
| 95 | |
| 96 | FT_Error |
| 97 | hb_buffer_clear( HB_Buffer buffer ) |
| 98 | { |
| 99 | buffer->in_length = 0; |
| 100 | buffer->out_length = 0; |
| 101 | buffer->in_pos = 0; |
| 102 | buffer->out_pos = 0; |
| 103 | |
| 104 | return FT_Err_Ok; |
| 105 | } |
| 106 | |
| 107 | FT_Error |
| 108 | hb_buffer_add_glyph( HB_Buffer buffer, |
| 109 | FT_UInt glyph_index, |
| 110 | FT_UInt properties, |
| 111 | FT_UInt cluster ) |
| 112 | { |
| 113 | FT_Error error; |
| 114 | HB_GlyphItem glyph; |
| 115 | |
| 116 | error = hb_buffer_ensure( buffer, buffer->in_length + 1 ); |
| 117 | if ( error ) |
| 118 | return error; |
| 119 | |
| 120 | glyph = &buffer->in_string[buffer->in_length]; |
| 121 | glyph->gindex = glyph_index; |
| 122 | glyph->properties = properties; |
| 123 | glyph->cluster = cluster; |
| 124 | glyph->component = 0; |
| 125 | glyph->ligID = 0; |
| 126 | glyph->gproperties = HB_GLYPH_PROPERTIES_UNKNOWN; |
| 127 | |
| 128 | buffer->in_length++; |
| 129 | |
| 130 | return FT_Err_Ok; |
| 131 | } |
| 132 | |
| 133 | /* The following function copies `num_out' elements from `glyph_data' |
| 134 | to `buffer->out_string', advancing the in array pointer in the structure |
| 135 | by `num_in' elements, and the out array pointer by `num_out' elements. |
| 136 | Finally, it sets the `length' field of `out' equal to |
| 137 | `pos' of the `out' structure. |
| 138 | |
| 139 | If `component' is 0xFFFF, the component value from buffer->in_pos |
| 140 | will copied `num_out' times, otherwise `component' itself will |
| 141 | be used to fill the `component' fields. |
| 142 | |
| 143 | If `ligID' is 0xFFFF, the ligID value from buffer->in_pos |
| 144 | will copied `num_out' times, otherwise `ligID' itself will |
| 145 | be used to fill the `ligID' fields. |
| 146 | |
| 147 | The properties for all replacement glyphs are taken |
| 148 | from the glyph at position `buffer->in_pos'. |
| 149 | |
| 150 | The cluster value for the glyph at position buffer->in_pos is used |
| 151 | for all replacement glyphs */ |
| 152 | FT_Error |
| 153 | hb_buffer_add_output_glyphs( HB_Buffer buffer, |
| 154 | FT_UShort num_in, |
| 155 | FT_UShort num_out, |
| 156 | FT_UShort *glyph_data, |
| 157 | FT_UShort component, |
| 158 | FT_UShort ligID ) |
| 159 | { |
| 160 | FT_Error error; |
| 161 | FT_UShort i; |
| 162 | FT_UInt properties; |
| 163 | FT_UInt cluster; |
| 164 | |
| 165 | error = hb_buffer_ensure( buffer, buffer->out_pos + num_out ); |
| 166 | if ( error ) |
| 167 | return error; |
| 168 | |
| 169 | properties = buffer->in_string[buffer->in_pos].properties; |
| 170 | cluster = buffer->in_string[buffer->in_pos].cluster; |
| 171 | if ( component == 0xFFFF ) |
| 172 | component = buffer->in_string[buffer->in_pos].component; |
| 173 | if ( ligID == 0xFFFF ) |
| 174 | ligID = buffer->in_string[buffer->in_pos].ligID; |
| 175 | |
| 176 | for ( i = 0; i < num_out; i++ ) |
| 177 | { |
| 178 | HB_GlyphItem item = &buffer->out_string[buffer->out_pos + i]; |
| 179 | |
| 180 | item->gindex = glyph_data[i]; |
| 181 | item->properties = properties; |
| 182 | item->cluster = cluster; |
| 183 | item->component = component; |
| 184 | item->ligID = ligID; |
| 185 | item->gproperties = HB_GLYPH_PROPERTIES_UNKNOWN; |
| 186 | } |
| 187 | |
| 188 | buffer->in_pos += num_in; |
| 189 | buffer->out_pos += num_out; |
| 190 | |
| 191 | buffer->out_length = buffer->out_pos; |
| 192 | |
| 193 | return FT_Err_Ok; |
| 194 | } |
| 195 | |
| 196 | FT_Error |
| 197 | hb_buffer_add_output_glyph( HB_Buffer buffer, |
| 198 | FT_UInt glyph_index, |
| 199 | FT_UShort component, |
| 200 | FT_UShort ligID ) |
| 201 | { |
| 202 | FT_UShort glyph_data = glyph_index; |
| 203 | |
| 204 | return hb_buffer_add_output_glyphs ( buffer, 1, 1, |
| 205 | &glyph_data, component, ligID ); |
| 206 | } |
| 207 | |
| 208 | FT_Error |
| 209 | hb_buffer_copy_output_glyph ( HB_Buffer buffer ) |
| 210 | { |
| 211 | FT_Error error; |
| 212 | |
| 213 | error = hb_buffer_ensure( buffer, buffer->out_pos + 1 ); |
| 214 | if ( error ) |
| 215 | return error; |
| 216 | |
| 217 | buffer->out_string[buffer->out_pos++] = buffer->in_string[buffer->in_pos++]; |
| 218 | buffer->out_length = buffer->out_pos; |
| 219 | |
| 220 | return FT_Err_Ok; |
| 221 | } |
| 222 | |
| 223 | FT_UShort |
| 224 | hb_buffer_allocate_ligid( HB_Buffer buffer ) |
| 225 | { |
Behdad Esfahbod | dd810b7 | 2007-08-29 08:46:50 +0000 | [diff] [blame^] | 226 | return ++buffer->max_ligID; |
Behdad Esfahbod | 9f8da38 | 2006-03-31 12:28:09 +0000 | [diff] [blame] | 227 | } |