hb-ot-shape.cc 16.8 KB
Newer Older
B
Behdad Esfahbod 已提交
1
/*
B
Behdad Esfahbod 已提交
2
 * Copyright (C) 2009,2010  Red Hat, Inc.
B
Behdad Esfahbod 已提交
3
 *
B
Behdad Esfahbod 已提交
4
 *  This is part of HarfBuzz, a text shaping library.
B
Behdad Esfahbod 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * 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.
 *
 * Red Hat Author(s): Behdad Esfahbod
 */

27
#include "hb-ot-shape.h"
B
Behdad Esfahbod 已提交
28

29
#include "hb-buffer-private.hh"
B
Behdad Esfahbod 已提交
30

B
Behdad Esfahbod 已提交
31 32
#include "hb-open-type-private.hh"

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

B
Behdad Esfahbod 已提交
35
/* XXX vertical */
B
Behdad Esfahbod 已提交
36
hb_tag_t default_features[] = {
37
  HB_TAG('c','a','l','t'),
B
Behdad Esfahbod 已提交
38 39
  HB_TAG('c','c','m','p'),
  HB_TAG('c','l','i','g'),
40 41
  HB_TAG('c','s','w','h'),
  HB_TAG('c','u','r','s'),
B
Behdad Esfahbod 已提交
42
  HB_TAG('k','e','r','n'),
43 44
  HB_TAG('l','i','g','a'),
  HB_TAG('l','o','c','l'),
B
Behdad Esfahbod 已提交
45 46
  HB_TAG('m','a','r','k'),
  HB_TAG('m','k','m','k'),
47
  HB_TAG('r','l','i','g')
B
Behdad Esfahbod 已提交
48 49
};

B
Behdad Esfahbod 已提交
50 51 52 53 54 55
enum {
  MASK_ALWAYS_ON = 1 << 0,
  MASK_RTLM      = 1 << 1
};
#define MASK_BITS_USED 2

B
Behdad Esfahbod 已提交
56 57 58 59 60
struct lookup_map {
  unsigned int index;
  hb_mask_t mask;
};

B
Behdad Esfahbod 已提交
61 62 63 64 65

static void
add_feature (hb_face_t    *face,
	     hb_tag_t      table_tag,
	     unsigned int  feature_index,
B
Behdad Esfahbod 已提交
66 67
	     hb_mask_t     mask,
	     lookup_map   *lookups,
B
Behdad Esfahbod 已提交
68 69 70 71
	     unsigned int *num_lookups,
	     unsigned int  room_lookups)
{
  unsigned int i = room_lookups - *num_lookups;
B
Behdad Esfahbod 已提交
72 73 74 75
  lookups += *num_lookups;

  unsigned int *lookup_indices = (unsigned int *) lookups;

B
Behdad Esfahbod 已提交
76 77
  hb_ot_layout_feature_get_lookup_indexes (face, table_tag, feature_index, 0,
					   &i,
B
Behdad Esfahbod 已提交
78 79
					   lookup_indices);

B
Behdad Esfahbod 已提交
80
  *num_lookups += i;
B
Behdad Esfahbod 已提交
81 82 83 84 85

  while (i--) {
    lookups[i].mask = mask;
    lookups[i].index = lookup_indices[i];
  }
B
Behdad Esfahbod 已提交
86 87 88 89 90
}

static int
cmp_lookups (const void *p1, const void *p2)
{
B
Behdad Esfahbod 已提交
91 92
  const lookup_map *a = (const lookup_map *) p1;
  const lookup_map *b = (const lookup_map *) p2;
B
Behdad Esfahbod 已提交
93

B
Behdad Esfahbod 已提交
94
  return a->index - b->index;
B
Behdad Esfahbod 已提交
95 96
}

97 98 99

#define MAX_FEATURES 100

