hb-graphite2.cc 10.7 KB
Newer Older
1
/*
B
Behdad Esfahbod 已提交
2 3
 * Copyright © 2011  Martin Hosken
 * Copyright © 2011  SIL International
4
 * Copyright © 2011,2012  Google, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
B
Behdad Esfahbod 已提交
25 26
 *
 * Google Author(s): Behdad Esfahbod
27 28
 */

29 30 31 32
#define HB_SHAPER graphite2
#define hb_graphite2_shaper_font_data_t gr_font
#include "hb-shaper-impl-private.hh"

B
Behdad Esfahbod 已提交
33 34
#include "hb-graphite2.h"

B
Behdad Esfahbod 已提交
35 36
#include <graphite2/Segment.h>

37

38 39
HB_SHAPER_DATA_ENSURE_DECLARE(graphite2, face)
HB_SHAPER_DATA_ENSURE_DECLARE(graphite2, font)
40 41


42 43 44
/*
 * shaper face data
 */
45

46 47
typedef struct hb_graphite2_tablelist_t {
  struct hb_graphite2_tablelist_t *next;
B
Behdad Esfahbod 已提交
48
  hb_blob_t *blob;
B
Behdad Esfahbod 已提交
49
  unsigned int tag;
50
} hb_graphite2_tablelist_t;
51

52 53
struct hb_graphite2_shaper_face_data_t {
  hb_face_t *face;
B
Behdad Esfahbod 已提交
54
  gr_face   *grface;
55 56
  hb_graphite2_tablelist_t *tlist;
};
57

B
Behdad Esfahbod 已提交
58
static const void *hb_graphite2_get_table (const void *data, unsigned int tag, size_t *len)
59
{
B
Behdad Esfahbod 已提交
60 61 62 63 64 65 66 67 68 69 70 71
  hb_graphite2_shaper_face_data_t *face_data = (hb_graphite2_shaper_face_data_t *) data;
  hb_graphite2_tablelist_t *tlist = face_data->tlist;

  hb_blob_t *blob = NULL;

  for (hb_graphite2_tablelist_t *p = tlist; p; p = p->next)
    if (p->tag == tag) {
      blob = p->blob;
      break;
    }

  if (unlikely (!blob))
B
Behdad Esfahbod 已提交
72
  {
B
Behdad Esfahbod 已提交
73
    blob = face_data->face->reference_table (tag);
B
Behdad Esfahbod 已提交
74 75 76

    hb_graphite2_tablelist_t *p = (hb_graphite2_tablelist_t *) calloc (1, sizeof (hb_graphite2_tablelist_t));
    if (unlikely (!p)) {
B
Minor  
Behdad Esfahbod 已提交
77
      hb_blob_destroy (blob);
B
Behdad Esfahbod 已提交
78 79
      return NULL;
    }
B
Behdad Esfahbod 已提交
80 81 82 83 84 85 86
    p->blob = blob;
    p->tag = tag;

    /* TODO Not thread-safe, but fairly harmless.
     * We can do the double-chcked pointer cmpexch thing here. */
    p->next = face_data->tlist;
    face_data->tlist = p;
B
Behdad Esfahbod 已提交
87 88 89 90 91 92
  }

  unsigned int tlen;
  const char *d = hb_blob_get_data (blob, &tlen);
  *len = tlen;
  return d;
93 94
}

95 96
hb_graphite2_shaper_face_data_t *
_hb_graphite2_shaper_face_data_create (hb_face_t *face)
97
{
B
Behdad Esfahbod 已提交
98
  hb_blob_t *silf_blob = face->reference_table (HB_GRAPHITE2_TAG_SILF);
B
Behdad Esfahbod 已提交
99 100
  /* Umm, we just reference the table to check whether it exists.
   * Maybe add better API for this? */
101 102 103 104 105
  if (!hb_blob_get_length (silf_blob))
  {
    hb_blob_destroy (silf_blob);
    return NULL;
  }
B
Behdad Esfahbod 已提交
106 107 108 109 110
  hb_blob_destroy (silf_blob);

  hb_graphite2_shaper_face_data_t *data = (hb_graphite2_shaper_face_data_t *) calloc (1, sizeof (hb_graphite2_shaper_face_data_t));
  if (unlikely (!data))
    hb_blob_destroy (silf_blob);
111 112

  data->face = face;
B
Behdad Esfahbod 已提交
113
  data->grface = gr_make_face (data, &hb_graphite2_get_table, gr_face_preloadAll);
114

B
Behdad Esfahbod 已提交
115 116 117 118 119
  if (unlikely (!data->grface)) {
    free (data);
    return NULL;
  }

120
  return data;
121 122
}

