hb-shape-plan.cc 12.4 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
/*
 * 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,
57
			   const int                     *coords,
B
Behdad Esfahbod 已提交
58 59
			   unsigned int                   num_coords,
			   const char * const            *shaper_list)
B
Behdad Esfahbod 已提交
60
{
B
Behdad Esfahbod 已提交
61 62 63 64 65 66 67 68
  hb_feature_t *features = nullptr;
  if (copy && num_user_features && !(features = (hb_feature_t *) calloc (num_user_features, sizeof (hb_feature_t))))
    goto bail;

  this->props = *props;
  this->num_user_features = num_user_features;
  this->user_features = copy ? features : user_features;
  if (copy && num_user_features)
69
  {
B
Behdad Esfahbod 已提交
70
    memcpy (features, user_features, num_user_features * sizeof (hb_feature_t));
71 72 73 74 75 76 77 78 79
    /* Make start/end uniform to easier catch bugs. */
    for (unsigned int i = 0; i < num_user_features; i++)
    {
      if (features[0].start != HB_FEATURE_GLOBAL_START)
	features[0].start = 1;
      if (features[0].end   != HB_FEATURE_GLOBAL_END)
	features[0].end   = 2;
    }
  }
B
Behdad Esfahbod 已提交
80 81
  this->shaper_func = nullptr;
  this->shaper_name = nullptr;
82
#ifndef HB_NO_OT_SHAPE
83
  this->ot.init (face, coords, num_coords);
84
#endif
B
Behdad Esfahbod 已提交
85 86 87 88 89

  /*
   * Choose shaper.
   */

B
Behdad Esfahbod 已提交
90 91
#define HB_SHAPER_PLAN(shaper) \
	HB_STMT_START { \
92
	  if (face->data.shaper) \
93
	  { \
B
Behdad Esfahbod 已提交
94 95 96
	    this->shaper_func = _hb_##shaper##_shape; \
	    this->shaper_name = #shaper; \
	    return true; \
B
Behdad Esfahbod 已提交
97 98 99
	  } \
	} HB_STMT_END

100 101
  if (unlikely (shaper_list))
  {
B
Behdad Esfahbod 已提交
102
    for (; *shaper_list; shaper_list++)
103
      if (false)
B
Behdad Esfahbod 已提交
104 105 106 107 108 109 110
	;
#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 已提交
111 112
  else
  {
B
Behdad Esfahbod 已提交
113
    const hb_shaper_entry_t *shapers = _hb_shapers_get ();
B
Behdad Esfahbod 已提交
114 115 116
    for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++)
      if (false)
	;
117
#define HB_SHAPER_IMPLEMENT(shaper) \
B
Behdad Esfahbod 已提交
118 119
      else if (shapers[i].func == _hb_##shaper##_shape) \
	HB_SHAPER_PLAN (shaper);
120 121
#include "hb-shaper-list.hh"
#undef HB_SHAPER_IMPLEMENT
B
Behdad Esfahbod 已提交
122
  }
B
Behdad Esfahbod 已提交
123
#undef HB_SHAPER_PLAN
B
Behdad Esfahbod 已提交
124 125 126 127

bail:
  ::free (features);
  return false;
B
Behdad Esfahbod 已提交
128 129
}

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
bool
hb_shape_plan_key_t::user_features_match (const hb_shape_plan_key_t *other)
{
  if (this->num_user_features != other->num_user_features)
    return false;
  for (unsigned int i = 0; i < num_user_features; i++)
  {
    if (this->user_features[i].tag   != other->user_features[i].tag   ||
	this->user_features[i].value != other->user_features[i].value ||
	(this->user_features[i].start == HB_FEATURE_GLOBAL_START &&
	 this->user_features[i].end   == HB_FEATURE_GLOBAL_END) !=
	(other->user_features[i].start == HB_FEATURE_GLOBAL_START &&
	 other->user_features[i].end   == HB_FEATURE_GLOBAL_END))
      return false;
  }
  return true;
}

bool
hb_shape_plan_key_t::equal (const hb_shape_plan_key_t *other)
{
  return hb_segment_properties_equal (&this->props, &other->props) &&
	 this->user_features_match (other) &&
153
#ifndef HB_NO_OT_SHAPE
154
	 this->ot.equal (&other->ot) &&
155
#endif
156 157 158
	 this->shaper_func == other->shaper_func;
}

159 160 161 162 163

/*
 * hb_shape_plan_t
 */

164

165
/**
166
 * hb_shape_plan_create: (Xconstructor)
167 168 169 170 171 172 173 174 175 176
 * @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 已提交
177
 * Since: 0.9.7
178
 **/
