Commit 37fcc5ff71ef2fc657623d908e50d6b388fae78e
1 parent
b444ab33
Create ResourceFinder from NameWatcher in QPDFPageObjectHelper
Showing
4 changed files
with
67 additions
and
37 deletions
libqpdf/QPDFPageObjectHelper.cc
| ... | ... | @@ -8,6 +8,7 @@ |
| 8 | 8 | #include <qpdf/QPDFMatrix.hh> |
| 9 | 9 | #include <qpdf/QIntC.hh> |
| 10 | 10 | #include <qpdf/QPDFAcroFormDocumentHelper.hh> |
| 11 | +#include <qpdf/ResourceFinder.hh> | |
| 11 | 12 | |
| 12 | 13 | class ContentProvider: public QPDFObjectHandle::StreamDataProvider |
| 13 | 14 | { |
| ... | ... | @@ -670,38 +671,6 @@ QPDFPageObjectHelper::addContentTokenFilter( |
| 670 | 671 | } |
| 671 | 672 | } |
| 672 | 673 | |
| 673 | -class NameWatcher: public QPDFObjectHandle::TokenFilter | |
| 674 | -{ | |
| 675 | - public: | |
| 676 | - NameWatcher() : | |
| 677 | - saw_bad(false) | |
| 678 | - { | |
| 679 | - } | |
| 680 | - virtual ~NameWatcher() | |
| 681 | - { | |
| 682 | - } | |
| 683 | - virtual void handleToken(QPDFTokenizer::Token const&); | |
| 684 | - std::set<std::string> names; | |
| 685 | - bool saw_bad; | |
| 686 | -}; | |
| 687 | - | |
| 688 | -void | |
| 689 | -NameWatcher::handleToken(QPDFTokenizer::Token const& token) | |
| 690 | -{ | |
| 691 | - if (token.getType() == QPDFTokenizer::tt_name) | |
| 692 | - { | |
| 693 | - // Create a name object and get its name. This canonicalizes | |
| 694 | - // the representation of the name | |
| 695 | - this->names.insert( | |
| 696 | - QPDFObjectHandle::newName(token.getValue()).getName()); | |
| 697 | - } | |
| 698 | - else if (token.getType() == QPDFTokenizer::tt_bad) | |
| 699 | - { | |
| 700 | - saw_bad = true; | |
| 701 | - } | |
| 702 | - writeToken(token); | |
| 703 | -} | |
| 704 | - | |
| 705 | 674 | bool |
| 706 | 675 | QPDFPageObjectHelper::removeUnreferencedResourcesHelper( |
| 707 | 676 | QPDFPageObjectHelper ph, std::set<std::string>& unresolved) |
| ... | ... | @@ -712,10 +681,10 @@ QPDFPageObjectHelper::removeUnreferencedResourcesHelper( |
| 712 | 681 | QTC::TC("qpdf", "QPDFPageObjectHelper filter form xobject"); |
| 713 | 682 | } |
| 714 | 683 | |
| 715 | - NameWatcher nw; | |
| 684 | + ResourceFinder rf; | |
| 716 | 685 | try |
| 717 | 686 | { |
| 718 | - ph.filterContents(&nw); | |
| 687 | + ph.filterContents(&rf); | |
| 719 | 688 | } |
| 720 | 689 | catch (std::exception& e) |
| 721 | 690 | { |
| ... | ... | @@ -725,7 +694,7 @@ QPDFPageObjectHelper::removeUnreferencedResourcesHelper( |
| 725 | 694 | " from this object"); |
| 726 | 695 | return false; |
| 727 | 696 | } |
| 728 | - if (nw.saw_bad) | |
| 697 | + if (rf.sawBad()) | |
| 729 | 698 | { |
| 730 | 699 | QTC::TC("qpdf", "QPDFPageObjectHelper bad token finding names"); |
| 731 | 700 | ph.oh.warnIfPossible( |
| ... | ... | @@ -760,7 +729,7 @@ QPDFPageObjectHelper::removeUnreferencedResourcesHelper( |
| 760 | 729 | } |
| 761 | 730 | |
| 762 | 731 | std::set<std::string> local_unresolved; |
| 763 | - for (auto const& name: nw.names) | |
| 732 | + for (auto const& name: rf.getNames()) | |
| 764 | 733 | { |
| 765 | 734 | if (! known_names.count(name)) |
| 766 | 735 | { |
| ... | ... | @@ -804,7 +773,7 @@ QPDFPageObjectHelper::removeUnreferencedResourcesHelper( |
| 804 | 773 | // xobject, so don't remove it. |
| 805 | 774 | QTC::TC("qpdf", "QPDFPageObjectHelper resolving unresolved"); |
| 806 | 775 | } |
| 807 | - else if (! nw.names.count(key)) | |
| 776 | + else if (! rf.getNames().count(key)) | |
| 808 | 777 | { |
| 809 | 778 | dict.removeKey(key); |
| 810 | 779 | } | ... | ... |
libqpdf/ResourceFinder.cc
0 → 100644
| 1 | +#include <qpdf/ResourceFinder.hh> | |
| 2 | + | |
| 3 | +ResourceFinder::ResourceFinder() : | |
| 4 | + saw_bad(false) | |
| 5 | +{ | |
| 6 | +} | |
| 7 | + | |
| 8 | +void | |
| 9 | +ResourceFinder::handleToken(QPDFTokenizer::Token const& token) | |
| 10 | +{ | |
| 11 | + if ((token.getType() == QPDFTokenizer::tt_word) && | |
| 12 | + (! this->last_name.empty())) | |
| 13 | + { | |
| 14 | + this->names.insert(this->last_name); | |
| 15 | + } | |
| 16 | + else if (token.getType() == QPDFTokenizer::tt_name) | |
| 17 | + { | |
| 18 | + this->last_name = | |
| 19 | + QPDFObjectHandle::newName(token.getValue()).getName(); | |
| 20 | + } | |
| 21 | + else if (token.getType() == QPDFTokenizer::tt_bad) | |
| 22 | + { | |
| 23 | + saw_bad = true; | |
| 24 | + } | |
| 25 | + writeToken(token); | |
| 26 | +} | |
| 27 | + | |
| 28 | +std::set<std::string> const& | |
| 29 | +ResourceFinder::getNames() const | |
| 30 | +{ | |
| 31 | + return this->names; | |
| 32 | +} | |
| 33 | + | |
| 34 | +bool | |
| 35 | +ResourceFinder::sawBad() const | |
| 36 | +{ | |
| 37 | + return this->saw_bad; | |
| 38 | +} | ... | ... |
libqpdf/build.mk
libqpdf/qpdf/ResourceFinder.hh
0 → 100644
| 1 | +#ifndef RESOURCEFINDER_HH | |
| 2 | +#define RESOURCEFINDER_HH | |
| 3 | + | |
| 4 | +#include <qpdf/QPDFObjectHandle.hh> | |
| 5 | + | |
| 6 | +class ResourceFinder: public QPDFObjectHandle::TokenFilter | |
| 7 | +{ | |
| 8 | + public: | |
| 9 | + ResourceFinder(); | |
| 10 | + virtual ~ResourceFinder() = default; | |
| 11 | + virtual void handleToken(QPDFTokenizer::Token const&) override; | |
| 12 | + std::set<std::string> const& getNames() const; | |
| 13 | + bool sawBad() const; | |
| 14 | + | |
| 15 | + private: | |
| 16 | + std::string last_name; | |
| 17 | + std::set<std::string> names; | |
| 18 | + std::map<std::string, std::set<std::string>> names_by_resource_type; | |
| 19 | + bool saw_bad; | |
| 20 | +}; | |
| 21 | + | |
| 22 | +#endif // RESOURCEFINDER_HH | ... | ... |