Commit ef3a8025fb7664e2533c8380abe7ae96e581a0e9
1 parent
ccad589f
In QPDFWriter replace map lenghts with ObjTable new_obj
Showing
4 changed files
with
29 additions
and
48 deletions
include/qpdf/QPDF.hh
| ... | ... | @@ -744,14 +744,13 @@ class QPDF |
| 744 | 744 | generateHintStream( |
| 745 | 745 | QPDF& qpdf, |
| 746 | 746 | QPDFWriter::NewObjTable const& new_obj, |
| 747 | - std::map<int, qpdf_offset_t> const& lengths, | |
| 748 | 747 | QPDFWriter::ObjTable const& obj, |
| 749 | 748 | std::shared_ptr<Buffer>& hint_stream, |
| 750 | 749 | int& S, |
| 751 | 750 | int& O, |
| 752 | 751 | bool compressed) |
| 753 | 752 | { |
| 754 | - return qpdf.generateHintStream(new_obj, lengths, obj, hint_stream, S, O, compressed); | |
| 753 | + return qpdf.generateHintStream(new_obj, obj, hint_stream, S, O, compressed); | |
| 755 | 754 | } |
| 756 | 755 | |
| 757 | 756 | static void |
| ... | ... | @@ -1103,7 +1102,6 @@ class QPDF |
| 1103 | 1102 | |
| 1104 | 1103 | void generateHintStream( |
| 1105 | 1104 | QPDFWriter::NewObjTable const& new_obj, |
| 1106 | - std::map<int, qpdf_offset_t> const& lengths, | |
| 1107 | 1105 | QPDFWriter::ObjTable const& obj, |
| 1108 | 1106 | std::shared_ptr<Buffer>& hint_stream, |
| 1109 | 1107 | int& S, |
| ... | ... | @@ -1378,20 +1376,13 @@ class QPDF |
| 1378 | 1376 | int outputLengthNextN( |
| 1379 | 1377 | int in_object, |
| 1380 | 1378 | int n, |
| 1381 | - std::map<int, qpdf_offset_t> const& lengths, | |
| 1382 | - QPDFWriter::ObjTable const& obj); | |
| 1383 | - void calculateHPageOffset( | |
| 1384 | - QPDFWriter::NewObjTable const& new_obj, | |
| 1385 | - std::map<int, qpdf_offset_t> const& lengths, | |
| 1386 | - QPDFWriter::ObjTable const& obj); | |
| 1387 | - void calculateHSharedObject( | |
| 1388 | 1379 | QPDFWriter::NewObjTable const& new_obj, |
| 1389 | - std::map<int, qpdf_offset_t> const& lengths, | |
| 1390 | - QPDFWriter::ObjTable const& obj); | |
| 1391 | - void calculateHOutline( | |
| 1392 | - QPDFWriter::NewObjTable const& new_obj, | |
| 1393 | - std::map<int, qpdf_offset_t> const& lengths, | |
| 1394 | 1380 | QPDFWriter::ObjTable const& obj); |
| 1381 | + void | |
| 1382 | + calculateHPageOffset(QPDFWriter::NewObjTable const& new_obj, QPDFWriter::ObjTable const& obj); | |
| 1383 | + void | |
| 1384 | + calculateHSharedObject(QPDFWriter::NewObjTable const& new_obj, QPDFWriter::ObjTable const& obj); | |
| 1385 | + void calculateHOutline(QPDFWriter::NewObjTable const& new_obj, QPDFWriter::ObjTable const& obj); | |
| 1395 | 1386 | void writeHPageOffset(BitWriter&); |
| 1396 | 1387 | void writeHSharedObject(BitWriter&); |
| 1397 | 1388 | void writeHGeneric(BitWriter&, HGeneric&); | ... | ... |
libqpdf/QPDFWriter.cc
| ... | ... | @@ -1050,7 +1050,8 @@ QPDFWriter::closeObject(int objid) |
| 1050 | 1050 | // Write a newline before endobj as it makes the file easier to repair. |
| 1051 | 1051 | writeString("\nendobj\n"); |
| 1052 | 1052 | writeStringQDF("\n"); |
| 1053 | - m->lengths[objid] = m->pipeline->getCount() - m->new_obj[objid].xref.getOffset(); | |
| 1053 | + auto& new_obj = m->new_obj[objid]; | |
| 1054 | + new_obj.length = m->pipeline->getCount() - new_obj.xref.getOffset(); | |
| 1054 | 1055 | } |
| 1055 | 1056 | |
| 1056 | 1057 | void |
| ... | ... | @@ -2309,8 +2310,7 @@ QPDFWriter::writeHintStream(int hint_id) |
| 2309 | 2310 | int S = 0; |
| 2310 | 2311 | int O = 0; |
| 2311 | 2312 | bool compressed = (m->compress_streams && !m->qdf_mode); |
| 2312 | - QPDF::Writer::generateHintStream( | |
| 2313 | - m->pdf, m->new_obj, m->lengths, m->obj, hint_buffer, S, O, compressed); | |
| 2313 | + QPDF::Writer::generateHintStream(m->pdf, m->new_obj, m->obj, hint_buffer, S, O, compressed); | |
| 2314 | 2314 | |
| 2315 | 2315 | openObject(hint_id); |
| 2316 | 2316 | setDataKey(hint_id); | ... | ... |
libqpdf/QPDF_linearization.cc
| ... | ... | @@ -1457,33 +1457,29 @@ nbits(int val) |
| 1457 | 1457 | |
| 1458 | 1458 | int |
| 1459 | 1459 | QPDF::outputLengthNextN( |
| 1460 | - int in_object, | |
| 1461 | - int n, | |
| 1462 | - std::map<int, qpdf_offset_t> const& lengths, | |
| 1463 | - QPDFWriter::ObjTable const& obj) | |
| 1460 | + int in_object, int n, QPDFWriter::NewObjTable const& new_obj, QPDFWriter::ObjTable const& obj) | |
| 1464 | 1461 | { |
| 1465 | 1462 | // Figure out the length of a series of n consecutive objects in the output file starting with |
| 1466 | 1463 | // whatever object in_object from the input file mapped to. |
| 1467 | 1464 | |
| 1468 | 1465 | int first = obj[in_object].renumber; |
| 1466 | + int last = first + n; | |
| 1469 | 1467 | if (first <= 0) { |
| 1470 | 1468 | stopOnError("found object that is not renumbered while writing linearization data"); |
| 1471 | 1469 | } |
| 1472 | - int length = 0; | |
| 1473 | - for (int i = 0; i < n; ++i) { | |
| 1474 | - if (lengths.count(first + i) == 0) { | |
| 1470 | + qpdf_offset_t length = 0; | |
| 1471 | + for (int i = first; i < last; ++i) { | |
| 1472 | + auto l = new_obj[i].length; | |
| 1473 | + if (l == 0) { | |
| 1475 | 1474 | stopOnError("found item with unknown length while writing linearization data"); |
| 1476 | 1475 | } |
| 1477 | - length += toI((*(lengths.find(first + toI(i)))).second); | |
| 1476 | + length += l; | |
| 1478 | 1477 | } |
| 1479 | - return length; | |
| 1478 | + return toI(length); | |
| 1480 | 1479 | } |
| 1481 | 1480 | |
| 1482 | 1481 | void |
| 1483 | -QPDF::calculateHPageOffset( | |
| 1484 | - QPDFWriter::NewObjTable const& new_obj, | |
| 1485 | - std::map<int, qpdf_offset_t> const& lengths, | |
| 1486 | - QPDFWriter::ObjTable const& obj) | |
| 1482 | +QPDF::calculateHPageOffset(QPDFWriter::NewObjTable const& new_obj, QPDFWriter::ObjTable const& obj) | |
| 1487 | 1483 | { |
| 1488 | 1484 | // Page Offset Hint Table |
| 1489 | 1485 | |
| ... | ... | @@ -1498,7 +1494,7 @@ QPDF::calculateHPageOffset( |
| 1498 | 1494 | |
| 1499 | 1495 | int min_nobjects = cphe.at(0).nobjects; |
| 1500 | 1496 | int max_nobjects = min_nobjects; |
| 1501 | - int min_length = outputLengthNextN(pages.at(0).getObjectID(), min_nobjects, lengths, obj); | |
| 1497 | + int min_length = outputLengthNextN(pages.at(0).getObjectID(), min_nobjects, new_obj, obj); | |
| 1502 | 1498 | int max_length = min_length; |
| 1503 | 1499 | int max_shared = cphe.at(0).nshared_objects; |
| 1504 | 1500 | |
| ... | ... | @@ -1515,7 +1511,7 @@ QPDF::calculateHPageOffset( |
| 1515 | 1511 | // assignments. |
| 1516 | 1512 | |
| 1517 | 1513 | int nobjects = cphe.at(i).nobjects; |
| 1518 | - int length = outputLengthNextN(pages.at(i).getObjectID(), nobjects, lengths, obj); | |
| 1514 | + int length = outputLengthNextN(pages.at(i).getObjectID(), nobjects, new_obj, obj); | |
| 1519 | 1515 | int nshared = cphe.at(i).nshared_objects; |
| 1520 | 1516 | |
| 1521 | 1517 | min_nobjects = std::min(min_nobjects, nobjects); |
| ... | ... | @@ -1565,9 +1561,7 @@ QPDF::calculateHPageOffset( |
| 1565 | 1561 | |
| 1566 | 1562 | void |
| 1567 | 1563 | QPDF::calculateHSharedObject( |
| 1568 | - QPDFWriter::NewObjTable const& new_obj, | |
| 1569 | - std::map<int, qpdf_offset_t> const& lengths, | |
| 1570 | - QPDFWriter::ObjTable const& obj) | |
| 1564 | + QPDFWriter::NewObjTable const& new_obj, QPDFWriter::ObjTable const& obj) | |
| 1571 | 1565 | { |
| 1572 | 1566 | CHSharedObject& cso = m->c_shared_object_data; |
| 1573 | 1567 | std::vector<CHSharedObjectEntry>& csoe = cso.entries; |
| ... | ... | @@ -1575,12 +1569,12 @@ QPDF::calculateHSharedObject( |
| 1575 | 1569 | std::vector<HSharedObjectEntry>& soe = so.entries; |
| 1576 | 1570 | soe.clear(); |
| 1577 | 1571 | |
| 1578 | - int min_length = outputLengthNextN(csoe.at(0).object, 1, lengths, obj); | |
| 1572 | + int min_length = outputLengthNextN(csoe.at(0).object, 1, new_obj, obj); | |
| 1579 | 1573 | int max_length = min_length; |
| 1580 | 1574 | |
| 1581 | 1575 | for (size_t i = 0; i < toS(cso.nshared_total); ++i) { |
| 1582 | 1576 | // Assign absolute numbers to deltas; adjust later |
| 1583 | - int length = outputLengthNextN(csoe.at(i).object, 1, lengths, obj); | |
| 1577 | + int length = outputLengthNextN(csoe.at(i).object, 1, new_obj, obj); | |
| 1584 | 1578 | min_length = std::min(min_length, length); |
| 1585 | 1579 | max_length = std::max(max_length, length); |
| 1586 | 1580 | soe.emplace_back(); |
| ... | ... | @@ -1610,10 +1604,7 @@ QPDF::calculateHSharedObject( |
| 1610 | 1604 | } |
| 1611 | 1605 | |
| 1612 | 1606 | void |
| 1613 | -QPDF::calculateHOutline( | |
| 1614 | - QPDFWriter::NewObjTable const& new_obj, | |
| 1615 | - std::map<int, qpdf_offset_t> const& lengths, | |
| 1616 | - QPDFWriter::ObjTable const& obj) | |
| 1607 | +QPDF::calculateHOutline(QPDFWriter::NewObjTable const& new_obj, QPDFWriter::ObjTable const& obj) | |
| 1617 | 1608 | { |
| 1618 | 1609 | HGeneric& cho = m->c_outline_data; |
| 1619 | 1610 | |
| ... | ... | @@ -1626,7 +1617,7 @@ QPDF::calculateHOutline( |
| 1626 | 1617 | ho.first_object = obj[cho.first_object].renumber; |
| 1627 | 1618 | ho.first_object_offset = new_obj[ho.first_object].xref.getOffset(); |
| 1628 | 1619 | ho.nobjects = cho.nobjects; |
| 1629 | - ho.group_length = outputLengthNextN(cho.first_object, ho.nobjects, lengths, obj); | |
| 1620 | + ho.group_length = outputLengthNextN(cho.first_object, ho.nobjects, new_obj, obj); | |
| 1630 | 1621 | } |
| 1631 | 1622 | |
| 1632 | 1623 | template <class T, class int_type> |
| ... | ... | @@ -1756,7 +1747,6 @@ QPDF::writeHGeneric(BitWriter& w, HGeneric& t) |
| 1756 | 1747 | void |
| 1757 | 1748 | QPDF::generateHintStream( |
| 1758 | 1749 | QPDFWriter::NewObjTable const& new_obj, |
| 1759 | - std::map<int, qpdf_offset_t> const& lengths, | |
| 1760 | 1750 | QPDFWriter::ObjTable const& obj, |
| 1761 | 1751 | std::shared_ptr<Buffer>& hint_buffer, |
| 1762 | 1752 | int& S, |
| ... | ... | @@ -1764,9 +1754,9 @@ QPDF::generateHintStream( |
| 1764 | 1754 | bool compressed) |
| 1765 | 1755 | { |
| 1766 | 1756 | // Populate actual hint table values |
| 1767 | - calculateHPageOffset(new_obj, lengths, obj); | |
| 1768 | - calculateHSharedObject(new_obj, lengths, obj); | |
| 1769 | - calculateHOutline(new_obj, lengths, obj); | |
| 1757 | + calculateHPageOffset(new_obj, obj); | |
| 1758 | + calculateHSharedObject(new_obj, obj); | |
| 1759 | + calculateHOutline(new_obj, obj); | |
| 1770 | 1760 | |
| 1771 | 1761 | // Write the hint stream itself into a compressed memory buffer. Write through a counter so we |
| 1772 | 1762 | // can get offsets. | ... | ... |
libqpdf/qpdf/QPDFWriter_private.hh
| ... | ... | @@ -18,6 +18,7 @@ struct QPDFWriter::Object |
| 18 | 18 | struct QPDFWriter::NewObject |
| 19 | 19 | { |
| 20 | 20 | QPDFXRefEntry xref; |
| 21 | + qpdf_offset_t length{0}; | |
| 21 | 22 | }; |
| 22 | 23 | |
| 23 | 24 | class QPDFWriter::ObjTable: public ::ObjTable<QPDFWriter::Object> |
| ... | ... | @@ -102,7 +103,6 @@ class QPDFWriter::Members |
| 102 | 103 | size_t object_queue_front{0}; |
| 103 | 104 | QPDFWriter::ObjTable obj; |
| 104 | 105 | QPDFWriter::NewObjTable new_obj; |
| 105 | - std::map<int, qpdf_offset_t> lengths; | |
| 106 | 106 | int next_objid{1}; |
| 107 | 107 | int cur_stream_length_id{0}; |
| 108 | 108 | size_t cur_stream_length{0}; | ... | ... |