hb-shape-plan.cc 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright © 2012  Google, Inc.
 *
 *  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.
 *
 * Google Author(s): Behdad Esfahbod
 */

27 28 29 30 31
#include "hb.hh"
#include "hb-shape-plan.hh"
#include "hb-shaper.hh"
#include "hb-font.hh"
#include "hb-buffer.hh"
32

B
Behdad Esfahbod 已提交
33

B
Behdad Esfahbod 已提交
34 35
/**
 * SECTION:hb-shape-plan
B
Behdad Esfahbod 已提交
36
 * @title: hb-shape-plan
B
Behdad Esfahbod 已提交
37 38 39 40 41 42 43 44 45 46
 * @short_description: Object representing a shaping plan
 * @include: hb.h
 *
 * Shape plans are not used for shaping directly, but can be access to query
 * certain information about how shaping will perform given a set of input
 * parameters (script, language, direction, features, etc.)
 * Most client would not need to deal with shape plans directly.
 **/


B
Behdad Esfahbod 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59
/*
 * hb_shape_plan_key_t
 */

bool
hb_shape_plan_key_t::init (bool                           copy,
			   hb_face_t                     *face,
			   const hb_segment_properties_t *props,
			   const hb_feature_t            *user_features,
			   unsigned int                   num_user_features,
			   const int                     *orig_coords,
			   unsigned int                   num_coords,
			   const char * const            *shaper_list)
