hb-shape-plan.cc 12.8 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 27
/*
 * 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
 */

#include "hb-shape-plan-private.hh"
B
Behdad Esfahbod 已提交
28
#include "hb-shaper-private.hh"
29
#include "hb-font-private.hh"
30
#include "hb-buffer-private.hh"
31

B
Behdad Esfahbod 已提交
32 33 34 35 36 37

#ifndef HB_DEBUG_SHAPE_PLAN
#define HB_DEBUG_SHAPE_PLAN (HB_DEBUG+0)
#endif


B
Behdad Esfahbod 已提交
38 39 40
#define HB_SHAPER_IMPLEMENT(shaper) \
	HB_SHAPER_DATA_ENSURE_DECLARE(shaper, face) \
	HB_SHAPER_DATA_ENSURE_DECLARE(shaper, font)
B
Behdad Esfahbod 已提交
41 42 43 44
#include "hb-shaper-list.hh"
#undef HB_SHAPER_IMPLEMENT


B
Behdad Esfahbod 已提交
45
static void
B
Behdad Esfahbod 已提交
46 47 48 49 50
hb_shape_plan_plan (hb_shape_plan_t    *shape_plan,
		    const hb_feature_t *user_features,
		    unsigned int        num_user_features,
		    const char * const *shaper_list)
{
B
Behdad Esfahbod 已提交
51 52 53 54 55
  DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan,
		  "num_features=%d shaper_list=%p",
		  num_user_features,
		  shaper_list);

B
Behdad Esfahbod 已提交
56 57 58 59
  const hb_shaper_pair_t *shapers = _hb_shapers_get ();

#define HB_SHAPER_PLAN(shaper) \
	HB_STMT_START { \
60
	  if (hb_##shaper##_shaper_face_data_ensure (shape_plan->face_unsafe)) { \
61
	    HB_SHAPER_DATA (shaper, shape_plan) = \
B
Behdad Esfahbod 已提交
62
	      HB_SHAPER_DATA_CREATE_FUNC (shaper, shape_plan) (shape_plan, user_features, num_user_features); \
63
	    shape_plan->shaper_func = _hb_##shaper##_shape; \
64
	    shape_plan->shaper_name = #shaper; \
65
	    return; \
B
Behdad Esfahbod 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
	  } \
	} HB_STMT_END

  if (likely (!shaper_list)) {
    for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++)
      if (0)
	;
#define HB_SHAPER_IMPLEMENT(shaper) \
      else if (shapers[i].func == _hb_##shaper##_shape) \
	HB_SHAPER_PLAN (shaper);
#include "hb-shaper-list.hh"
#undef HB_SHAPER_IMPLEMENT
  } else {
    for (; *shaper_list; shaper_list++)
      if (0)
	;
#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
  }

#undef HB_SHAPER_PLAN
}

92 93 94 95 96

/*
 * hb_shape_plan_t
 */

97
/**
98
 * hb_shape_plan_create: (Xconstructor)
99 100 101 102 103 104 105 106 107 108
 * @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 已提交
109
 * Since: 0.9.7
110
 **/
111 112 113 114 115 116 117
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)
{
118 119 120
  DEBUG_MSG_FUNC (SHAPE_PLAN, NULL,
		  "face=%p num_features=%d shaper_list=%p",
		  face,
B
Behdad Esfahbod 已提交
121 122 123
		  num_user_features,
		  shaper_list);

124
  hb_shape_plan_t *shape_plan;
125
  hb_feature_t *features = NULL;
126 127 128

  if (unlikely (!face))
    face = hb_face_get_empty ();
B
Behdad Esfahbod 已提交
129
  if (unlikely (!props))
130
    return hb_shape_plan_get_empty ();
131
  if (num_user_features && !(features = (hb_feature_t *) malloc (num_user_features * sizeof (hb_feature_t))))
132
    return hb_shape_plan_get_empty ();
133 134 135 136
  if (!(shape_plan = hb_object_create<hb_shape_plan_t> ())) {
    free (features);
    return hb_shape_plan_get_empty ();
  }
137

B
Behdad Esfahbod 已提交
138 139
  assert (props->direction != HB_DIRECTION_INVALID);

140
  hb_face_make_immutable (face);
141
  shape_plan->default_shaper_list = shaper_list == NULL;
142
  shape_plan->face_unsafe = face;
B
Behdad Esfahbod 已提交
143
  shape_plan->props = *props;
144 145 146 147
  shape_plan->num_user_features = num_user_features;
  shape_plan->user_features = features;
  if (num_user_features)
    memcpy (features, user_features, num_user_features * sizeof (hb_feature_t));
148

B
Behdad Esfahbod 已提交
149
  hb_shape_plan_plan (shape_plan, user_features, num_user_features, shaper_list);
B
Minor  
Behdad Esfahbod 已提交
150

151 152 153
  return shape_plan;
}

