"Fossies" - the Fresh Open Source Software Archive

Member "poppler-0.82.0/qt5/tests/check_fonts.cpp" (25 Oct 2019, 7096 Bytes) of package /linux/misc/poppler-0.82.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include <QtTest/QtTest>
    2 
    3 #include <poppler-qt5.h>
    4 
    5 #include <memory>
    6 
    7 class TestFontsData: public QObject
    8 {
    9     Q_OBJECT
   10 public:
   11     TestFontsData(QObject *parent = nullptr) : QObject(parent) { }
   12 private slots:
   13     void checkNoFonts();
   14     void checkType1();
   15     void checkType3();
   16     void checkTrueType();
   17     void checkFontIterator();
   18     void checkSecondDocumentQuery();
   19     void checkMultipleIterations();
   20     void checkIteratorFonts();
   21 };
   22 
   23 
   24 static QList<Poppler::FontInfo> loadFontsViaIterator( Poppler::Document *doc, int from = 0, int count = -1 )
   25 {
   26     int num = count == -1 ? doc->numPages() - from : count;
   27     QList<Poppler::FontInfo> list;
   28     std::unique_ptr< Poppler::FontIterator > it( doc->newFontIterator( from ) );
   29     while ( it->hasNext() && num )
   30     {
   31         list += it->next();
   32         --num;
   33     }
   34     return list;
   35 }
   36 
   37 namespace Poppler
   38 {
   39 static bool operator==( const FontInfo &f1, const FontInfo &f2 )
   40 {
   41     if ( f1.name() != f2.name() )
   42         return false;
   43     if ( f1.file() != f2.file() )
   44         return false;
   45     if ( f1.isEmbedded() != f2.isEmbedded() )
   46         return false;
   47     if ( f1.isSubset() != f2.isSubset() )
   48         return false;
   49     if ( f1.type() != f2.type() )
   50         return false;
   51     if ( f1.typeName() != f2.typeName() )
   52         return false;
   53     return true;
   54 }
   55 }
   56 
   57 void TestFontsData::checkNoFonts()
   58 {
   59     Poppler::Document *doc;
   60     doc = Poppler::Document::load(TESTDATADIR "/tests/image.pdf");
   61     QVERIFY( doc );
   62 
   63     QList<Poppler::FontInfo> listOfFonts = doc->fonts();
   64     QCOMPARE( listOfFonts.size(), 0 );
   65 
   66     delete doc;
   67 }
   68 
   69 void TestFontsData::checkType1()
   70 {
   71     Poppler::Document *doc;
   72     doc = Poppler::Document::load(TESTDATADIR "/tests/text.pdf");
   73     QVERIFY( doc );
   74 
   75     QList<Poppler::FontInfo> listOfFonts = doc->fonts();
   76     QCOMPARE( listOfFonts.size(), 1 );
   77     QCOMPARE( listOfFonts.at(0).name(), QLatin1String("Helvetica") );
   78     QCOMPARE( listOfFonts.at(0).type(), Poppler::FontInfo::Type1 );
   79     QCOMPARE( listOfFonts.at(0).typeName(), QLatin1String("Type 1") );
   80 
   81     QCOMPARE( listOfFonts.at(0).isEmbedded(), false );
   82     QCOMPARE( listOfFonts.at(0).isSubset(), false );
   83 
   84     delete doc;
   85 }
   86 
   87 void TestFontsData::checkType3()
   88 {
   89     Poppler::Document *doc;
   90     doc = Poppler::Document::load(TESTDATADIR "/tests/type3.pdf");
   91     QVERIFY( doc );
   92 
   93     QList<Poppler::FontInfo> listOfFonts = doc->fonts();
   94     QCOMPARE( listOfFonts.size(), 2 );
   95     QCOMPARE( listOfFonts.at(0).name(), QLatin1String("Helvetica") );
   96     QCOMPARE( listOfFonts.at(0).type(), Poppler::FontInfo::Type1 );
   97     QCOMPARE( listOfFonts.at(0).typeName(), QLatin1String("Type 1") );
   98 
   99     QCOMPARE( listOfFonts.at(0).isEmbedded(), false );
  100     QCOMPARE( listOfFonts.at(0).isSubset(), false );
  101 
  102     QCOMPARE( listOfFonts.at(1).name(), QString() );
  103     QCOMPARE( listOfFonts.at(1).type(), Poppler::FontInfo::Type3 );
  104     QCOMPARE( listOfFonts.at(1).typeName(), QLatin1String("Type 3") );
  105 
  106     QCOMPARE( listOfFonts.at(1).isEmbedded(), true );
  107     QCOMPARE( listOfFonts.at(1).isSubset(), false );
  108 
  109     delete doc;
  110 }
  111 
  112 void TestFontsData::checkTrueType()
  113 {
  114     Poppler::Document *doc;
  115     doc = Poppler::Document::load(TESTDATADIR "/unittestcases/truetype.pdf");
  116     QVERIFY( doc );
  117 
  118     QList<Poppler::FontInfo> listOfFonts = doc->fonts();
  119     QCOMPARE( listOfFonts.size(), 2 );
  120     QCOMPARE( listOfFonts.at(0).name(), QLatin1String("Arial-BoldMT") );
  121     QCOMPARE( listOfFonts.at(0).type(), Poppler::FontInfo::TrueType );
  122     QCOMPARE( listOfFonts.at(0).typeName(), QLatin1String("TrueType") );
  123 
  124     QCOMPARE( listOfFonts.at(0).isEmbedded(), false );
  125     QCOMPARE( listOfFonts.at(0).isSubset(), false );
  126 
  127     QCOMPARE( listOfFonts.at(1).name(), QLatin1String("ArialMT") );
  128     QCOMPARE( listOfFonts.at(1).type(), Poppler::FontInfo::TrueType );
  129     QCOMPARE( listOfFonts.at(1).typeName(), QLatin1String("TrueType") );
  130 
  131     QCOMPARE( listOfFonts.at(1).isEmbedded(), false );
  132     QCOMPARE( listOfFonts.at(1).isSubset(), false );
  133 
  134     delete doc;
  135 }
  136 
  137 void TestFontsData::checkFontIterator()
  138 {
  139     // loading a 1-page document
  140     Poppler::Document *doc;
  141     doc = Poppler::Document::load(TESTDATADIR "/tests/type3.pdf");
  142     QVERIFY( doc );
  143     // loading a 6-pages document
  144     Poppler::Document *doc6 = Poppler::Document::load(TESTDATADIR "/tests/cropbox.pdf");
  145     QVERIFY( doc6 );
  146 
  147     std::unique_ptr< Poppler::FontIterator > it;
  148 
  149     // some tests with the 1-page document:
  150     // - check a default iterator
  151     it.reset( doc->newFontIterator() );
  152     QVERIFY( it->hasNext() );
  153     // - check an iterator for negative pages to behave as 0
  154     it.reset( doc->newFontIterator( -1 ) );
  155     QVERIFY( it->hasNext() );
  156     // - check an iterator for pages out of the page limit
  157     it.reset( doc->newFontIterator( 1 ) );
  158     QVERIFY( !it->hasNext() );
  159     // - check that it reaches the end after 1 iteration
  160     it.reset( doc->newFontIterator() );
  161     QVERIFY( it->hasNext() );
  162     it->next();
  163     QVERIFY( !it->hasNext() );
  164 
  165     // some tests with the 6-page document:
  166     // - check a default iterator
  167     it.reset( doc6->newFontIterator() );
  168     QVERIFY( it->hasNext() );
  169     // - check an iterator for pages out of the page limit
  170     it.reset( doc6->newFontIterator( 6 ) );
  171     QVERIFY( !it->hasNext() );
  172     // - check that it reaches the end after 6 iterations
  173     it.reset( doc6->newFontIterator() );
  174     QVERIFY( it->hasNext() );
  175     it->next();
  176     QVERIFY( it->hasNext() );
  177     it->next();
  178     QVERIFY( it->hasNext() );
  179     it->next();
  180     QVERIFY( it->hasNext() );
  181     it->next();
  182     QVERIFY( it->hasNext() );
  183     it->next();
  184     QVERIFY( it->hasNext() );
  185     it->next();
  186     QVERIFY( !it->hasNext() );
  187 
  188     delete doc;
  189     delete doc6;
  190 }
  191 
  192 void TestFontsData::checkSecondDocumentQuery()
  193 {
  194     Poppler::Document *doc;
  195     doc = Poppler::Document::load(TESTDATADIR "/tests/type3.pdf");
  196     QVERIFY( doc );
  197 
  198     QList<Poppler::FontInfo> listOfFonts = doc->fonts();
  199     QCOMPARE( listOfFonts.size(), 2 );
  200     // check we get the very same result when calling fonts() again (#19405)
  201     QList<Poppler::FontInfo> listOfFonts2 = doc->fonts();
  202     QCOMPARE( listOfFonts, listOfFonts2 );
  203 
  204     delete doc;
  205 }
  206 
  207 void TestFontsData::checkMultipleIterations()
  208 {
  209     Poppler::Document *doc;
  210     doc = Poppler::Document::load(TESTDATADIR "/tests/type3.pdf");
  211     QVERIFY( doc );
  212 
  213     QList<Poppler::FontInfo> listOfFonts = loadFontsViaIterator( doc );
  214     QCOMPARE( listOfFonts.size(), 2 );
  215     QList<Poppler::FontInfo> listOfFonts2 = loadFontsViaIterator( doc );
  216     QCOMPARE( listOfFonts, listOfFonts2 );
  217 
  218     delete doc;
  219 }
  220 
  221 void TestFontsData::checkIteratorFonts()
  222 {
  223     Poppler::Document *doc;
  224     doc = Poppler::Document::load(TESTDATADIR "/tests/fonts.pdf");
  225     QVERIFY( doc );
  226 
  227     QList<Poppler::FontInfo> listOfFonts = doc->fonts();
  228     QCOMPARE( listOfFonts.size(), 3 );
  229     
  230     // check we get the very same result when gatering fonts using the iterator
  231     QList<Poppler::FontInfo> listOfFonts2 = loadFontsViaIterator( doc );
  232     QCOMPARE( listOfFonts, listOfFonts2 );
  233 
  234     delete doc;
  235 }
  236 
  237 QTEST_GUILESS_MAIN(TestFontsData)
  238 #include "check_fonts.moc"
  239