sylware / charfbuzz (public) (License: LGPLv3) (since 2019-09-09) (hash sha1)
partial C implementation of harfbuzz C API for roman scripts

/hb-shape-plan.c (929804aae7b6e5271cef5e9f1d654dcce7a8ca95) (15942 bytes) (mode 100644) (type blob)

// C99 port from c++ is protected by a GNU Lesser GPLv3
// Copyright © 2013 Sylvain BERTRAND <sylvain.bertrand@gmail.com>
//                                   <sylware@legeek.net>       
#include <stddef.h>
#include <assert.h>

#include <ft2build.h>
#include FT_FREETYPE_H

#include "hb.h"
#include "hb-private.h"
#include "hb-atomic-private.h"
#include "hb-buffer-private.h"
#include "hb-shaper-private.h"
#include "hb-face-private.h"
#include "hb-font-private.h"
#include "hb-shape-plan-private.h"

static hb_shape_plan_t hb_shape_plan_nil = {
  REF_CNT_INVALID_VAL, 
  TRUE,//default_shaper_list
  NULL,//face
  HB_SEGMENT_PROPERTIES_DEFAULT,//props
  NULL,//shaper_func
  NULL,//shaper_name
  {
#ifdef HAVE_GRAPHITE2
    HB_SHAPER_DATA_INVALID,
#endif
#ifdef HAVE_OT
    HB_SHAPER_DATA_INVALID,
#endif
    HB_SHAPER_DATA_INVALID//fallback
  }
};

//TODO no user-feature caching for now.
struct hb_shape_plan_proposal_t
{
  const hb_segment_properties_t props;
  const char * const *shaper_list;
  hb_shape_func_t *shaper_func;
};

hb_shape_plan_t *
hb_shape_plan_get_empty(void)
{
  return &hb_shape_plan_nil;
}

#ifdef HAVE_GRAPHITE2
static inline hb_bool_t
hb_graphite2_shaper_face_data_ensure(hb_face_t *face)
{
  while (1) {
    struct hb_graphite2_shaper_face_data_t *data = hb_atomic_ptr_get(
                                                  &face->shaper_data.graphite2);
    if (data)
      return !HB_SHAPER_DATA_IS_INVALID(data);

    if (!data)
      data = hb_graphite2_shaper_face_data_create(face);

    if (!data)
      data = HB_SHAPER_DATA_INVALID;

    void *expected = NULL;
    if (hb_atomic_ptr_cmpexch(&face->shaper_data.graphite2, &expected, &data))
      return !HB_SHAPER_DATA_IS_INVALID(data);

    if (data != HB_SHAPER_DATA_INVALID && data != HB_SHAPER_DATA_SUCCEEDED)
      hb_graphite2_shaper_face_data_destroy(data);
  }
}

static inline hb_bool_t
hb_graphite2_shaper_font_data_ensure(hb_font_t *font)
{
  while (1) {
    struct hb_graphite2_shaper_font_data_t *data = hb_atomic_ptr_get(
                                                  &font->shaper_data.graphite2);
    if (data)
      return !HB_SHAPER_DATA_IS_INVALID(data);

    if (!data)
      data = hb_graphite2_shaper_font_data_create(font);

    if (!data)
      data = HB_SHAPER_DATA_INVALID;

    void *expected = NULL;
    if (hb_atomic_ptr_cmpexch(&font->shaper_data.graphite2, &expected, &data))
      return !HB_SHAPER_DATA_IS_INVALID(data);

    if (data != HB_SHAPER_DATA_INVALID && data != HB_SHAPER_DATA_SUCCEEDED)
      hb_graphite2_shaper_font_data_destroy(data);
  }
}
#endif

