2012-06-21 18:35:02 +00:00
|
|
|
// See the "Optimization" section of the manual.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-05-03 12:21:01 +00:00
|
|
|
#include <qpdf/assert_debug.h>
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDF.hh>
|
|
|
|
|
|
|
|
#include <qpdf/QPDFExc.hh>
|
|
|
|
#include <qpdf/QPDF_Array.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QPDF_Dictionary.hh>
|
|
|
|
#include <qpdf/QTC.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
QPDF::ObjUser::ObjUser() :
|
|
|
|
ou_type(ou_bad),
|
|
|
|
pageno(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDF::ObjUser::ObjUser(user_e type) :
|
|
|
|
ou_type(type),
|
|
|
|
pageno(0)
|
|
|
|
{
|
2022-05-03 12:21:01 +00:00
|
|
|
qpdf_assert_debug(type == ou_root);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDF::ObjUser::ObjUser(user_e type, int pageno) :
|
|
|
|
ou_type(type),
|
|
|
|
pageno(pageno)
|
|
|
|
{
|
2022-05-03 12:21:01 +00:00
|
|
|
qpdf_assert_debug((type == ou_page) || (type == ou_thumb));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDF::ObjUser::ObjUser(user_e type, std::string const& key) :
|
|
|
|
ou_type(type),
|
|
|
|
pageno(0),
|
|
|
|
key(key)
|
|
|
|
{
|
2022-05-03 12:21:01 +00:00
|
|
|
qpdf_assert_debug((type == ou_trailer_key) || (type == ou_root_key));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::ObjUser::operator<(ObjUser const& rhs) const
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->ou_type < rhs.ou_type) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (this->ou_type == rhs.ou_type) {
|
|
|
|
if (this->pageno < rhs.pageno) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (this->pageno == rhs.pageno) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return (this->key < rhs.key);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-25 14:50:55 +00:00
|
|
|
void
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDF::optimize(
|
|
|
|
std::map<int, int> const& object_stream_data,
|
|
|
|
bool allow_changes,
|
|
|
|
std::function<int(QPDFObjectHandle&)> skip_stream_parameters)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!this->m->obj_user_to_objects.empty()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// already optimized
|
|
|
|
return;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2013-06-15 01:35:10 +00:00
|
|
|
// The PDF specification indicates that /Outlines is supposed to
|
|
|
|
// be an indirect reference. Force it to be so if it exists and
|
|
|
|
// is direct. (This has been seen in the wild.)
|
|
|
|
QPDFObjectHandle root = getRoot();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (root.getKey("/Outlines").isDictionary()) {
|
2013-06-15 01:35:10 +00:00
|
|
|
QPDFObjectHandle outlines = root.getKey("/Outlines");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!outlines.isIndirect()) {
|
2013-06-15 01:35:10 +00:00
|
|
|
QTC::TC("qpdf", "QPDF_optimization indirect outlines");
|
|
|
|
root.replaceKey("/Outlines", makeIndirectObject(outlines));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// Traverse pages tree pushing all inherited resources down to the
|
2017-08-22 01:33:44 +00:00
|
|
|
// page level. This also initializes this->m->all_pages.
|
2012-06-22 16:52:13 +00:00
|
|
|
pushInheritedAttributesToPage(allow_changes, false);
|
2012-06-22 15:50:02 +00:00
|
|
|
|
|
|
|
// Traverse pages
|
2019-06-21 03:35:23 +00:00
|
|
|
int n = toI(this->m->all_pages.size());
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int pageno = 0; pageno < n; ++pageno) {
|
|
|
|
updateObjectMaps(
|
|
|
|
ObjUser(ObjUser::ou_page, pageno),
|
|
|
|
this->m->all_pages.at(toS(pageno)),
|
|
|
|
skip_stream_parameters);
|
2012-06-22 15:50:02 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// Traverse document-level items
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto const& key: this->m->trailer.getKeys()) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (key == "/Root") {
|
2022-02-08 14:18:08 +00:00
|
|
|
// handled separately
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
|
|
|
updateObjectMaps(
|
|
|
|
ObjUser(ObjUser::ou_trailer_key, key),
|
|
|
|
this->m->trailer.getKey(key),
|
|
|
|
skip_stream_parameters);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto const& key: root.getKeys()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Technically, /I keys from /Thread dictionaries are supposed
|
|
|
|
// to be handled separately, but we are going to disregard
|
|
|
|
// that specification for now. There is loads of evidence
|
|
|
|
// that pdlin and Acrobat both disregard things like this from
|
|
|
|
// time to time, so this is almost certain not to cause any
|
|
|
|
// problems.
|
2022-04-02 21:14:10 +00:00
|
|
|
updateObjectMaps(
|
|
|
|
ObjUser(ObjUser::ou_root_key, key),
|
|
|
|
root.getKey(key),
|
|
|
|
skip_stream_parameters);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjUser root_ou = ObjUser(ObjUser::ou_root);
|
2013-06-14 15:58:37 +00:00
|
|
|
QPDFObjGen root_og = QPDFObjGen(root.getObjGen());
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->obj_user_to_objects[root_ou].insert(root_og);
|
|
|
|
this->m->object_to_obj_users[root_og].insert(root_ou);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
filterCompressedObjects(object_stream_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-22 15:50:02 +00:00
|
|
|
QPDF::pushInheritedAttributesToPage()
|
|
|
|
{
|
|
|
|
// Public API should not have access to allow_changes.
|
2012-06-22 16:52:13 +00:00
|
|
|
pushInheritedAttributesToPage(true, false);
|
2012-06-22 15:50:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-22 16:52:13 +00:00
|
|
|
QPDF::pushInheritedAttributesToPage(bool allow_changes, bool warn_skipped_keys)
|
2012-06-21 13:12:51 +00:00
|
|
|
{
|
|
|
|
// Traverse pages tree pushing all inherited resources down to the
|
|
|
|
// page level.
|
|
|
|
|
2012-07-11 19:29:41 +00:00
|
|
|
// The record of whether we've done this is cleared by
|
|
|
|
// updateAllPagesCache(). If we're warning for skipped keys,
|
|
|
|
// re-traverse unconditionally.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->m->pushed_inherited_attributes_to_pages && (!warn_skipped_keys)) {
|
2012-07-11 19:29:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-01 19:30:25 +00:00
|
|
|
// Calling getAllPages() resolves any duplicated page objects,
|
|
|
|
// repairs broken nodes, and detects loops, so we don't have to do
|
|
|
|
// those activities here.
|
2019-01-29 01:13:10 +00:00
|
|
|
getAllPages();
|
|
|
|
|
2012-06-21 13:12:51 +00:00
|
|
|
// key_ancestors is a mapping of page attribute keys to a stack of
|
2012-06-22 15:50:02 +00:00
|
|
|
// Pages nodes that contain values for them.
|
2022-04-02 21:14:10 +00:00
|
|
|
std::map<std::string, std::vector<QPDFObjectHandle>> key_ancestors;
|
2012-06-22 15:50:02 +00:00
|
|
|
pushInheritedAttributesToPageInternal(
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->trailer.getKey("/Root").getKey("/Pages"),
|
2022-04-02 21:14:10 +00:00
|
|
|
key_ancestors,
|
|
|
|
allow_changes,
|
2022-07-12 17:40:30 +00:00
|
|
|
warn_skipped_keys);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!key_ancestors.empty()) {
|
|
|
|
throw std::logic_error("key_ancestors not empty after"
|
|
|
|
" pushing inherited attributes to pages");
|
2019-01-04 16:50:02 +00:00
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->pushed_inherited_attributes_to_pages = true;
|
2022-06-25 17:55:45 +00:00
|
|
|
this->m->ever_pushed_inherited_attributes_to_pages = true;
|
2012-06-21 13:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-22 15:50:02 +00:00
|
|
|
QPDF::pushInheritedAttributesToPageInternal(
|
2015-02-21 23:22:32 +00:00
|
|
|
QPDFObjectHandle cur_pages,
|
2022-04-02 21:14:10 +00:00
|
|
|
std::map<std::string, std::vector<QPDFObjectHandle>>& key_ancestors,
|
|
|
|
bool allow_changes,
|
2022-07-12 17:40:30 +00:00
|
|
|
bool warn_skipped_keys)
|
2015-02-21 23:22:32 +00:00
|
|
|
{
|
2022-07-12 19:16:00 +00:00
|
|
|
// Make a list of inheritable keys. Only the keys /MediaBox,
|
|
|
|
// /CropBox, /Resources, and /Rotate are inheritable
|
|
|
|
// attributes. Push this object onto the stack of pages nodes
|
|
|
|
// that have values for this attribute.
|
2022-02-08 14:18:08 +00:00
|
|
|
|
2022-07-12 19:16:00 +00:00
|
|
|
std::set<std::string> inheritable_keys;
|
|
|
|
for (auto const& key: cur_pages.getKeys()) {
|
|
|
|
if ((key == "/MediaBox") || (key == "/CropBox") ||
|
|
|
|
(key == "/Resources") || (key == "/Rotate")) {
|
|
|
|
if (!allow_changes) {
|
|
|
|
throw QPDFExc(
|
|
|
|
qpdf_e_internal,
|
|
|
|
this->m->file->getName(),
|
|
|
|
this->m->last_object_description,
|
|
|
|
this->m->file->getLastOffset(),
|
|
|
|
"optimize detected an "
|
|
|
|
"inheritable attribute when called "
|
|
|
|
"in no-change mode");
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
|
2022-07-12 19:16:00 +00:00
|
|
|
// This is an inheritable resource
|
|
|
|
inheritable_keys.insert(key);
|
|
|
|
QPDFObjectHandle oh = cur_pages.getKey(key);
|
|
|
|
QTC::TC(
|
|
|
|
"qpdf",
|
|
|
|
"QPDF opt direct pages resource",
|
|
|
|
oh.isIndirect() ? 0 : 1);
|
|
|
|
if (!oh.isIndirect()) {
|
|
|
|
if (!oh.isScalar()) {
|
|
|
|
// Replace shared direct object non-scalar
|
|
|
|
// resources with indirect objects to avoid
|
|
|
|
// copying large structures around.
|
|
|
|
cur_pages.replaceKey(key, makeIndirectObject(oh));
|
|
|
|
oh = cur_pages.getKey(key);
|
|
|
|
} else {
|
|
|
|
// It's okay to copy scalars.
|
|
|
|
QTC::TC("qpdf", "QPDF opt inherited scalar");
|
2012-06-22 16:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-12 19:16:00 +00:00
|
|
|
key_ancestors[key].push_back(oh);
|
|
|
|
if (key_ancestors[key].size() > 1) {
|
|
|
|
QTC::TC("qpdf", "QPDF opt key ancestors depth > 1");
|
|
|
|
}
|
|
|
|
// Remove this resource from this node. It will be
|
|
|
|
// reattached at the page level.
|
|
|
|
cur_pages.removeKey(key);
|
|
|
|
} else if (!((key == "/Type") || (key == "/Parent") ||
|
|
|
|
(key == "/Kids") || (key == "/Count"))) {
|
|
|
|
// Warn when flattening, but not if the key is at the top
|
|
|
|
// level (i.e. "/Parent" not set), as we don't change these;
|
|
|
|
// but flattening removes intermediate /Pages nodes.
|
|
|
|
if ((warn_skipped_keys) && (cur_pages.hasKey("/Parent"))) {
|
|
|
|
QTC::TC("qpdf", "QPDF unknown key not inherited");
|
|
|
|
setLastObjectDescription("Pages object", cur_pages.getObjGen());
|
|
|
|
warn(
|
|
|
|
qpdf_e_pages,
|
|
|
|
this->m->last_object_description,
|
|
|
|
0,
|
|
|
|
("Unknown key " + key +
|
|
|
|
" in /Pages object"
|
|
|
|
" is being discarded as a result of"
|
|
|
|
" flattening the /Pages tree"));
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2022-07-12 19:16:00 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-09-01 19:30:25 +00:00
|
|
|
// Process descendant nodes. This method does not perform loop
|
|
|
|
// detection because all code paths that lead here follow a call
|
|
|
|
// to getAllPages, which already throws an exception in the event
|
|
|
|
// of a loop in the pages tree.
|
2022-07-12 19:16:00 +00:00
|
|
|
for (auto& kid: cur_pages.getKey("/Kids").aitems()) {
|
|
|
|
if (kid.isDictionaryOfType("/Pages")) {
|
|
|
|
pushInheritedAttributesToPageInternal(
|
|
|
|
kid, key_ancestors, allow_changes, warn_skipped_keys);
|
|
|
|
} else {
|
|
|
|
// Add all available inheritable attributes not present in
|
|
|
|
// this object to this object.
|
|
|
|
for (auto const& iter: key_ancestors) {
|
|
|
|
std::string const& key = iter.first;
|
|
|
|
if (!kid.hasKey(key)) {
|
|
|
|
QTC::TC("qpdf", "QPDF opt resource inherited");
|
|
|
|
kid.replaceKey(key, iter.second.back());
|
|
|
|
} else {
|
|
|
|
QTC::TC("qpdf", "QPDF opt page resource hides ancestor");
|
2022-07-12 17:40:30 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2022-07-12 19:16:00 +00:00
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
|
2022-07-12 19:16:00 +00:00
|
|
|
// For each inheritable key, pop the stack. If the stack
|
|
|
|
// becomes empty, remove it from the map. That way, the
|
|
|
|
// invariant that the list of keys in key_ancestors is exactly
|
|
|
|
// those keys for which inheritable attributes are available.
|
2022-02-08 14:18:08 +00:00
|
|
|
|
2022-07-12 19:16:00 +00:00
|
|
|
if (!inheritable_keys.empty()) {
|
|
|
|
QTC::TC("qpdf", "QPDF opt inheritable keys");
|
|
|
|
for (auto const& key: inheritable_keys) {
|
|
|
|
key_ancestors[key].pop_back();
|
|
|
|
if (key_ancestors[key].empty()) {
|
|
|
|
QTC::TC("qpdf", "QPDF opt erase empty key ancestor");
|
|
|
|
key_ancestors.erase(key);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-12 19:16:00 +00:00
|
|
|
} else {
|
|
|
|
QTC::TC("qpdf", "QPDF opt no inheritable keys");
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-12-25 14:50:55 +00:00
|
|
|
QPDF::updateObjectMaps(
|
2022-04-02 21:14:10 +00:00
|
|
|
ObjUser const& ou,
|
|
|
|
QPDFObjectHandle oh,
|
2020-12-25 14:50:55 +00:00
|
|
|
std::function<int(QPDFObjectHandle&)> skip_stream_parameters)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2013-06-14 15:22:04 +00:00
|
|
|
std::set<QPDFObjGen> visited;
|
2020-12-25 14:50:55 +00:00
|
|
|
updateObjectMapsInternal(ou, oh, skip_stream_parameters, visited, true, 0);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-12-25 14:50:55 +00:00
|
|
|
QPDF::updateObjectMapsInternal(
|
2022-04-02 21:14:10 +00:00
|
|
|
ObjUser const& ou,
|
|
|
|
QPDFObjectHandle oh,
|
2020-12-25 14:50:55 +00:00
|
|
|
std::function<int(QPDFObjectHandle&)> skip_stream_parameters,
|
2022-04-02 21:14:10 +00:00
|
|
|
std::set<QPDFObjGen>& visited,
|
|
|
|
bool top,
|
2020-12-25 14:50:55 +00:00
|
|
|
int depth)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// Traverse the object tree from this point taking care to avoid
|
|
|
|
// crossing page boundaries.
|
|
|
|
|
|
|
|
bool is_page_node = false;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (oh.isDictionaryOfType("/Page")) {
|
2022-01-26 08:00:23 +00:00
|
|
|
is_page_node = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!top) {
|
2022-01-26 08:00:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (oh.isIndirect()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjGen og(oh.getObjGen());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (visited.count(og)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDF opt loop detected");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->m->obj_user_to_objects[ou].insert(og);
|
|
|
|
this->m->object_to_obj_users[og].insert(ou);
|
|
|
|
visited.insert(og);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (oh.isArray()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
int n = oh.getArrayNItems();
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
updateObjectMapsInternal(
|
2022-04-02 21:14:10 +00:00
|
|
|
ou,
|
|
|
|
oh.getArrayItem(i),
|
|
|
|
skip_stream_parameters,
|
|
|
|
visited,
|
|
|
|
false,
|
|
|
|
1 + depth);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (oh.isDictionary() || oh.isStream()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjectHandle dict = oh;
|
2020-12-25 14:50:55 +00:00
|
|
|
bool is_stream = oh.isStream();
|
|
|
|
int ssp = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (is_stream) {
|
2022-02-08 14:18:08 +00:00
|
|
|
dict = oh.getDict();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (skip_stream_parameters) {
|
2020-12-25 14:50:55 +00:00
|
|
|
ssp = skip_stream_parameters(oh);
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto const& key: dict.getKeys()) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (is_page_node && (key == "/Thumb")) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Traverse page thumbnail dictionaries as a special
|
|
|
|
// case.
|
|
|
|
updateObjectMapsInternal(
|
2020-10-22 09:11:58 +00:00
|
|
|
ObjUser(ObjUser::ou_thumb, ou.pageno),
|
2022-04-02 21:14:10 +00:00
|
|
|
dict.getKey(key),
|
|
|
|
skip_stream_parameters,
|
|
|
|
visited,
|
|
|
|
false,
|
|
|
|
1 + depth);
|
|
|
|
} else if (is_page_node && (key == "/Parent")) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Don't traverse back up the page tree
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (
|
|
|
|
((ssp >= 1) && (key == "/Length")) ||
|
|
|
|
((ssp >= 2) &&
|
|
|
|
((key == "/Filter") || (key == "/DecodeParms")))) {
|
2020-12-25 14:50:55 +00:00
|
|
|
// Don't traverse into stream parameters that we are
|
|
|
|
// not going to write.
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
updateObjectMapsInternal(
|
2022-04-02 21:14:10 +00:00
|
|
|
ou,
|
|
|
|
dict.getKey(key),
|
|
|
|
skip_stream_parameters,
|
|
|
|
visited,
|
|
|
|
false,
|
|
|
|
1 + depth);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::filterCompressedObjects(std::map<int, int> const& object_stream_data)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (object_stream_data.empty()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transform object_to_obj_users and obj_user_to_objects so that
|
|
|
|
// they refer only to uncompressed objects. If something is a
|
|
|
|
// user of a compressed object, then it is really a user of the
|
|
|
|
// object stream that contains it.
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
std::map<ObjUser, std::set<QPDFObjGen>> t_obj_user_to_objects;
|
|
|
|
std::map<QPDFObjGen, std::set<ObjUser>> t_object_to_obj_users;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& i1: this->m->obj_user_to_objects) {
|
|
|
|
ObjUser const& ou = i1.first;
|
2022-05-21 14:18:15 +00:00
|
|
|
// Loop over objects.
|
|
|
|
for (auto const& og: i1.second) {
|
2022-04-30 17:23:18 +00:00
|
|
|
auto i2 = object_stream_data.find(og.getObj());
|
|
|
|
if (i2 == object_stream_data.end()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
t_obj_user_to_objects[ou].insert(og);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-04-30 17:23:18 +00:00
|
|
|
t_obj_user_to_objects[ou].insert(QPDFObjGen(i2->second, 0));
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& i1: this->m->object_to_obj_users) {
|
|
|
|
QPDFObjGen const& og = i1.first;
|
2022-05-21 14:18:15 +00:00
|
|
|
// Loop over obj_users.
|
|
|
|
for (auto const& ou: i1.second) {
|
2022-04-30 17:23:18 +00:00
|
|
|
auto i2 = object_stream_data.find(og.getObj());
|
|
|
|
if (i2 == object_stream_data.end()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
t_object_to_obj_users[og].insert(ou);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-04-30 17:23:18 +00:00
|
|
|
t_object_to_obj_users[QPDFObjGen(i2->second, 0)].insert(ou);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->obj_user_to_objects = t_obj_user_to_objects;
|
|
|
|
this->m->object_to_obj_users = t_object_to_obj_users;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|