2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 10:58:58 +00:00

Add QPDF::getVersionAsPDFVersion

This commit is contained in:
Jay Berkenbilt 2022-02-08 11:07:37 -05:00
parent 8082af09be
commit cfd5147d92
79 changed files with 138 additions and 21 deletions

View File

@ -3,6 +3,9 @@
* Add new class PDFVersion for more convenient comparison of PDF
version numbers from the %!PDF header.
* Add QPDF::getVersionAsPDFVersion() to return the PDF version and
extension together as a PDFVersion object instead of a string.
2022-02-06 Jay Berkenbilt <ejb@ql.org>
* Pl_Buffer and QPDFWriter: add getBufferSharedPointer(), which

View File

@ -65,9 +65,9 @@ class PDFVersion
int getExtensionLevel() const;
private:
int major;
int minor;
int extension;
int major_version;
int minor_version;
int extension_level;
};
#endif // PDFVERSION_HH

View File

@ -44,6 +44,7 @@
#include <qpdf/QPDFStreamFilter.hh>
#include <qpdf/Buffer.hh>
#include <qpdf/InputSource.hh>
#include <qpdf/PDFVersion.hh>
class QPDF_Stream;
class BitStream;
@ -260,6 +261,10 @@ class QPDF
QPDF_DLL
std::string getFilename() const;
// Return PDF Version and extension level together as a PDFVersion object
QPDF_DLL
PDFVersion getVersionAsPDFVersion();
// Return just the PDF version from the file
QPDF_DLL
std::string getPDFVersion() const;
QPDF_DLL

View File

@ -7,30 +7,30 @@ PDFVersion::PDFVersion() :
{
}
PDFVersion::PDFVersion(int major, int minor, int extension) :
major(major),
minor(minor),
extension(extension)
PDFVersion::PDFVersion(int major_version, int minor_version, int extension_level) :
major_version(major_version),
minor_version(minor_version),
extension_level(extension_level)
{
}
bool
PDFVersion::operator<(PDFVersion const& rhs) const
{
return ((this->major < rhs.major) ? true :
(this->major > rhs.major) ? false :
(this->minor < rhs.minor) ? true :
(this->minor > rhs.minor) ? false :
(this->extension < rhs.minor) ? true :
return ((this->major_version < rhs.major_version) ? true :
(this->major_version > rhs.major_version) ? false :
(this->minor_version < rhs.minor_version) ? true :
(this->minor_version > rhs.minor_version) ? false :
(this->extension_level < rhs.extension_level) ? true :
false);
}
bool
PDFVersion::operator==(PDFVersion const& rhs) const
{
return ((this->major == rhs.major) &&
(this->minor == rhs.minor) &&
(this->extension == rhs.extension));
return ((this->major_version == rhs.major_version) &&
(this->minor_version == rhs.minor_version) &&
(this->extension_level == rhs.extension_level));
}
void
@ -45,25 +45,25 @@ PDFVersion::updateIfGreater(PDFVersion const& other)
void
PDFVersion::getVersion(std::string& version, int& extension_level) const
{
extension_level = this->extension;
version = QUtil::int_to_string(this->major) + "." +
QUtil::int_to_string(this->minor);
extension_level = this->extension_level;
version = QUtil::int_to_string(this->major_version) + "." +
QUtil::int_to_string(this->minor_version);
}
int
PDFVersion::getMajor() const
{
return this->major;
return this->major_version;
}
int
PDFVersion::getMinor() const
{
return this->minor;
return this->minor_version;
}
int
PDFVersion::getExtensionLevel() const
{
return this->extension;
return this->extension_level;
}

View File

@ -11,6 +11,7 @@
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <regex>
#include <qpdf/QTC.hh>
#include <qpdf/QUtil.hh>
@ -2742,6 +2743,24 @@ QPDF::getFilename() const
return this->m->file->getName();
}
PDFVersion
QPDF::getVersionAsPDFVersion()
{
int major = 1;
int minor = 3;
int extension_level = getExtensionLevel();
std::regex v("^[[:space:]]*([0-9]+)\\.([0-9]+)");
std::smatch m;
if (std::regex_search(this->m->pdf_version, m, v))
{
major = QUtil::string_to_int(m[1].str().c_str());
minor = QUtil::string_to_int(m[2].str().c_str());
}
return PDFVersion(major, minor, extension_level);
}
std::string
QPDF::getPDFVersion() const
{

View File

@ -34,6 +34,18 @@ int main()
v2.getVersion(version, extension_level);
assert(version == "1.7");
assert(extension_level == 8);
assert(PDFVersion(1, 2) < PDFVersion(1, 3));
assert(PDFVersion(1, 2) < PDFVersion(1, 2, 1));
assert(PDFVersion(1, 2, 1) < PDFVersion(1, 2, 2));
assert(! (PDFVersion(1, 2, 2) < PDFVersion(1, 2, 2)));
assert(! (PDFVersion(1, 2, 3) < PDFVersion(1, 2, 2)));
assert(PDFVersion(1, 1) < PDFVersion(1, 2, 2));
assert(! (PDFVersion(1, 3) < PDFVersion(1, 2, 2)));
assert(! (PDFVersion(2, 0) < PDFVersion(1, 9, 9)));
assert(! (PDFVersion(2, 0) < PDFVersion(2, 0)));
assert(PDFVersion(2, 0) == PDFVersion(2, 0));
assert(PDFVersion(2, 0, 1) == PDFVersion(2, 0, 1));
std::cout << "PDFVersion assertions passed" << std::endl;
return 0;
}

View File

