hb-ot-shape.cc 13.3 KB
Newer Older
B
Behdad Esfahbod 已提交
1 2 3
/*
 * Copyright (C) 2009  Red Hat, Inc.
 *
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 31 32

#include "hb-ot-layout.h"

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

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

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

B
Behdad Esfahbod 已提交
59 60 61 62 63

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

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

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

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

  while (i--) {
    lookups[i].mask = mask;
    lookups[i].index = lookup_indices[i];
  }
B
Behdad Esfahbod 已提交
84 85
}

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
static hb_bool_t
maybe_add_feature (hb_face_t    *face,
		   hb_tag_t      table_tag,
		   unsigned int  script_index,
		   unsigned int  language_index,
		   hb_tag_t      feature_tag,
		   hb_mask_t     mask,
		   lookup_map   *lookups,
		   unsigned int *num_lookups,
		   unsigned int  room_lookups)
{
  unsigned int feature_index;
  if (hb_ot_layout_language_find_feature (face, table_tag, script_index, language_index,
					  feature_tag,
					  &feature_index))
  {
    add_feature (face, table_tag, feature_index, mask, lookups, num_lookups, room_lookups);
    return TRUE;
  }
  return FALSE;
}

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

B
Behdad Esfahbod 已提交
114
  return a->index - b->index;
B
Behdad Esfahbod 已提交
115 116 117
}

static void
B
Behdad Esfahbod 已提交
118
setup_lookups (hb_face_t    *face,
B
Behdad Esfahbod 已提交
119 120 121 122
	       hb_buffer_t  *buffer,
	       hb_feature_t *features,
	       unsigned int  num_features,
	       hb_tag_t      table_tag,
B
Behdad Esfahbod 已提交
123
	       lookup_map   *lookups,
124 125
	       unsigned int *num_lookups,
	       hb_direction_t original_direction)
B
Behdad Esfahbod 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
{
  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 已提交
141
    add_feature (face, table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
B
Behdad Esfahbod 已提交
142 143

  for (i = 0; i < ARRAY_LENGTH (default_features); i++)
144 145 146 147 148 149 150 151 152 153 154 155 156 157
    maybe_add_feature (face, table_tag, script_index, language_index, default_features[i], 1, lookups, num_lookups, room_lookups);

  switch (original_direction) {
    case HB_DIRECTION_LTR:
      maybe_add_feature (face, table_tag, script_index, language_index, HB_TAG ('l','t','r','a'), 1, lookups, num_lookups, room_lookups);
      maybe_add_feature (face, table_tag, script_index, language_index, HB_TAG ('l','t','r','m'), 1, lookups, num_lookups, room_lookups);
      break;
    case HB_DIRECTION_RTL:
      maybe_add_feature (face, table_tag, script_index, language_index, HB_TAG ('r','t','l','a'), 1, lookups, num_lookups, room_lookups);
      break;
    case HB_DIRECTION_TTB:
    case HB_DIRECTION_BTT:
    default:
      break;
B
Behdad Esfahbod 已提交
158 159
  }

B
Behdad Esfahbod 已提交
160
  unsigned int next_bit = MASK_BITS_USED;
B
Behdad Esfahbod 已提交
161
  hb_mask_t global_mask = 0;
B
Behdad Esfahbod 已提交
162 163
  for (i = 0; i < num_features; i++)
  {
B
Behdad Esfahbod 已提交
164
    hb_feature_t *feature = &features[i];
165
    if (!hb_ot_layout_language_find_feature (face, table_tag, script_index, language_index,
B
Behdad Esfahbod 已提交
166
					     feature->tag,
167 168 169
					     &feature_index))
      continue;

B
Behdad Esfahbod 已提交
170 171
    if (feature->value == 1 && feature->start == 0 && feature->end == (unsigned int) -1) {
      add_feature (face, table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
B
Behdad Esfahbod 已提交
172
      continue;
B
Behdad Esfahbod 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186
    }

    /* Allocate bits for the features */

    unsigned int bits_needed = _hb_bit_storage (feature->value);
    if (!bits_needed)
      continue; /* Feature disabled */

    if (next_bit + bits_needed > 8 * sizeof (hb_mask_t))
      continue; /* Oh well... */

    unsigned int mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
    unsigned int value = feature->value << next_bit;
    next_bit += bits_needed;
B
Behdad Esfahbod 已提交
187

188
    add_feature (face, table_tag, feature_index, mask, lookups, num_lookups, room_lookups);
B
Behdad Esfahbod 已提交
189

B
Behdad Esfahbod 已提交
190 191
    if (feature->start == 0 && feature->end == (unsigned int) -1)
      global_mask |= value;
B
Behdad Esfahbod 已提交
192
    else
B
Behdad Esfahbod 已提交
193
      buffer->or_masks (mask, feature->start, feature->end);
B
Behdad Esfahbod 已提交
194 195
  }

B
Behdad Esfahbod 已提交
196 197
  if (global_mask)
    buffer->or_masks (global_mask, 0, (unsigned int) -1);
B
Behdad Esfahbod 已提交
198

