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 485 QPDFWriter& write(std::string_view str);
486 486 QPDFWriter& write(size_t count, char c);
487 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 491 template <typename... Args>
489 492 QPDFWriter& write_qdf(Args&&... args);
490 493 template <typename... Args>
... ...
libqpdf/QPDFWriter.cc
... ... @@ -853,6 +853,20 @@ QPDFWriter::write(size_t count, char c)
853 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 870 template <typename... Args>
857 871 QPDFWriter&
858 872 QPDFWriter::write_qdf(Args&&... args)
... ... @@ -1149,7 +1163,7 @@ QPDFWriter::writeTrailer(
1149 1163 if (value.null()) {
1150 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 1167 if (key == "/Size") {
1154 1168 write(size);
1155 1169 if (which == t_lin_first) {
... ... @@ -1184,7 +1198,7 @@ QPDFWriter::writeTrailer(
1184 1198 computeDeterministicIDData();
1185 1199 }
1186 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 1203 write("]");
1190 1204  
... ... @@ -1472,7 +1486,7 @@ QPDFWriter::unparseObject(
1472 1486  
1473 1487 for (auto const& [key, value]: object.as_dictionary()) {
1474 1488 if (!value.null()) {
1475   - write(indent_large).write(Name::normalize(key)).write(" ");
  1489 + write(indent_large).write_name(key).write(" ");
1476 1490 if (key == "/Contents" && object.isDictionaryOfType("/Sig") &&
1477 1491 object.hasKey("/ByteRange")) {
1478 1492 QTC::TC("qpdf", "QPDFWriter no encryption sig contents");
... ... @@ -2255,22 +2269,22 @@ QPDFWriter::writeEncryptionDictionary()
2255 2269 }
2256 2270 }
2257 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 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 2276 write(" /P ").write(enc.getP());
2263 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 2280 write(" /R ").write(enc.getR());
2267 2281  
2268 2282 if (V >= 4) {
2269 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 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 2289 write(" /V ").write(enc.getV()).write(" >>");
2276 2290 closeObject(m->encryption_dict_objid);
... ...