Imported Upstream version 0.9.3
[platform/upstream/harfbuzz.git] / src / hb-buffer-private.hh
1 /*
2  * Copyright © 1998-2004  David Turner and Werner Lemberg
3  * Copyright © 2004,2007,2009,2010  Red Hat, Inc.
4  * Copyright © 2011  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
40 ASSERT_STATIC (sizeof (hb_glyph_info_t) == 20);
41 ASSERT_STATIC (sizeof (hb_glyph_info_t) == sizeof (hb_glyph_position_t));
42
43
44 /*
45  * hb_segment_properties_t
46  */
47
48 typedef 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
57 static inline hb_bool_t
58 hb_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
68 static inline unsigned int
69 hb_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
84 struct 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   bool in_error; /* Allocation failed */
96   bool have_output; /* Whether we have an output buffer going on */
97   bool have_positions; /* Whether we have positions */
98
99   unsigned int idx; /* Cursor into ->info and ->pos arrays */
100   unsigned int len; /* Length of ->info and ->pos arrays */
101   unsigned int out_len; /* Length of ->out array if have_output */
102
103   unsigned int allocated; /* Length of allocated arrays */
104   hb_glyph_info_t     *info;
105   hb_glyph_info_t     *out_info;
106   hb_glyph_position_t *pos;
107
108   inline hb_glyph_info_t &cur (unsigned int i = 0) { return info[idx + i]; }
109   inline hb_glyph_info_t cur (unsigned int i = 0) const { return info[idx + i]; }
110
111   inline hb_glyph_position_t &cur_pos (unsigned int i = 0) { return pos[idx + i]; }
112   inline hb_glyph_position_t cur_pos (unsigned int i = 0) const { return pos[idx + i]; }
113
114   inline hb_glyph_info_t &prev (void) { return out_info[out_len - 1]; }
115   inline hb_glyph_info_t prev (void) const { return info[out_len - 1]; }
116
117   unsigned int serial;
118   uint8_t allocated_var_bytes[8];
119   const char *allocated_var_owner[8];
120
121
122   /* Methods */
123
124   HB_INTERNAL void reset (void);
125
126   inline unsigned int backtrack_len (void) const
127   { return have_output? out_len : idx; }
128   inline unsigned int next_serial (void) { return serial++; }
129
130   HB_INTERNAL void allocate_var (unsigned int byte_i, unsigned int count, const char *owner);
131   HB_INTERNAL void deallocate_var (unsigned int byte_i, unsigned int count, const char *owner);
132   HB_INTERNAL void deallocate_var_all (void);
133
134   HB_INTERNAL void add (hb_codepoint_t  codepoint,
135                         hb_mask_t       mask,
136                         unsigned int    cluster);
137
138   HB_INTERNAL void reverse_range (unsigned int start, unsigned int end);
139   HB_INTERNAL void reverse (void);
140   HB_INTERNAL void reverse_clusters (void);
141   HB_INTERNAL void guess_properties (void);
142
143   HB_INTERNAL void swap_buffers (void);
144   HB_INTERNAL void clear_output (void);
145   HB_INTERNAL void clear_positions (void);
146
147   HB_INTERNAL void replace_glyphs (unsigned int num_in,
148                                    unsigned int num_out,
149                                    const hb_codepoint_t *glyph_data);
150
151   HB_INTERNAL void replace_glyph (hb_codepoint_t glyph_index);
152   /* Makes a copy of the glyph at idx to output and replace glyph_index */
153   HB_INTERNAL void output_glyph (hb_codepoint_t glyph_index);
154   /* Copies glyph at idx to output but doesn't advance idx */
155   HB_INTERNAL void copy_glyph (void);
156   /* Copies glyph at idx to output and advance idx.
157    * If there's no output, just advance idx. */
158   HB_INTERNAL void next_glyph (void);
159   /* Advance idx without copying to output. */
160   inline void skip_glyph (void) { idx++; }
161
162   inline void reset_masks (hb_mask_t mask)
163   {
164     for (unsigned int j = 0; j < len; j++)
165       info[j].mask = mask;
166   }
167   inline void add_masks (hb_mask_t mask)
168   {
169     for (unsigned int j = 0; j < len; j++)
170       info[j].mask |= mask;
171   }
172   HB_INTERNAL void set_masks (hb_mask_t value,
173                               hb_mask_t mask,
174                               unsigned int cluster_start,
175                               unsigned int cluster_end);
176
177   HB_INTERNAL void merge_clusters (unsigned int start,
178                                    unsigned int end);
179   HB_INTERNAL void merge_out_clusters (unsigned int start,
180                                        unsigned int end);
181
182   /* Internal methods */
183   HB_INTERNAL bool enlarge (unsigned int size);
184
185   inline bool ensure (unsigned int size)
186   { return likely (size < allocated) ? true : enlarge (size); }
187
188   HB_INTERNAL bool make_room_for (unsigned int num_in, unsigned int num_out);
189
190   HB_INTERNAL void *get_scratch_buffer (unsigned int *size);
191 };
192
193
194 #define HB_BUFFER_XALLOCATE_VAR(b, func, var, owner) \
195   b->func (offsetof (hb_glyph_info_t, var) - offsetof(hb_glyph_info_t, var1), \
196            sizeof (b->info[0].var), owner)
197 #define HB_BUFFER_ALLOCATE_VAR(b, var) \
198         HB_BUFFER_XALLOCATE_VAR (b, allocate_var, var (), #var)
199 #define HB_BUFFER_DEALLOCATE_VAR(b, var) \
200         HB_BUFFER_XALLOCATE_VAR (b, deallocate_var, var (), #var)
201
202
203 #endif /* HB_BUFFER_PRIVATE_HH */