B
Behdad Esfahbod 已提交
60
{
B
Behdad Esfahbod 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  hb_feature_t *features = nullptr;
  int *coords = nullptr;
  if (copy && num_user_features && !(features = (hb_feature_t *) calloc (num_user_features, sizeof (hb_feature_t))))
    goto bail;
  if (copy && num_coords && !(coords = (int *) calloc (num_coords, sizeof (int))))
    goto bail;

  this->props = *props;
  this->num_user_features = num_user_features;
  this->user_features = copy ? features : user_features;
  if (copy && num_user_features)
    memcpy (features, user_features, num_user_features * sizeof (hb_feature_t));
  this->num_coords = num_coords;
  this->coords = copy ? coords : orig_coords;
  if (copy && num_coords)
    memcpy (coords, orig_coords, num_coords * sizeof (int));
  this->shaper_func = nullptr;
  this->shaper_name = nullptr;

  /*
   * Choose shaper.
   */

B
Behdad Esfahbod 已提交
84 85
#define HB_SHAPER_PLAN(shaper) \
	HB_STMT_START { \
86
	  if (hb_##shaper##_shaper_face_data_ensure (face)) \
87
	  { \
B
Behdad Esfahbod 已提交
88 89 90
	    this->shaper_func = _hb_##shaper##_shape; \
	    this->shaper_name = #shaper; \
	    return true; \
B
Behdad Esfahbod 已提交
91 92 93
	  } \
	} HB_STMT_END

94 95
  if (unlikely (shaper_list))
  {
B
Behdad Esfahbod 已提交
96
    for (; *shaper_list; shaper_list++)
97
      if (false)
B
Behdad Esfahbod 已提交
98 99 100 101 102 103 104
	;
#define HB_SHAPER_IMPLEMENT(shaper) \
      else if (0 == strcmp (*shaper_list, #shaper)) \
	HB_SHAPER_PLAN (shaper);
#include "hb-shaper-list.hh"
#undef HB_SHAPER_IMPLEMENT
  }
B
Behdad Esfahbod 已提交
105 106 107 108 109 110
  else
  {
    const hb_shaper_pair_static_t *shapers = _hb_shapers_get ();
    for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++)
      if (false)
	;
111
#define HB_SHAPER_IMPLEMENT(shaper) \
B
Behdad Esfahbod 已提交
112 113
      else if (shapers[i].func == _hb_##shaper##_shape) \
	HB_SHAPER_PLAN (shaper);
114 115
#include "hb-shaper-list.hh"
#undef HB_SHAPER_IMPLEMENT
B
Behdad Esfahbod 已提交
116
  }
B
Behdad Esfahbod 已提交
117
#undef HB_SHAPER_PLAN
B
Behdad Esfahbod 已提交
118 119 120 121 122

bail:
  ::free (coords);
  ::free (features);
  return false;
B
Behdad Esfahbod 已提交
123 124
}

125 126 127 128 129

/*
 * hb_shape_plan_t
 */

130

131
/**
132
 * hb_shape_plan_create: (Xconstructor)
133 134 135 136 137 138 139 140 141 142
 * @face: 
 * @props: 
 * @user_features: (array length=num_user_features):
 * @num_user_features: 
 * @shaper_list: (array zero-terminated=1):
 *
 * 
 *
 * Return value: (transfer full):
 *
S
Sascha Brawer 已提交
143
 * Since: 0.9.7
144
 **/
145 146 147 148 149 150
hb_shape_plan_t *
hb_shape_plan_create (hb_face_t                     *face,
		      const hb_segment_properties_t *props,
		      const hb_feature_t            *user_features,
		      unsigned int                   num_user_features,
		      const char * const            *shaper_list)
B
Behdad Esfahbod 已提交
151 152 153
{
  return hb_shape_plan_create2 (face, props,
				user_features, num_user_features,
B
Behdad Esfahbod 已提交
154
				nullptr, 0,
B
Behdad Esfahbod 已提交
155 156 157 158 159 160 161 162
				shaper_list);
}

hb_shape_plan_t *
hb_shape_plan_create2 (hb_face_t                     *face,
		       const hb_segment_properties_t *props,
		       const hb_feature_t            *user_features,
		       unsigned int                   num_user_features,
B
Behdad Esfahbod 已提交
163
		       const int                     *coords,
B
Behdad Esfahbod 已提交
164 165
		       unsigned int                   num_coords,
		       const char * const            *shaper_list)
166
{
B
Behdad Esfahbod 已提交
167
  DEBUG_MSG_FUNC (SHAPE_PLAN, nullptr,
B
Behdad Esfahbod 已提交
168
		  "face=%p num_features=%d num_coords=%d shaper_list=%p",
169
		  face,
B
Behdad Esfahbod 已提交
170
		  num_user_features,
B
Behdad Esfahbod 已提交
171
		  num_coords,
B
Behdad Esfahbod 已提交
172 173
		  shaper_list);

174 175
  assert (props->direction != HB_DIRECTION_INVALID);

176 177
  hb_shape_plan_t *shape_plan;

B
Behdad Esfahbod 已提交
178
  if (unlikely (!props))
B
Minor  
Behdad Esfahbod 已提交
179
    goto bail;
B
Behdad Esfahbod 已提交
180
  if (!(shape_plan = hb_object_create<hb_shape_plan_t> ()))
B
Minor  
Behdad Esfahbod 已提交
181
    goto bail;
182

B
Minor  
Behdad Esfahbod 已提交
183 184
  if (unlikely (!face))
    face = hb_face_get_empty ();
185
  hb_face_make_immutable (face);
186
  shape_plan->face_unsafe = face;
187

B
Behdad Esfahbod 已提交
188 189 190 191 192 193 194 195 196
  if (unlikely (!shape_plan->key.init (true,
				       face,
				       props,
				       user_features,
				       num_user_features,
				       coords,
				       num_coords,
				       shaper_list)))
    goto bail2;
197
  if (unlikely (!shape_plan->ot.init0 (face, &shape_plan->key)))
B
Behdad Esfahbod 已提交
198
    goto bail3;
199

200
  return shape_plan;
B
Minor  
Behdad Esfahbod 已提交
201

B
Behdad Esfahbod 已提交
202 203 204 205
bail3:
  shape_plan->key.free ();
bail2:
  free (shape_plan);
B
Minor  
Behdad Esfahbod 已提交
206 207
bail:
  return hb_shape_plan_get_empty ();
208 209
}

210 211 212 213 214 215 216
/**
 * hb_shape_plan_get_empty:
 *
 * 
 *
 * Return value: (transfer full):
 *
S
Sascha Brawer 已提交
217
 * Since: 0.9.7
218
 **/
219 220 221
hb_shape_plan_t *
hb_shape_plan_get_empty (void)
{
222
  return const_cast<hb_shape_plan_t *> (&Null(hb_shape_plan_t));
223 224
}

225 226 227 228 229 230 231 232
/**
 * hb_shape_plan_reference: (skip)
 * @shape_plan: a shape plan.
 *
 * 
 *
 * Return value: (transfer full):
 *
S
Sascha Brawer 已提交
233
 * Since: 0.9.7
234
 **/
235 236 237 238 239 240
hb_shape_plan_t *
hb_shape_plan_reference (hb_shape_plan_t *shape_plan)
{
  return hb_object_reference (shape_plan);
}

241 242 243 244 245 246
/**
 * hb_shape_plan_destroy: (skip)
 * @shape_plan: a shape plan.
 *
 * 
 *
S
Sascha Brawer 已提交
247
 * Since: 0.9.7
248
 **/
249 250 251 252 253
void
hb_shape_plan_destroy (hb_shape_plan_t *shape_plan)
{
  if (!hb_object_destroy (shape_plan)) return;

254
  shape_plan->ot.fini ();
B
Behdad Esfahbod 已提交
255
  shape_plan->key.free ();
256 257
  free (shape_plan);
}
B
Behdad Esfahbod 已提交
258

259 260 261 262 263 264 265 266 267 268 269 270
/**
 * hb_shape_plan_set_user_data: (skip)
 * @shape_plan: a shape plan.
 * @key: 
 * @data: 
 * @destroy: 
 * @replace: 
 *
 * 
 *
 * Return value: 
 *
S
Sascha Brawer 已提交
271
 * Since: 0.9.7
272
 **/
273 274 275 276 277 278 279 280 281 282
hb_bool_t
hb_shape_plan_set_user_data (hb_shape_plan_t    *shape_plan,
			     hb_user_data_key_t *key,
			     void *              data,
			     hb_destroy_func_t   destroy,
			     hb_bool_t           replace)
{
  return hb_object_set_user_data (shape_plan, key, data, destroy, replace);
}

283 284 285 286 287 288 289 290 291
/**
 * hb_shape_plan_get_user_data: (skip)
 * @shape_plan: a shape plan.
 * @key: 
 *
 * 
 *
 * Return value: (transfer none):
 *
S
Sascha Brawer 已提交
292
 * Since: 0.9.7
293
 **/
294 295 296 297 298 299 300
void *
hb_shape_plan_get_user_data (hb_shape_plan_t    *shape_plan,
			     hb_user_data_key_t *key)
{
  return hb_object_get_user_data (shape_plan, key);
}

B
Behdad Esfahbod 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
/**
 * hb_shape_plan_get_shaper:
 * @shape_plan: a shape plan.
 *
 * 
 *
 * Return value: (transfer none):
 *
 * Since: 0.9.7
 **/
const char *
hb_shape_plan_get_shaper (hb_shape_plan_t *shape_plan)
{
  return shape_plan->key.shaper_name;
}

B
Behdad Esfahbod 已提交
317

318 319 320 321 322 323 324 325 326 327 328 329
/**
 * hb_shape_plan_execute:
 * @shape_plan: a shape plan.
 * @font: a font.
 * @buffer: a buffer.
 * @features: (array length=num_features):
 * @num_features: 
 *
 * 
 *
 * Return value: 
 *
S
Sascha Brawer 已提交
330
 * Since: 0.9.7
331
 **/
B
Behdad Esfahbod 已提交
332
hb_bool_t
333
hb_shape_plan_execute (hb_shape_plan_t    *shape_plan,
B
Behdad Esfahbod 已提交
334 335 336 337 338
		       hb_font_t          *font,
		       hb_buffer_t        *buffer,
		       const hb_feature_t *features,
		       unsigned int        num_features)
{
B
Behdad Esfahbod 已提交
339
  DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan,
340
		  "num_features=%d shaper_func=%p, shaper_name=%s",
B
Behdad Esfahbod 已提交
341
		  num_features,
342 343
		  shape_plan->key.shaper_func,
		  shape_plan->key.shaper_name);
B
Behdad Esfahbod 已提交
344

345 346 347
  if (unlikely (!buffer->len))
    return true;

348
  assert (!hb_object_is_immutable (buffer));
349 350 351
  assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE);

  if (unlikely (hb_object_is_inert (shape_plan)))
B
Behdad Esfahbod 已提交
352 353
    return false;

354
  assert (shape_plan->face_unsafe == font->face);
355
  assert (hb_segment_properties_equal (&shape_plan->key.props, &buffer->props));
356

B
Behdad Esfahbod 已提交
357 358
#define HB_SHAPER_EXECUTE(shaper) \
	HB_STMT_START { \
359
	  return hb_##shaper##_shaper_font_data_ensure (font) && \
360
		 _hb_##shaper##_shape (shape_plan, font, buffer, features, num_features); \
B
Behdad Esfahbod 已提交
361 362
	} HB_STMT_END

363
  if (false)
B
Behdad Esfahbod 已提交
364
    ;
B
Behdad Esfahbod 已提交
365
#define HB_SHAPER_IMPLEMENT(shaper) \
366
  else if (shape_plan->key.shaper_func == _hb_##shaper##_shape) \
B
Behdad Esfahbod 已提交
367
    HB_SHAPER_EXECUTE (shaper);
B
Behdad Esfahbod 已提交
368 369 370 371 372 373 374
#include "hb-shaper-list.hh"
#undef HB_SHAPER_IMPLEMENT

#undef HB_SHAPER_EXECUTE

  return false;
}
B
Behdad Esfahbod 已提交
375 376 377


/*
B
Behdad Esfahbod 已提交
378
 * Caching
B
Behdad Esfahbod 已提交
379 380
 */

381 382 383
static inline bool
hb_shape_plan_key_user_features_equal (const hb_shape_plan_key_t *key1,
				       const hb_shape_plan_key_t *key2)
384
{
385
  if (key1->num_user_features != key2->num_user_features)
B
Behdad Esfahbod 已提交
386
    return false;
387 388 389
  return 0 == hb_memcmp(key1->user_features,
			key2->user_features,
			key1->num_user_features * sizeof (key1->user_features[0]));
B
Behdad Esfahbod 已提交
390 391
}

392 393 394
static inline bool
hb_shape_plan_key_coords_equal (const hb_shape_plan_key_t *key2,
				const hb_shape_plan_key_t *key1)
B
Behdad Esfahbod 已提交
395
{
396
  if (key1->num_coords != key2->num_coords)
B
Behdad Esfahbod 已提交
397
    return false;
398 399 400
  return 0 == hb_memcmp(key1->coords,
			key2->coords,
			key1->num_coords * sizeof (key1->coords[0]));
401 402
}

403 404 405
static bool
hb_shape_plan_key_equal (const hb_shape_plan_key_t *key1,
			 const hb_shape_plan_key_t *key2)
B
Behdad Esfahbod 已提交
406
{
407 408 409 410
  return hb_segment_properties_equal (&key1->props, &key2->props) &&
	 hb_shape_plan_key_user_features_equal (key1, key2) &&
	 hb_shape_plan_key_coords_equal (key1, key2) &&
	 key1->shaper_func == key2->shaper_func;
B
Behdad Esfahbod 已提交
411 412
}

B
Behdad Esfahbod 已提交
413
static inline bool
B
Behdad Esfahbod 已提交
414 415
_has_non_global_user_features (const hb_feature_t *user_features,
			       unsigned int        num_user_features)
416
{
B
Behdad Esfahbod 已提交
417 418 419 420
  while (num_user_features)
  {
    if (user_features->start != HB_FEATURE_GLOBAL_START ||
	user_features->end   != HB_FEATURE_GLOBAL_END)
421
      return true;
422 423 424
    num_user_features--;
    user_features++;
  }
425 426 427
  return false;
}

B
Behdad Esfahbod 已提交
428
static inline bool
B
Behdad Esfahbod 已提交
429 430
_has_coords (const int    *coords,
	     unsigned int  num_coords)
B
Behdad Esfahbod 已提交
431
{
B
Behdad Esfahbod 已提交
432
  return num_coords;
B
Behdad Esfahbod 已提交
433 434 435
}

static inline bool
B
Behdad Esfahbod 已提交
436 437 438 439
_dont_cache (const hb_feature_t *user_features,
	     unsigned int        num_user_features,
	     const int          *coords,
	     unsigned int        num_coords)
B
Behdad Esfahbod 已提交
440
{
B
Behdad Esfahbod 已提交
441 442
  return _has_non_global_user_features (user_features, num_user_features) ||
	 _has_coords (coords, num_coords);
B
Behdad Esfahbod 已提交
443 444
}

445 446 447 448 449 450 451 452 453 454 455 456
/**
 * hb_shape_plan_create_cached:
 * @face: 
 * @props: 
 * @user_features: (array length=num_user_features):
 * @num_user_features: 
 * @shaper_list: (array zero-terminated=1):
 *
 * 
 *
 * Return value: (transfer full):
 *
S
Sascha Brawer 已提交
457
 * Since: 0.9.7
458
 **/
B
Behdad Esfahbod 已提交
459 460 461 462 463 464
hb_shape_plan_t *
hb_shape_plan_create_cached (hb_face_t                     *face,
			     const hb_segment_properties_t *props,
			     const hb_feature_t            *user_features,
			     unsigned int                   num_user_features,
			     const char * const            *shaper_list)
B
Behdad Esfahbod 已提交
465 466 467
{
  return hb_shape_plan_create_cached2 (face, props,
				       user_features, num_user_features,
B
Behdad Esfahbod 已提交
468
				       nullptr, 0,
B
Behdad Esfahbod 已提交
469 470 471 472 473 474 475 476 477 478 479
				       shaper_list);
}

hb_shape_plan_t *
hb_shape_plan_create_cached2 (hb_face_t                     *face,
			      const hb_segment_properties_t *props,
			      const hb_feature_t            *user_features,
			      unsigned int                   num_user_features,
			      const int                     *coords,
			      unsigned int                   num_coords,
			      const char * const            *shaper_list)
B
Behdad Esfahbod 已提交
480
{
B
Behdad Esfahbod 已提交
481
  DEBUG_MSG_FUNC (SHAPE_PLAN, nullptr,
482 483
		  "face=%p num_features=%d shaper_list=%p",
		  face,
B
Behdad Esfahbod 已提交
484 485 486
		  num_user_features,
		  shaper_list);

B
Behdad Esfahbod 已提交
487
retry:
488
  hb_face_t::plan_node_t *cached_plan_nodes = face->shape_plans;
B
Behdad Esfahbod 已提交
489

B
Behdad Esfahbod 已提交
490 491
  bool dont_cache = _dont_cache (user_features, num_user_features,
				 coords, num_coords) ||
B
Behdad Esfahbod 已提交
492 493 494
		    hb_object_is_inert (face);

  if (!dont_cache)
B
Behdad Esfahbod 已提交
495 496 497 498 499 500 501 502 503 504 505 506
  {
    hb_shape_plan_key_t key;
    if (!key.init (false,
		   face,
		   props,
		   user_features,
		   num_user_features,
		   coords,
		   num_coords,
		   shaper_list))
      return hb_shape_plan_get_empty ();

507
    for (hb_face_t::plan_node_t *node = cached_plan_nodes; node; node = node->next)
508
      if (hb_shape_plan_key_equal (&node->shape_plan->key, &key))
509 510 511 512
      {
        DEBUG_MSG_FUNC (SHAPE_PLAN, node->shape_plan, "fulfilled from cache");
        return hb_shape_plan_reference (node->shape_plan);
      }
B
Behdad Esfahbod 已提交
513
  }
B
Behdad Esfahbod 已提交
514

B
Behdad Esfahbod 已提交
515 516 517 518
  hb_shape_plan_t *shape_plan = hb_shape_plan_create2 (face, props,
						       user_features, num_user_features,
						       coords, num_coords,
						       shaper_list);
B
Behdad Esfahbod 已提交
519

B
Behdad Esfahbod 已提交
520
  if (dont_cache)
B
Behdad Esfahbod 已提交
521
    return shape_plan;
522

B
Behdad Esfahbod 已提交
523 524 525 526 527 528 529
  hb_face_t::plan_node_t *node = (hb_face_t::plan_node_t *) calloc (1, sizeof (hb_face_t::plan_node_t));
  if (unlikely (!node))
    return shape_plan;

  node->shape_plan = shape_plan;
  node->next = cached_plan_nodes;

530 531
  if (unlikely (!face->shape_plans.cmpexch (cached_plan_nodes, node)))
  {
B
Behdad Esfahbod 已提交
532 533 534 535
    hb_shape_plan_destroy (shape_plan);
    free (node);
    goto retry;
  }
536
  DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan, "inserted into cache");
B
Behdad Esfahbod 已提交
537 538 539

  return hb_shape_plan_reference (shape_plan);
}