Remove lock_instance()
[framework/uifw/harfbuzz.git] / src / hb-ot-shape.c
1 /*
2  * Copyright (C) 2009  Red Hat, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Red Hat Author(s): Behdad Esfahbod
25  */
26
27 #include "hb-ot-shape-private.h"
28
29 #include "hb-buffer-private.h"
30
31 #include "hb-ot-layout.h"
32
33 hb_tag_t default_features[] = {
34   /* GSUB */
35   HB_TAG('c','c','m','p'),
36   HB_TAG('l','o','c','l'),
37   HB_TAG('l','i','g','a'),
38   HB_TAG('c','l','i','g'),
39   /* GPOS */
40   HB_TAG('k','e','r','n'),
41   HB_TAG('m','a','r','k'),
42   HB_TAG('m','k','m','k'),
43 };
44
45
46 static void
47 add_feature (hb_face_t    *face,
48              hb_tag_t      table_tag,
49              unsigned int  feature_index,
50              unsigned int *lookups,
51              unsigned int *num_lookups,
52              unsigned int  room_lookups)
53 {
54   unsigned int i = room_lookups - *num_lookups;
55   hb_ot_layout_feature_get_lookup_indexes (face, table_tag, feature_index, 0,
56                                            &i,
57                                            lookups + *num_lookups);
58   *num_lookups += i;
59 }
60
61 static int
62 cmp_lookups (const void *p1, const void *p2)
63 {
64   unsigned int a = * (const unsigned int *) p1;
65   unsigned int b = * (const unsigned int *) p2;
66
67   return a - b;
68 }
69
70 static void
71 setup_lookups (hb_face_t    *face,
72                hb_buffer_t  *buffer,
73                hb_feature_t *features,
74                unsigned int  num_features,
75                hb_tag_t      table_tag,
76                unsigned int *lookups,
77                unsigned int *num_lookups)
78 {
79   unsigned int i, j, script_index, language_index, feature_index, room_lookups;
80
81   room_lookups = *num_lookups;
82   *num_lookups = 0;
83
84   hb_ot_layout_table_choose_script (face, table_tag,
85                                     hb_ot_tags_from_script (buffer->script),
86                                     &script_index);
87   hb_ot_layout_script_find_language (face, table_tag, script_index,
88                                      hb_ot_tag_from_language (buffer->language),
89                                      &language_index);
90
91   if (hb_ot_layout_language_get_required_feature_index (face, table_tag, script_index, language_index,
92                                                         &feature_index))
93     add_feature (face, table_tag, feature_index, lookups, num_lookups, room_lookups);
94
95   for (i = 0; i < ARRAY_LENGTH (default_features); i++)
96   {
97     if (hb_ot_layout_language_find_feature (face, table_tag, script_index, language_index,
98                                             default_features[i],
99                                             &feature_index))
100       add_feature (face, table_tag, feature_index, lookups, num_lookups, room_lookups);
101   }
102
103   qsort (lookups, *num_lookups, sizeof (lookups[0]), cmp_lookups);
104
105   if (*num_lookups)
106   {
107     for (i = 1, j = 0; i < *num_lookups; i++)
108       if (lookups[i] != lookups[j])
109         lookups[++j] = lookups[i];
110     lookups[j++] = lookups[i - 1];
111     *num_lookups = j;
112   }
113 }
114
115
116 hb_bool_t
117 _hb_ot_substitute_complex (hb_font_t    *font HB_GNUC_UNUSED,
118                            hb_face_t    *face,
119                            hb_buffer_t  *buffer,
120                            hb_feature_t *features,
121                            unsigned int  num_features)
122 {
123   unsigned int lookups[1000];
124   unsigned int num_lookups = ARRAY_LENGTH (lookups);
125   unsigned int i;
126
127   if (!hb_ot_layout_has_substitution (face))
128     return FALSE;
129
130   setup_lookups (face, buffer, features, num_features,
131                  HB_OT_TAG_GSUB,
132                  lookups, &num_lookups);
133
134   for (i = 0; i < num_lookups; i++)
135     hb_ot_layout_substitute_lookup (face, buffer, lookups[i], 0xFFFF);
136
137   return TRUE;
138 }
139
140 hb_bool_t
141 _hb_ot_position_complex (hb_font_t    *font,
142                          hb_face_t    *face,
143                          hb_buffer_t  *buffer,
144                          hb_feature_t *features,
145                          unsigned int  num_features)
146 {
147   unsigned int lookups[1000];
148   unsigned int num_lookups = ARRAY_LENGTH (lookups);
149   unsigned int i;
150
151   if (!hb_ot_layout_has_positioning (face))
152     return FALSE;
153
154   setup_lookups (face, buffer, features, num_features,
155                  HB_OT_TAG_GPOS,
156                  lookups, &num_lookups);
157
158   for (i = 0; i < num_lookups; i++)
159     hb_ot_layout_position_lookup (font, face, buffer, lookups[i], 0xFFFF);
160
161   hb_ot_layout_position_finish (font, face, buffer);
162
163   return TRUE;
164 }