Commit e3fdb57851124eefeff17ed6d3239ccc8ccec8ed

Authored by m-holger
1 parent d9ae295d

Refactor `QPDFWriter` to add `write_name` and `write_string` methods, replace re…

…dundant normalization and string unparse logic across multiple write calls.
include/qpdf/QPDFWriter.hh
@@ -485,6 +485,9 @@ class QPDFWriter @@ -485,6 +485,9 @@ class QPDFWriter
485 QPDFWriter& write(std::string_view str); 485 QPDFWriter& write(std::string_view str);
486 QPDFWriter& write(size_t count, char c); 486 QPDFWriter& write(size_t count, char c);
487 QPDFWriter& write(std::integral auto val); 487 QPDFWriter& write(std::integral auto val);
  488 + QPDFWriter& write_name(std::string const& str);
  489 + QPDFWriter& write_string(std::string const& str, bool force_binary = false);
  490 +
488 template <typename... Args> 491 template <typename... Args>
489 QPDFWriter& write_qdf(Args&&... args); 492 QPDFWriter& write_qdf(Args&&... args);
490 template <typename... Args> 493 template <typename... Args>
libqpdf/QPDFWriter.cc
@@ -853,6 +853,20 @@ QPDFWriter::write(size_t count, char c) @@ -853,6 +853,20 @@ QPDFWriter::write(size_t count, char c)
853 return *this; 853 return *this;
854 } 854 }
855 855
  856 +QPDFWriter&
  857 +QPDFWriter::write_name(std::string const& str)
  858 +{
  859 + m->pipeline->write(Name::normalize(str));
  860 + return *this;
  861 +}
  862 +
  863 +QPDFWriter&
  864 +QPDFWriter::write_string(std::string const& str, bool force_binary)
  865 +{
  866 + m->pipeline->write(QPDF_String(str).unparse(force_binary));
  867 + return *this;
  868 +}
  869 +
856 template <typename... Args> 870 template <typename... Args>
857 QPDFWriter& 871 QPDFWriter&
858 QPDFWriter::write_qdf(Args&&... args) 872 QPDFWriter::write_qdf(Args&&... args)
@@ -1149,7 +1163,7 @@ QPDFWriter::writeTrailer( @@ -1149,7 +1163,7 @@ QPDFWriter::writeTrailer(
1149 if (value.null()) { 1163 if (value.null()) {
1150 continue; 1164 continue;
1151 } 1165 }
1152 - write_qdf(" ").write_no_qdf(" ").write(Name::normalize(key)).write(" "); 1166 + write_qdf(" ").write_no_qdf(" ").write_name(key).write(" ");
1153 if (key == "/Size") { 1167 if (key == "/Size") {
1154 write(size); 1168 write(size);
1155 if (which == t_lin_first) { 1169 if (which == t_lin_first) {
@@ -1184,7 +1198,7 @@ QPDFWriter::writeTrailer( @@ -1184,7 +1198,7 @@ QPDFWriter::writeTrailer(
1184 computeDeterministicIDData(); 1198 computeDeterministicIDData();
1185 } 1199 }
1186 generateID(); 1200 generateID();
1187 - write(QPDF_String(m->id1).unparse(true)).write(QPDF_String(m->id2).unparse(true)); 1201 + write_string(m->id1, true).write_string(m->id2, true);
1188 } 1202 }
1189 write("]"); 1203 write("]");
1190 1204
@@ -1472,7 +1486,7 @@ QPDFWriter::unparseObject( @@ -1472,7 +1486,7 @@ QPDFWriter::unparseObject(
1472 1486
1473 for (auto const& [key, value]: object.as_dictionary()) { 1487 for (auto const& [key, value]: object.as_dictionary()) {
1474 if (!value.null()) { 1488 if (!value.null()) {
1475 - write(indent_large).write(Name::normalize(key)).write(" "); 1489 + write(indent_large).write_name(key).write(" ");
1476 if (key == "/Contents" && object.isDictionaryOfType("/Sig") && 1490 if (key == "/Contents" && object.isDictionaryOfType("/Sig") &&
1477 object.hasKey("/ByteRange")) { 1491 object.hasKey("/ByteRange")) {
1478 QTC::TC("qpdf", "QPDFWriter no encryption sig contents"); 1492 QTC::TC("qpdf", "QPDFWriter no encryption sig contents");
@@ -2255,22 +2269,22 @@ QPDFWriter::writeEncryptionDictionary() @@ -2255,22 +2269,22 @@ QPDFWriter::writeEncryptionDictionary()
2255 } 2269 }
2256 } 2270 }
2257 write(" /Filter /Standard /Length ").write(enc.getLengthBytes() * 8); 2271 write(" /Filter /Standard /Length ").write(enc.getLengthBytes() * 8);
2258 - write(" /O ").write(QPDF_String(enc.getO()).unparse(true)); 2272 + write(" /O ").write_string(enc.getO(), true);
2259 if (V >= 4) { 2273 if (V >= 4) {
2260 - write(" /OE ").write(QPDF_String(enc.getOE()).unparse(true)); 2274 + write(" /OE ").write_string(enc.getOE(), true);
2261 } 2275 }
2262 write(" /P ").write(enc.getP()); 2276 write(" /P ").write(enc.getP());
2263 if (V >= 5) { 2277 if (V >= 5) {
2264 - write(" /Perms ").write(QPDF_String(enc.getPerms()).unparse(true)); 2278 + write(" /Perms ").write_string(enc.getPerms(), true);
2265 } 2279 }
2266 write(" /R ").write(enc.getR()); 2280 write(" /R ").write(enc.getR());
2267 2281
2268 if (V >= 4) { 2282 if (V >= 4) {
2269 write(" /StmF /StdCF /StrF /StdCF"); 2283 write(" /StmF /StdCF /StrF /StdCF");
2270 } 2284 }
2271 - write(" /U ").write(QPDF_String(enc.getU()).unparse(true)); 2285 + write(" /U ").write_string(enc.getU(), true);
2272 if (V >= 4) { 2286 if (V >= 4) {
2273 - write(" /UE ").write(QPDF_String(enc.getUE()).unparse(true)); 2287 + write(" /UE ").write_string(enc.getUE(), true);
2274 } 2288 }
2275 write(" /V ").write(enc.getV()).write(" >>"); 2289 write(" /V ").write(enc.getV()).write(" >>");
2276 closeObject(m->encryption_dict_objid); 2290 closeObject(m->encryption_dict_objid);