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("/Subtype", QPDFObjectHandle::newName("/Form"));
// apdict.replaceKey("/BBox", QPDFObjectHandle::parse("[ 0 0 20 20 ]"));
apdict.replaceKey("/Resources", "<< >>"_qpdf);
apdict.replaceKey("/Type", "/XObject"_qpdf);
apdict.replaceKey("/Subtype", "/Form"_qpdf);
apdict.replaceKey("/BBox", "[ 0 0 20 20 ]"_qpdf);
apdict.replaceKey("/Resources", "<< >>"_qpdf)
.replaceKey("/Type", "/XObject"_qpdf)
.replaceKey("/Subtype", "/Form"_qpdf)
.replaceKey("/BBox", "[ 0 0 20 20 ]"_qpdf);
auto annot = q.makeIndirectObject(QPDFObjectHandle::parse(
&q,
("<<"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -604,8 +604,8 @@ QPDF_Stream::replaceFilterData(
QPDFObjectHandle const& decode_parms,
size_t length)
{
this->stream_dict.replaceKey("/Filter", filter);
this->stream_dict.replaceKey("/DecodeParms", decode_parms);
this->stream_dict.replaceKey("/Filter", filter)
.replaceKey("/DecodeParms", decode_parms);
if (length == 0) {
QTC::TC("qpdf", "QPDF_Stream unknown stream 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(v.size() - 1)));
auto node = q.makeIndirectObject(QPDFObjectHandle::newDictionary());
node.replaceKey("/Nums", nums);
node.replaceKey("/Limits", limits);
node.replaceKey("/Nums", nums).replaceKey("/Limits", limits);
return node;
};

View File

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

View File

@ -538,8 +538,7 @@ test_9(QPDF& pdf, char const* arg2)
"data for other stream\n",
QPDFObjectHandle::newNull(),
QPDFObjectHandle::newNull());
root.replaceKey("/QStream", qstream);
root.replaceKey("/RStream", rstream);
root.replaceKey("/QStream", qstream).replaceKey("/RStream", rstream);
QPDFWriter w(pdf, "a.pdf");
w.setStaticID(true);
w.setStreamDataMode(qpdf_s_preserve);
@ -909,8 +908,7 @@ test_24(QPDF& pdf, char const* arg2)
QPDFObjectHandle res1 = QPDFObjectHandle::newReserved(&pdf);
QPDFObjectHandle res2 = QPDFObjectHandle::newReserved(&pdf);
QPDFObjectHandle trailer = pdf.getTrailer();
trailer.replaceKey("Array1", res1);
trailer.replaceKey("Array2", res2);
trailer.replaceKey("Array1", res1).replaceKey("Array2", res2);
QPDFObjectHandle array1 = 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, false);
QPDFObjectHandle s2 = QPDFObjectHandle::newStream(&oldpdf, "potato\n");
pdf.getTrailer().replaceKey("/QTest", pdf.copyForeignObject(qtest));
pdf.getTrailer().replaceKey("/QTest2", QPDFObjectHandle::newArray());
pdf.getTrailer()
.replaceKey("/QTest", pdf.copyForeignObject(qtest))
.replaceKey("/QTest2", QPDFObjectHandle::newArray());
pdf.getTrailer().getKey("/QTest2").appendItem(
pdf.copyForeignObject(s1));
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 direct objects merged from r2.
pdf.getTrailer().replaceKey("/QTest1", r1);
pdf.getTrailer().replaceKey("/QTest2", r2);
pdf.getTrailer().replaceKey("/QTest3", r3);
pdf.getTrailer()
.replaceKey("/QTest1", r1)
.replaceKey("/QTest2", r2)
.replaceKey("/QTest3", r3);
QPDFWriter w(pdf, "a.pdf");
w.setQDFMode(true);
w.setStaticID(true);
@ -2321,9 +2321,9 @@ test_62(QPDF& pdf, char const* arg2)
long long q2 = QIntC::to_longlong(q2_l);
unsigned int q3_i = UINT_MAX;
long long q3 = QIntC::to_longlong(q3_i);
t.replaceKey("/Q1", QPDFObjectHandle::newInteger(q1));
t.replaceKey("/Q2", QPDFObjectHandle::newInteger(q2));
t.replaceKey("/Q3", QPDFObjectHandle::newInteger(q3));
t.replaceKey("/Q1", QPDFObjectHandle::newInteger(q1))
.replaceKey("/Q2", QPDFObjectHandle::newInteger(q2))
.replaceKey("/Q3", QPDFObjectHandle::newInteger(q3));
assert_compare_numbers(q1, t.getKey("/Q1").getIntValue());
assert_compare_numbers(q1_l, t.getKey("/Q1").getUIntValue());
assert_compare_numbers(INT_MAX, t.getKey("/Q1").getIntValueAsInt());

View File

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