123 124
void
_hb_graphite2_shaper_face_data_destroy (hb_graphite2_shaper_face_data_t *data)
125
{
126 127
  hb_graphite2_tablelist_t *tlist = data->tlist;

B
Behdad Esfahbod 已提交
128 129
  while (tlist)
  {
130
    hb_graphite2_tablelist_t *old = tlist;
B
Behdad Esfahbod 已提交
131 132
    hb_blob_destroy (tlist->blob);
    tlist = tlist->next;
B
Minor  
Behdad Esfahbod 已提交
133
    free (old);
B
Behdad Esfahbod 已提交
134
  }
135

136
  gr_face_destroy (data->grface);
137

138
  free (data);
139 140
}

141 142 143 144 145 146 147
gr_face *
hb_graphite2_face_get_gr_face (hb_face_t *face)
{
  if (unlikely (!hb_graphite2_shaper_face_data_ensure (face))) return NULL;
  return HB_SHAPER_DATA_GET (face)->grface;
}

B
Behdad Esfahbod 已提交
148

149 150 151
/*
 * shaper font data
 */
B
Behdad Esfahbod 已提交
152

B
Behdad Esfahbod 已提交
153
static float hb_graphite2_get_advance (const void *hb_font, unsigned short gid)
154 155 156
{
  return ((hb_font_t *) hb_font)->get_glyph_h_advance (gid);
}
B
Behdad Esfahbod 已提交
157

158 159 160 161
hb_graphite2_shaper_font_data_t *
_hb_graphite2_shaper_font_data_create (hb_font_t *font)
{
  if (unlikely (!hb_graphite2_shaper_face_data_ensure (font->face))) return NULL;
B
Behdad Esfahbod 已提交
162

163 164
  hb_face_t *face = font->face;
  hb_graphite2_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
B
Behdad Esfahbod 已提交
165

B
Behdad Esfahbod 已提交
166
  return gr_make_font_with_advance_fn (font->x_scale, font, &hb_graphite2_get_advance, face_data->grface);
167 168
}

169 170
void
_hb_graphite2_shaper_font_data_destroy (hb_graphite2_shaper_font_data_t *data)
171
{
172 173
  gr_font_destroy (data);
}
B
Behdad Esfahbod 已提交
174

175 176 177 178 179 180 181
gr_font *
hb_graphite2_font_get_gr_font (hb_font_t *font)
{
  if (unlikely (!hb_graphite2_shaper_font_data_ensure (font))) return NULL;
  return HB_SHAPER_DATA_GET (font);
}

B
Behdad Esfahbod 已提交
182

183 184 185
/*
 * shaper shape_plan data
 */
B
Behdad Esfahbod 已提交
186

187
struct hb_graphite2_shaper_shape_plan_data_t {};
B
Behdad Esfahbod 已提交
188

189
hb_graphite2_shaper_shape_plan_data_t *
B
Minor  
Behdad Esfahbod 已提交
190 191 192
_hb_graphite2_shaper_shape_plan_data_create (hb_shape_plan_t    *shape_plan HB_UNUSED,
					     const hb_feature_t *user_features HB_UNUSED,
					     unsigned int        num_user_features HB_UNUSED)
