From 90772fbdbb6a19d33b0487db8c4dbffdbd3ba3cf Mon Sep 17 00:00:00 2001 From: Daniel Hammerschmid Date: Thu, 12 Mar 2026 15:00:54 +0100 Subject: [PATCH] HPDF_StructureElement_AddChild may now move an element from one parent to another --- include/hpdf_error.h | 1 - include/hpdf_objects.h | 5 +++++ include/hpdf_structure_element.h | 4 ++++ src/hpdf_array.c | 28 ++++++++++++++++++++++++++++ src/hpdf_structure_element.c | 25 +++++++++++++++++++++---- 5 files changed, 58 insertions(+), 5 deletions(-) diff --git a/include/hpdf_error.h b/include/hpdf_error.h index 7a86b0cd..0ecc14ff 100644 --- a/include/hpdf_error.h +++ b/include/hpdf_error.h @@ -146,7 +146,6 @@ extern "C" { #define HPDF_NAME_CANNOT_GET_NAMES 0x1084 #define HPDF_INVALID_ICC_COMPONENT_NUM 0x1085 #define HPDF_PAGE_INVALID_OPERATOR_STACK 0x1086 -#define HPDF_STRUCTURE_ELEMENT_CANNOT_SET_PARENT 0x1087 /*---------------------------------------------------------------------------*/ diff --git a/include/hpdf_objects.h b/include/hpdf_objects.h index 028d9794..8d7cafc5 100644 --- a/include/hpdf_objects.h +++ b/include/hpdf_objects.h @@ -365,6 +365,11 @@ HPDF_Array_Add (HPDF_Array array, void *obj); +HPDF_STATUS +HPDF_Array_Remove (HPDF_Array array, + void *obj); + + HPDF_STATUS HPDF_Array_Insert (HPDF_Array array, void *target, diff --git a/include/hpdf_structure_element.h b/include/hpdf_structure_element.h index f8c6a701..e9976d9c 100644 --- a/include/hpdf_structure_element.h +++ b/include/hpdf_structure_element.h @@ -43,6 +43,10 @@ HPDF_STATUS HPDF_StructureElement_AddChild (HPDF_StructureElement parent, HPDF_StructureElement child); +HPDF_STATUS +HPDF_StructureElement_RemoveChild (HPDF_StructureElement parent, + HPDF_StructureElement child); + HPDF_STATUS HPDF_StructureElement_AddMarkedContentSequence (HPDF_StructureElement structure_element, HPDF_UINT mcid, diff --git a/src/hpdf_array.c b/src/hpdf_array.c index 14715eae..9f85fa1f 100644 --- a/src/hpdf_array.c +++ b/src/hpdf_array.c @@ -225,6 +225,34 @@ HPDF_Array_Add (HPDF_Array array, return ret; } +HPDF_STATUS +HPDF_Array_Remove (HPDF_Array array, + void *target) +{ + HPDF_UINT i; + + for (i = 0; i < array->list->count; i++) { + void *ptr = HPDF_List_ItemAt (array->list, i); + void *obj_ptr; + + HPDF_Obj_Header *header = (HPDF_Obj_Header *)ptr; + if (header->obj_class == HPDF_OCLASS_PROXY) + obj_ptr = ((HPDF_Proxy)ptr)->obj; + else + obj_ptr = ptr; + + if (obj_ptr == target) { + HPDF_List_RemoveByIndex (array->list, i); + + if (header->obj_class == HPDF_OCLASS_PROXY) + HPDF_Obj_Free (array->mmgr, ptr); + + return HPDF_OK; + } + } + + return HPDF_ITEM_NOT_FOUND; +} HPDF_UINT HPDF_Array_Items (HPDF_Array array) diff --git a/src/hpdf_structure_element.c b/src/hpdf_structure_element.c index f2de847b..9e772e90 100644 --- a/src/hpdf_structure_element.c +++ b/src/hpdf_structure_element.c @@ -264,8 +264,11 @@ HPDF_StructureElement_AddChild (HPDF_StructureElement parent, HPDF_PTRACE((" HPDF_StructureElement_AddChild\n")); - if (HPDF_Dict_GetItem (child, "P", HPDF_OCLASS_DICT)) - return HPDF_SetError (parent->error, HPDF_STRUCTURE_ELEMENT_CANNOT_SET_PARENT, 0); + if (HPDF_Dict_GetItem (child, "P", HPDF_OCLASS_DICT)) { + HPDF_StructureElement old_parent = (HPDF_StructureElement)HPDF_Dict_GetItem (child, "P", HPDF_OCLASS_DICT); + if ((ret = HPDF_StructureElement_RemoveChild (old_parent, child)) != HPDF_OK) + return ret; + } if ((ret = HPDF_Dict_Add (child, "P", parent)) != HPDF_OK) return ret; @@ -283,6 +286,20 @@ HPDF_StructureElement_AddChild (HPDF_StructureElement parent, return HPDF_Array_Add (children, child); } +HPDF_STATUS +HPDF_StructureElement_RemoveChild (HPDF_StructureElement parent, + HPDF_StructureElement child) +{ + HPDF_PTRACE((" HPDF_StructureElement_RemoveChild\n")); + + HPDF_Array children = (HPDF_Array)HPDF_Dict_GetItem (parent, "K", HPDF_OCLASS_ARRAY); + if (!children) { + return HPDF_OK; + } + + return HPDF_Array_Remove (children, child); +} + HPDF_STATUS HPDF_StructureElement_AddMarkedContentSequence (HPDF_StructureElement structure_element, HPDF_UINT mcid, @@ -292,7 +309,7 @@ HPDF_StructureElement_AddMarkedContentSequence (HPDF_StructureElement structure HPDF_Dict pg; HPDF_STATUS ret; - HPDF_PTRACE((" HPDF_StructureElement_AddChild\n")); + HPDF_PTRACE((" HPDF_StructureElement_AddMarkedContentSequence\n")); pg = (HPDF_Dict)HPDF_Dict_GetItem (structure_element, "Pg", HPDF_OCLASS_DICT); if (!pg) { @@ -343,7 +360,7 @@ HPDF_StructureElement_AddObjectReference (HPDF_StructureElement structure_elemen HPDF_Array children; HPDF_STATUS ret; - HPDF_PTRACE((" HPDF_StructureElement_SetObjectReference\n")); + HPDF_PTRACE((" HPDF_StructureElement_AddObjectReference\n")); HPDF_Dict objr = HPDF_Dict_New (structure_element->mmgr); if (!objr)