B
Behdad Esfahbod 已提交
100
struct hb_mask_allocator_t {
101

B
Behdad Esfahbod 已提交
102 103 104 105
  struct feature_info_t {
    hb_tag_t tag;
    unsigned int value;
    bool global;
106

B
Behdad Esfahbod 已提交
107 108 109 110 111
    static int
    cmp (const void *p1, const void *p2)
    {
      const feature_info_t *a = (const feature_info_t *) p1;
      const feature_info_t *b = (const feature_info_t *) p2;
112

B
Behdad Esfahbod 已提交
113 114
      if (a->tag != b->tag)
        return a->tag < b->tag ? -1 : 1;
115

B
Behdad Esfahbod 已提交
116 117 118
      return p1 < p2 ? -1 : 1;
    }
  };
119

B
Behdad Esfahbod 已提交
120 121 122 123 124 125 126 127
  struct feature_map_t {
    hb_tag_t tag; /* should be first */
    unsigned int index;
    unsigned int shift;
    hb_mask_t mask;

    static int
    cmp (const void *p1, const void *p2)
128
    {
B
Behdad Esfahbod 已提交
129 130 131 132 133 134 135
      const feature_map_t *a = (const feature_map_t *) p1;
      const feature_map_t *b = (const feature_map_t *) p2;

      return a->tag < b->tag ? -1 : a->tag > b->tag ? 1 : 0;
    }
  };

136
  hb_mask_allocator_t (void) : count (0) {}
B
Behdad Esfahbod 已提交
137

B
Behdad Esfahbod 已提交
138 139 140
  void add_feature (hb_tag_t tag,
		    unsigned int value,
		    bool global)
141
  {
B
Behdad Esfahbod 已提交
142 143
    feature_info_t *info = &infos[count++];
    info->tag = tag;
B
Behdad Esfahbod 已提交
144
    info->value = value;
B
Behdad Esfahbod 已提交
145
    info->global = global;
146 147
  }

148 149 150 151
  void compile (hb_face_t *face,
		hb_tag_t table_tag,
		unsigned int script_index,
		unsigned int language_index)
152
  {
B
Behdad Esfahbod 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165
    global_mask = 0;
    next_bit = MASK_BITS_USED;

    if (!count)
      return;

    qsort (infos, count, sizeof (infos[0]), feature_info_t::cmp);

    unsigned int j = 0;
    for (unsigned int i = 1; i < count; i++)
      if (infos[i].tag != infos[j].tag)
	infos[++j] = infos[i];
      else {
B
Behdad Esfahbod 已提交
166 167 168 169
	if (infos[i].global)
	  infos[j] = infos[i];
	else {
	  infos[j].global = infos[j].global && (infos[j].value == infos[i].value);
B
Behdad Esfahbod 已提交
170
	  infos[j].value = MAX (infos[j].value, infos[i].value);
B
Behdad Esfahbod 已提交
171
	}
B
Behdad Esfahbod 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
      }
    count = j + 1;

    /* Allocate bits now */
    j = 0;
    for (unsigned int i = 0; i < count; i++) {
      const feature_info_t *info = &infos[i];

      unsigned int bits_needed;

      if (info->global && info->value == 1)
        /* Uses the global bit */
        bits_needed = 0;
      else
        bits_needed = _hb_bit_storage (info->value);

      if (!info->value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
        continue; /* Feature disabled, or not enough bits. */

      unsigned int feature_index;
      if (!hb_ot_layout_language_find_feature (face, table_tag, script_index, language_index,
					       info->tag, &feature_index))
        continue;

      feature_map_t *map = &maps[j++];

      map->tag = info->tag;
      map->index = feature_index;
      if (info->global && info->value == 1) {
        /* Uses the global bit */
        map->shift = 0;
	map->mask = 1;
      } else {
	map->shift = next_bit;
	map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
	next_bit += bits_needed;
      }

      if (info->global && map->mask != 1)
        global_mask |= map->mask;
    }
    count = j;
214 215
  }

B
Behdad Esfahbod 已提交
216 217 218 219 220
  hb_mask_t get_global_mask (void) { return global_mask; }
  const feature_map_t *find_feature (hb_tag_t tag) const {
    static const feature_map_t off_map = { HB_TAG_NONE, Index::NOT_FOUND_INDEX, 0, 0 };
    const feature_map_t *map = (const feature_map_t *) bsearch (&tag, maps, count, sizeof (maps[0]), feature_map_t::cmp);
    return map ? map : &off_map;
221 222
  }

B
Behdad Esfahbod 已提交
223 224 225 226 227 228 229 230 231 232

  private:

  unsigned int count;
  feature_info_t infos[MAX_FEATURES];
  feature_map_t maps[MAX_FEATURES];

  hb_mask_t global_mask;
  unsigned int next_bit;
};
233

B
Behdad Esfahbod 已提交
234
static void
B
Behdad Esfahbod 已提交
235
setup_lookups (hb_face_t    *face,
B
Behdad Esfahbod 已提交
236 237 238 239
	       hb_buffer_t  *buffer,
	       hb_feature_t *features,
	       unsigned int  num_features,
	       hb_tag_t      table_tag,
B
Behdad Esfahbod 已提交
240
	       lookup_map   *lookups,
241 242
	       unsigned int *num_lookups,
	       hb_direction_t original_direction)
B
Behdad Esfahbod 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
{
  unsigned int i, j, script_index, language_index, feature_index, room_lookups;

  room_lookups = *num_lookups;
  *num_lookups = 0;

  hb_ot_layout_table_choose_script (face, table_tag,
				    hb_ot_tags_from_script (buffer->script),
				    &script_index);
  hb_ot_layout_script_find_language (face, table_tag, script_index,
				     hb_ot_tag_from_language (buffer->language),
				     &language_index);

  if (hb_ot_layout_language_get_required_feature_index (face, table_tag, script_index, language_index,
							&feature_index))
B
Behdad Esfahbod 已提交
258
    add_feature (face, table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
B
Behdad Esfahbod 已提交
259

260

261
  hb_mask_allocator_t allocator;
B
Behdad Esfahbod 已提交
262 263 264

  switch (original_direction) {
    case HB_DIRECTION_LTR:
B
Behdad Esfahbod 已提交
265 266
      allocator.add_feature (HB_TAG ('l','t','r','a'), 1, true);
      allocator.add_feature (HB_TAG ('l','t','r','m'), 1, true);
B
Behdad Esfahbod 已提交
267 268
      break;
    case HB_DIRECTION_RTL:
B
Behdad Esfahbod 已提交
269 270 271
      allocator.add_feature (HB_TAG ('r','t','l','a'), 1, true);
      //allocator.add_feature (HB_TAG ('r','t','l','m'), false);
      allocator.add_feature (HB_TAG ('r','t','l','m'), 1, true);
B
Behdad Esfahbod 已提交
272 273 274 275 276 277 278 279
      break;
    case HB_DIRECTION_TTB:
    case HB_DIRECTION_BTT:
    default:
      break;
  }

  for (i = 0; i < ARRAY_LENGTH (default_features); i++)
B
Behdad Esfahbod 已提交
280 281 282 283 284 285 286 287
    allocator.add_feature (default_features[i], 1, true);

  /* XXX complex-shaper features go here */

  for (unsigned int i = 0; i < num_features; i++) {
    const hb_feature_t *feature = &features[i];
    allocator.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
  }
B
Behdad Esfahbod 已提交
288 289 290


  /* Compile features */
291
  allocator.compile (face, table_tag, script_index, language_index);
B
Behdad Esfahbod 已提交
292 293 294 295 296


  /* Gather lookup indices for features and set buffer masks at the same time */

  const hb_mask_allocator_t::feature_map_t *map;
297

B
Behdad Esfahbod 已提交
298 299 300
  hb_mask_t global_mask = allocator.get_global_mask ();
  if (global_mask)
    buffer->set_masks (global_mask, global_mask, 0, (unsigned int) -1);
301 302 303

  switch (original_direction) {
    case HB_DIRECTION_LTR:
B
Behdad Esfahbod 已提交
304 305 306 307
      map = allocator.find_feature (HB_TAG ('l','t','r','a'));
      add_feature (face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
      map = allocator.find_feature (HB_TAG ('l','t','r','m'));
      add_feature (face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
308 309
      break;
    case HB_DIRECTION_RTL:
B
Behdad Esfahbod 已提交
310 311 312 313
      map = allocator.find_feature (HB_TAG ('r','t','l','a'));
      add_feature (face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
      //map = allocator.find_feature (HB_TAG ('r','t','l','m'));
      add_feature (face, table_tag, map->index, MASK_RTLM, lookups, num_lookups, room_lookups);
314 315 316 317 318
      break;
    case HB_DIRECTION_TTB:
    case HB_DIRECTION_BTT:
    default:
      break;
B
Behdad Esfahbod 已提交
319 320
  }

321
  for (i = 0; i < ARRAY_LENGTH (default_features); i++)
B
Behdad Esfahbod 已提交
322
  {
B
Behdad Esfahbod 已提交
323 324
    map = allocator.find_feature (default_features[i]);
    add_feature (face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
325
  }
B
Behdad Esfahbod 已提交
326

327 328
  for (i = 0; i < num_features; i++)
  {
B
Behdad Esfahbod 已提交
329 330 331 332 333
    hb_feature_t *feature = &features[i];
    map = allocator.find_feature (feature->tag);
    add_feature (face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
    if (!(feature->start == 0 && feature->end == (unsigned int)-1))
      buffer->set_masks (features[i].value << map->shift, map->mask, feature->start, feature->end);
B
Behdad Esfahbod 已提交
334 335
  }

336

B
Behdad Esfahbod 已提交
337
  /* Sort lookups and merge duplicates */
B
Behdad Esfahbod 已提交
338

B
Behdad Esfahbod 已提交
339 340 341 342 343
  qsort (lookups, *num_lookups, sizeof (lookups[0]), cmp_lookups);

  if (*num_lookups)
  {
    for (i = 1, j = 0; i < *num_lookups; i++)
B
Behdad Esfahbod 已提交
344
      if (lookups[i].index != lookups[j].index)
B
Behdad Esfahbod 已提交
345
	lookups[++j] = lookups[i];
B
Behdad Esfahbod 已提交
346 347 348
      else
        lookups[j].mask |= lookups[i].mask;
    j++;
B
Behdad Esfahbod 已提交
349 350 351 352 353
    *num_lookups = j;
  }
}


354 355 356 357 358
static hb_bool_t
hb_ot_substitute_complex (hb_font_t    *font HB_UNUSED,
			  hb_face_t    *face,
			  hb_buffer_t  *buffer,
			  hb_feature_t *features,
359 360
			  unsigned int  num_features,
			  hb_direction_t original_direction)
B
Behdad Esfahbod 已提交
361
{
362
  lookup_map lookups[1000]; /* FIXME */
B
Behdad Esfahbod 已提交
363 364 365 366 367 368
  unsigned int num_lookups = ARRAY_LENGTH (lookups);
  unsigned int i;

  if (!hb_ot_layout_has_substitution (face))
    return FALSE;

B
Behdad Esfahbod 已提交
369
  setup_lookups (face, buffer, features, num_features,
B
Behdad Esfahbod 已提交
370
		 HB_OT_TAG_GSUB,
371 372
		 lookups, &num_lookups,
		 original_direction);
B
Behdad Esfahbod 已提交
373 374

  for (i = 0; i < num_lookups; i++)
B
Behdad Esfahbod 已提交
375
    hb_ot_layout_substitute_lookup (face, buffer, lookups[i].index, lookups[i].mask);
B
Behdad Esfahbod 已提交
376

377
  return TRUE;
B
Behdad Esfahbod 已提交
378 379
}

380 381 382 383 384
static hb_bool_t
hb_ot_position_complex (hb_font_t    *font,
			hb_face_t    *face,
			hb_buffer_t  *buffer,
			hb_feature_t *features,
385 386
			unsigned int  num_features,
			hb_direction_t original_direction)
B
Behdad Esfahbod 已提交
387
{
B
Behdad Esfahbod 已提交
388
  lookup_map lookups[1000];
B
Behdad Esfahbod 已提交
389 390 391 392 393 394
  unsigned int num_lookups = ARRAY_LENGTH (lookups);
  unsigned int i;

  if (!hb_ot_layout_has_positioning (face))
    return FALSE;

B
Behdad Esfahbod 已提交
395
  setup_lookups (face, buffer, features, num_features,
B
Behdad Esfahbod 已提交
396
		 HB_OT_TAG_GPOS,
397 398
		 lookups, &num_lookups,
		 original_direction);
B
Behdad Esfahbod 已提交
399 400

  for (i = 0; i < num_lookups; i++)
B
Behdad Esfahbod 已提交
401
    hb_ot_layout_position_lookup (font, face, buffer, lookups[i].index, lookups[i].mask);
B
Behdad Esfahbod 已提交
402 403 404

  hb_ot_layout_position_finish (font, face, buffer);

405
  return TRUE;
B
Behdad Esfahbod 已提交
406
}
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425


/* Main shaper */

/* Prepare */

static inline hb_bool_t
is_variation_selector (hb_codepoint_t unicode)
{
  return unlikely ((unicode >=  0x180B && unicode <=  0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
		   (unicode >=  0xFE00 && unicode <=  0xFE0F) || /* VARIATION SELECTOR-1..16 */
		   (unicode >= 0xE0100 && unicode <= 0xE01EF));  /* VARIATION SELECTOR-17..256 */
}

static void
hb_form_clusters (hb_buffer_t *buffer)
{
  unsigned int count = buffer->len;
  for (unsigned int i = 1; i < count; i++)
426
    if (buffer->unicode->v.get_general_category (buffer->info[i].codepoint) == HB_CATEGORY_NON_SPACING_MARK)
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
      buffer->info[i].cluster = buffer->info[i - 1].cluster;
}

static hb_direction_t
hb_ensure_native_direction (hb_buffer_t *buffer)
{
  hb_direction_t original_direction = buffer->direction;

  /* TODO vertical */
  if (HB_DIRECTION_IS_HORIZONTAL (original_direction) &&
      original_direction != _hb_script_get_horizontal_direction (buffer->script))
  {
    hb_buffer_reverse_clusters (buffer);
    buffer->direction = HB_DIRECTION_REVERSE (buffer->direction);
  }

  return original_direction;
}


/* Substitute */

static void
hb_mirror_chars (hb_buffer_t *buffer)
{
452
  hb_unicode_get_mirroring_func_t get_mirroring = buffer->unicode->v.get_mirroring;
453 454 455 456 457 458

  if (HB_DIRECTION_IS_FORWARD (buffer->direction))
    return;

  unsigned int count = buffer->len;
  for (unsigned int i = 0; i < count; i++) {
B
Behdad Esfahbod 已提交
459 460 461 462 463
    hb_codepoint_t codepoint = get_mirroring (buffer->info[i].codepoint);
    if (likely (codepoint == buffer->info[i].codepoint))
      buffer->info[i].mask |= MASK_RTLM;
    else
      buffer->info[i].codepoint = codepoint;
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
  }
}

static void
hb_map_glyphs (hb_font_t    *font,
	       hb_face_t    *face,
	       hb_buffer_t  *buffer)
{
  if (unlikely (!buffer->len))
    return;

  unsigned int count = buffer->len - 1;
  for (unsigned int i = 0; i < count; i++) {
    if (unlikely (is_variation_selector (buffer->info[i + 1].codepoint))) {
      buffer->info[i].codepoint = hb_font_get_glyph (font, face, buffer->info[i].codepoint, buffer->info[i + 1].codepoint);
      i++;
    } else {
      buffer->info[i].codepoint = hb_font_get_glyph (font, face, buffer->info[i].codepoint, 0);
    }
  }
  buffer->info[count].codepoint = hb_font_get_glyph (font, face, buffer->info[count].codepoint, 0);
}

static void
hb_substitute_default (hb_font_t    *font,
		       hb_face_t    *face,
		       hb_buffer_t  *buffer,
		       hb_feature_t *features HB_UNUSED,
		       unsigned int  num_features HB_UNUSED)
{
  hb_map_glyphs (font, face, buffer);
}

static void
B
Behdad Esfahbod 已提交
498 499 500 501 502
hb_substitute_complex_fallback (hb_font_t    *font HB_UNUSED,
				hb_face_t    *face HB_UNUSED,
				hb_buffer_t  *buffer HB_UNUSED,
				hb_feature_t *features HB_UNUSED,
				unsigned int  num_features HB_UNUSED)
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
{
  /* TODO Arabic */
}


/* Position */

static void
hb_position_default (hb_font_t    *font,
		     hb_face_t    *face,
		     hb_buffer_t  *buffer,
		     hb_feature_t *features HB_UNUSED,
		     unsigned int  num_features HB_UNUSED)
{
  hb_buffer_clear_positions (buffer);

  unsigned int count = buffer->len;
  for (unsigned int i = 0; i < count; i++) {
    hb_glyph_metrics_t metrics;
    hb_font_get_glyph_metrics (font, face, buffer->info[i].codepoint, &metrics);
    buffer->pos[i].x_advance = metrics.x_advance;
    buffer->pos[i].y_advance = metrics.y_advance;
  }
}

static void
B
Behdad Esfahbod 已提交
529 530 531 532 533
hb_position_complex_fallback (hb_font_t    *font HB_UNUSED,
			      hb_face_t    *face HB_UNUSED,
			      hb_buffer_t  *buffer HB_UNUSED,
			      hb_feature_t *features HB_UNUSED,
			      unsigned int  num_features HB_UNUSED)
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
{
  /* TODO Mark pos */
}

static void
hb_truetype_kern (hb_font_t    *font,
		  hb_face_t    *face,
		  hb_buffer_t  *buffer,
		  hb_feature_t *features HB_UNUSED,
		  unsigned int  num_features HB_UNUSED)
{
  /* TODO Check for kern=0 */
  unsigned int count = buffer->len;
  for (unsigned int i = 1; i < count; i++) {
    hb_position_t kern, kern1, kern2;
    kern = hb_font_get_kerning (font, face, buffer->info[i - 1].codepoint, buffer->info[i].codepoint);
    kern1 = kern >> 1;
    kern2 = kern - kern1;
    buffer->pos[i - 1].x_advance += kern1;
    buffer->pos[i].x_advance += kern2;
    buffer->pos[i].x_offset += kern2;
  }
}

static void
B
Behdad Esfahbod 已提交
559 560 561 562 563
hb_position_complex_fallback_visual (hb_font_t    *font,
				     hb_face_t    *face,
				     hb_buffer_t  *buffer,
				     hb_feature_t *features,
				     unsigned int  num_features)
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
{
  hb_truetype_kern (font, face, buffer, features, num_features);
}


/* Do it! */

void
hb_ot_shape (hb_font_t    *font,
	     hb_face_t    *face,
	     hb_buffer_t  *buffer,
	     hb_feature_t *features,
	     unsigned int  num_features)
{
  hb_direction_t original_direction;
  hb_bool_t substitute_fallback, position_fallback;

  hb_form_clusters (buffer);

B
Behdad Esfahbod 已提交
583
  /* SUBSTITUTE */
B
Minor  
Behdad Esfahbod 已提交
584
  {
B
Behdad Esfahbod 已提交
585

B
Minor  
Behdad Esfahbod 已提交
586
    buffer->clear_masks ();
B
Behdad Esfahbod 已提交
587

B
Minor  
Behdad Esfahbod 已提交
588 589
    /* Mirroring needs to see the original direction */
    hb_mirror_chars (buffer);
590

B
Minor  
Behdad Esfahbod 已提交
591
    original_direction = hb_ensure_native_direction (buffer);
592

B
Minor  
Behdad Esfahbod 已提交
593
    hb_substitute_default (font, face, buffer, features, num_features);
B
Behdad Esfahbod 已提交
594

B
Minor  
Behdad Esfahbod 已提交
595
    substitute_fallback = !hb_ot_substitute_complex (font, face, buffer, features, num_features, original_direction);
596

B
Minor  
Behdad Esfahbod 已提交
597 598
    if (substitute_fallback)
      hb_substitute_complex_fallback (font, face, buffer, features, num_features);
599

B
Minor  
Behdad Esfahbod 已提交
600
  }
B
Behdad Esfahbod 已提交
601 602

  /* POSITION */
B
Minor  
Behdad Esfahbod 已提交
603
  {
B
Behdad Esfahbod 已提交
604

B
Minor  
Behdad Esfahbod 已提交
605
    buffer->clear_masks ();
B
Behdad Esfahbod 已提交
606

B
Minor  
Behdad Esfahbod 已提交
607
    hb_position_default (font, face, buffer, features, num_features);
608

B
Minor  
Behdad Esfahbod 已提交
609
    position_fallback = !hb_ot_position_complex (font, face, buffer, features, num_features, original_direction);
610

B
Minor  
Behdad Esfahbod 已提交
611 612
    if (position_fallback)
      hb_position_complex_fallback (font, face, buffer, features, num_features);
613

B
Minor  
Behdad Esfahbod 已提交
614 615
    if (HB_DIRECTION_IS_BACKWARD (buffer->direction))
      hb_buffer_reverse (buffer);
616

B
Minor  
Behdad Esfahbod 已提交
617 618 619
    if (position_fallback)
      hb_position_complex_fallback_visual (font, face, buffer, features, num_features);
  }
620 621 622

  buffer->direction = original_direction;
}