154 155 156 157 158 159 160
/**
 * hb_shape_plan_get_empty:
 *
 * 
 *
 * Return value: (transfer full):
 *
S
Sascha Brawer 已提交
161
 * Since: 0.9.7
162
 **/
163 164 165 166 167
hb_shape_plan_t *
hb_shape_plan_get_empty (void)
{
  static const hb_shape_plan_t _hb_shape_plan_nil = {
    HB_OBJECT_HEADER_STATIC,
168

169
    true, /* default_shaper_list */
170
    NULL, /* face */
171
    HB_SEGMENT_PROPERTIES_DEFAULT, /* props */
172

B
Behdad Esfahbod 已提交
173
    NULL, /* shaper_func */
174
    NULL, /* shaper_name */
175

176 177 178
    NULL, /* user_features */
    0,    /* num_user_featurs */

179 180 181 182 183
    {
#define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID,
#include "hb-shaper-list.hh"
#undef HB_SHAPER_IMPLEMENT
    }
184 185 186 187 188
  };

  return const_cast<hb_shape_plan_t *> (&_hb_shape_plan_nil);
}

189 190 191 192 193 194 195 196
/**
 * hb_shape_plan_reference: (skip)
 * @shape_plan: a shape plan.
 *
 * 
 *
 * Return value: (transfer full):
 *
S
Sascha Brawer 已提交
197
 * Since: 0.9.7
198
 **/
199 200 201 202 203 204
hb_shape_plan_t *
hb_shape_plan_reference (hb_shape_plan_t *shape_plan)
{
  return hb_object_reference (shape_plan);
}

205 206 207 208 209 210
/**
 * hb_shape_plan_destroy: (skip)
 * @shape_plan: a shape plan.
 *
 * 
 *
S
Sascha Brawer 已提交
211
 * Since: 0.9.7
212
 **/
213 214 215 216 217 218 219 220 221
void
hb_shape_plan_destroy (hb_shape_plan_t *shape_plan)
{
  if (!hb_object_destroy (shape_plan)) return;

#define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, shape_plan);
#include "hb-shaper-list.hh"
#undef HB_SHAPER_IMPLEMENT

222
  free (shape_plan->user_features);
B
Behdad Esfahbod 已提交
223

224 225
  free (shape_plan);
}
B
Behdad Esfahbod 已提交
226

227 228 229 230 231 232 233 234 235 236 237 238
/**
 * hb_shape_plan_set_user_data: (skip)
 * @shape_plan: a shape plan.
 * @key: 
 * @data: 
 * @destroy: 
 * @replace: 
 *
 * 
 *
 * Return value: 
 *
S
Sascha Brawer 已提交
239
 * Since: 0.9.7
240
 **/
241 242 243 244 245 246 247 248 249 250
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);
}

251 252 253 254 255 256 257 258 259
/**
 * hb_shape_plan_get_user_data: (skip)
 * @shape_plan: a shape plan.
 * @key: 
 *
 * 
 *
 * Return value: (transfer none):
 *
S
Sascha Brawer 已提交
260
 * Since: 0.9.7
261
 **/
262 263 264 265 266 267 268
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 已提交
269

270 271 272 273 274 275 276 277 278 279 280 281
/**
 * 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 已提交
282
 * Since: 0.9.7
283
 **/
B
Behdad Esfahbod 已提交
284
hb_bool_t
285
hb_shape_plan_execute (hb_shape_plan_t    *shape_plan,
B
Behdad Esfahbod 已提交
286 287 288 289 290
		       hb_font_t          *font,
		       hb_buffer_t        *buffer,
		       const hb_feature_t *features,
		       unsigned int        num_features)
{
B
Behdad Esfahbod 已提交
291 292 293 294 295
  DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan,
		  "num_features=%d shaper_func=%p",
		  num_features,
		  shape_plan->shaper_func);