193 194 195
{
  return (hb_graphite2_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
}
B
Behdad Esfahbod 已提交
196

197
void
B
Minor  
Behdad Esfahbod 已提交
198
_hb_graphite2_shaper_shape_plan_data_destroy (hb_graphite2_shaper_shape_plan_data_t *data HB_UNUSED)
199 200
{
}
B
Behdad Esfahbod 已提交
201 202


203 204 205
/*
 * shaper
 */
206

207 208 209 210 211 212
struct hb_graphite2_cluster_t {
  unsigned int base_char;
  unsigned int num_chars;
  unsigned int base_glyph;
  unsigned int num_glyphs;
};
213

B
Behdad Esfahbod 已提交
214
hb_bool_t
215 216 217 218 219
_hb_graphite2_shape (hb_shape_plan_t    *shape_plan,
		     hb_font_t          *font,
		     hb_buffer_t        *buffer,
		     const hb_feature_t *features,
		     unsigned int        num_features)
220
{
221 222 223
  hb_face_t *face = font->face;
  gr_face *grface = HB_SHAPER_DATA_GET (face)->grface;
  gr_font *grfont = HB_SHAPER_DATA_GET (font);
B
Behdad Esfahbod 已提交
224 225

  const char *lang = hb_language_to_string (hb_buffer_get_language (buffer));
226
  const char *lang_end = lang ? strchr (lang, '-') : NULL;
227
  int lang_len = lang_end ? lang_end - lang : -1;
228
  gr_feature_val *feats = gr_face_featureval_for_lang (grface, lang ? hb_tag_from_string (lang, lang_len) : 0);
B
Behdad Esfahbod 已提交
229 230 231

  while (num_features--)
  {
232
    const gr_feature_ref *fref = gr_face_find_fref (grface, features->tag);
B
Behdad Esfahbod 已提交
233 234 235 236 237 238 239 240 241 242
    if (fref)
      gr_fref_set_feature_value (fref, features->value, feats);
    features++;
  }

  gr_segment *seg = NULL;
  const gr_slot *is;
  unsigned int ci = 0, ic = 0;
  float curradvx = 0., curradvy = 0.;

243
  unsigned int scratch_size;
B
Behdad Esfahbod 已提交
244
  hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
245

246
  uint32_t *chars = (uint32_t *) scratch;
B
Behdad Esfahbod 已提交
247

248 249
  for (unsigned int i = 0; i < buffer->len; ++i)
    chars[i] = buffer->info[i].codepoint;
B
Behdad Esfahbod 已提交
250 251

  hb_tag_t script_tag[2];
B
Minor  
Behdad Esfahbod 已提交
252
  hb_ot_tags_from_script (hb_buffer_get_script (buffer), &script_tag[0], &script_tag[1]);
B
Behdad Esfahbod 已提交
253

254
  seg = gr_make_seg (grfont, grface,
B
Behdad Esfahbod 已提交
255 256
		     script_tag[1] == HB_TAG_NONE ? script_tag[0] : script_tag[1],
		     feats,
257
		     gr_utf32, chars, buffer->len,
B
Minor  
Behdad Esfahbod 已提交
258
		     2 | (hb_buffer_get_direction (buffer) == HB_DIRECTION_RTL ? 1 : 0));
B
Behdad Esfahbod 已提交
259

260 261 262 263 264 265 266 267 268 269 270 271
  if (unlikely (!seg)) {
    if (feats) gr_featureval_destroy (feats);
    return false;
  }

  unsigned int glyph_count = gr_seg_n_slots (seg);
  if (unlikely (!glyph_count)) {
    if (feats) gr_featureval_destroy (feats);
    gr_seg_destroy (seg);
    return false;
  }

272 273 274
  scratch = buffer->get_scratch_buffer (&scratch_size);
  while ((DIV_CEIL (sizeof (hb_graphite2_cluster_t) * buffer->len, sizeof (*scratch)) +
	  DIV_CEIL (sizeof (hb_codepoint_t) * glyph_count, sizeof (*scratch))) > scratch_size)
275 276 277 278 279 280 281
  {
    buffer->ensure (buffer->allocated * 2);
    if (unlikely (buffer->in_error)) {
      if (feats) gr_featureval_destroy (feats);
      gr_seg_destroy (seg);
      return false;
    }
282 283 284 285 286 287 288 289 290 291
    scratch = buffer->get_scratch_buffer (&scratch_size);
  }

#define ALLOCATE_ARRAY(Type, name, len) \
  Type *name = (Type *) scratch; \
  { \
    unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
    assert (_consumed <= scratch_size); \
    scratch += _consumed; \
    scratch_size -= _consumed; \
292
  }
B
Behdad Esfahbod 已提交
293

294 295
  ALLOCATE_ARRAY (hb_graphite2_cluster_t, clusters, buffer->len);
  ALLOCATE_ARRAY (hb_codepoint_t, gids, glyph_count);
B
Behdad Esfahbod 已提交
296

297 298
#undef ALLOCATE_ARRAY

299 300 301
  memset (clusters, 0, sizeof (clusters[0]) * buffer->len);

  hb_codepoint_t *pg = gids;
B
Behdad Esfahbod 已提交
302 303 304 305 306 307 308
  for (is = gr_seg_first_slot (seg), ic = 0; is; is = gr_slot_next_in_segment (is), ic++)
  {
    unsigned int before = gr_slot_before (is);
    unsigned int after = gr_slot_after (is);
    *pg = gr_slot_gid (is);
    pg++;
    while (clusters[ci].base_char > before && ci)
309
    {
B
Behdad Esfahbod 已提交
310 311
      clusters[ci-1].num_chars += clusters[ci].num_chars;
      clusters[ci-1].num_glyphs += clusters[ci].num_glyphs;
B
Minor  
Behdad Esfahbod 已提交
312
      ci--;
313 314
    }

B
Behdad Esfahbod 已提交
315
    if (gr_slot_can_insert_before (is) && clusters[ci].num_chars && before >= clusters[ci].base_char + clusters[ci].num_chars)
316
    {
317
      hb_graphite2_cluster_t *c = clusters + ci + 1;
B
Behdad Esfahbod 已提交
318 319 320 321
      c->base_char = clusters[ci].base_char + clusters[ci].num_chars;
      c->num_chars = before - c->base_char;
      c->base_glyph = ic;
      c->num_glyphs = 0;
B
Minor  
Behdad Esfahbod 已提交
322
      ci++;
323
    }
B
Minor  
Behdad Esfahbod 已提交
324
    clusters[ci].num_glyphs++;
B
Behdad Esfahbod 已提交
325 326 327 328

    if (clusters[ci].base_char + clusters[ci].num_chars < after + 1)
	clusters[ci].num_chars = after + 1 - clusters[ci].base_char;
  }
B
Minor  
Behdad Esfahbod 已提交
329
  ci++;
B
Behdad Esfahbod 已提交
330

B
Behdad Esfahbod 已提交
331
  //buffer->clear_output ();
B
Minor  
Behdad Esfahbod 已提交
332
  for (unsigned int i = 0; i < ci; ++i)
B
Behdad Esfahbod 已提交
333 334 335 336 337 338 339 340 341 342
  {
    for (unsigned int j = 0; j < clusters[i].num_glyphs; ++j)
    {
      hb_glyph_info_t *info = &buffer->info[clusters[i].base_glyph + j];
      info->codepoint = gids[clusters[i].base_glyph + j];
      info->cluster = gr_cinfo_base(gr_seg_cinfo(seg, clusters[i].base_char));
    }
  }
  buffer->len = glyph_count;
  //buffer->swap_buffers ();
B
Behdad Esfahbod 已提交
343

344 345 346
  if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
    curradvx = gr_seg_advance_X(seg);

B
Behdad Esfahbod 已提交
347
  hb_glyph_position_t *pPos;
B
Minor  
Behdad Esfahbod 已提交
348 349
  for (pPos = hb_buffer_get_glyph_positions (buffer, NULL), is = gr_seg_first_slot (seg);
       is; pPos++, is = gr_slot_next_in_segment (is))
B
Behdad Esfahbod 已提交
350
  {
351 352 353 354 355 356 357 358 359 360
    pPos->x_offset = gr_slot_origin_X (is) - curradvx;
    pPos->y_offset = gr_slot_origin_Y (is) - curradvy;
    pPos->x_advance = gr_slot_advance_X (is, grface, grfont);
    pPos->y_advance = gr_slot_advance_Y (is, grface, grfont);
    if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
      curradvx -= pPos->x_advance;
    pPos->x_offset = gr_slot_origin_X (is) - curradvx;
    if (!HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
      curradvx += pPos->x_advance;
    pPos->y_offset = gr_slot_origin_Y (is) - curradvy;
B
Behdad Esfahbod 已提交
361 362
    curradvy += pPos->y_advance;
  }
363 364 365 366 367
  if (!HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
    pPos[-1].x_advance += gr_seg_advance_X(seg) - curradvx;

  if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
    hb_buffer_reverse_clusters (buffer);
368

B
Behdad Esfahbod 已提交
369
  if (feats) gr_featureval_destroy (feats);
370 371 372
  gr_seg_destroy (seg);

  return true;
373
}