B
Behdad Esfahbod 已提交
199 200 201 202 203
  qsort (lookups, *num_lookups, sizeof (lookups[0]), cmp_lookups);

  if (*num_lookups)
  {
    for (i = 1, j = 0; i < *num_lookups; i++)
B
Behdad Esfahbod 已提交
204
      if (lookups[i].index != lookups[j].index)
B
Behdad Esfahbod 已提交
205
	lookups[++j] = lookups[i];
B
Behdad Esfahbod 已提交
206 207 208
      else
        lookups[j].mask |= lookups[i].mask;
    j++;
B
Behdad Esfahbod 已提交
209 210 211 212 213
    *num_lookups = j;
  }
}


214 215 216 217 218
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,
219 220
			  unsigned int  num_features,
			  hb_direction_t original_direction)
B
Behdad Esfahbod 已提交
221
{
B
Behdad Esfahbod 已提交
222
  lookup_map lookups[1000];
B
Behdad Esfahbod 已提交
223 224 225 226 227 228
  unsigned int num_lookups = ARRAY_LENGTH (lookups);
  unsigned int i;

  if (!hb_ot_layout_has_substitution (face))
    return FALSE;

B
Behdad Esfahbod 已提交
229
  setup_lookups (face, buffer, features, num_features,
B
Behdad Esfahbod 已提交
230
		 HB_OT_TAG_GSUB,
231 232
		 lookups, &num_lookups,
		 original_direction);
B
Behdad Esfahbod 已提交
233 234

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

237
  return TRUE;
B
Behdad Esfahbod 已提交
238 239
}

240 241 242 243 244
static hb_bool_t
hb_ot_position_complex (hb_font_t    *font,
			hb_face_t    *face,
			hb_buffer_t  *buffer,
			hb_feature_t *features,
245 246
			unsigned int  num_features,
			hb_direction_t original_direction)
B
Behdad Esfahbod 已提交
247
{
B
Behdad Esfahbod 已提交
248
  lookup_map lookups[1000];
B
Behdad Esfahbod 已提交
249 250 251 252 253 254
  unsigned int num_lookups = ARRAY_LENGTH (lookups);
  unsigned int i;

  if (!hb_ot_layout_has_positioning (face))
    return FALSE;

B
Behdad Esfahbod 已提交
255
  setup_lookups (face, buffer, features, num_features,
B
Behdad Esfahbod 已提交
256
		 HB_OT_TAG_GPOS,
257 258
		 lookups, &num_lookups,
		 original_direction);
B
Behdad Esfahbod 已提交
259 260

  for (i = 0; i < num_lookups; i++)
B
Behdad Esfahbod 已提交
261
    hb_ot_layout_position_lookup (font, face, buffer, lookups[i].index, lookups[i].mask);
B
Behdad Esfahbod 已提交
262 263 264

  hb_ot_layout_position_finish (font, face, buffer);

265
  return TRUE;
B
Behdad Esfahbod 已提交
266
}
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318


/* 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++)
    if (buffer->unicode->get_general_category (buffer->info[i].codepoint) == HB_CATEGORY_NON_SPACING_MARK)
      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)
{
  hb_unicode_get_mirroring_func_t get_mirroring = buffer->unicode->get_mirroring;

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

  unsigned int count = buffer->len;
  for (unsigned int i = 0; i < count; i++) {
B
Behdad Esfahbod 已提交
319 320 321 322 323
    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;
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
  }
}

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 已提交
358 359 360 361 362
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)
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
{
  /* 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 已提交
389 390 391 392 393
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)
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
{
  /* 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 已提交
419 420 421 422 423
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)
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
{
  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 已提交
443 444 445 446
  /* SUBSTITUTE */

  buffer->clear_masks ();

B
Behdad Esfahbod 已提交
447 448
  /* Mirroring needs to see the original direction */
  hb_mirror_chars (buffer);
449 450 451

  original_direction = hb_ensure_native_direction (buffer);

B
Behdad Esfahbod 已提交
452 453
  hb_substitute_default (font, face, buffer, features, num_features);

454
  substitute_fallback = !hb_ot_substitute_complex (font, face, buffer, features, num_features, original_direction);
455 456

  if (substitute_fallback)
B
Behdad Esfahbod 已提交
457
    hb_substitute_complex_fallback (font, face, buffer, features, num_features);
458

B
Behdad Esfahbod 已提交
459 460 461 462 463

  /* POSITION */

  buffer->clear_masks ();

464 465
  hb_position_default (font, face, buffer, features, num_features);

466
  position_fallback = !hb_ot_position_complex (font, face, buffer, features, num_features, original_direction);
467 468

  if (position_fallback)
B
Behdad Esfahbod 已提交
469
    hb_position_complex_fallback (font, face, buffer, features, num_features);
470 471 472 473 474

  if (HB_DIRECTION_IS_BACKWARD (buffer->direction))
    hb_buffer_reverse (buffer);

  if (position_fallback)
B
Behdad Esfahbod 已提交
475
    hb_position_complex_fallback_visual (font, face, buffer, features, num_features);
476 477 478

  buffer->direction = original_direction;
}