From 5901fcad4c33b6058569e93ec9eeaa12cc880469 Mon Sep 17 00:00:00 2001 From: m-holger Date: Sun, 6 Feb 2022 06:37:20 +0000 Subject: C-API expose QPDFObjectHandle::getKeyIfDict --- include/qpdf/qpdf-c.h | 2 ++ libqpdf/qpdf-c.cc | 9 +++++++++ qpdf/qpdf-ctest.c | 11 +++++++++++ qpdf/qpdf.testcov | 1 + 4 files changed, 23 insertions(+) diff --git a/include/qpdf/qpdf-c.h b/include/qpdf/qpdf-c.h index c4e88de8..a7e2d042 100644 --- a/include/qpdf/qpdf-c.h +++ b/include/qpdf/qpdf-c.h @@ -812,6 +812,8 @@ extern "C" { QPDF_BOOL qpdf_oh_has_key(qpdf_data qpdf, qpdf_oh oh, char const* key); QPDF_DLL qpdf_oh qpdf_oh_get_key(qpdf_data qpdf, qpdf_oh oh, char const* key); + QPDF_DLL + qpdf_oh qpdf_oh_get_key_if_dict(qpdf_data qpdf, qpdf_oh oh, char const* key); QPDF_DLL QPDF_BOOL qpdf_oh_is_or_has_name( diff --git a/libqpdf/qpdf-c.cc b/libqpdf/qpdf-c.cc index ac728e8b..e0b852a5 100644 --- a/libqpdf/qpdf-c.cc +++ b/libqpdf/qpdf-c.cc @@ -1550,6 +1550,15 @@ qpdf_oh qpdf_oh_get_key(qpdf_data qpdf, qpdf_oh oh, char const* key) }); } +qpdf_oh qpdf_oh_get_key_if_dict(qpdf_data qpdf, qpdf_oh oh, char const* key) +{ + return do_with_oh( + qpdf, oh, return_null(qpdf), [qpdf, key](QPDFObjectHandle& o) { + QTC::TC("qpdf", "qpdf-c called qpdf_oh_get_key_if_dict"); + return new_object(qpdf, o.getKeyIfDict(key)); + }); +} + QPDF_BOOL qpdf_oh_is_or_has_name(qpdf_data qpdf, qpdf_oh oh, char const* key) { return do_with_oh( diff --git a/qpdf/qpdf-ctest.c b/qpdf/qpdf-ctest.c index 7738bdf6..0867468a 100644 --- a/qpdf/qpdf-ctest.c +++ b/qpdf/qpdf-ctest.c @@ -592,6 +592,17 @@ static void test24(char const* infile, "/Encoding"); assert(strcmp(qpdf_oh_get_name(qpdf, encoding), "/WinAnsiEncoding") == 0); + qpdf_oh res = qpdf_oh_get_key_if_dict(qpdf, page1, "/Resources"); + assert(qpdf_oh_has_key(qpdf, res, "/Font")); + /* check no warning when called with null */ + while (qpdf_more_warnings(qpdf)) + { + qpdf_next_warning(qpdf); + } + res = qpdf_oh_get_key_if_dict( + qpdf, qpdf_oh_get_key_if_dict(qpdf, page1, "/Missing"), "/Font"); + assert(! qpdf_more_warnings(qpdf)); + /* Look at page contents to exercise stream functions */ qpdf_oh contents = qpdf_oh_get_key(qpdf, page1, "/Contents"); assert(qpdf_oh_is_stream(qpdf, contents)); diff --git a/qpdf/qpdf.testcov b/qpdf/qpdf.testcov index 5e87b831..f0594da8 100644 --- a/qpdf/qpdf.testcov +++ b/qpdf/qpdf.testcov @@ -504,6 +504,7 @@ qpdf-c called qpdf_oh_dict_more_keys 0 qpdf-c called qpdf_oh_dict_next_key 0 qpdf-c called qpdf_oh_has_key 0 qpdf-c called qpdf_oh_get_key 0 +qpdf-c called qpdf_oh_get_key_if_dict 0 qpdf-c called qpdf_oh_is_or_has_name 0 qpdf-c called qpdf_oh_new_null 0 qpdf-c called qpdf_oh_new_bool 0 -- cgit v1.2.3-54-g00ecf