2
1
mirror of https://github.com/qpdf/qpdf.git synced 2025-01-23 15:18:33 +00:00

Refactor QPDFObjectHandle::copyObject and rename to makeDirect

This commit is contained in:
m-holger 2022-11-15 12:00:51 +00:00 committed by Jay Berkenbilt
parent 585ecf17c8
commit 4325e6e4a7
2 changed files with 18 additions and 26 deletions

View File

@ -1628,7 +1628,7 @@ class QPDFObjectHandle
void objectWarning(std::string const& warning); void objectWarning(std::string const& warning);
void assertType(char const* type_name, bool istype); void assertType(char const* type_name, bool istype);
bool dereference(); bool dereference();
void copyObject(std::set<QPDFObjGen>& visited, bool stop_at_streams); void makeDirect(std::set<QPDFObjGen>& visited, bool stop_at_streams);
void disconnect(); void disconnect();
void setParsedOffset(qpdf_offset_t offset); void setParsedOffset(qpdf_offset_t offset);
void parseContentStream_internal( void parseContentStream_internal(

View File

@ -2218,19 +2218,10 @@ QPDFObjectHandle::unsafeShallowCopy()
} }
void void
QPDFObjectHandle::copyObject( QPDFObjectHandle::makeDirect(
std::set<QPDFObjGen>& visited, bool stop_at_streams) std::set<QPDFObjGen>& visited, bool stop_at_streams)
{ {
assertInitialized(); assertInitialized();
if (isStream()) {
QTC::TC(
"qpdf", "QPDFObjectHandle copy stream", stop_at_streams ? 0 : 1);
if (stop_at_streams) {
return;
}
throw std::runtime_error(
"attempt to make a stream into a direct object");
}
auto cur_og = getObjGen(); auto cur_og = getObjGen();
if (cur_og.getObj() != 0) { if (cur_og.getObj() != 0) {
@ -2243,40 +2234,41 @@ QPDFObjectHandle::copyObject(
visited.insert(cur_og); visited.insert(cur_og);
} }
if (isReserved()) {
throw std::logic_error("QPDFObjectHandle: attempting to make a"
" reserved object handle direct");
}
std::shared_ptr<QPDFObject> new_obj;
if (isBool() || isInteger() || isName() || isNull() || isReal() || if (isBool() || isInteger() || isName() || isNull() || isReal() ||
isString()) { isString()) {
new_obj = obj->copy(true); this->obj = obj->copy(true);
} else if (isArray()) { } else if (isArray()) {
std::vector<QPDFObjectHandle> items; std::vector<QPDFObjectHandle> items;
auto array = asArray(); auto array = asArray();
int n = array->getNItems(); int n = array->getNItems();
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
items.push_back(array->getItem(i)); items.push_back(array->getItem(i));
items.back().copyObject(visited, stop_at_streams); items.back().makeDirect(visited, stop_at_streams);
} }
new_obj = QPDF_Array::create(items); this->obj = QPDF_Array::create(items);
} else if (isDictionary()) { } else if (isDictionary()) {
std::map<std::string, QPDFObjectHandle> items; std::map<std::string, QPDFObjectHandle> items;
auto dict = asDictionary(); auto dict = asDictionary();
for (auto const& key: getKeys()) { for (auto const& key: getKeys()) {
items[key] = dict->getKey(key); items[key] = dict->getKey(key);
items[key].copyObject(visited, stop_at_streams); items[key].makeDirect(visited, stop_at_streams);
} }
new_obj = QPDF_Dictionary::create(items); this->obj = QPDF_Dictionary::create(items);
} else if (isStream()) {
QTC::TC(
"qpdf", "QPDFObjectHandle copy stream", stop_at_streams ? 0 : 1);
if (!stop_at_streams) {
throw std::runtime_error(
"attempt to make a stream into a direct object");
}
} else if (isReserved()) {
throw std::logic_error("QPDFObjectHandle: attempting to make a"
" reserved object handle direct");
} else { } else {
throw std::logic_error("QPDFObjectHandle::makeDirectInternal: " throw std::logic_error("QPDFObjectHandle::makeDirectInternal: "
"unknown object type"); "unknown object type");
} }
this->obj = new_obj;
if (cur_og.getObj()) { if (cur_og.getObj()) {
visited.erase(cur_og); visited.erase(cur_og);
} }
@ -2304,7 +2296,7 @@ void
QPDFObjectHandle::makeDirect(bool allow_streams) QPDFObjectHandle::makeDirect(bool allow_streams)
{ {
std::set<QPDFObjGen> visited; std::set<QPDFObjGen> visited;
copyObject(visited, allow_streams); makeDirect(visited, allow_streams);
} }
void void