#ifdef HAVE_OT
static inline hb_bool_t
hb_ot_shaper_face_data_ensure(hb_face_t *face)
{
  while (1) {
    struct hb_ot_shaper_face_data_t *data = hb_atomic_ptr_get(
                                                         &face->shaper_data.ot);
    if (data)
      return !HB_SHAPER_DATA_IS_INVALID(data);

    if (!data)
      data = hb_ot_shaper_face_data_create(face);

    if (!data)
      data = HB_SHAPER_DATA_INVALID;

    void *expected = NULL;
    if (hb_atomic_ptr_cmpexch(&face->shaper_data.ot, &expected, &data))
      return !HB_SHAPER_DATA_IS_INVALID(data);

    if (data != HB_SHAPER_DATA_INVALID && data != HB_SHAPER_DATA_SUCCEEDED)
      hb_ot_shaper_face_data_destroy(data);
  }
}

static inline hb_bool_t
hb_ot_shaper_font_data_ensure(hb_font_t *font)
{
  while (1) {
    struct hb_ot_shaper_font_data_t *data = hb_atomic_ptr_get(
                                                         &font->shaper_data.ot);
    if (data)
      return !HB_SHAPER_DATA_IS_INVALID(data);

    if (!data)
      data = hb_ot_shaper_font_data_create(font);

    if (!data)
      data = HB_SHAPER_DATA_INVALID;

    void *expected = NULL;
    if (hb_atomic_ptr_cmpexch(&font->shaper_data.ot, &expected, &data))
      return !HB_SHAPER_DATA_IS_INVALID(data);

    if (data != HB_SHAPER_DATA_INVALID && data != HB_SHAPER_DATA_SUCCEEDED)
      hb_ot_shaper_font_data_destroy(data);
  }
}
#endif

static inline hb_bool_t
hb_fallback_shaper_face_data_ensure(hb_face_t *face)
{
  while (1) {
    struct hb_fallback_shaper_face_data_t *data = hb_atomic_ptr_get(
                                                  &face->shaper_data.fallback);
    if (data)
      return !HB_SHAPER_DATA_IS_INVALID(data);

    if (!data)
      data = hb_fallback_shaper_face_data_create(face);

    if (!data)
      data = HB_SHAPER_DATA_INVALID;

    void *expected = NULL;
    if (hb_atomic_ptr_cmpexch(&face->shaper_data.fallback, &expected, &data))
      return !HB_SHAPER_DATA_IS_INVALID(data);

    if (data != HB_SHAPER_DATA_INVALID && data != HB_SHAPER_DATA_SUCCEEDED)
      hb_fallback_shaper_face_data_destroy(data);
  }
}

static inline hb_bool_t
hb_fallback_shaper_font_data_ensure(hb_font_t *font)
{
  while (1) {
    struct hb_fallback_shaper_font_data_t *data = hb_atomic_ptr_get(
                                                  &font->shaper_data.fallback);
    if (data)
      return !HB_SHAPER_DATA_IS_INVALID(data);

    if (!data)
      data = hb_fallback_shaper_font_data_create(font);

    if (!data)
      data = HB_SHAPER_DATA_INVALID;

    void *expected = NULL;
    if (hb_atomic_ptr_cmpexch(&font->shaper_data.fallback, &expected, &data))
      return !HB_SHAPER_DATA_IS_INVALID(data);

    if (data != HB_SHAPER_DATA_INVALID && data != HB_SHAPER_DATA_SUCCEEDED)
      hb_fallback_shaper_font_data_destroy(data);
  }
}

