hb-buffer-private.hh revision 0bc7a38463bfb37f12efeb1b8cb2024cf4a347bc
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
137  inline unsigned int backtrack_len (void) const
138  { return have_output? out_len : idx; }
139  inline unsigned int next_serial (void) { return serial++; }
140
141  HB_INTERNAL void allocate_var (unsigned int byte_i, unsigned int count, const char *owner);
142  HB_INTERNAL void deallocate_var (unsigned int byte_i, unsigned int count, const char *owner);
143  HB_INTERNAL void assert_var (unsigned int byte_i, unsigned int count, const char *owner);
144  HB_INTERNAL void deallocate_var_all (void);
145
146  HB_INTERNAL void add (hb_codepoint_t  codepoint,
147			hb_mask_t       mask,
148			unsigned int    cluster);
149
150  HB_INTERNAL void reverse_range (unsigned int start, unsigned int end);
151  HB_INTERNAL void reverse (void);
152  HB_INTERNAL void reverse_clusters (void);
153  HB_INTERNAL void guess_properties (void);
154
155  HB_INTERNAL void swap_buffers (void);
156  HB_INTERNAL void remove_output (void);
157  HB_INTERNAL void clear_output (void);
158  HB_INTERNAL void clear_positions (void);
159
160  HB_INTERNAL void replace_glyphs (unsigned int num_in,
161				   unsigned int num_out,
162				   const hb_codepoint_t *glyph_data);
163
164  HB_INTERNAL void replace_glyph (hb_codepoint_t glyph_index);
165  /* Makes a copy of the glyph at idx to output and replace glyph_index */
166  HB_INTERNAL void output_glyph (hb_codepoint_t glyph_index);
167  HB_INTERNAL void output_info (hb_glyph_info_t &glyph_info);
168  /* Copies glyph at idx to output but doesn't advance idx */
169  HB_INTERNAL void copy_glyph (void);
170  /* Copies glyph at idx to output and advance idx.
171   * If there's no output, just advance idx. */
172  inline void
173  next_glyph (void)
174  {
175    if (have_output)
176    {
177      if (unlikely (out_info != info || out_len != idx)) {
178	if (unlikely (!make_room_for (1, 1))) return;
179	out_info[out_len] = info[idx];
180      }
181      out_len++;
182    }
183
184    idx++;
185  }
186
187  /* Advance idx without copying to output. */
188  inline void skip_glyph (void) { idx++; }
189
190  inline void reset_masks (hb_mask_t mask)
191  {
192    for (unsigned int j = 0; j < len; j++)
193      info[j].mask = mask;
194  }
195  inline void add_masks (hb_mask_t mask)
196  {
197    for (unsigned int j = 0; j < len; j++)
198      info[j].mask |= mask;
199  }
200  HB_INTERNAL void set_masks (hb_mask_t value,
201			      hb_mask_t mask,
202			      unsigned int cluster_start,
203			      unsigned int cluster_end);
204
205  HB_INTERNAL void merge_clusters (unsigned int start,
206				   unsigned int end);
207  HB_INTERNAL void merge_out_clusters (unsigned int start,
208				       unsigned int end);
209
210  /* Internal methods */
211  HB_INTERNAL bool enlarge (unsigned int size);
212
213  inline bool ensure (unsigned int size)
214  { return likely (size < allocated) ? true : enlarge (size); }
215
216  HB_INTERNAL bool make_room_for (unsigned int num_in, unsigned int num_out);
217
218  HB_INTERNAL void *get_scratch_buffer (unsigned int *size);
219
220  inline void clear_context (unsigned int side) { context_len[side] = 0; }
221};
222
223
224#define HB_BUFFER_XALLOCATE_VAR(b, func, var, owner) \
225  b->func (offsetof (hb_glyph_info_t, var) - offsetof(hb_glyph_info_t, var1), \
226	   sizeof (b->info[0].var), owner)
227#define HB_BUFFER_ALLOCATE_VAR(b, var) \
228	HB_BUFFER_XALLOCATE_VAR (b, allocate_var, var (), #var)
229#define HB_BUFFER_DEALLOCATE_VAR(b, var) \
230	HB_BUFFER_XALLOCATE_VAR (b, deallocate_var, var (), #var)
231#define HB_BUFFER_ASSERT_VAR(b, var) \
232	HB_BUFFER_XALLOCATE_VAR (b, assert_var, var (), #var)
233
234
235#endif /* HB_BUFFER_PRIVATE_HH */
236