296 297
  if (unlikely (hb_object_is_inert (shape_plan) ||
		hb_object_is_inert (buffer)))
B
Behdad Esfahbod 已提交
298 299
    return false;

300
  assert (shape_plan->face_unsafe == font->face);
301 302
  assert (hb_segment_properties_equal (&shape_plan->props, &buffer->props));

B
Behdad Esfahbod 已提交
303 304
#define HB_SHAPER_EXECUTE(shaper) \
	HB_STMT_START { \
305 306 307
	  return HB_SHAPER_DATA (shaper, shape_plan) && \
		 hb_##shaper##_shaper_font_data_ensure (font) && \
		 _hb_##shaper##_shape (shape_plan, font, buffer, features, num_features); \
B
Behdad Esfahbod 已提交
308 309
	} HB_STMT_END

B
Behdad Esfahbod 已提交
310 311
  if (0)
    ;
B
Behdad Esfahbod 已提交
312
#define HB_SHAPER_IMPLEMENT(shaper) \
B
Behdad Esfahbod 已提交
313 314
  else if (shape_plan->shaper_func == _hb_##shaper##_shape) \
    HB_SHAPER_EXECUTE (shaper);
B
Behdad Esfahbod 已提交
315 316 317 318 319 320 321
#include "hb-shaper-list.hh"
#undef HB_SHAPER_IMPLEMENT

#undef HB_SHAPER_EXECUTE

  return false;
}
B
Behdad Esfahbod 已提交
322 323 324 325 326 327 328


/*
 * caching
 */

#if 0
B
Behdad Esfahbod 已提交
329
static unsigned int
B
Behdad Esfahbod 已提交
330 331 332
hb_shape_plan_hash (const hb_shape_plan_t *shape_plan)
{
  return hb_segment_properties_hash (&shape_plan->props) +
B
Behdad Esfahbod 已提交
333
	 shape_plan->default_shaper_list ? 0 : (intptr_t) shape_plan->shaper_func;
B
Behdad Esfahbod 已提交
334 335 336
}
#endif

337 338 339 340 341
/* User-feature caching is currently somewhat dumb:
 * it only finds matches where the feature array is identical,
 * not cases where the feature lists would be compatible for plan purposes
 * but have different ranges, for example.
 */
B
Behdad Esfahbod 已提交
342 343 344 345
struct hb_shape_plan_proposal_t
{
  const hb_segment_properties_t  props;
  const char * const            *shaper_list;
346 347
  const hb_feature_t            *user_features;
  unsigned int                   num_user_features;
B
Behdad Esfahbod 已提交
348 349 350
  hb_shape_func_t               *shaper_func;
};

351 352 353 354 355 356 357 358 359 360 361 362 363
static inline hb_bool_t
hb_shape_plan_user_features_match (const hb_shape_plan_t          *shape_plan,
				   const hb_shape_plan_proposal_t *proposal)
{
  if (proposal->num_user_features != shape_plan->num_user_features) return false;
  for (unsigned int i = 0, n = proposal->num_user_features; i < n; i++)
    if (proposal->user_features[i].tag   != shape_plan->user_features[i].tag   ||
        proposal->user_features[i].value != shape_plan->user_features[i].value ||
        proposal->user_features[i].start != shape_plan->user_features[i].start ||
        proposal->user_features[i].end   != shape_plan->user_features[i].end) return false;
  return true;
}

B
Behdad Esfahbod 已提交
364 365 366 367 368
static hb_bool_t
hb_shape_plan_matches (const hb_shape_plan_t          *shape_plan,
		       const hb_shape_plan_proposal_t *proposal)
{
  return hb_segment_properties_equal (&shape_plan->props, &proposal->props) &&
369
	 hb_shape_plan_user_features_match (shape_plan, proposal) &&
B
Behdad Esfahbod 已提交
370 371 372 373
	 ((shape_plan->default_shaper_list && proposal->shaper_list == NULL) ||
	  (shape_plan->shaper_func == proposal->shaper_func));
}

