Merge pull request #5407 from harfbuzz/kbts-touchup

[kbts] Clean up font initialization
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2025-07-23 23:55:44 -06:00
commit 3750c0e991
1 file changed
+39 -36
+39 -36
View File
@@ -44,13 +44,6 @@
hb_kbts_face_data_t *
_hb_kbts_shaper_face_data_create (hb_face_t *face)
{
kbts_font *kb_font = nullptr;
void *data = nullptr;
void *scratch = nullptr;
void *memory = nullptr;
size_t scratch_size = 0;
size_t memory_size = 0;
hb_blob_t *blob = hb_face_reference_blob (face);
unsigned int blob_length;
@@ -58,45 +51,47 @@ _hb_kbts_shaper_face_data_create (hb_face_t *face)
if (unlikely (!blob_length))
{
DEBUG_MSG (KBTS, blob, "Empty blob");
goto fail;
hb_blob_destroy (blob);
return nullptr;
}
data = hb_malloc (blob_length);
if (unlikely (!data))
goto fail;
void *data = hb_malloc (blob_length);
if (likely (data))
hb_memcpy (data, blob_data, blob_length);
kb_font = (kbts_font *) hb_calloc (1, sizeof (kbts_font));
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);
goto fail;
return nullptr;
}
// Copy the blob data because kbts_ReadFontData() will modify it.
hb_memcpy (data, blob_data, blob_length);
// The `data` and `memory` pointers will be freed by kbts_FreeFont().
scratch_size = kbts_ReadFontHeader (kb_font, data, blob_length);
scratch = hb_malloc (scratch_size);
memory_size = kbts_ReadFontData (kb_font, scratch, scratch_size);
memory = scratch;
if (memory_size > scratch_size)
memory = hb_realloc (memory, memory_size);
kbts_PostReadFontInitialize (kb_font, memory, memory_size);
if (unlikely (!kbts_FontIsValid(kb_font)))
size_t memory_size;
{
DEBUG_MSG (KBTS, face, "kbts_font creation failed");
_hb_kbts_shaper_face_data_destroy ((hb_kbts_face_data_t *) kb_font);
kb_font = nullptr;
goto fail;
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);
}
fail:
hb_blob_destroy (blob);
void *memory = hb_malloc (memory_size);
if (unlikely (!kbts_PostReadFontInitialize (kb_font, memory, memory_size)))
{
DEBUG_MSG (KBTS, face, "kbts_PostReadFontInitialize failed");
hb_free (memory);
hb_free (data);
hb_free (kb_font);
return nullptr;
}
return (hb_kbts_face_data_t *) kb_font;
}
@@ -105,6 +100,9 @@ void
_hb_kbts_shaper_face_data_destroy (hb_kbts_face_data_t *data)
{
kbts_font *font = (kbts_font *) data;
assert (kbts_FontIsValid (font));
hb_free (font->FileBase);
hb_free (font->GlyphLookupMatrix);
hb_free (font);
@@ -140,10 +138,10 @@ _hb_kbts_shape (hb_shape_plan_t *shape_plan,
case HB_DIRECTION_BTT:
DEBUG_MSG (KBTS, face, "Vertical direction is not supported");
return false;
default:
case HB_DIRECTION_INVALID:
DEBUG_MSG (KBTS, face, "Invalid direction");
return false;
default: break;
}
kbts_script kb_script = KBTS_SCRIPT_DONT_KNOW;
@@ -205,6 +203,11 @@ _hb_kbts_shape (hb_shape_plan_t *shape_plan,
{
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);