hb-ot-shape.cc 11.9 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 33 34 35 36 37 38 39 40 41 42 43 44

#include "hb-ot-layout.h"

hb_tag_t default_features[] = {
  /* GSUB */
  HB_TAG('c','c','m','p'),
  HB_TAG('l','o','c','l'),
  HB_TAG('l','i','g','a'),
  HB_TAG('c','l','i','g'),
  /* GPOS */
  HB_TAG('k','e','r','n'),
  HB_TAG('m','a','r','k'),
  HB_TAG('m','k','m','k'),
};

B
Behdad Esfahbod 已提交
45 46 47 48 49
struct lookup_map {
  unsigned int index;
  hb_mask_t mask;
};

B
Behdad Esfahbod 已提交
50 51 52 53 54

static void
add_feature (hb_face_t    *face,
	     hb_tag_t      table_tag,
	     unsigned int  feature_index,
B
Behdad Esfahbod 已提交
55 56
	     hb_mask_t     mask,
	     lookup_map   *lookups,
B
Behdad Esfahbod 已提交
57 58 59 60
	     unsigned int *num_lookups,
	     unsigned int  room_lookups)
{
  unsigned int i = room_lookups - *num_lookups;
B
Behdad Esfahbod 已提交
61 62 63 64
  lookups += *num_lookups;

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

B
Behdad Esfahbod 已提交
65 66
  hb_ot_layout_feature_get_lookup_indexes (face, table_tag, feature_index, 0,
					   &i,
B
Behdad Esfahbod 已提交
67 68
					   lookup_indices);

B
Behdad Esfahbod 已提交
69
  *num_lookups += i;
B
Behdad Esfahbod 已提交
70 71 72 73 74

  while (i--) {
    lookups[i].mask = mask;
    lookups[i].index = lookup_indices[i];
  }
B
Behdad Esfahbod 已提交
75 76 77 78 79
}

static int
cmp_lookups (const void *p1, const void *p2)
{
B
Behdad Esfahbod 已提交
80 81
  const lookup_map *a = (const lookup_map *) p1;
  const lookup_map *b = (const lookup_map *) p2;
B
Behdad Esfahbod 已提交
82

B
Behdad Esfahbod 已提交
83
  return a->index - b->index;
B
Behdad Esfahbod 已提交
84 85 86
}

