2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 10:58:58 +00:00

Use fluent replaceKey

This commit is contained in:
Jay Berkenbilt 2022-04-29 20:39:54 -04:00
parent d8fdf632a9
commit ab9d557cb0
13 changed files with 73 additions and 82 deletions

View File

@ -106,10 +106,10 @@ process(
// apdict.replaceKey("/Type", QPDFObjectHandle::newName("/XObject")); // apdict.replaceKey("/Type", QPDFObjectHandle::newName("/XObject"));
// apdict.replaceKey("/Subtype", QPDFObjectHandle::newName("/Form")); // apdict.replaceKey("/Subtype", QPDFObjectHandle::newName("/Form"));
// apdict.replaceKey("/BBox", QPDFObjectHandle::parse("[ 0 0 20 20 ]")); // apdict.replaceKey("/BBox", QPDFObjectHandle::parse("[ 0 0 20 20 ]"));
apdict.replaceKey("/Resources", "<< >>"_qpdf); apdict.replaceKey("/Resources", "<< >>"_qpdf)
apdict.replaceKey("/Type", "/XObject"_qpdf); .replaceKey("/Type", "/XObject"_qpdf)
apdict.replaceKey("/Subtype", "/Form"_qpdf); .replaceKey("/Subtype", "/Form"_qpdf)
apdict.replaceKey("/BBox", "[ 0 0 20 20 ]"_qpdf); .replaceKey("/BBox", "[ 0 0 20 20 ]"_qpdf);
auto annot = q.makeIndirectObject(QPDFObjectHandle::parse( auto annot = q.makeIndirectObject(QPDFObjectHandle::parse(
&q, &q,
("<<" ("<<"

View File

@ -183,9 +183,9 @@ add_page(
" /Subtype /Image" " /Subtype /Image"
" /BitsPerComponent 8" " /BitsPerComponent 8"
">>"_qpdf; ">>"_qpdf;
image_dict.replaceKey("/ColorSpace", newName(color_space)); image_dict.replaceKey("/ColorSpace", newName(color_space))
image_dict.replaceKey("/Width", newInteger(width)); .replaceKey("/Width", newInteger(width))
image_dict.replaceKey("/Height", newInteger(height)); .replaceKey("/Height", newInteger(height));
image.replaceDict(image_dict); image.replaceDict(image_dict);
// Provide the stream data. // Provide the stream data.
@ -202,9 +202,9 @@ add_page(
xobject.replaceKey("/Im1", image); xobject.replaceKey("/Im1", image);
QPDFObjectHandle resources = QPDFObjectHandle::newDictionary(); QPDFObjectHandle resources = QPDFObjectHandle::newDictionary();
resources.replaceKey("/ProcSet", procset); resources.replaceKey("/ProcSet", procset)
resources.replaceKey("/Font", rfont); .replaceKey("/Font", rfont)
resources.replaceKey("/XObject", xobject); .replaceKey("/XObject", xobject);
// Create the page content stream // Create the page content stream
QPDFObjectHandle contents = QPDFObjectHandle contents =
@ -215,8 +215,7 @@ add_page(
" /Type /Page" " /Type /Page"
" /MediaBox [0 0 612 392]" " /MediaBox [0 0 612 392]"
">>"_qpdf); ">>"_qpdf);
page.replaceKey("/Contents", contents); page.replaceKey("/Contents", contents).replaceKey("/Resources", resources);
page.replaceKey("/Resources", resources);
// Add the page to the PDF file // Add the page to the PDF file
dh.addPage(page, false); dh.addPage(page, false);

View File

@ -341,9 +341,10 @@ NNTreeIterator::split(
first_node.replaceKey(key, first_half); first_node.replaceKey(key, first_half);
QPDFObjectHandle new_kids = QPDFObjectHandle::newArray(); QPDFObjectHandle new_kids = QPDFObjectHandle::newArray();
new_kids.appendItem(first_node); new_kids.appendItem(first_node);
to_split.removeKey("/Limits"); // already shouldn't be there for root to_split
to_split.removeKey(impl.details.itemsKey()); .removeKey("/Limits") // already shouldn't be there for root
to_split.replaceKey("/Kids", new_kids); .removeKey(impl.details.itemsKey())
.replaceKey("/Kids", new_kids);
if (is_leaf) { if (is_leaf) {
QTC::TC("qpdf", "NNTree split root + leaf"); QTC::TC("qpdf", "NNTree split root + leaf");
this->node = first_node; this->node = first_node;
@ -884,9 +885,8 @@ NNTreeImpl::repair()
for (auto const& i : *this) { for (auto const& i : *this) {
repl.insert(i.first, i.second); repl.insert(i.first, i.second);
} }
this->oh.replaceKey("/Kids", new_node.getKey("/Kids")); this->oh.replaceKey("/Kids", new_node.getKey("/Kids"))
this->oh.replaceKey( .replaceKey(details.itemsKey(), new_node.getKey(details.itemsKey()));
details.itemsKey(), new_node.getKey(details.itemsKey()));
} }
NNTreeImpl::iterator NNTreeImpl::iterator

View File

@ -2436,13 +2436,11 @@ QPDF::replaceForeignIndirectObjects(
QTC::TC("qpdf", "QPDF replace dictionary"); QTC::TC("qpdf", "QPDF replace dictionary");
result = QPDFObjectHandle::newDictionary(); result = QPDFObjectHandle::newDictionary();
std::set<std::string> keys = foreign.getKeys(); std::set<std::string> keys = foreign.getKeys();
for (std::set<std::string>::iterator iter = keys.begin(); for (auto const& iter : keys) {
iter != keys.end();
++iter) {
result.replaceKey( result.replaceKey(
*iter, iter,
replaceForeignIndirectObjects( replaceForeignIndirectObjects(
foreign.getKey(*iter), obj_copier, false)); foreign.getKey(iter), obj_copier, false));
} }
} else if (foreign.isStream()) { } else if (foreign.isStream()) {
QTC::TC("qpdf", "QPDF replace stream"); QTC::TC("qpdf", "QPDF replace stream");
@ -2452,13 +2450,11 @@ QPDF::replaceForeignIndirectObjects(
QPDFObjectHandle dict = result.getDict(); QPDFObjectHandle dict = result.getDict();
QPDFObjectHandle old_dict = foreign.getDict(); QPDFObjectHandle old_dict = foreign.getDict();
std::set<std::string> keys = old_dict.getKeys(); std::set<std::string> keys = old_dict.getKeys();
for (std::set<std::string>::iterator iter = keys.begin(); for (auto const& iter : keys) {
iter != keys.end();
++iter) {
dict.replaceKey( dict.replaceKey(
*iter, iter,
replaceForeignIndirectObjects( replaceForeignIndirectObjects(
old_dict.getKey(*iter), obj_copier, false)); old_dict.getKey(iter), obj_copier, false));
} }
copyStreamData(result, foreign); copyStreamData(result, foreign);
} else { } else {

View File

@ -748,8 +748,7 @@ QPDFAcroFormDocumentHelper::adjustAppearanceStream(
auto existing_old = subdict.getKey(old_key); auto existing_old = subdict.getKey(old_key);
if (!existing_old.isNull()) { if (!existing_old.isNull()) {
QTC::TC("qpdf", "QPDFAcroFormDocumentHelper ap rename"); QTC::TC("qpdf", "QPDFAcroFormDocumentHelper ap rename");
subdict.replaceKey(new_key, existing_old); subdict.replaceKey(new_key, existing_old).removeKey(old_key);
subdict.removeKey(old_key);
} }
} }
} }

View File

@ -103,8 +103,8 @@ QPDFFileSpecObjectHelper::createFileSpec(
QPDFFileSpecObjectHelper result(oh); QPDFFileSpecObjectHelper result(oh);
result.setFilename(filename); result.setFilename(filename);
auto ef = QPDFObjectHandle::newDictionary(); auto ef = QPDFObjectHandle::newDictionary();
ef.replaceKey("/F", efsoh.getObjectHandle()); ef.replaceKey("/F", efsoh.getObjectHandle())
ef.replaceKey("/UF", efsoh.getObjectHandle()); .replaceKey("/UF", efsoh.getObjectHandle());
oh.replaceKey("/EF", ef); oh.replaceKey("/EF", ef);
return result; return result;
} }

View File

@ -44,9 +44,8 @@ QPDFPageLabelDocumentHelper::getLabelForPage(long long page_idx)
QIntC::range_check(start, offset); QIntC::range_check(start, offset);
start += offset; start += offset;
result = QPDFObjectHandle::newDictionary(); result = QPDFObjectHandle::newDictionary();
result.replaceKey("/S", S); result.replaceKey("/S", S).replaceKey("/P", P).replaceKey(
result.replaceKey("/P", P); "/St", QPDFObjectHandle::newInteger(start));
result.replaceKey("/St", QPDFObjectHandle::newInteger(start));
return result; return result;
} }

View File

@ -78,8 +78,8 @@ QPDFObjectHandle
InlineImageTracker::convertIIDict(QPDFObjectHandle odict) InlineImageTracker::convertIIDict(QPDFObjectHandle odict)
{ {
QPDFObjectHandle dict = QPDFObjectHandle::newDictionary(); QPDFObjectHandle dict = QPDFObjectHandle::newDictionary();
dict.replaceKey("/Type", QPDFObjectHandle::newName("/XObject")); dict.replaceKey("/Type", QPDFObjectHandle::newName("/XObject"))
dict.replaceKey("/Subtype", QPDFObjectHandle::newName("/Image")); .replaceKey("/Subtype", QPDFObjectHandle::newName("/Image"));
std::set<std::string> keys = odict.getKeys(); std::set<std::string> keys = odict.getKeys();
for (auto key : keys) { for (auto key : keys) {
QPDFObjectHandle value = odict.getKey(key); QPDFObjectHandle value = odict.getKey(key);
@ -752,11 +752,11 @@ QPDFPageObjectHelper::getFormXObjectForPage(bool handle_transformations)
} }
QPDFObjectHandle result = QPDFObjectHandle::newStream(qpdf); QPDFObjectHandle result = QPDFObjectHandle::newStream(qpdf);
QPDFObjectHandle newdict = result.getDict(); QPDFObjectHandle newdict = result.getDict();
newdict.replaceKey("/Type", QPDFObjectHandle::newName("/XObject")); newdict.replaceKey("/Type", QPDFObjectHandle::newName("/XObject"))
newdict.replaceKey("/Subtype", QPDFObjectHandle::newName("/Form")); .replaceKey("/Subtype", QPDFObjectHandle::newName("/Form"))
newdict.replaceKey( .replaceKey(
"/Resources", getAttribute("/Resources", false).shallowCopy()); "/Resources", getAttribute("/Resources", false).shallowCopy())
newdict.replaceKey("/Group", getAttribute("/Group", false).shallowCopy()); .replaceKey("/Group", getAttribute("/Group", false).shallowCopy());
QPDFObjectHandle bbox = getTrimBox(false).shallowCopy(); QPDFObjectHandle bbox = getTrimBox(false).shallowCopy();
if (!bbox.isRectangle()) { if (!bbox.isRectangle()) {
this->oh.warnIfPossible("bounding box is invalid; form" this->oh.warnIfPossible("bounding box is invalid; form"

View File

@ -604,8 +604,8 @@ QPDF_Stream::replaceFilterData(
QPDFObjectHandle const& decode_parms, QPDFObjectHandle const& decode_parms,
size_t length) size_t length)
{ {
this->stream_dict.replaceKey("/Filter", filter); this->stream_dict.replaceKey("/Filter", filter)
this->stream_dict.replaceKey("/DecodeParms", decode_parms); .replaceKey("/DecodeParms", decode_parms);
if (length == 0) { if (length == 0) {
QTC::TC("qpdf", "QPDF_Stream unknown stream length"); QTC::TC("qpdf", "QPDF_Stream unknown stream length");
this->stream_dict.removeKey("/Length"); this->stream_dict.removeKey("/Length");

View File

@ -70,8 +70,7 @@ test_bsearch()
limits.appendItem(QPDFObjectHandle::newInteger(v.at(0))); limits.appendItem(QPDFObjectHandle::newInteger(v.at(0)));
limits.appendItem(QPDFObjectHandle::newInteger(v.at(v.size() - 1))); limits.appendItem(QPDFObjectHandle::newInteger(v.at(v.size() - 1)));
auto node = q.makeIndirectObject(QPDFObjectHandle::newDictionary()); auto node = q.makeIndirectObject(QPDFObjectHandle::newDictionary());
node.replaceKey("/Nums", nums); node.replaceKey("/Nums", nums).replaceKey("/Limits", limits);
node.replaceKey("/Limits", limits);
return node; return node;
}; };

View File

@ -60,15 +60,14 @@ runtest(int n)
rfont.replaceKey("/F1", font); rfont.replaceKey("/F1", font);
QPDFObjectHandle resources = QPDFObjectHandle::newDictionary(); QPDFObjectHandle resources = QPDFObjectHandle::newDictionary();
resources.replaceKey("/ProcSet", procset); resources.replaceKey("/ProcSet", procset).replaceKey("/Font", rfont);
resources.replaceKey("/Font", rfont);
QPDFObjectHandle page = QPDFObjectHandle page =
pdf.makeIndirectObject(QPDFObjectHandle::newDictionary()); pdf.makeIndirectObject(QPDFObjectHandle::newDictionary());
page.replaceKey("/Type", newName("/Page")); page.replaceKey("/Type", newName("/Page"))
page.replaceKey("/MediaBox", mediabox); .replaceKey("/MediaBox", mediabox)
page.replaceKey("/Contents", contents); .replaceKey("/Contents", contents)
page.replaceKey("/Resources", resources); .replaceKey("/Resources", resources);
QPDFPageDocumentHelper(pdf).addPage(page, true); QPDFPageDocumentHelper(pdf).addPage(page, true);

View File

@ -538,8 +538,7 @@ test_9(QPDF& pdf, char const* arg2)
"data for other stream\n", "data for other stream\n",
QPDFObjectHandle::newNull(), QPDFObjectHandle::newNull(),
QPDFObjectHandle::newNull()); QPDFObjectHandle::newNull());
root.replaceKey("/QStream", qstream); root.replaceKey("/QStream", qstream).replaceKey("/RStream", rstream);
root.replaceKey("/RStream", rstream);
QPDFWriter w(pdf, "a.pdf"); QPDFWriter w(pdf, "a.pdf");
w.setStaticID(true); w.setStaticID(true);
w.setStreamDataMode(qpdf_s_preserve); w.setStreamDataMode(qpdf_s_preserve);
@ -909,8 +908,7 @@ test_24(QPDF& pdf, char const* arg2)
QPDFObjectHandle res1 = QPDFObjectHandle::newReserved(&pdf); QPDFObjectHandle res1 = QPDFObjectHandle::newReserved(&pdf);
QPDFObjectHandle res2 = QPDFObjectHandle::newReserved(&pdf); QPDFObjectHandle res2 = QPDFObjectHandle::newReserved(&pdf);
QPDFObjectHandle trailer = pdf.getTrailer(); QPDFObjectHandle trailer = pdf.getTrailer();
trailer.replaceKey("Array1", res1); trailer.replaceKey("Array1", res1).replaceKey("Array2", res2);
trailer.replaceKey("Array2", res2);
QPDFObjectHandle array1 = QPDFObjectHandle::newArray(); QPDFObjectHandle array1 = QPDFObjectHandle::newArray();
QPDFObjectHandle array2 = QPDFObjectHandle::newArray(); QPDFObjectHandle array2 = QPDFObjectHandle::newArray();
@ -1086,8 +1084,9 @@ test_27(QPDF& pdf, char const* arg2)
dh.addPage(O3.getKey("/OtherPage"), false); dh.addPage(O3.getKey("/OtherPage"), false);
dh.addPage(O3, false); dh.addPage(O3, false);
QPDFObjectHandle s2 = QPDFObjectHandle::newStream(&oldpdf, "potato\n"); QPDFObjectHandle s2 = QPDFObjectHandle::newStream(&oldpdf, "potato\n");
pdf.getTrailer().replaceKey("/QTest", pdf.copyForeignObject(qtest)); pdf.getTrailer()
pdf.getTrailer().replaceKey("/QTest2", QPDFObjectHandle::newArray()); .replaceKey("/QTest", pdf.copyForeignObject(qtest))
.replaceKey("/QTest2", QPDFObjectHandle::newArray());
pdf.getTrailer().getKey("/QTest2").appendItem( pdf.getTrailer().getKey("/QTest2").appendItem(
pdf.copyForeignObject(s1)); pdf.copyForeignObject(s1));
pdf.getTrailer().getKey("/QTest2").appendItem( pdf.getTrailer().getKey("/QTest2").appendItem(
@ -2259,9 +2258,10 @@ test_60(QPDF& pdf, char const* arg2)
// The only differences between /QTest and /QTest3 should be // The only differences between /QTest and /QTest3 should be
// the direct objects merged from r2. // the direct objects merged from r2.
pdf.getTrailer().replaceKey("/QTest1", r1); pdf.getTrailer()
pdf.getTrailer().replaceKey("/QTest2", r2); .replaceKey("/QTest1", r1)
pdf.getTrailer().replaceKey("/QTest3", r3); .replaceKey("/QTest2", r2)
.replaceKey("/QTest3", r3);
QPDFWriter w(pdf, "a.pdf"); QPDFWriter w(pdf, "a.pdf");
w.setQDFMode(true); w.setQDFMode(true);
w.setStaticID(true); w.setStaticID(true);
@ -2321,9 +2321,9 @@ test_62(QPDF& pdf, char const* arg2)
long long q2 = QIntC::to_longlong(q2_l); long long q2 = QIntC::to_longlong(q2_l);
unsigned int q3_i = UINT_MAX; unsigned int q3_i = UINT_MAX;
long long q3 = QIntC::to_longlong(q3_i); long long q3 = QIntC::to_longlong(q3_i);
t.replaceKey("/Q1", QPDFObjectHandle::newInteger(q1)); t.replaceKey("/Q1", QPDFObjectHandle::newInteger(q1))
t.replaceKey("/Q2", QPDFObjectHandle::newInteger(q2)); .replaceKey("/Q2", QPDFObjectHandle::newInteger(q2))
t.replaceKey("/Q3", QPDFObjectHandle::newInteger(q3)); .replaceKey("/Q3", QPDFObjectHandle::newInteger(q3));
assert_compare_numbers(q1, t.getKey("/Q1").getIntValue()); assert_compare_numbers(q1, t.getKey("/Q1").getIntValue());
assert_compare_numbers(q1_l, t.getKey("/Q1").getUIntValue()); assert_compare_numbers(q1_l, t.getKey("/Q1").getUIntValue());
assert_compare_numbers(INT_MAX, t.getKey("/Q1").getIntValueAsInt()); assert_compare_numbers(INT_MAX, t.getKey("/Q1").getIntValueAsInt());

View File

@ -191,11 +191,11 @@ create_pdf(char const* filename)
QPDFObjectHandle font = QPDFObjectHandle font =
pdf.makeIndirectObject(QPDFObjectHandle::newDictionary()); pdf.makeIndirectObject(QPDFObjectHandle::newDictionary());
font.replaceKey("/Type", newName("/Font")); font.replaceKey("/Type", newName("/Font"))
font.replaceKey("/Subtype", newName("/Type1")); .replaceKey("/Subtype", newName("/Type1"))
font.replaceKey("/Name", newName("/F1")); .replaceKey("/Name", newName("/F1"))
font.replaceKey("/BaseFont", newName("/Helvetica")); .replaceKey("/BaseFont", newName("/Helvetica"))
font.replaceKey("/Encoding", newName("/WinAnsiEncoding")); .replaceKey("/Encoding", newName("/WinAnsiEncoding"));
QPDFObjectHandle procset = QPDFObjectHandle procset =
pdf.makeIndirectObject(QPDFObjectHandle::newArray()); pdf.makeIndirectObject(QPDFObjectHandle::newArray());
@ -216,12 +216,12 @@ create_pdf(char const* filename)
for (size_t pageno = 1; pageno <= npages; ++pageno) { for (size_t pageno = 1; pageno <= npages; ++pageno) {
QPDFObjectHandle image = QPDFObjectHandle::newStream(&pdf); QPDFObjectHandle image = QPDFObjectHandle::newStream(&pdf);
QPDFObjectHandle image_dict = image.getDict(); QPDFObjectHandle image_dict = image.getDict();
image_dict.replaceKey("/Type", newName("/XObject")); image_dict.replaceKey("/Type", newName("/XObject"))
image_dict.replaceKey("/Subtype", newName("/Image")); .replaceKey("/Subtype", newName("/Image"))
image_dict.replaceKey("/ColorSpace", newName("/DeviceGray")); .replaceKey("/ColorSpace", newName("/DeviceGray"))
image_dict.replaceKey("/BitsPerComponent", newInteger(8)); .replaceKey("/BitsPerComponent", newInteger(8))
image_dict.replaceKey("/Width", newInteger(width)); .replaceKey("/Width", newInteger(width))
image_dict.replaceKey("/Height", newInteger(height)); .replaceKey("/Height", newInteger(height));
ImageProvider* p = new ImageProvider(pageno); ImageProvider* p = new ImageProvider(pageno);
std::shared_ptr<QPDFObjectHandle::StreamDataProvider> provider(p); std::shared_ptr<QPDFObjectHandle::StreamDataProvider> provider(p);
image.replaceStreamData( image.replaceStreamData(
@ -231,18 +231,18 @@ create_pdf(char const* filename)
xobject.replaceKey("/Im1", image); xobject.replaceKey("/Im1", image);
QPDFObjectHandle resources = QPDFObjectHandle::newDictionary(); QPDFObjectHandle resources = QPDFObjectHandle::newDictionary();
resources.replaceKey("/ProcSet", procset); resources.replaceKey("/ProcSet", procset)
resources.replaceKey("/Font", rfont); .replaceKey("/Font", rfont)
resources.replaceKey("/XObject", xobject); .replaceKey("/XObject", xobject);
QPDFObjectHandle contents = create_page_contents(pdf, pageno); QPDFObjectHandle contents = create_page_contents(pdf, pageno);
QPDFObjectHandle page = QPDFObjectHandle page =
pdf.makeIndirectObject(QPDFObjectHandle::newDictionary()); pdf.makeIndirectObject(QPDFObjectHandle::newDictionary());
page.replaceKey("/Type", newName("/Page")); page.replaceKey("/Type", newName("/Page"))
page.replaceKey("/MediaBox", mediabox); .replaceKey("/MediaBox", mediabox)
page.replaceKey("/Contents", contents); .replaceKey("/Contents", contents)
page.replaceKey("/Resources", resources); .replaceKey("/Resources", resources);
dh.addPage(page, false); dh.addPage(page, false);
} }