179 180 181 182 183 184
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 已提交
185 186 187
{
  return hb_shape_plan_create2 (face, props,
				user_features, num_user_features,
B
Behdad Esfahbod 已提交
188
				nullptr, 0,
B
Behdad Esfahbod 已提交
189 190 191 192 193 194 195 196
				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 已提交
197
		       const int                     *coords,
B
Behdad Esfahbod 已提交
198 199
		       unsigned int                   num_coords,
		       const char * const            *shaper_list)
200
{
B
Behdad Esfahbod 已提交
201
  DEBUG_MSG_FUNC (SHAPE_PLAN, nullptr,
B
Behdad Esfahbod 已提交
202
		  "face=%p num_features=%d num_coords=%d shaper_list=%p",
203
		  face,
B
Behdad Esfahbod 已提交
204
		  num_user_features,
B
Behdad Esfahbod 已提交
205
		  num_coords,
B
Behdad Esfahbod 已提交
206 207
		  shaper_list);

208 209
  assert (props->direction != HB_DIRECTION_INVALID);

210 211
  hb_shape_plan_t *shape_plan;

B
Behdad Esfahbod 已提交
212
  if (unlikely (!props))
B
Minor  
Behdad Esfahbod 已提交
213
    goto bail;
B
Behdad Esfahbod 已提交
214
  if (!(shape_plan = hb_object_create<hb_shape_plan_t> ()))
B
Minor  
Behdad Esfahbod 已提交
215
    goto bail;
216

B
Minor  
Behdad Esfahbod 已提交
217 218
  if (unlikely (!face))
    face = hb_face_get_empty ();
219
  hb_face_make_immutable (face);
220
  shape_plan->face_unsafe = face;
221

B
Behdad Esfahbod 已提交
222 223 224 225 226 227 228 229 230
  if (unlikely (!shape_plan->key.init (true,
				       face,
				       props,
				       user_features,
				       num_user_features,
				       coords,
				       num_coords,
				       shaper_list)))
    goto bail2;
231
#ifndef HB_NO_OT_SHAPE
232
  if (unlikely (!shape_plan->ot.init0 (face, &shape_plan->key)))
B
Behdad Esfahbod 已提交
233
    goto bail3;
234
#endif
235

236
  return shape_plan;
B
Minor  
Behdad Esfahbod 已提交
237

238
#ifndef HB_NO_OT_SHAPE
B
Behdad Esfahbod 已提交
239
bail3:
240
#endif
B
Behdad Esfahbod 已提交
241 242 243
  shape_plan->key.free ();
bail2:
  free (shape_plan);
B
Minor  
Behdad Esfahbod 已提交
244 245
bail:
  return hb_shape_plan_get_empty ();
246 247
}

248 249 250 251 252 253 254
/**
 * hb_shape_plan_get_empty:
 *
 * 
 *
 * Return value: (transfer full):
 *
S
Sascha Brawer 已提交
255
 * Since: 0.9.7
256
 **/
257
hb_shape_plan_t *
258
hb_shape_plan_get_empty ()
259
{
260
  return const_cast<hb_shape_plan_t *> (&Null(hb_shape_plan_t));
261 262
}

263 264 265 266 267 268 269 270
/**
 * hb_shape_plan_reference: (skip)
 * @shape_plan: a shape plan.
 *
 * 
 *
 * Return value: (transfer full):
 *
S
Sascha Brawer 已提交
271
 * Since: 0.9.7
272
 **/
273 274 275 276 277 278
hb_shape_plan_t *
hb_shape_plan_reference (hb_shape_plan_t *shape_plan)
{
  return hb_object_reference (shape_plan);
}

279 280 281 282 283 284
/**
 * hb_shape_plan_destroy: (skip)
 * @shape_plan: a shape plan.
 *
 * 
 *
S
Sascha Brawer 已提交
285
 * Since: 0.9.7
286
 **/
287 288 289 290 291
void
hb_shape_plan_destroy (hb_shape_plan_t *shape_plan)
{
  if (!hb_object_destroy (shape_plan)) return;

292
#ifndef HB_NO_OT_SHAPE
293
  shape_plan->ot.fini ();
294
#endif
B
Behdad Esfahbod 已提交
295
  shape_plan->key.free ();
296 297
  free (shape_plan);
}
B
Behdad Esfahbod 已提交
298

299 300 301 302 303 304 305 306 307 308 309 310
/**
 * hb_shape_plan_set_user_data: (skip)
 * @shape_plan: a shape plan.
 * @key: 
 * @data: 
 * @destroy: 
 * @replace: 
 *
 * 
 *
 * Return value: 
 *
S
Sascha Brawer 已提交
311
 * Since: 0.9.7
312
 **/
313 314 315 316 317 318 319 320 321 322
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);
}

323 324 325 326 327 328 329 330 331
/**
 * hb_shape_plan_get_user_data: (skip)
 * @shape_plan: a shape plan.
 * @key: 
 *
 * 
 *
 * Return value: (transfer none):
 *
S
Sascha Brawer 已提交
332
 * Since: 0.9.7
333
 **/
334 335 336 337 338 339 340
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 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
/**
 * 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 已提交
357

358 359 360 361 362 363 364 365 366 367 368 369
/**
 * 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 已提交
370
 * Since: 0.9.7
371
 **/
