2
1
mirror of https://github.com/qpdf/qpdf.git synced 2025-01-23 07:08:30 +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 assertType(char const* type_name, bool istype);
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 setParsedOffset(qpdf_offset_t offset);
void parseContentStream_internal(

View File

@ -2218,19 +2218,10 @@ QPDFObjectHandle::unsafeShallowCopy()
}
void
QPDFObjectHandle::copyObject(
QPDFObjectHandle::makeDirect(
std::set<QPDFObjGen>& visited, bool stop_at_streams)
{
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();
if (cur_og.getObj() != 0) {
@ -2243,40 +2234,41 @@ QPDFObjectHandle::copyObject(
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() ||
isString()) {
new_obj = obj->copy(true);
this->obj = obj->copy(true);
} else if (isArray()) {
std::vector<QPDFObjectHandle> items;
auto array = asArray();
int n = array->getNItems();
for (int i = 0; i < n; ++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()) {
std::map<std::string, QPDFObjectHandle> items;
auto dict = asDictionary();
for (auto const& key: getKeys()) {
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 {
throw std::logic_error("QPDFObjectHandle::makeDirectInternal: "
"unknown object type");
}
this->obj = new_obj;
if (cur_og.getObj()) {
visited.erase(cur_og);
}
@ -2304,7 +2296,7 @@ void
QPDFObjectHandle::makeDirect(bool allow_streams)
{
std::set<QPDFObjGen> visited;
copyObject(visited, allow_streams);
makeDirect(visited, allow_streams);
}
void