hb-buffer-private.hh revision 82ecaff736e245e117d70b6ec1497508c6eb08d2
1/*
2 * Copyright © 1998-2004  David Turner and Werner Lemberg
3 * Copyright © 2004,2007,2009,2010  Red Hat, Inc.
4 * Copyright © 2011,2012  Google, Inc.
5 *
6 *  This is part of HarfBuzz, a text shaping library.
7 *
8 * Permission is hereby granted, without written agreement and without
9 * license or royalty fees, to use, copy, modify, and distribute this
10 * software and its documentation for any purpose, provided that the
11 * above copyright notice and the following two paragraphs appear in
12 * all copies of this software.
13 *
14 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
15 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
17 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * DAMAGE.
19 *
20 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
21 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
23 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
24 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 *
26 * Red Hat Author(s): Owen Taylor, Behdad Esfahbod
27 * Google Author(s): Behdad Esfahbod
28 */
29
30#ifndef HB_BUFFER_PRIVATE_HH
31#define HB_BUFFER_PRIVATE_HH
32
33#include "hb-private.hh"
34#include "hb-buffer.h"
35#include "hb-object-private.hh"
36#include "hb-unicode-private.hh"
37
38
39
40ASSERT_STATIC (sizeof (hb_glyph_info_t) == 20);
41ASSERT_STATIC (sizeof (hb_glyph_info_t) == sizeof (hb_glyph_position_t));
42
43
44/*
45 * hb_segment_properties_t
46 */
47
48typedef struct hb_segment_properties_t {
49    hb_direction_t      direction;
50    hb_script_t         script;
51    hb_language_t       language;
52    ASSERT_POD ();
53} hb_segment_properties_t;
54
55#define _HB_BUFFER_PROPS_DEFAULT { HB_DIRECTION_INVALID, HB_SCRIPT_INVALID, HB_LANGUAGE_INVALID }
56
57static inline hb_bool_t
58hb_segment_properties_equal (const hb_segment_properties_t *a,
59			     const hb_segment_properties_t *b)
60{
61  return a->direction == b->direction &&
62	 a->script    == b->script    &&
63	 a->language  == b->language;
64}
65
66
67#if 0
68static inline unsigned int
69hb_segment_properties_hash (const hb_segment_properties_t *p)
70{
71  /* TODO improve */
72  return (unsigned int) p->direction +
73	 (unsigned int) p->script +
74	 (intptr_t) (p->language);
75}
76#endif
77
78
79
80/*
81 * hb_buffer_t
82 */
83
84struct hb_buffer_t {
85  hb_object_header_t header;
86  ASSERT_POD ();
87
88  /* Information about how the text in the buffer should be treated */
89
90  hb_unicode_funcs_t *unicode; /* Unicode functions */
91  hb_segment_properties_t props; /* Script, language, direction */
92
93  /* Buffer contents */
94
95  hb_buffer_content_type_t content_type;
96
97  bool in_error; /* Allocation failed */
98  bool have_output; /* Whether we have an output buffer going on */
99  bool have_positions; /* Whether we have positions */
100
101  unsigned int idx; /* Cursor into ->info and ->pos arrays */
102  unsigned int len; /* Length of ->info and ->pos arrays */
103  unsigned int out_len; /* Length of ->out array if have_output */
104
105  unsigned int allocated; /* Length of allocated arrays */
106  hb_glyph_info_t     *info;
107  hb_glyph_info_t     *out_info;
108  hb_glyph_position_t *pos;
109
110  inline hb_glyph_info_t &cur (unsigned int i = 0) { return info[idx + i]; }
111  inline hb_glyph_info_t cur (unsigned int i = 0) const { return info[idx + i]; }
112
113  inline hb_glyph_position_t &cur_pos (unsigned int i = 0) { return pos[idx + i]; }
114  inline hb_glyph_position_t cur_pos (unsigned int i = 0) const { return pos[idx + i]; }
115
116  inline hb_glyph_info_t &prev (void) { return out_info[out_len - 1]; }
117  inline hb_glyph_info_t prev (void) const { return info[out_len - 1]; }
118
119  unsigned int serial;
120
121  /* These reflect current allocations of the bytes in glyph_info_t's var1 and var2. */
122  uint8_t allocated_var_bytes[8];
123  const char *allocated_var_owner[8];
124
125  /* Text before / after the main buffer contents.
126   * Always in Unicode, and ordered outward.
127   * Index 0 is for "pre-context", 1 for "post-context". */
128  static const unsigned int CONTEXT_LENGTH = 5;
129  hb_codepoint_t context[2][CONTEXT_LENGTH];
130  unsigned int context_len[2];
131
132
133  /* Methods */
134
135  HB_INTERNAL void reset (void);
136  HB_INTERNAL void clear (void);
137
138  inline unsigned int backtrack_len (void) const
139  { return have_output? out_len : idx; }
140  inline unsigned int next_serial (void) { return serial++; }
141
142  HB_INTERNAL void allocate_var (unsigned int byte_i, unsigned int count, const char *owner);
143  HB_INTERNAL void deallocate_var (unsigned int byte_i, unsigned int count, const char *owner);
144  HB_INTERNAL void assert_var (unsigned int byte_i, unsigned int count, const char *owner);
145  HB_INTERNAL void deallocate_var_all (void);
146
147  HB_INTERNAL void add (hb_codepoint_t  codepoint,
148			hb_mask_t       mask,
149			unsigned int    cluster);
150
151  HB_INTERNAL void reverse_range (unsigned int start, unsigned int end);
152  HB_INTERNAL void reverse (void);
153  HB_INTERNAL void reverse_clusters (void);
154  HB_INTERNAL void guess_properties (void);
155
156  HB_INTERNAL void swap_buffers (void);
157  HB_INTERNAL void remove_output (void);
158  HB_INTERNAL void clear_output (void);
159  HB_INTERNAL void clear_positions (void);
160
161  HB_INTERNAL void replace_glyphs (unsigned int num_in,
162				   unsigned int num_out,
163				   const hb_codepoint_t *glyph_data);
164
165  HB_INTERNAL void replace_glyph (hb_codepoint_t glyph_index);
166  /* Makes a copy of the glyph at idx to output and replace glyph_index */
167  HB_INTERNAL void output_glyph (hb_codepoint_t glyph_index);
168  HB_INTERNAL void output_info (hb_glyph_info_t &glyph_info);
169  /* Copies glyph at idx to output but doesn't advance idx */
170  HB_INTERNAL void copy_glyph (void);
171  /* Copies glyph at idx to output and advance idx.
172   * If there's no output, just advance idx. */
173  inline void
174  next_glyph (void)
175  {
176    if (have_output)
177    {
178      if (unlikely (out_info != info || out_len != idx)) {
179	if (unlikely (!make_room_for (1, 1))) return;
180	out_info[out_len] = info[idx];
181      }
182      out_len++;
183    }
184
185    idx++;
186  }
187
188  /* Advance idx without copying to output. */
189  inline void skip_glyph (void) { idx++; }
190
191  inline void reset_masks (hb_mask_t mask)
192  {
193    for (unsigned int j = 0; j < len; j++)
194      info[j].mask = mask;
195  }
196  inline void add_masks (hb_mask_t mask)
197  {
198    for (unsigned int j = 0; j < len; j++)
199      info[j].mask |= mask;
200  }
201  HB_INTERNAL void set_masks (hb_mask_t value,
202			      hb_mask_t mask,
203			      unsigned int cluster_start,
204			      unsigned int cluster_end);
205
206  HB_INTERNAL void merge_clusters (unsigned int start,
207				   unsigned int end);
208  HB_INTERNAL void merge_out_clusters (unsigned int start,
209				       unsigned int end);
210
211  /* Internal methods */
212  HB_INTERNAL bool enlarge (unsigned int size);
213
214  inline bool ensure (unsigned int size)
215  { return likely (size < allocated) ? true : enlarge (size); }
216
217  HB_INTERNAL bool make_room_for (unsigned int num_in, unsigned int num_out);
218
219  HB_INTERNAL void *get_scratch_buffer (unsigned int *size);
220
221  inline void clear_context (unsigned int side) { context_len[side] = 0; }
222};
223
224
225#define HB_BUFFER_XALLOCATE_VAR(b, func, var, owner) \
226  b->func (offsetof (hb_glyph_info_t, var) - offsetof(hb_glyph_info_t, var1), \
227	   sizeof (b->info[0].var), owner)
228#define HB_BUFFER_ALLOCATE_VAR(b, var) \
229	HB_BUFFER_XALLOCATE_VAR (b, allocate_var, var (), #var)
230#define HB_BUFFER_DEALLOCATE_VAR(b, var) \
231	HB_BUFFER_XALLOCATE_VAR (b, deallocate_var, var (), #var)
232#define HB_BUFFER_ASSERT_VAR(b, var) \
233	HB_BUFFER_XALLOCATE_VAR (b, assert_var, var (), #var)
234
235
236#endif /* HB_BUFFER_PRIVATE_HH */
237