B
Behdad Esfahbod 已提交
372
hb_bool_t
373
hb_shape_plan_execute (hb_shape_plan_t    *shape_plan,
B
Behdad Esfahbod 已提交
374 375 376 377 378
		       hb_font_t          *font,
		       hb_buffer_t        *buffer,
		       const hb_feature_t *features,
		       unsigned int        num_features)
{
B
Behdad Esfahbod 已提交
379
  DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan,
380
		  "num_features=%d shaper_func=%p, shaper_name=%s",
B
Behdad Esfahbod 已提交
381
		  num_features,
382 383
		  shape_plan->key.shaper_func,
		  shape_plan->key.shaper_name);
B
Behdad Esfahbod 已提交
384

385 386 387
  if (unlikely (!buffer->len))
    return true;

388
  assert (!hb_object_is_immutable (buffer));
389 390 391
  assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE);

  if (unlikely (hb_object_is_inert (shape_plan)))
B
Behdad Esfahbod 已提交
392 393
    return false;

394
  assert (shape_plan->face_unsafe == font->face);
395
  assert (hb_segment_properties_equal (&shape_plan->key.props, &buffer->props));
396

B
Behdad Esfahbod 已提交
397 398
#define HB_SHAPER_EXECUTE(shaper) \
	HB_STMT_START { \
399
	  return font->data.shaper && \
400
		 _hb_##shaper##_shape (shape_plan, font, buffer, features, num_features); \
B
Behdad Esfahbod 已提交
401 402
	} HB_STMT_END

403
  if (false)
B
Behdad Esfahbod 已提交
404
    ;
B
Behdad Esfahbod 已提交
405
#define HB_SHAPER_IMPLEMENT(shaper) \
406
  else if (shape_plan->key.shaper_func == _hb_##shaper##_shape) \
B
Behdad Esfahbod 已提交
407
    HB_SHAPER_EXECUTE (shaper);
B
Behdad Esfahbod 已提交
408 409 410 411 412 413 414
#include "hb-shaper-list.hh"
#undef HB_SHAPER_IMPLEMENT

#undef HB_SHAPER_EXECUTE

  return false;
}
B
Behdad Esfahbod 已提交
415 416 417


/*
B
Behdad Esfahbod 已提交
418
 * Caching
B
Behdad Esfahbod 已提交
419 420
 */

421 422 423 424 425 426 427 428 429 430 431 432
/**
 * 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 已提交
433
 * Since: 0.9.7
434
 **/
B
Behdad Esfahbod 已提交
435 436 437 438 439 440
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 已提交
441 442 443
{
  return hb_shape_plan_create_cached2 (face, props,
				       user_features, num_user_features,
B
Behdad Esfahbod 已提交
444
				       nullptr, 0,
B
Behdad Esfahbod 已提交
445 446 447 448 449 450 451 452 453 454 455
				       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 已提交
456
{
B
Behdad Esfahbod 已提交
457
  DEBUG_MSG_FUNC (SHAPE_PLAN, nullptr,
458 459
		  "face=%p num_features=%d shaper_list=%p",
		  face,
B
Behdad Esfahbod 已提交
460 461 462
		  num_user_features,
		  shaper_list);

B
Behdad Esfahbod 已提交
463
retry:
464
  hb_face_t::plan_node_t *cached_plan_nodes = face->shape_plans;
B
Behdad Esfahbod 已提交
465

466
  bool dont_cache = hb_object_is_inert (face);
B
Behdad Esfahbod 已提交
467

468
  if (likely (!dont_cache))
B
Behdad Esfahbod 已提交
469 470 471 472 473 474 475 476 477 478 479 480
  {
    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 ();

481
    for (hb_face_t::plan_node_t *node = cached_plan_nodes; node; node = node->next)
482
      if (node->shape_plan->key.equal (&key))
483 484 485 486
      {
        DEBUG_MSG_FUNC (SHAPE_PLAN, node->shape_plan, "fulfilled from cache");
        return hb_shape_plan_reference (node->shape_plan);
      }
B
Behdad Esfahbod 已提交
487
  }
B
Behdad Esfahbod 已提交
488

B
Behdad Esfahbod 已提交
489 490 491 492
  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 已提交
493

494
  if (unlikely (dont_cache))
B
Behdad Esfahbod 已提交
495
    return shape_plan;
496

B
Behdad Esfahbod 已提交
497 498 499 500 501 502 503
  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;

504 505
  if (unlikely (!face->shape_plans.cmpexch (cached_plan_nodes, node)))
  {
B
Behdad Esfahbod 已提交
506 507 508 509
    hb_shape_plan_destroy (shape_plan);
    free (node);
    goto retry;
  }
510
  DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan, "inserted into cache");
B
Behdad Esfahbod 已提交
511 512 513

  return hb_shape_plan_reference (shape_plan);
}