Commit ce66604290a3a845c57100a8a675f43be4630d18

Authored by m-holger
1 parent f89196b6

Tune indentations in QPDFWriter::unparseObject

Set indent differently for qdf and non-qdf files.
Combine with preceding newline for qdf files.
Showing 1 changed file with 17 additions and 22 deletions
libqpdf/QPDFWriter.cc
... ... @@ -1441,8 +1441,13 @@ QPDFWriter::unparseObject(
1441 1441 if (level < 0) {
1442 1442 throw std::logic_error("invalid level in QPDFWriter::unparseObject");
1443 1443 }
1444   -
1445   - std::string const indent(static_cast<size_t>(2 * level), ' ');
  1444 + // For non-qdf, "indent" is a single space between tokens.
  1445 + // For qdf, indent includes the preceding newline.
  1446 + std::string indent = " ";
  1447 + if (m->qdf_mode) {
  1448 + indent.append(static_cast<size_t>(2 * level), ' ');
  1449 + indent[0] = '\n';
  1450 + }
1446 1451  
1447 1452 if (auto const tc = object.getTypeCode(); tc == ::ot_array) {
1448 1453 // Note: PDF spec 1.4 implementation note 121 states that
... ... @@ -1451,16 +1456,12 @@ QPDFWriter::unparseObject(
1451 1456 // unconditionally for all arrays because it looks nicer and
1452 1457 // doesn't make the files that much bigger.
1453 1458 writeString("[");
1454   - writeStringQDF("\n");
1455 1459 for (auto const& item: object.getArrayAsVector()) {
1456   - writeStringQDF(indent);
  1460 + writeString(indent);
1457 1461 writeStringQDF(" ");
1458   - writeStringNoQDF(" ");
1459 1462 unparseChild(item, level + 1, child_flags);
1460   - writeStringQDF("\n");
1461 1463 }
1462   - writeStringQDF(indent);
1463   - writeStringNoQDF(" ");
  1464 + writeString(indent);
1464 1465 writeString("]");
1465 1466 } else if (tc == ::ot_dictionary) {
1466 1467 // Make a shallow copy of this object so we can modify it
... ... @@ -1619,14 +1620,12 @@ QPDFWriter::unparseObject(
1619 1620 }
1620 1621  
1621 1622 writeString("<<");
1622   - writeStringQDF("\n");
1623 1623  
1624 1624 for (auto& item: object.getDictAsMap()) {
1625 1625 if (!item.second.isNull()) {
1626 1626 auto const& key = item.first;
1627   - writeStringQDF(indent);
  1627 + writeString(indent);
1628 1628 writeStringQDF(" ");
1629   - writeStringNoQDF(" ");
1630 1629 writeString(QPDF_Name::normalizeName(key));
1631 1630 writeString(" ");
1632 1631 if (key == "/Contents" && object.isDictionaryOfType("/Sig") &&
... ... @@ -1639,14 +1638,13 @@ QPDFWriter::unparseObject(
1639 1638 } else {
1640 1639 unparseChild(item.second, level + 1, child_flags);
1641 1640 }
1642   - writeStringQDF("\n");
1643 1641 }
1644 1642 }
1645 1643  
1646 1644 if (flags & f_stream) {
1647   - writeStringQDF(indent);
1648   - writeStringQDF(" ");
1649   - writeString(" /Length ");
  1645 + writeString(indent);
  1646 + writeStringQDF(" ");
  1647 + writeString("/Length ");
1650 1648  
1651 1649 if (this->m->direct_stream_lengths) {
1652 1650 writeString(std::to_string(stream_length));
... ... @@ -1654,17 +1652,14 @@ QPDFWriter::unparseObject(
1654 1652 writeString(std::to_string(this->m->cur_stream_length_id));
1655 1653 writeString(" 0 R");
1656 1654 }
1657   - writeStringQDF("\n");
1658 1655 if (compress && (flags & f_filtered)) {
1659   - writeStringQDF(indent);
1660   - writeStringQDF(" ");
1661   - writeString(" /Filter /FlateDecode");
1662   - writeStringQDF("\n");
  1656 + writeString(indent);
  1657 + writeStringQDF(" ");
  1658 + writeString("/Filter /FlateDecode");
1663 1659 }
1664 1660 }
1665 1661  
1666   - writeStringQDF(indent);
1667   - writeStringNoQDF(" ");
  1662 + writeString(indent);
1668 1663 writeString(">>");
1669 1664 } else if (tc == ::ot_stream) {
1670 1665 // Write stream data to a buffer.
... ...