static void
hb_shape_plan_plan(hb_shape_plan_t *shape_plan,
                   const hb_feature_t *user_features,
                   unsigned num_user_features,
                   const char * const *shaper_list)
{
  struct hb_shaper_pair_t *shapers = hb_shapers_get();

  if (!shaper_list) {
    for (unsigned i = 0; i < HB_SHAPERS_COUNT; ++i)
      if (0)
        ;
#ifdef HAVE_GRAPHITE2
      else if (shapers[i].func == hb_graphite2_shape) {
        if (hb_graphite2_shaper_face_data_ensure(shape_plan->face)) {
          shape_plan->shaper_data.graphite2 =
                hb_graphite2_shaper_shape_plan_data_create(shape_plan,
                                                           user_features,
                                                           num_user_features);
          shape_plan->shaper_func = hb_graphite2_shape;
          shape_plan->shaper_name ="graphite2";
          return;
        }
      }
#endif
#ifdef HAVE_OT
      else if (shapers[i].func == hb_ot_shape) {
        if (hb_ot_shaper_face_data_ensure(shape_plan->face)) {
          shape_plan->shaper_data.ot = hb_ot_shaper_shape_plan_data_create(
                                                             shape_plan,
                                                             user_features,
                                                             num_user_features);
          shape_plan->shaper_func = hb_ot_shape;
          shape_plan->shaper_name ="ot";
          return;
        }
      }
#endif
      else if (shapers[i].func == hb_fallback_shape) {
        if (hb_fallback_shaper_face_data_ensure(shape_plan->face)) {
          shape_plan->shaper_data.fallback =
                 hb_fallback_shaper_shape_plan_data_create(shape_plan,
                                                           user_features,
                                                           num_user_features);
          shape_plan->shaper_func = hb_fallback_shape;
          shape_plan->shaper_name = "fallback";
          return;
        }
      }
  } else {
    for (; *shaper_list; ++shaper_list)
      if (0)
        ;
#ifdef HAVE_GRAPHITE2
      else if (0 == strcmp(*shaper_list, "graphite2")) {
        if (hb_graphite2_shaper_face_data_ensure(shape_plan->face)) {
          shape_plan->shaper_data.graphite2 =
                hb_graphite2_shaper_shape_plan_data_create(shape_plan,
                                                           user_features,
                                                           num_user_features);
          shape_plan->shaper_func = hb_graphite2_shape;
          shape_plan->shaper_name = "graphite2";
          return;
        }
      }
#endif
#ifdef HAVE_OT
      else if (0 == strcmp(*shaper_list, "ot")) {
        if (hb_ot_shaper_face_data_ensure(shape_plan->face)) {
          shape_plan->shaper_data.ot = hb_ot_shaper_shape_plan_data_create(
                                                             shape_plan,
                                                             user_features,
                                                             num_user_features);
          shape_plan->shaper_func = hb_ot_shape;
          shape_plan->shaper_name = "ot";
          return;
        }
      }
#endif
      else if (0 == strcmp(*shaper_list, "fallback")) {
        if (hb_fallback_shaper_face_data_ensure(shape_plan->face)) {
          shape_plan->shaper_data.fallback =
                 hb_fallback_shaper_shape_plan_data_create(shape_plan,
                                                             user_features,
                                                             num_user_features);
          shape_plan->shaper_func = hb_fallback_shape;
          shape_plan->shaper_name = "fallback";
          return;
        }
      }
  }
}

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)
{
  assert(props->direction != HB_DIRECTION_INVALID);

  if (!face)
    face = hb_face_get_empty();

  if (!props || hb_atomic_int32_get(&face->ref_cnt) == REF_CNT_INVALID_VAL)
    return hb_shape_plan_get_empty();

  hb_shape_plan_t *shape_plan = calloc(1, sizeof(*shape_plan));
  if (!shape_plan)
    return hb_shape_plan_get_empty();
  hb_atomic_int32_set(&shape_plan->ref_cnt, 1);

  shape_plan->default_shaper_list = shaper_list == NULL;
  hb_face_make_immutable(face);
  shape_plan->face = hb_face_reference(face);
  shape_plan->props = *props;

  hb_shape_plan_plan(shape_plan, user_features, num_user_features, shaper_list);
  return shape_plan;
}

static hb_bool_t
hb_shape_plan_matches(hb_shape_plan_t *shape_plan,
                      struct hb_shape_plan_proposal_t *proposal)
{
  return hb_segment_properties_equal(&shape_plan->props, &proposal->props) &&
          ((shape_plan->default_shaper_list && proposal->shaper_list == NULL) ||
                            (shape_plan->shaper_func == proposal->shaper_func));
}

hb_shape_plan_t *
hb_shape_plan_reference(hb_shape_plan_t *shape_plan)
{
  if (hb_atomic_int32_get(&shape_plan->ref_cnt) != REF_CNT_INVALID_VAL)
    hb_atomic_int32_add(&shape_plan->ref_cnt, 1);
  return shape_plan;
}

