From 46bc4e95f90feb3222b764f55f4de15d428ae0af Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Fri, 28 Nov 2025 03:03:57 +0200 Subject: [PATCH] [kbts] Update to version 2.0 --- src/hb-kbts.cc | 148 ++++++++-------------- subprojects/kbts.wrap | 2 +- test/shape/data/in-house/tests/kbts.tests | 14 +- 3 files changed, 61 insertions(+), 103 deletions(-) diff --git a/src/hb-kbts.cc b/src/hb-kbts.cc index 0159256b5..f3d18f536 100644 --- a/src/hb-kbts.cc +++ b/src/hb-kbts.cc @@ -31,6 +31,9 @@ #define KB_TEXT_SHAPE_IMPLEMENTATION #define KB_TEXT_SHAPE_STATIC #define KB_TEXT_SHAPE_NO_CRT +#define KBTS_MALLOC(a, b) hb_malloc(b) +#define KBTS_FREE(a, b) hb_free(b) +#define KBTS_MEMCPY hb_memcpy #define KBTS_MEMSET hb_memset #pragma GCC diagnostic push @@ -55,40 +58,21 @@ _hb_kbts_shaper_face_data_create (hb_face_t *face) return nullptr; } - void *data = hb_malloc (blob_length); - if (likely (data)) - hb_memcpy (data, blob_data, blob_length); - - hb_blob_destroy (blob); - blob = nullptr; - - if (unlikely (!data)) - { - DEBUG_MSG (KBTS, face, "Failed to allocate memory for font data"); - return nullptr; - } - kbts_font *kb_font = (kbts_font *) hb_calloc (1, sizeof (kbts_font)); if (unlikely (!kb_font)) { - hb_free (data); + hb_blob_destroy (blob); return nullptr; } - size_t memory_size; - { - unsigned scratch_size = kbts_ReadFontHeader (kb_font, data, blob_length); - void *scratch = hb_malloc (scratch_size); - memory_size = kbts_ReadFontData (kb_font, scratch, scratch_size); - hb_free (scratch); - } + *kb_font = kbts_FontFromMemory((void *)blob_data, blob_length, face->index, nullptr, nullptr); + hb_blob_destroy (blob); + blob = nullptr; - void *memory = hb_malloc (memory_size); - if (unlikely (!kbts_PostReadFontInitialize (kb_font, memory, memory_size))) + if (unlikely (!kbts_FontIsValid (kb_font))) { - DEBUG_MSG (KBTS, face, "kbts_PostReadFontInitialize failed"); - hb_free (memory); - hb_free (data); + DEBUG_MSG (KBTS, face, "Failed create font from data"); + kbts_FreeFont (kb_font); hb_free (kb_font); return nullptr; } @@ -103,8 +87,7 @@ _hb_kbts_shaper_face_data_destroy (hb_kbts_face_data_t *data) assert (kbts_FontIsValid (font)); - hb_free (font->FileBase); - hb_free (font->GlyphLookupMatrix); + kbts_FreeFont (font); hb_free (font); } @@ -144,6 +127,10 @@ _hb_kbts_shape (hb_shape_plan_t *shape_plan, return false; } + kbts_glyph_storage *kb_glyph_storage = kbts_PlaceGlyphStorage (nullptr, nullptr, hb_malloc (kbts_SizeOfGlyphStorage ())); + if (unlikely (!kb_glyph_storage)) + return false; + kbts_script kb_script = KBTS_SCRIPT_DONT_KNOW; kbts_language kb_language = KBTS_LANGUAGE_DEFAULT; { @@ -167,93 +154,64 @@ _hb_kbts_shape (hb_shape_plan_t *shape_plan, kb_language = (kbts_language) hb_uint32_swap (language); } - hb_vector_t kb_glyphs; - if (unlikely (!kb_glyphs.resize_full (buffer->len, false, true))) - return false; - for (size_t i = 0; i < buffer->len; ++i) - kb_glyphs.arrayZ[i] = kbts_CodepointToGlyph (kb_font, buffer->info[i].codepoint); - - if (num_features) { - for (unsigned int i = 0; i < num_features; ++i) + kbts_glyph_config *kb_config = nullptr; + if (num_features) { - hb_feature_t feature = features[i]; - for (unsigned int j = 0; j < buffer->len; ++j) + hb_vector_t kb_features; + for (unsigned int j = 0; j < num_features; ++j) { - kbts_glyph *kb_glyph = &kb_glyphs.arrayZ[j]; - if (hb_in_range (j, feature.start, feature.end)) - { - if (!kb_glyph->Config) - kb_glyph->Config = (kbts_glyph_config *) hb_calloc (1, sizeof (kbts_glyph_config)); - kbts_glyph_config *config = kb_glyph->Config; - while (!kbts_GlyphConfigOverrideFeatureFromTag (config, hb_uint32_swap (feature.tag), - feature.value > 1, feature.value)) - { - config->FeatureOverrides = (kbts_feature_override *) hb_realloc (config->FeatureOverrides, - config->RequiredFeatureOverrideCapacity); - config->FeatureOverrideCapacity += 1; - } - } + if (hb_in_range (i, features[j].start, features[j].end)) + kb_features.push({ hb_uint32_swap (features[j].tag), (int)features[j].value }); } + if (kb_features) + kb_config = kbts_CreateGlyphConfig (kb_features.arrayZ, kb_features.length, nullptr, nullptr); } + kbts_PushGlyph (kb_glyph_storage, kb_font, buffer->info[i].codepoint, kb_config, buffer->info[i].cluster); } - kbts_shape_state *kb_shape_state; - { - size_t kb_shape_state_size = kbts_SizeOfShapeState (kb_font); - void *kb_shape_state_buffer = hb_malloc (kb_shape_state_size); - if (unlikely (!kb_shape_state_buffer)) - { - DEBUG_MSG (KBTS, face, "Failed to allocate memory for shape state"); - return false; - } - kb_shape_state = kbts_PlaceShapeState (kb_shape_state_buffer, kb_shape_state_size); - } - kbts_shape_config kb_shape_config = kbts_ShapeConfig (kb_font, kb_script, kb_language); - uint32_t glyph_count = buffer->len; - uint32_t glyph_capacity = kb_glyphs.length; - while (kbts_Shape (kb_shape_state, &kb_shape_config, KBTS_DIRECTION_LTR, kb_direction, - kb_glyphs.arrayZ, &glyph_count, glyph_capacity)) - { - glyph_capacity = kb_shape_state->RequiredGlyphCapacity; - /* kb increases capacity by a fixed number only. We increase it by 50% to - * avoid O(n^2) behavior in case of expanding text. - * - * https://github.com/JimmyLefevre/kb/issues/32 - */ - glyph_capacity += glyph_capacity / 2; - if (unlikely (!kb_glyphs.resize_full (glyph_capacity, false, true))) - return false; - } + uint32_t glyph_count = 0; + hb_glyph_info_t *info = buffer->info; + hb_glyph_position_t *pos = buffer->pos; + kbts_glyph *kb_glyph = nullptr; + kbts_glyph_iterator kb_output; + + kbts_shape_config *kb_shape_config = kbts_CreateShapeConfig (kb_font, kb_script, kb_language, nullptr, nullptr); + hb_bool_t res = kbts_ShapeDirect (kb_shape_config, kb_glyph_storage, kb_direction, + nullptr, nullptr, &kb_output) == KBTS_SHAPE_ERROR_NONE; + if (unlikely (!res)) + goto done; + + for (auto it = kb_output; kbts_GlyphIteratorNext (&it, &kb_glyph); ) + glyph_count += 1; hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_GLYPHS); hb_buffer_set_length (buffer, glyph_count); - hb_glyph_info_t *info = buffer->info; - hb_glyph_position_t *pos = buffer->pos; - buffer->clear_positions (); - for (size_t i = 0; i < glyph_count; ++i) - { - kbts_glyph kb_glyph = kb_glyphs.arrayZ[i]; - info[i].codepoint = kb_glyph.Id; - info[i].cluster = 0; // FIXME - pos[i].x_advance = font->em_scalef_x (kb_glyph.AdvanceX); - pos[i].y_advance = font->em_scalef_y (kb_glyph.AdvanceY); - pos[i].x_offset = font->em_scalef_x (kb_glyph.OffsetX); - pos[i].y_offset = font->em_scalef_y (kb_glyph.OffsetY); - if (kb_glyph.Config) - hb_free (kb_glyph.Config->FeatureOverrides); + for (auto it = kb_output; kbts_GlyphIteratorNext (&it, &kb_glyph); info++, pos++) + { + info->codepoint = kb_glyph->Id; + info->cluster = kb_glyph->UserIdOrCodepointIndex; + pos->x_advance = font->em_scalef_x (kb_glyph->AdvanceX); + pos->y_advance = font->em_scalef_y (kb_glyph->AdvanceY); + pos->x_offset = font->em_scalef_x (kb_glyph->OffsetX); + pos->y_offset = font->em_scalef_y (kb_glyph->OffsetY); } - hb_free (kb_shape_state); +done: + kbts_DestroyShapeConfig (kb_shape_config); + while (kbts_GlyphIteratorNext (&kb_output, &kb_glyph)) + kbts_DestroyGlyphConfig (kb_glyph->Config); + kbts_FreeAllGlyphs (kb_glyph_storage); + hb_free (kb_glyph_storage); buffer->clear_glyph_flags (); buffer->unsafe_to_break (); - return true; + return res; } #endif \ No newline at end of file diff --git a/subprojects/kbts.wrap b/subprojects/kbts.wrap index 0730ea66c..0e89575dc 100644 --- a/subprojects/kbts.wrap +++ b/subprojects/kbts.wrap @@ -1,5 +1,5 @@ [wrap-git] url = https://github.com/JimmyLefevre/kb.git -revision = 880ebea2d4d9ee9b2478eecd1ba060751adc5d45 +revision = 96b3fe37abefa0955d766e7f8b649a035737359c depth = 1 patch_directory = kbts \ No newline at end of file diff --git a/test/shape/data/in-house/tests/kbts.tests b/test/shape/data/in-house/tests/kbts.tests index eb2a9cce4..e11d110e5 100644 --- a/test/shape/data/in-house/tests/kbts.tests +++ b/test/shape/data/in-house/tests/kbts.tests @@ -1,8 +1,8 @@ @shapers=kbts -../fonts/872d2955d326bd6676a06f66b8238ebbaabc212f.ttf;;U+0628,U+0628,U+0628;[uni0628.fina=0+883|uni0628.medi_High=0+244|uni0628.init_High=0+233] -../fonts/872d2955d326bd6676a06f66b8238ebbaabc212f.ttf;--features=-calt[0];U+0628,U+0628,U+0628,U+0628,U+0628;[uni0628.fina=0+883|uni0628.medi=0+244|uni0628.medi_High=0+244|uni0628.medi=0+244|uni0628.init=0+190] -../fonts/872d2955d326bd6676a06f66b8238ebbaabc212f.ttf;--features=-calt;U+0628,U+0628,U+0628,U+0628,U+0628;[uni0628.fina=0+883|uni0628.medi=0+244|uni0628.medi=0+244|uni0628.medi=0+244|uni0628.init=0+190] -../fonts/7bbd3175734d5d291e1c15271ec0cbb97b626ebf.ttf;;U+0066,U+0066,U+0069;[f_f_i=0+795] -../fonts/7bbd3175734d5d291e1c15271ec0cbb97b626ebf.ttf;--features=-liga;U+0066,U+0066,U+0069;[f=0+300|f=0+300|i=0+263] -../fonts/7bbd3175734d5d291e1c15271ec0cbb97b626ebf.ttf;;U+0648,U+0628,U+0627;[alef-ar.fina=0+229|beh-ar.init=0+190|waw-ar=0@83,0+483] -#../fonts/7bbd3175734d5d291e1c15271ec0cbb97b626ebf.ttf;--features=ss01;U+0648,U+0628,U+0627;[alef-ar.fina=0+229|beh-ar.init.LD=0+190|waw-ar=0+400] +../fonts/872d2955d326bd6676a06f66b8238ebbaabc212f.ttf;;U+0628,U+0628,U+0628;[uni0628.fina=2+883|uni0628.medi_High=1+244|uni0628.init_High=0+233] +../fonts/872d2955d326bd6676a06f66b8238ebbaabc212f.ttf;--features=-calt[0];U+0628,U+0628,U+0628,U+0628,U+0628;[uni0628.fina=4+883|uni0628.medi=3+244|uni0628.medi_High=2+244|uni0628.medi=1+244|uni0628.init=0+190] +../fonts/872d2955d326bd6676a06f66b8238ebbaabc212f.ttf;--features=-calt;U+0628,U+0628,U+0628,U+0628,U+0628;[uni0628.fina=4+883|uni0628.medi=3+244|uni0628.medi=2+244|uni0628.medi=1+244|uni0628.init=0+190] +../fonts/7bbd3175734d5d291e1c15271ec0cbb97b626ebf.ttf;;U+0066,U+0066,U+0069,U+0066;[f_f_i=0+795|f=3+300] +../fonts/7bbd3175734d5d291e1c15271ec0cbb97b626ebf.ttf;--features=-liga;U+0066,U+0066,U+0069,U+0066;[f=0+300|f=1+300|i=2+263|f=3+300] +../fonts/7bbd3175734d5d291e1c15271ec0cbb97b626ebf.ttf;;U+0648,U+0628,U+0627;[alef-ar.fina=2+229|beh-ar.init=1+190|waw-ar=0@83,0+483] +#../fonts/7bbd3175734d5d291e1c15271ec0cbb97b626ebf.ttf;--features=ss01;U+0648,U+0628,U+0627;[alef-ar.fina=2+229|beh-ar.init.LD=1+190|waw-ar=0+400]