374 375 376 377 378 379 380 381 382 383 384 385
static inline hb_bool_t
hb_non_global_user_features_present (const hb_feature_t *user_features,
				     unsigned int        num_user_features)
{
  while (num_user_features)
    if (user_features->start != 0 || user_features->end != (unsigned int) -1)
      return true;
    else
      num_user_features--, user_features++;
  return false;
}

386 387 388 389 390 391 392 393 394 395 396 397
/**
 * 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 已提交
398
 * Since: 0.9.7
399
 **/
B
Behdad Esfahbod 已提交
400 401 402 403 404 405 406
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)
{
407 408 409
  DEBUG_MSG_FUNC (SHAPE_PLAN, NULL,
		  "face=%p num_features=%d shaper_list=%p",
		  face,
B
Behdad Esfahbod 已提交
410 411 412
		  num_user_features,
		  shaper_list);

B
Behdad Esfahbod 已提交
413 414 415
  hb_shape_plan_proposal_t proposal = {
    *props,
    shaper_list,
416 417
    user_features,
    num_user_features,
B
Behdad Esfahbod 已提交
418 419 420 421
    NULL
  };

  if (shaper_list) {
B
Behdad Esfahbod 已提交
422 423
    /* Choose shaper.  Adapted from hb_shape_plan_plan().
     * Must choose shaper exactly the same way as that function. */
B
Behdad Esfahbod 已提交
424 425 426 427
    for (const char * const *shaper_item = shaper_list; *shaper_item; shaper_item++)
      if (0)
	;
#define HB_SHAPER_IMPLEMENT(shaper) \
B
Behdad Esfahbod 已提交
428 429 430 431 432 433
      else if (0 == strcmp (*shaper_item, #shaper) && \
	       hb_##shaper##_shaper_face_data_ensure (face)) \
      { \
	proposal.shaper_func = _hb_##shaper##_shape; \
	break; \
      }
B
Behdad Esfahbod 已提交
434 435 436
#include "hb-shaper-list.hh"
#undef HB_SHAPER_IMPLEMENT

B
Behdad Esfahbod 已提交
437
    if (unlikely (!proposal.shaper_func))
B
Behdad Esfahbod 已提交
438 439 440 441 442 443 444 445
      return hb_shape_plan_get_empty ();
  }


retry:
  hb_face_t::plan_node_t *cached_plan_nodes = (hb_face_t::plan_node_t *) hb_atomic_ptr_get (&face->shape_plans);
  for (hb_face_t::plan_node_t *node = cached_plan_nodes; node; node = node->next)
    if (hb_shape_plan_matches (node->shape_plan, &proposal))
446 447
    {
      DEBUG_MSG_FUNC (SHAPE_PLAN, node->shape_plan, "fulfilled from cache");
B
Behdad Esfahbod 已提交
448
      return hb_shape_plan_reference (node->shape_plan);
449
    }
B
Behdad Esfahbod 已提交
450 451 452 453 454

  /* Not found. */

  hb_shape_plan_t *shape_plan = hb_shape_plan_create (face, props, user_features, num_user_features, shaper_list);

B
Behdad Esfahbod 已提交
455 456 457 458
  /* Don't add to the cache if face is inert. */
  if (unlikely (hb_object_is_inert (face)))
    return shape_plan;

459 460 461 462 463
  /* Don't add the plan to the cache if there were user features with non-global ranges */

  if (hb_non_global_user_features_present (user_features, num_user_features))
    return shape_plan;

B
Behdad Esfahbod 已提交
464 465 466 467 468 469 470 471 472 473 474 475
  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;

  if (!hb_atomic_ptr_cmpexch (&face->shape_plans, cached_plan_nodes, node)) {
    hb_shape_plan_destroy (shape_plan);
    free (node);
    goto retry;
  }
476
  DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan, "inserted into cache");
B
Behdad Esfahbod 已提交
477 478 479

  return hb_shape_plan_reference (shape_plan);
}
480

481 482 483 484 485 486 487 488
/**
 * hb_shape_plan_get_shaper:
 * @shape_plan: a shape plan.
 *
 * 
 *
 * Return value: (transfer none):
 *
S
Sascha Brawer 已提交
489
 * Since: 0.9.7
490
 **/
491 492 493
const char *
hb_shape_plan_get_shaper (hb_shape_plan_t *shape_plan)
{
B
Behdad Esfahbod 已提交
494
  return shape_plan->shaper_name;
495
}