void
hb_shape_plan_destroy(hb_shape_plan_t *shape_plan)
{
  if (!shape_plan)
    return;
  if (hb_atomic_int32_get(&shape_plan->ref_cnt) == REF_CNT_INVALID_VAL)
    return;
  hb_atomic_int32_add(&shape_plan->ref_cnt, -1);
  if (hb_atomic_int32_get(&shape_plan->ref_cnt) > 0)
    return;
  hb_atomic_int32_set(&shape_plan->ref_cnt, REF_CNT_INVALID_VAL);

#ifdef HAVE_GRAPHITE2
  if (shape_plan->shaper_data.graphite2 &&
      shape_plan->shaper_data.graphite2 != HB_SHAPER_DATA_INVALID &&
      shape_plan->shaper_data.graphite2 != HB_SHAPER_DATA_SUCCEEDED)
    hb_graphite2_shaper_shape_plan_data_destroy(
                                             shape_plan->shaper_data.graphite2);
#endif
#ifdef HAVE_OT
  if (shape_plan->shaper_data.ot &&
      shape_plan->shaper_data.ot != HB_SHAPER_DATA_INVALID &&
      shape_plan->shaper_data.ot != HB_SHAPER_DATA_SUCCEEDED)
    hb_ot_shaper_shape_plan_data_destroy(shape_plan->shaper_data.ot);
#endif
  if (shape_plan->shaper_data.fallback &&
      shape_plan->shaper_data.fallback != HB_SHAPER_DATA_INVALID &&
      shape_plan->shaper_data.fallback != HB_SHAPER_DATA_SUCCEEDED)
    hb_fallback_shaper_shape_plan_data_destroy(
                                              shape_plan->shaper_data.fallback);

  hb_face_destroy(shape_plan->face);
  free(shape_plan);
}

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)
{
  if (num_user_features)
    return hb_shape_plan_create(face, props, user_features, num_user_features,
                                                                   shaper_list);

  struct hb_shape_plan_proposal_t proposal = {
    *props,
    shaper_list,
    NULL
  };

  if (shaper_list) {
    //Choose shaper.  Adapted from hb_shape_plan_plan().
    for (const char * const *shaper_item = shaper_list; *shaper_item;
                                                                  shaper_item++)
      if (0)
	;
#ifdef HAVE_GRAPHITE2
      else if (0 == strcmp(*shaper_item, "graphite2")) {
         if (hb_graphite2_shaper_face_data_ensure(face))
           proposal.shaper_func = hb_graphite2_shape;
      }
#endif
#ifdef HAVE_OT
      else if (0 == strcmp(*shaper_item, "ot")) {
         if (hb_ot_shaper_face_data_ensure(face))
           proposal.shaper_func = hb_ot_shape;
      }
#endif
      else if (0 == strcmp(*shaper_item, "fallback")) {
         if (hb_fallback_shaper_face_data_ensure(face))
           proposal.shaper_func = hb_fallback_shape;
      }

    if (!proposal.shaper_list)
      return hb_shape_plan_get_empty();
  }

  struct plan_node_t *cached_plan_nodes = hb_atomic_ptr_get(&face->shape_plans);
  hb_shape_plan_t *shape_plan;
  while (1) {
    for (struct plan_node_t *node = cached_plan_nodes; node; node = node->next)
      if (hb_shape_plan_matches(node->shape_plan, &proposal))
        return hb_shape_plan_reference(node->shape_plan);

    //Not found.

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

    struct plan_node_t *node = calloc(1, sizeof(*node));
    if (!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))
      break;

    hb_shape_plan_destroy(shape_plan);
    free(node);
  }

  //Release our reference on face.
  hb_face_destroy(face);
  return hb_shape_plan_reference(shape_plan);
}

