Commit d9ae295dc76ca7db020a7a35c04acbc7d5ae1e90

Authored by m-holger
1 parent 485399c7

Refactor `QPDFWriter` to introduce `indent_large`, replace redundant `indent` ha…

…ndling, leverage `std::string_view`, and streamline QDF indent logic.
Showing 1 changed file with 10 additions and 9 deletions
libqpdf/QPDFWriter.cc
... ... @@ -1309,13 +1309,14 @@ QPDFWriter::unparseObject(
1309 1309 if (level < 0) {
1310 1310 throw std::logic_error("invalid level in QPDFWriter::unparseObject");
1311 1311 }
1312   - // For non-qdf, "indent" is a single space between tokens. For qdf, indent includes the
1313   - // preceding newline.
1314   - std::string indent = " ";
  1312 + // For non-qdf, "indent" and "indent_large" are a single space between tokens. For qdf, they
  1313 + // include the preceding newline.
  1314 + std::string indent_large = " ";
1315 1315 if (m->qdf_mode) {
1316   - indent.append(static_cast<size_t>(2 * level), ' ');
1317   - indent[0] = '\n';
  1316 + indent_large.append(static_cast<size_t>(2 * (level + 1)), ' ');
  1317 + indent_large[0] = '\n';
1318 1318 }
  1319 + std::string_view indent{indent_large.data(), m->qdf_mode ? indent_large.size() - 2 : 1};
1319 1320  
1320 1321 if (auto const tc = object.getTypeCode(); tc == ::ot_array) {
1321 1322 // Note: PDF spec 1.4 implementation note 121 states that Acrobat requires a space after the
... ... @@ -1323,7 +1324,7 @@ QPDFWriter::unparseObject(
1323 1324 // for all arrays because it looks nicer and doesn't make the files that much bigger.
1324 1325 write("[");
1325 1326 for (auto const& item: object.as_array()) {
1326   - write(indent).write_qdf(" ");
  1327 + write(indent_large);
1327 1328 unparseChild(item, level + 1, child_flags);
1328 1329 }
1329 1330 write(indent).write("]");
... ... @@ -1471,7 +1472,7 @@ QPDFWriter::unparseObject(
1471 1472  
1472 1473 for (auto const& [key, value]: object.as_dictionary()) {
1473 1474 if (!value.null()) {
1474   - write(indent).write_qdf(" ").write(Name::normalize(key)).write(" ");
  1475 + write(indent_large).write(Name::normalize(key)).write(" ");
1475 1476 if (key == "/Contents" && object.isDictionaryOfType("/Sig") &&
1476 1477 object.hasKey("/ByteRange")) {
1477 1478 QTC::TC("qpdf", "QPDFWriter no encryption sig contents");
... ... @@ -1483,7 +1484,7 @@ QPDFWriter::unparseObject(
1483 1484 }
1484 1485  
1485 1486 if (flags & f_stream) {
1486   - write(indent).write_qdf(" ").write("/Length ");
  1487 + write(indent_large).write("/Length ");
1487 1488  
1488 1489 if (m->direct_stream_lengths) {
1489 1490 write(stream_length);
... ... @@ -1491,7 +1492,7 @@ QPDFWriter::unparseObject(
1491 1492 write(m->cur_stream_length_id).write(" 0 R");
1492 1493 }
1493 1494 if (compress && (flags & f_filtered)) {
1494   - write(indent).write_qdf(" ").write("/Filter /FlateDecode");
  1495 + write(indent_large).write("/Filter /FlateDecode");
1495 1496 }
1496 1497 }
1497 1498  
... ...