@ -1,4 +1,5 @@
version: 1.3
extension level: 0
null
As PDFVersion: 1.3/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.6
extension level: 0
null
As PDFVersion: 1.6/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 1
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 1 >> >>
As PDFVersion: 1.7/1
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 2
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 2 /URL (http://something.adobe.com) >> >>
As PDFVersion: 1.7/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 3
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 3 >> >>
As PDFVersion: 1.7/3
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 0
null
As PDFVersion: 1.8/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 2
<< /ADBE << /BaseVersion /1.8 /ExtensionLevel 2 >> >>
As PDFVersion: 1.8/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 5
<< /ADBE << /BaseVersion /1.8 /ExtensionLevel 5 >> >>
As PDFVersion: 1.8/5
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 0
null
As PDFVersion: 1.8/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 2
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 2 /URL (http://something.adobe.com) >> >>
As PDFVersion: 1.7/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 2
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 2 /URL (http://something.adobe.com) >> >>
As PDFVersion: 1.7/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 2
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 2 /URL (http://something.adobe.com) >> >>
As PDFVersion: 1.7/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 2
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 2 /URL (http://something.adobe.com) >> >>
As PDFVersion: 1.7/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 3
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 3 >> >>
As PDFVersion: 1.7/3
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 0
null
As PDFVersion: 1.8/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 2
<< /ADBE << /BaseVersion /1.8 /ExtensionLevel 2 >> >>
As PDFVersion: 1.8/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 5
<< /ADBE << /BaseVersion /1.8 /ExtensionLevel 5 >> >>
As PDFVersion: 1.8/5
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 0
null
As PDFVersion: 1.8/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.3
extension level: 0
<< /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.3/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.6
extension level: 0
<< /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.6/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 1
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 1 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.7/1
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 2
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 2 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.7/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 3
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 3 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.7/3
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 0
<< /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.8/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 2
<< /ADBE << /BaseVersion /1.8 /ExtensionLevel 2 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.8/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 5
<< /ADBE << /BaseVersion /1.8 /ExtensionLevel 5 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.8/5
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 0
<< /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.8/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 2
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 2 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.7/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 2
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 2 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.7/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 2
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 2 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.7/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 2
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 2 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.7/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 3
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 3 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.7/3
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 0
<< /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.8/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 2
<< /ADBE << /BaseVersion /1.8 /ExtensionLevel 2 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.8/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 5
<< /ADBE << /BaseVersion /1.8 /ExtensionLevel 5 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.8/5
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 0
<< /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.8/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.3
extension level: 0
null
As PDFVersion: 1.3/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.6
extension level: 0
null
As PDFVersion: 1.6/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 1
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 1 >> >>
As PDFVersion: 1.7/1
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 2
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 2 >> >>
As PDFVersion: 1.7/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 3
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 3 >> >>
As PDFVersion: 1.7/3
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 0
null
As PDFVersion: 1.8/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 2
<< /ADBE << /BaseVersion /1.8 /ExtensionLevel 2 >> >>
As PDFVersion: 1.8/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 5
<< /ADBE << /BaseVersion /1.8 /ExtensionLevel 5 >> >>
As PDFVersion: 1.8/5
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 0
null
As PDFVersion: 1.8/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.3
extension level: 0
null
As PDFVersion: 1.3/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.6
extension level: 0
null
As PDFVersion: 1.6/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 1
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 1 >> >>
As PDFVersion: 1.7/1
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 2
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 2 >> >>
As PDFVersion: 1.7/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 3
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 3 >> >>
As PDFVersion: 1.7/3
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 0
null
As PDFVersion: 1.8/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 2
<< /ADBE << /BaseVersion /1.8 /ExtensionLevel 2 >> >>
As PDFVersion: 1.8/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 5
<< /ADBE << /BaseVersion /1.8 /ExtensionLevel 5 >> >>
As PDFVersion: 1.8/5
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 0
null
As PDFVersion: 1.8/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.3
extension level: 0
<< /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.3/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.6
extension level: 0
<< /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.6/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 1
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 1 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.7/1
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 2
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 2 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.7/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 3
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 3 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.7/3
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 0
<< /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.8/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 2
<< /ADBE << /BaseVersion /1.8 /ExtensionLevel 2 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.8/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 5
<< /ADBE << /BaseVersion /1.8 /ExtensionLevel 5 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.8/5
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 0
<< /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.8/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.5
extension level: 0
<< /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.5/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.6
extension level: 0
<< /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.6/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 1
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 1 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.7/1
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 2
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 2 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.7/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.7
extension level: 3
<< /ADBE << /BaseVersion /1.7 /ExtensionLevel 3 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.7/3
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 0
<< /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.8/0
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 2
<< /ADBE << /BaseVersion /1.8 /ExtensionLevel 2 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.8/2
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 5
<< /ADBE << /BaseVersion /1.8 /ExtensionLevel 5 >> /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.8/5
test 34 done

View File

@ -1,4 +1,5 @@
version: 1.8
extension level: 0
<< /Potato << /BaseVersion /3.14159 /ExtensionLevel 16059 >> >>
As PDFVersion: 1.8/0
test 34 done

View File

@ -1327,6 +1327,12 @@ static void test_34(QPDF& pdf, char const* arg2)
std::cout << "version: " << pdf.getPDFVersion() << std::endl
<< "extension level: " << pdf.getExtensionLevel() << std::endl
<< pdf.getRoot().getKey("/Extensions").unparse() << std::endl;
auto v = pdf.getVersionAsPDFVersion();
std::string v_string;
int extension_level;
v.getVersion(v_string, extension_level);
std::cout << "As PDFVersion: " << v_string << "/" << extension_level
<< std::endl;
}
static void test_35(QPDF& pdf, char const* arg2)