aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2019-01-26 00:15:23 +0100
committerJay Berkenbilt <ejb@ql.org>2019-01-27 13:50:30 +0100
commit8cb245739c76a1766473174500275d5d8b215d98 (patch)
tree283d752d2967adad8699e8e3ccca6fbb1d97692b /libqpdf
parent009767d97a0dfebbb9bb71efb4b894b25fb59dd8 (diff)
downloadqpdf-8cb245739c76a1766473174500275d5d8b215d98.tar.zst
Add QPDFObjectHandle::getUniqueResourceName
Diffstat (limited to 'libqpdf')
-rw-r--r--libqpdf/QPDFObjectHandle.cc24
-rw-r--r--libqpdf/QPDFPageDocumentHelper.cc26
2 files changed, 27 insertions, 23 deletions
diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc
index b802a55c..4e77bbaa 100644
--- a/libqpdf/QPDFObjectHandle.cc
+++ b/libqpdf/QPDFObjectHandle.cc
@@ -931,6 +931,30 @@ QPDFObjectHandle::getResourceNames()
return result;
}
+std::string
+QPDFObjectHandle::getUniqueResourceName(std::string const& prefix,
+ int& min_suffix)
+{
+ std::set<std::string> names = getResourceNames();
+ int max_suffix = min_suffix + names.size();
+ while (min_suffix <= max_suffix)
+ {
+ std::string candidate = prefix + QUtil::int_to_string(min_suffix);
+ if (names.count(candidate) == 0)
+ {
+ return candidate;
+ }
+ // Increment after return; min_suffix should be the value
+ // used, not the next value.
+ ++min_suffix;
+ }
+ // This could only happen if there is a coding error.
+ // The number of candidates we test is more than the
+ // number of keys we're checking against.
+ throw std::logic_error("unable to find unconflicting name in"
+ " QPDFObjectHandle::getUniqueResourceName");
+}
+
// Indirect object accessors
QPDF*
QPDFObjectHandle::getOwningQPDF()
diff --git a/libqpdf/QPDFPageDocumentHelper.cc b/libqpdf/QPDFPageDocumentHelper.cc
index 3eec789b..58d6ed22 100644
--- a/libqpdf/QPDFPageDocumentHelper.cc
+++ b/libqpdf/QPDFPageDocumentHelper.cc
@@ -155,35 +155,15 @@ QPDFPageDocumentHelper::flattenAnnotationsForPage(
{
QTC::TC("qpdf", "QPDFPageDocumentHelper non-widget annotation");
}
- std::set<std::string> names = resources.getResourceNames();
- std::string name;
- int max_fx = next_fx + names.size() + 1;
- while (next_fx <= max_fx)
- {
- std::string candidate = "/Fxo" + QUtil::int_to_string(next_fx);
- if (names.count(candidate) == 0)
- {
- name = candidate;
- break;
- }
- ++next_fx;
- }
- if (name.empty())
- {
- // This could only happen if there is a coding error.
- // The number of candidates we test is more than the
- // number of keys we're checking against.
- name = "/FxConflict";
- }
+ std::string name = resources.getUniqueResourceName(
+ "/Fxo", next_fx);
std::string content = aoh.getPageContentForAppearance(
name, rotate, required_flags, forbidden_flags);
if (! content.empty())
{
resources.mergeResources(
- QPDFObjectHandle::parse(
- "<< /XObject << " + name + " null >> >>"));
+ QPDFObjectHandle::parse("<< /XObject << >> >>"));
resources.getKey("/XObject").replaceKey(name, as);
- names.insert(name);
++next_fx;
}
new_content += content;