1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 // This file is available under and governed by the GNU General Public
  26 // License version 2 only, as published by the Free Software Foundation.
  27 // However, the following notice accompanied the original version of this
  28 // file:
  29 //
  30 /*
  31  * Copyright © 2012  Google, Inc.
  32  *
  33  *  This is part of HarfBuzz, a text shaping library.
  34  *
  35  * Permission is hereby granted, without written agreement and without
  36  * license or royalty fees, to use, copy, modify, and distribute this
  37  * software and its documentation for any purpose, provided that the
  38  * above copyright notice and the following two paragraphs appear in
  39  * all copies of this software.
  40  *
  41  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  42  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  43  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  44  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  45  * DAMAGE.
  46  *
  47  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  48  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  49  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  50  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  51  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  52  *
  53  * Google Author(s): Behdad Esfahbod
  54  */
  55 
  56 #include "hb-shape-plan-private.hh"
  57 #include "hb-shaper-private.hh"
  58 #include "hb-font-private.hh"
  59 #include "hb-buffer-private.hh"
  60 
  61 
  62 #ifndef HB_DEBUG_SHAPE_PLAN
  63 #define HB_DEBUG_SHAPE_PLAN (HB_DEBUG+0)
  64 #endif
  65 
  66 
  67 #define HB_SHAPER_IMPLEMENT(shaper) \
  68         HB_SHAPER_DATA_ENSURE_DECLARE(shaper, face) \
  69         HB_SHAPER_DATA_ENSURE_DECLARE(shaper, font)
  70 #include "hb-shaper-list.hh"
  71 #undef HB_SHAPER_IMPLEMENT
  72 
  73 
  74 static void
  75 hb_shape_plan_plan (hb_shape_plan_t    *shape_plan,
  76                     const hb_feature_t *user_features,
  77                     unsigned int        num_user_features,
  78                     const char * const *shaper_list)
  79 {
  80   DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan,
  81                   "num_features=%d shaper_list=%p",
  82                   num_user_features,
  83                   shaper_list);
  84 
  85   const hb_shaper_pair_t *shapers = _hb_shapers_get ();
  86 
  87 #define HB_SHAPER_PLAN(shaper) \
  88         HB_STMT_START { \
  89           if (hb_##shaper##_shaper_face_data_ensure (shape_plan->face_unsafe)) { \
  90             HB_SHAPER_DATA (shaper, shape_plan) = \
  91               HB_SHAPER_DATA_CREATE_FUNC (shaper, shape_plan) (shape_plan, user_features, num_user_features); \
  92             shape_plan->shaper_func = _hb_##shaper##_shape; \
  93             shape_plan->shaper_name = #shaper; \
  94             return; \
  95           } \
  96         } HB_STMT_END
  97 
  98   if (likely (!shaper_list)) {
  99     for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++)
 100       if (0)
 101         ;
 102 #define HB_SHAPER_IMPLEMENT(shaper) \
 103       else if (shapers[i].func == _hb_##shaper##_shape) \
 104         HB_SHAPER_PLAN (shaper);
 105 #include "hb-shaper-list.hh"
 106 #undef HB_SHAPER_IMPLEMENT
 107   } else {
 108     for (; *shaper_list; shaper_list++)
 109       if (0)
 110         ;
 111 #define HB_SHAPER_IMPLEMENT(shaper) \
 112       else if (0 == strcmp (*shaper_list, #shaper)) \
 113         HB_SHAPER_PLAN (shaper);
 114 #include "hb-shaper-list.hh"
 115 #undef HB_SHAPER_IMPLEMENT
 116   }
 117 
 118 #undef HB_SHAPER_PLAN
 119 }
 120 
 121 
 122 /*
 123  * hb_shape_plan_t
 124  */
 125 
 126 /**
 127  * hb_shape_plan_create: (Xconstructor)
 128  * @face:
 129  * @props:
 130  * @user_features: (array length=num_user_features):
 131  * @num_user_features:
 132  * @shaper_list: (array zero-terminated=1):
 133  *
 134  *
 135  *
 136  * Return value: (transfer full):
 137  *
 138  * Since: 0.9.7
 139  **/
 140 hb_shape_plan_t *
 141 hb_shape_plan_create (hb_face_t                     *face,
 142                       const hb_segment_properties_t *props,
 143                       const hb_feature_t            *user_features,
 144                       unsigned int                   num_user_features,
 145                       const char * const            *shaper_list)
 146 {
 147   DEBUG_MSG_FUNC (SHAPE_PLAN, NULL,
 148                   "face=%p num_features=%d shaper_list=%p",
 149                   face,
 150                   num_user_features,
 151                   shaper_list);
 152 
 153   hb_shape_plan_t *shape_plan;
 154   hb_feature_t *features = NULL;
 155 
 156   if (unlikely (!face))
 157     face = hb_face_get_empty ();
 158   if (unlikely (!props))
 159     return hb_shape_plan_get_empty ();
 160   if (num_user_features && !(features = (hb_feature_t *) calloc (num_user_features, sizeof (hb_feature_t))))
 161     return hb_shape_plan_get_empty ();
 162   if (!(shape_plan = hb_object_create<hb_shape_plan_t> ())) {
 163     free (features);
 164     return hb_shape_plan_get_empty ();
 165   }
 166 
 167   assert (props->direction != HB_DIRECTION_INVALID);
 168 
 169   hb_face_make_immutable (face);
 170   shape_plan->default_shaper_list = shaper_list == NULL;
 171   shape_plan->face_unsafe = face;
 172   shape_plan->props = *props;
 173   shape_plan->num_user_features = num_user_features;
 174   shape_plan->user_features = features;
 175   if (num_user_features)
 176     memcpy (features, user_features, num_user_features * sizeof (hb_feature_t));
 177 
 178   hb_shape_plan_plan (shape_plan, user_features, num_user_features, shaper_list);
 179 
 180   return shape_plan;
 181 }
 182 
 183 /**
 184  * hb_shape_plan_get_empty:
 185  *
 186  *
 187  *
 188  * Return value: (transfer full):
 189  *
 190  * Since: 0.9.7
 191  **/
 192 hb_shape_plan_t *
 193 hb_shape_plan_get_empty (void)
 194 {
 195   static const hb_shape_plan_t _hb_shape_plan_nil = {
 196     HB_OBJECT_HEADER_STATIC,
 197 
 198     true, /* default_shaper_list */
 199     NULL, /* face */
 200     HB_SEGMENT_PROPERTIES_DEFAULT, /* props */
 201 
 202     NULL, /* shaper_func */
 203     NULL, /* shaper_name */
 204 
 205     NULL, /* user_features */
 206     0,    /* num_user_featurs */
 207 
 208     {
 209 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID,
 210 #include "hb-shaper-list.hh"
 211 #undef HB_SHAPER_IMPLEMENT
 212     }
 213   };
 214 
 215   return const_cast<hb_shape_plan_t *> (&_hb_shape_plan_nil);
 216 }
 217 
 218 /**
 219  * hb_shape_plan_reference: (skip)
 220  * @shape_plan: a shape plan.
 221  *
 222  *
 223  *
 224  * Return value: (transfer full):
 225  *
 226  * Since: 0.9.7
 227  **/
 228 hb_shape_plan_t *
 229 hb_shape_plan_reference (hb_shape_plan_t *shape_plan)
 230 {
 231   return hb_object_reference (shape_plan);
 232 }
 233 
 234 /**
 235  * hb_shape_plan_destroy: (skip)
 236  * @shape_plan: a shape plan.
 237  *
 238  *
 239  *
 240  * Since: 0.9.7
 241  **/
 242 void
 243 hb_shape_plan_destroy (hb_shape_plan_t *shape_plan)
 244 {
 245   if (!hb_object_destroy (shape_plan)) return;
 246 
 247 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, shape_plan);
 248 #include "hb-shaper-list.hh"
 249 #undef HB_SHAPER_IMPLEMENT
 250 
 251   free (shape_plan->user_features);
 252 
 253   free (shape_plan);
 254 }
 255 
 256 /**
 257  * hb_shape_plan_set_user_data: (skip)
 258  * @shape_plan: a shape plan.
 259  * @key:
 260  * @data:
 261  * @destroy:
 262  * @replace:
 263  *
 264  *
 265  *
 266  * Return value:
 267  *
 268  * Since: 0.9.7
 269  **/
 270 hb_bool_t
 271 hb_shape_plan_set_user_data (hb_shape_plan_t    *shape_plan,
 272                              hb_user_data_key_t *key,
 273                              void *              data,
 274                              hb_destroy_func_t   destroy,
 275                              hb_bool_t           replace)
 276 {
 277   return hb_object_set_user_data (shape_plan, key, data, destroy, replace);
 278 }
 279 
 280 /**
 281  * hb_shape_plan_get_user_data: (skip)
 282  * @shape_plan: a shape plan.
 283  * @key:
 284  *
 285  *
 286  *
 287  * Return value: (transfer none):
 288  *
 289  * Since: 0.9.7
 290  **/
 291 void *
 292 hb_shape_plan_get_user_data (hb_shape_plan_t    *shape_plan,
 293                              hb_user_data_key_t *key)
 294 {
 295   return hb_object_get_user_data (shape_plan, key);
 296 }
 297 
 298 
 299 /**
 300  * hb_shape_plan_execute:
 301  * @shape_plan: a shape plan.
 302  * @font: a font.
 303  * @buffer: a buffer.
 304  * @features: (array length=num_features):
 305  * @num_features:
 306  *
 307  *
 308  *
 309  * Return value:
 310  *
 311  * Since: 0.9.7
 312  **/
 313 hb_bool_t
 314 hb_shape_plan_execute (hb_shape_plan_t    *shape_plan,
 315                        hb_font_t          *font,
 316                        hb_buffer_t        *buffer,
 317                        const hb_feature_t *features,
 318                        unsigned int        num_features)
 319 {
 320   DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan,
 321                   "num_features=%d shaper_func=%p",
 322                   num_features,
 323                   shape_plan->shaper_func);
 324 
 325   if (unlikely (!buffer->len))
 326     return true;
 327 
 328   assert (!hb_object_is_inert (buffer));
 329   assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE);
 330 
 331   if (unlikely (hb_object_is_inert (shape_plan)))
 332     return false;
 333 
 334   assert (shape_plan->face_unsafe == font->face);
 335   assert (hb_segment_properties_equal (&shape_plan->props, &buffer->props));
 336 
 337 #define HB_SHAPER_EXECUTE(shaper) \
 338         HB_STMT_START { \
 339           return HB_SHAPER_DATA (shaper, shape_plan) && \
 340                  hb_##shaper##_shaper_font_data_ensure (font) && \
 341                  _hb_##shaper##_shape (shape_plan, font, buffer, features, num_features); \
 342         } HB_STMT_END
 343 
 344   if (0)
 345     ;
 346 #define HB_SHAPER_IMPLEMENT(shaper) \
 347   else if (shape_plan->shaper_func == _hb_##shaper##_shape) \
 348     HB_SHAPER_EXECUTE (shaper);
 349 #include "hb-shaper-list.hh"
 350 #undef HB_SHAPER_IMPLEMENT
 351 
 352 #undef HB_SHAPER_EXECUTE
 353 
 354   return false;
 355 }
 356 
 357 
 358 /*
 359  * caching
 360  */
 361 
 362 #if 0
 363 static unsigned int
 364 hb_shape_plan_hash (const hb_shape_plan_t *shape_plan)
 365 {
 366   return hb_segment_properties_hash (&shape_plan->props) +
 367          shape_plan->default_shaper_list ? 0 : (intptr_t) shape_plan->shaper_func;
 368 }
 369 #endif
 370 
 371 /* User-feature caching is currently somewhat dumb:
 372  * it only finds matches where the feature array is identical,
 373  * not cases where the feature lists would be compatible for plan purposes
 374  * but have different ranges, for example.
 375  */
 376 struct hb_shape_plan_proposal_t
 377 {
 378   const hb_segment_properties_t  props;
 379   const char * const            *shaper_list;
 380   const hb_feature_t            *user_features;
 381   unsigned int                   num_user_features;
 382   hb_shape_func_t               *shaper_func;
 383 };
 384 
 385 static inline hb_bool_t
 386 hb_shape_plan_user_features_match (const hb_shape_plan_t          *shape_plan,
 387                                    const hb_shape_plan_proposal_t *proposal)
 388 {
 389   if (proposal->num_user_features != shape_plan->num_user_features) return false;
 390   for (unsigned int i = 0, n = proposal->num_user_features; i < n; i++)
 391     if (proposal->user_features[i].tag   != shape_plan->user_features[i].tag   ||
 392         proposal->user_features[i].value != shape_plan->user_features[i].value ||
 393         proposal->user_features[i].start != shape_plan->user_features[i].start ||
 394         proposal->user_features[i].end   != shape_plan->user_features[i].end) return false;
 395   return true;
 396 }
 397 
 398 static hb_bool_t
 399 hb_shape_plan_matches (const hb_shape_plan_t          *shape_plan,
 400                        const hb_shape_plan_proposal_t *proposal)
 401 {
 402   return hb_segment_properties_equal (&shape_plan->props, &proposal->props) &&
 403          hb_shape_plan_user_features_match (shape_plan, proposal) &&
 404          ((shape_plan->default_shaper_list && proposal->shaper_list == NULL) ||
 405           (shape_plan->shaper_func == proposal->shaper_func));
 406 }
 407 
 408 static inline hb_bool_t
 409 hb_non_global_user_features_present (const hb_feature_t *user_features,
 410                                      unsigned int        num_user_features)
 411 {
 412   while (num_user_features)
 413     if (user_features->start != 0 || user_features->end != (unsigned int) -1)
 414       return true;
 415     else
 416       num_user_features--, user_features++;
 417   return false;
 418 }
 419 
 420 /**
 421  * hb_shape_plan_create_cached:
 422  * @face:
 423  * @props:
 424  * @user_features: (array length=num_user_features):
 425  * @num_user_features:
 426  * @shaper_list: (array zero-terminated=1):
 427  *
 428  *
 429  *
 430  * Return value: (transfer full):
 431  *
 432  * Since: 0.9.7
 433  **/
 434 hb_shape_plan_t *
 435 hb_shape_plan_create_cached (hb_face_t                     *face,
 436                              const hb_segment_properties_t *props,
 437                              const hb_feature_t            *user_features,
 438                              unsigned int                   num_user_features,
 439                              const char * const            *shaper_list)
 440 {
 441   DEBUG_MSG_FUNC (SHAPE_PLAN, NULL,
 442                   "face=%p num_features=%d shaper_list=%p",
 443                   face,
 444                   num_user_features,
 445                   shaper_list);
 446 
 447   hb_shape_plan_proposal_t proposal = {
 448     *props,
 449     shaper_list,
 450     user_features,
 451     num_user_features,
 452     NULL
 453   };
 454 
 455   if (shaper_list) {
 456     /* Choose shaper.  Adapted from hb_shape_plan_plan().
 457      * Must choose shaper exactly the same way as that function. */
 458     for (const char * const *shaper_item = shaper_list; *shaper_item; shaper_item++)
 459       if (0)
 460         ;
 461 #define HB_SHAPER_IMPLEMENT(shaper) \
 462       else if (0 == strcmp (*shaper_item, #shaper) && \
 463                hb_##shaper##_shaper_face_data_ensure (face)) \
 464       { \
 465         proposal.shaper_func = _hb_##shaper##_shape; \
 466         break; \
 467       }
 468 #include "hb-shaper-list.hh"
 469 #undef HB_SHAPER_IMPLEMENT
 470 
 471     if (unlikely (!proposal.shaper_func))
 472       return hb_shape_plan_get_empty ();
 473   }
 474 
 475 
 476 retry:
 477   hb_face_t::plan_node_t *cached_plan_nodes = (hb_face_t::plan_node_t *) hb_atomic_ptr_get (&face->shape_plans);
 478   for (hb_face_t::plan_node_t *node = cached_plan_nodes; node; node = node->next)
 479     if (hb_shape_plan_matches (node->shape_plan, &proposal))
 480     {
 481       DEBUG_MSG_FUNC (SHAPE_PLAN, node->shape_plan, "fulfilled from cache");
 482       return hb_shape_plan_reference (node->shape_plan);
 483     }
 484 
 485   /* Not found. */
 486 
 487   hb_shape_plan_t *shape_plan = hb_shape_plan_create (face, props, user_features, num_user_features, shaper_list);
 488 
 489   /* Don't add to the cache if face is inert. */
 490   if (unlikely (hb_object_is_inert (face)))
 491     return shape_plan;
 492 
 493   /* Don't add the plan to the cache if there were user features with non-global ranges */
 494 
 495   if (hb_non_global_user_features_present (user_features, num_user_features))
 496     return shape_plan;
 497 
 498   hb_face_t::plan_node_t *node = (hb_face_t::plan_node_t *) calloc (1, sizeof (hb_face_t::plan_node_t));
 499   if (unlikely (!node))
 500     return shape_plan;
 501 
 502   node->shape_plan = shape_plan;
 503   node->next = cached_plan_nodes;
 504 
 505   if (!hb_atomic_ptr_cmpexch (&face->shape_plans, cached_plan_nodes, node)) {
 506     hb_shape_plan_destroy (shape_plan);
 507     free (node);
 508     goto retry;
 509   }
 510   DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan, "inserted into cache");
 511 
 512   return hb_shape_plan_reference (shape_plan);
 513 }
 514 
 515 /**
 516  * hb_shape_plan_get_shaper:
 517  * @shape_plan: a shape plan.
 518  *
 519  *
 520  *
 521  * Return value: (transfer none):
 522  *
 523  * Since: 0.9.7
 524  **/
 525 const char *
 526 hb_shape_plan_get_shaper (hb_shape_plan_t *shape_plan)
 527 {
 528   return shape_plan->shaper_name;
 529 }