static void
B
Behdad Esfahbod 已提交
87
setup_lookups (hb_face_t    *face,
B
Behdad Esfahbod 已提交
88 89 90 91
	       hb_buffer_t  *buffer,
	       hb_feature_t *features,
	       unsigned int  num_features,
	       hb_tag_t      table_tag,
B
Behdad Esfahbod 已提交
92
	       lookup_map   *lookups,
B
Behdad Esfahbod 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	       unsigned int *num_lookups)
{
  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 已提交
109
    add_feature (face, table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
B
Behdad Esfahbod 已提交
110 111 112 113 114 115

  for (i = 0; i < ARRAY_LENGTH (default_features); i++)
  {
    if (hb_ot_layout_language_find_feature (face, table_tag, script_index, language_index,
					    default_features[i],
					    &feature_index))
B
Behdad Esfahbod 已提交
116
      add_feature (face, table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
B
Behdad Esfahbod 已提交
117 118
  }

B
Behdad Esfahbod 已提交
119 120 121 122 123 124
  /* Clear buffer masks. */
  unsigned int count = buffer->len;
  for (unsigned int i = 0; i < count; i++)
    buffer->info[i].mask = 1;

  unsigned int last_bit_used = 1;
B
Behdad Esfahbod 已提交
125
  unsigned int global_values = 0;
B
Behdad Esfahbod 已提交
126 127
  for (i = 0; i < num_features; i++)
  {
128 129 130 131 132
    if (!hb_ot_layout_language_find_feature (face, table_tag, script_index, language_index,
					     features[i].tag,
					     &feature_index))
      continue;

B
Behdad Esfahbod 已提交
133 134 135 136 137 138 139
    unsigned int bits_needed = _hb_bit_storage (features[i].value);
    if (!bits_needed)
      continue;
    unsigned int mask = (1 << (last_bit_used + bits_needed)) - (1 << last_bit_used);
    unsigned int value = features[i].value << last_bit_used;
    last_bit_used += bits_needed;

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

B
Behdad Esfahbod 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    if (features[i].start == 0 && features[i].end == (unsigned int)-1)
      global_values |= value;
    else
    {
      unsigned int start = features[i].start, end = features[i].end;
      unsigned int a = 0, b = buffer->len;
      while (a < b)
      {
        unsigned int h = a + ((b - a) / 2);
        if (buffer->info[h].cluster < start)
          a = h + 1;
        else
          b = h;
      }
      unsigned int count = buffer->len;
      for (unsigned int j = a; j < count && buffer->info[j].cluster < end; j++)
        buffer->info[j].mask |= value;
B
Behdad Esfahbod 已提交
159
    }
B
Behdad Esfahbod 已提交
160 161
  }

B
Behdad Esfahbod 已提交
162 163 164 165 166 167 168
  if (global_values)
  {
    unsigned int count = buffer->len;
    for (unsigned int j = 0; j < count; j++)
      buffer->info[j].mask |= global_values;
  }

B
Behdad Esfahbod 已提交
169 170 171 172 173
  qsort (lookups, *num_lookups, sizeof (lookups[0]), cmp_lookups);

  if (*num_lookups)
  {
    for (i = 1, j = 0; i < *num_lookups; i++)
B
Behdad Esfahbod 已提交
174
      if (lookups[i].index != lookups[j].index)
B
Behdad Esfahbod 已提交
175
	lookups[++j] = lookups[i];
B
Behdad Esfahbod 已提交
176 177 178
      else
        lookups[j].mask |= lookups[i].mask;
    j++;
B
Behdad Esfahbod 已提交
179 180 181 182 183
    *num_lookups = j;
  }
}


184 185 186 187 188 189
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,
			  unsigned int  num_features)
B
Behdad Esfahbod 已提交
190
{
B
Behdad Esfahbod 已提交
191
  lookup_map lookups[1000];
B
Behdad Esfahbod 已提交
192 193 194 195 196 197
  unsigned int num_lookups = ARRAY_LENGTH (lookups);
  unsigned int i;

  if (!hb_ot_layout_has_substitution (face))
    return FALSE;

B
Behdad Esfahbod 已提交
198
  setup_lookups (face, buffer, features, num_features,
B
Behdad Esfahbod 已提交
199 200 201 202
		 HB_OT_TAG_GSUB,
		 lookups, &num_lookups);

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

205
  return TRUE;
B
Behdad Esfahbod 已提交
206 207
}

208 209 210 211 212 213
static hb_bool_t
hb_ot_position_complex (hb_font_t    *font,
			hb_face_t    *face,
			hb_buffer_t  *buffer,
			hb_feature_t *features,
			unsigned int  num_features)
B
Behdad Esfahbod 已提交
214
{
B
Behdad Esfahbod 已提交
215
  lookup_map lookups[1000];
B
Behdad Esfahbod 已提交
216 217 218 219 220 221
  unsigned int num_lookups = ARRAY_LENGTH (lookups);
  unsigned int i;

  if (!hb_ot_layout_has_positioning (face))
    return FALSE;

B
Behdad Esfahbod 已提交
222
  setup_lookups (face, buffer, features, num_features,
B
Behdad Esfahbod 已提交
223 224 225 226
		 HB_OT_TAG_GPOS,
		 lookups, &num_lookups);

  for (i = 0; i < num_lookups; i++)
B
Behdad Esfahbod 已提交
227
    hb_ot_layout_position_lookup (font, face, buffer, lookups[i].index, lookups[i].mask);
B
Behdad Esfahbod 已提交
228 229 230

  hb_ot_layout_position_finish (font, face, buffer);

231
  return TRUE;
B
Behdad Esfahbod 已提交
232
}
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 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 319 320 321 322 323 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 358 359 360 361 362 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 389 390 391 392 393 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 419 420 421 422 423 424 425 426 427 428 429 430 431


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

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_mirror_chars (buffer);
  hb_map_glyphs (font, face, buffer);
}

static void
hb_substitute_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)
{
  /* 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
hb_position_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)
{
  /* 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
hb_position_fallback_visual (hb_font_t    *font,
			     hb_face_t    *face,
			     hb_buffer_t  *buffer,
			     hb_feature_t *features,
			     unsigned int  num_features)
{
  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);

  hb_substitute_default (font, face, buffer, features, num_features);

  /* We do this after substitute_default because mirroring needs to
   * see the original direction. */
  original_direction = hb_ensure_native_direction (buffer);

  substitute_fallback = !hb_ot_substitute_complex (font, face, buffer, features, num_features);

  if (substitute_fallback)
    hb_substitute_fallback (font, face, buffer, features, num_features);

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

  position_fallback = !hb_ot_position_complex (font, face, buffer, features, num_features);

  if (position_fallback)
    hb_position_fallback (font, face, buffer, features, num_features);

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

  if (position_fallback)
    hb_position_fallback_visual (font, face, buffer, features, num_features);

  buffer->direction = original_direction;
}