hb_bool_t
hb_shape_plan_execute(hb_shape_plan_t *shape_plan,
                      hb_font_t *font,
                      hb_buffer_t *buffer,
                      const hb_feature_t *features,
                      unsigned num_features)
{
  if (hb_atomic_int32_get(&shape_plan->ref_cnt) == REF_CNT_INVALID_VAL
      || hb_atomic_int32_get(&font->ref_cnt) == REF_CNT_INVALID_VAL
      || hb_atomic_int32_get(&buffer->ref_cnt) == REF_CNT_INVALID_VAL)
    return FALSE;

  assert(shape_plan->face == font->face);
  assert(hb_segment_properties_equal(&shape_plan->props, &buffer->props));

  if (0)
    ;
#ifdef HAVE_GRAPHITE2
  else if (shape_plan->shaper_func == hb_graphite2_shape)
    return shape_plan->shaper_data.graphite2
           && hb_graphite2_shaper_font_data_ensure(font)
           && hb_graphite2_shape(shape_plan, font, buffer, features,
                                                                  num_features);
#endif
#ifdef HAVE_OT
  else if (shape_plan->shaper_func == hb_ot_shape)
    return shape_plan->shaper_data.ot
           && hb_ot_shaper_font_data_ensure(font)
           && hb_ot_shape(shape_plan, font, buffer, features, num_features);
#endif
  else if (shape_plan->shaper_func == hb_fallback_shape)
    return shape_plan->shaper_data.fallback
           && hb_fallback_shaper_font_data_ensure(font)
           && hb_fallback_shape(shape_plan, font, buffer, features,
                                                                  num_features);
  return FALSE;
}



