1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright © 2011  Martin Hosken
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright © 2011  SIL International
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright © 2011,2012  Google, Inc.
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *  This is part of HarfBuzz, a text shaping library.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
8a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) * Permission is hereby granted, without written agreement and without
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * license or royalty fees, to use, copy, modify, and distribute this
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * software and its documentation for any purpose, provided that the
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * above copyright notice and the following two paragraphs appear in
12a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) * all copies of this software.
137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch *
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
15eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) * DAMAGE.
19a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) *
20a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
23e6402284a6517cc047655bc051a7f1269e182040Ben Murdoch * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) *
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * Google Author(s): Behdad Esfahbod
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) */
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
29a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#define HB_SHAPER graphite2
30a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#define hb_graphite2_shaper_font_data_t gr_font
31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "hb-shaper-impl-private.hh"
327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "hb-graphite2.h"
34a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
35a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include <graphite2/Segment.h>
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "hb-ot-tag.h"
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HB_SHAPER_DATA_ENSURE_DECLARE(graphite2, face)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HB_SHAPER_DATA_ENSURE_DECLARE(graphite2, font)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * shaper face data
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef struct hb_graphite2_tablelist_t {
49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  struct hb_graphite2_tablelist_t *next;
508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  hb_blob_t *blob;
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  unsigned int tag;
528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)} hb_graphite2_tablelist_t;
53a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)struct hb_graphite2_shaper_face_data_t {
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  hb_face_t *face;
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gr_face   *grface;
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  hb_graphite2_tablelist_t *tlist;
5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)};
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const void *hb_graphite2_get_table (const void *data, unsigned int tag, size_t *len)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
62a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  hb_graphite2_shaper_face_data_t *face_data = (hb_graphite2_shaper_face_data_t *) data;
63a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  hb_graphite2_tablelist_t *tlist = face_data->tlist;
64a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
65a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  hb_blob_t *blob = NULL;
66a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
67a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  for (hb_graphite2_tablelist_t *p = tlist; p; p = p->next)
68a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (p->tag == tag) {
69a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      blob = p->blob;
70a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      break;
71a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }
72a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
73a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (unlikely (!blob))
74a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  {
75a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    blob = face_data->face->reference_table (tag);
76a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
77a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    hb_graphite2_tablelist_t *p = (hb_graphite2_tablelist_t *) calloc (1, sizeof (hb_graphite2_tablelist_t));
78a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (unlikely (!p)) {
79a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      hb_blob_destroy (blob);
80a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      return NULL;
81a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }
82a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    p->blob = blob;
83a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    p->tag = tag;
84a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
85a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    /* TODO Not thread-safe, but fairly harmless.
86a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)     * We can do the double-chcked pointer cmpexch thing here. */
87a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    p->next = face_data->tlist;
88a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    face_data->tlist = p;
89a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
90a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
91a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  unsigned int tlen;
92a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const char *d = hb_blob_get_data (blob, &tlen);
93a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  *len = tlen;
94a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return d;
95a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
96a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
97a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)hb_graphite2_shaper_face_data_t *
98a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)_hb_graphite2_shaper_face_data_create (hb_face_t *face)
99a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles){
100a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  hb_blob_t *silf_blob = face->reference_table (HB_GRAPHITE2_TAG_SILF);
101a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  /* Umm, we just reference the table to check whether it exists.
102a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)   * Maybe add better API for this? */
103a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (!hb_blob_get_length (silf_blob))
104a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  {
105a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    hb_blob_destroy (silf_blob);
106a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return NULL;
107a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
108a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  hb_blob_destroy (silf_blob);
109a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
110a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  hb_graphite2_shaper_face_data_t *data = (hb_graphite2_shaper_face_data_t *) calloc (1, sizeof (hb_graphite2_shaper_face_data_t));
111a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (unlikely (!data))
112a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    hb_blob_destroy (silf_blob);
113a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
114a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  data->face = face;
115a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  data->grface = gr_make_face (data, &hb_graphite2_get_table, gr_face_preloadAll);
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (unlikely (!data->grface)) {
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    free (data);
119eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return NULL;
120a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
121eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
122eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return data;
123eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
124eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
125eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)_hb_graphite2_shaper_face_data_destroy (hb_graphite2_shaper_face_data_t *data)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  hb_graphite2_tablelist_t *tlist = data->tlist;
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (tlist)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    hb_graphite2_tablelist_t *old = tlist;
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    hb_blob_destroy (tlist->blob);
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    tlist = tlist->next;
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    free (old);
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
138f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gr_face_destroy (data->grface);
139f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
140f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  free (data);
1414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)gr_face *
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)hb_graphite2_face_get_gr_face (hb_face_t *face)
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (unlikely (!hb_graphite2_shaper_face_data_ensure (face))) return NULL;
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return HB_SHAPER_DATA_GET (face)->grface;
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * shaper font data
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static float hb_graphite2_get_advance (const void *hb_font, unsigned short gid)
156e6402284a6517cc047655bc051a7f1269e182040Ben Murdoch{
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return ((hb_font_t *) hb_font)->get_glyph_h_advance (gid);
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)hb_graphite2_shaper_font_data_t *
161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)_hb_graphite2_shaper_font_data_create (hb_font_t *font)
162868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles){
163868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (unlikely (!hb_graphite2_shaper_face_data_ensure (font->face))) return NULL;
164868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  hb_face_t *face = font->face;
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  hb_graphite2_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return gr_make_font_with_advance_fn (font->x_scale, font, &hb_graphite2_get_advance, face_data->grface);
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)_hb_graphite2_shaper_font_data_destroy (hb_graphite2_shaper_font_data_t *data)
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gr_font_destroy (data);
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
177f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)gr_font *
178f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)hb_graphite2_font_get_gr_font (hb_font_t *font)
179f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles){
180f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (unlikely (!hb_graphite2_shaper_font_data_ensure (font))) return NULL;
181f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return HB_SHAPER_DATA_GET (font);
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
183
184
185/*
186 * shaper shape_plan data
187 */
188
189struct hb_graphite2_shaper_shape_plan_data_t {};
190
191hb_graphite2_shaper_shape_plan_data_t *
192_hb_graphite2_shaper_shape_plan_data_create (hb_shape_plan_t    *shape_plan HB_UNUSED,
193					     const hb_feature_t *user_features HB_UNUSED,
194					     unsigned int        num_user_features HB_UNUSED)
195{
196  return (hb_graphite2_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
197}
198
199void
200_hb_graphite2_shaper_shape_plan_data_destroy (hb_graphite2_shaper_shape_plan_data_t *data HB_UNUSED)
201{
202}
203
204
205/*
206 * shaper
207 */
208
209struct hb_graphite2_cluster_t {
210  unsigned int base_char;
211  unsigned int num_chars;
212  unsigned int base_glyph;
213  unsigned int num_glyphs;
214};
215
216hb_bool_t
217_hb_graphite2_shape (hb_shape_plan_t    *shape_plan,
218		     hb_font_t          *font,
219		     hb_buffer_t        *buffer,
220		     const hb_feature_t *features,
221		     unsigned int        num_features)
222{
223  hb_face_t *face = font->face;
224  gr_face *grface = HB_SHAPER_DATA_GET (face)->grface;
225  gr_font *grfont = HB_SHAPER_DATA_GET (font);
226
227  const char *lang = hb_language_to_string (hb_buffer_get_language (buffer));
228  const char *lang_end = lang ? strchr (lang, '-') : NULL;
229  int lang_len = lang_end ? lang_end - lang : -1;
230  gr_feature_val *feats = gr_face_featureval_for_lang (grface, lang ? hb_tag_from_string (lang, lang_len) : 0);
231
232  while (num_features--)
233  {
234    const gr_feature_ref *fref = gr_face_find_fref (grface, features->tag);
235    if (fref)
236      gr_fref_set_feature_value (fref, features->value, feats);
237    features++;
238  }
239
240  gr_segment *seg = NULL;
241  const gr_slot *is;
242  unsigned int ci = 0, ic = 0;
243  float curradvx = 0., curradvy = 0.;
244
245  unsigned int scratch_size;
246  hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
247
248  uint32_t *chars = (uint32_t *) scratch;
249
250  for (unsigned int i = 0; i < buffer->len; ++i)
251    chars[i] = buffer->info[i].codepoint;
252
253  hb_tag_t script_tag[2];
254  hb_ot_tags_from_script (hb_buffer_get_script (buffer), &script_tag[0], &script_tag[1]);
255
256  seg = gr_make_seg (grfont, grface,
257		     script_tag[1] == HB_TAG_NONE ? script_tag[0] : script_tag[1],
258		     feats,
259		     gr_utf32, chars, buffer->len,
260		     2 | (hb_buffer_get_direction (buffer) == HB_DIRECTION_RTL ? 1 : 0));
261
262  if (unlikely (!seg)) {
263    if (feats) gr_featureval_destroy (feats);
264    return false;
265  }
266
267  unsigned int glyph_count = gr_seg_n_slots (seg);
268  if (unlikely (!glyph_count)) {
269    if (feats) gr_featureval_destroy (feats);
270    gr_seg_destroy (seg);
271    return false;
272  }
273
274  scratch = buffer->get_scratch_buffer (&scratch_size);
275  while ((DIV_CEIL (sizeof (hb_graphite2_cluster_t) * buffer->len, sizeof (*scratch)) +
276	  DIV_CEIL (sizeof (hb_codepoint_t) * glyph_count, sizeof (*scratch))) > scratch_size)
277  {
278    buffer->ensure (buffer->allocated * 2);
279    if (unlikely (buffer->in_error)) {
280      if (feats) gr_featureval_destroy (feats);
281      gr_seg_destroy (seg);
282      return false;
283    }
284    scratch = buffer->get_scratch_buffer (&scratch_size);
285  }
286
287#define ALLOCATE_ARRAY(Type, name, len) \
288  Type *name = (Type *) scratch; \
289  { \
290    unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
291    assert (_consumed <= scratch_size); \
292    scratch += _consumed; \
293    scratch_size -= _consumed; \
294  }
295
296  ALLOCATE_ARRAY (hb_graphite2_cluster_t, clusters, buffer->len);
297  ALLOCATE_ARRAY (hb_codepoint_t, gids, glyph_count);
298
299#undef ALLOCATE_ARRAY
300
301  memset (clusters, 0, sizeof (clusters[0]) * buffer->len);
302
303  hb_codepoint_t *pg = gids;
304  for (is = gr_seg_first_slot (seg), ic = 0; is; is = gr_slot_next_in_segment (is), ic++)
305  {
306    unsigned int before = gr_slot_before (is);
307    unsigned int after = gr_slot_after (is);
308    *pg = gr_slot_gid (is);
309    pg++;
310    while (clusters[ci].base_char > before && ci)
311    {
312      clusters[ci-1].num_chars += clusters[ci].num_chars;
313      clusters[ci-1].num_glyphs += clusters[ci].num_glyphs;
314      ci--;
315    }
316
317    if (gr_slot_can_insert_before (is) && clusters[ci].num_chars && before >= clusters[ci].base_char + clusters[ci].num_chars)
318    {
319      hb_graphite2_cluster_t *c = clusters + ci + 1;
320      c->base_char = clusters[ci].base_char + clusters[ci].num_chars;
321      c->num_chars = before - c->base_char;
322      c->base_glyph = ic;
323      c->num_glyphs = 0;
324      ci++;
325    }
326    clusters[ci].num_glyphs++;
327
328    if (clusters[ci].base_char + clusters[ci].num_chars < after + 1)
329	clusters[ci].num_chars = after + 1 - clusters[ci].base_char;
330  }
331  ci++;
332
333  //buffer->clear_output ();
334  for (unsigned int i = 0; i < ci; ++i)
335  {
336    for (unsigned int j = 0; j < clusters[i].num_glyphs; ++j)
337    {
338      hb_glyph_info_t *info = &buffer->info[clusters[i].base_glyph + j];
339      info->codepoint = gids[clusters[i].base_glyph + j];
340      info->cluster = gr_cinfo_base(gr_seg_cinfo(seg, clusters[i].base_char));
341    }
342  }
343  buffer->len = glyph_count;
344  //buffer->swap_buffers ();
345
346  if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
347    curradvx = gr_seg_advance_X(seg);
348
349  hb_glyph_position_t *pPos;
350  for (pPos = hb_buffer_get_glyph_positions (buffer, NULL), is = gr_seg_first_slot (seg);
351       is; pPos++, is = gr_slot_next_in_segment (is))
352  {
353    pPos->x_offset = gr_slot_origin_X (is) - curradvx;
354    pPos->y_offset = gr_slot_origin_Y (is) - curradvy;
355    pPos->x_advance = gr_slot_advance_X (is, grface, grfont);
356    pPos->y_advance = gr_slot_advance_Y (is, grface, grfont);
357    if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
358      curradvx -= pPos->x_advance;
359    pPos->x_offset = gr_slot_origin_X (is) - curradvx;
360    if (!HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
361      curradvx += pPos->x_advance;
362    pPos->y_offset = gr_slot_origin_Y (is) - curradvy;
363    curradvy += pPos->y_advance;
364  }
365  if (!HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
366    pPos[-1].x_advance += gr_seg_advance_X(seg) - curradvx;
367
368  if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
369    hb_buffer_reverse_clusters (buffer);
370
371  if (feats) gr_featureval_destroy (feats);
372  gr_seg_destroy (seg);
373
374  return true;
375}
376