Mode Type Size Ref File
100644 blob 200 1a0b33b52f3f0b8f3567e0fb7b411e88c4b3c2c9 ABBREVIATIONS
100644 blob 119 8bbabe4337eabadd3f5ed07192b85e02d50f5bad FIXME
100644 blob 100 8a85692a2fc36a478ef9662ba2db214375a79254 README
100644 blob 223 7f27bbbdef02188df78858a31e0c38bda3fe4443 harfbuzz.pc.in
100644 blob 1478 2931ccaca651b04a927605232dd3500f7a661dce hb-atomic-private.h
100644 blob 120 ee630543fb4ef3e9fa49f7362026407c0fd0a858 hb-blob-private.h
100644 blob 4983 607f12b39c30bbe160c4e3a6781b5a58b64de2fc hb-blob.c
100644 blob 3467 d3d0f41b11c3b100cdee91525924c23226acd0cf hb-blob.h
100644 blob 1299 045519300e0994a11484696fa5c533663e4338db hb-buffer-private.h
100644 blob 11835 a3b6deceb732205dfeb4b3798227732f4632ed8b hb-buffer.c
100644 blob 8424 87c4ce58e83744e68765567e4107533dae9d5b7f hb-buffer.h
100644 blob 3363 f5141f528597232bb1a0214b7f1469cc9af008b8 hb-common.c
100644 blob 12423 40c1887296421c6d63f32e479cd318d4c4d79d19 hb-common.h
100644 blob 1407 c4954fa1b2cadb133a419584de60fe16263f148b hb-coretext.h
100644 blob 1584 30ae4b1caf76dd03cc682eda7458728b5380ad6c hb-deprecated.h
100644 blob 1226 3ea9861f08c6fcc0422092c8bb38dd808b5c8757 hb-face-private.h
100644 blob 5452 4489a99ae3f4ebded37dab45f8cde0bf32cc43f1 hb-face.c
100644 blob 2815 f682c468de58095fb0a7c4cdd577b34a9cb78077 hb-face.h
100644 blob 3705 f027e1e59453b39ecb722e2c7ffd9126b579b1a5 hb-fallback-shape.c
100644 blob 3222 172e69a09927c17f177947f2d5f18318dd3f4545 hb-font-private.h
100644 blob 30726 ba271fab87b5d9d6534bf971604b6079eabfc9f5 hb-font.c
100644 blob 12957 7273db43edaf542ef9d6e969fa2d8408e7d5100e hb-font.h
100644 blob 2570 bb44c035ff48e7655a397e5b95872d75b92945ad hb-ft.c
100644 blob 1756 696251e143f31987eb599f8cb16db4f12ff8d901 hb-ft.h
100644 blob 4016 91eaa7075c5f9fe797de93912301d2e03d6b7248 hb-glib.c
100644 blob 1496 63a9d3366c58a0110881ad00d971830924b73a2a hb-glib.h
100644 blob 3185 4a88d569e8d3be4daa2eaf39192891426da46514 hb-gobject-structs.h
100644 blob 1334 ea1bd25df82d46a222436a1ad078d0fc95d5c303 hb-gobject.h
100644 blob 1441 3eae54acbd81dbf3e534c4ad2e42d57611f7024e hb-graphite2.h
100644 blob 1495 f2f35f0f2ce13b6b5c46453128e452ea9380eabf hb-icu.h
100644 blob 2180 7172c280ad7dbb708d6818d386bdbb6e852d80c8 hb-open-file-private.h
100644 blob 2659 c4079cec956598ccf9521c874408a618fe7c29dc hb-open-file.c
100644 blob 65 642c3d3d45fdc42619d000cc9f7b3c890bdc0190 hb-ot-face-private.h
100644 blob 2806 ecfd5b109b43f549366a61efff16daa92db87349 hb-ot-layout.c
100644 blob 9212 d2a314cad6cbf94ba04ff9eaf513a74714eec732 hb-ot-layout.h
100644 blob 753 42faaca3f7ebdafc1232c6810b5ea2124896a95b hb-ot-tag.c
100644 blob 1688 1bf12ab3c09b9aef612515871e587c7c9ca7e62b hb-ot-tag.h
100644 blob 1526 80739063991cf14c4b65cc9247f751226f7f7277 hb-ot.h
100644 blob 1528 ae4bc85a37900c9f54cb91986dadc216f620a358 hb-private.h
100644 blob 3544 bafdae96333f9c81a4b20a4435e6eadaa31a7d32 hb-set.h
100644 blob 1647 1a90aed635a86f8e1d7002ffa8c7d300a3dd8acf hb-shape-plan-private.h
100644 blob 15942 929804aae7b6e5271cef5e9f1d654dcce7a8ca95 hb-shape-plan.c
100644 blob 2747 8f54552f90ba504f6ba2e9cf01459621fc73f9b2 hb-shape-plan.h
100644 blob 1370 1ff445b5de783fb0aa041f34d75919614b21ad34 hb-shape.c
100644 blob 2232 10a35cb517e14c96670d71bbcff266fce23cad91 hb-shape.h
100644 blob 1129 78e3fe4e3529b39789c0763b7f8e1019b3d5a725 hb-shaper-private.h
100644 blob 2184 d1c8f9740fec1b4d19e0967f60391f1c5b17d155 hb-shaper.c
100644 blob 1261 86293900edd4d5fab1013bd0c32eeaad2abef41e hb-unicode-private.h
100644 blob 8084 462ddc1b9bcad35a3d4b3babb64772c93650e75c hb-unicode.c
100644 blob 14218 1c4e097b92d5ba628455fefe25b6f4697cc16f11 hb-unicode.h
100644 blob 1427 51887c8794bce4e37b35c4bb9c675baf55b63052 hb-uniscribe.h
100644 blob 5070 97eb7798f1dc725aad02cc651ffe859044647582 hb-utf-private.c
100644 blob 1423 636ce353798d395c780be99bb68350c3352f40a3 hb-utf-private.h
100644 blob 1849 43634f91ce8a10250c8279b19a1993843feb0f38 hb-version.h.in
100644 blob 1470 c5a938a3818c9aca6de00a961c1c8f73bb2c47db hb.h
100644 blob 7651 65c5ca88a67c30becee01c5a8816d964b03862f9 license.md
100755 blob 5264 01bff5ed2957a061fb9cc148058a73e2ab87b350 make
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/sylware/charfbuzz

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/sylware/charfbuzz

Clone this repository using git:
git clone git://git.rocketgit.com/user/sylware/charfbuzz

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main