Commit 8082af09bea1132cecc2d148eeb23bc05e66f6b2
1 parent
014dcb02
Add PDFVersion class
Showing
7 changed files
with
204 additions
and
0 deletions
ChangeLog
| 1 | +2022-02-08 Jay Berkenbilt <ejb@ql.org> | ||
| 2 | + | ||
| 3 | + * Add new class PDFVersion for more convenient comparison of PDF | ||
| 4 | + version numbers from the %!PDF header. | ||
| 5 | + | ||
| 1 | 2022-02-06 Jay Berkenbilt <ejb@ql.org> | 6 | 2022-02-06 Jay Berkenbilt <ejb@ql.org> |
| 2 | 7 | ||
| 3 | * Pl_Buffer and QPDFWriter: add getBufferSharedPointer(), which | 8 | * Pl_Buffer and QPDFWriter: add getBufferSharedPointer(), which |
include/qpdf/PDFVersion.hh
0 โ 100644
| 1 | +// Copyright (c) 2005-2022 Jay Berkenbilt | ||
| 2 | +// | ||
| 3 | +// This file is part of qpdf. | ||
| 4 | +// | ||
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 6 | +// you may not use this file except in compliance with the License. | ||
| 7 | +// You may obtain a copy of the License at | ||
| 8 | +// | ||
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 | ||
| 10 | +// | ||
| 11 | +// Unless required by applicable law or agreed to in writing, software | ||
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, | ||
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 14 | +// See the License for the specific language governing permissions and | ||
| 15 | +// limitations under the License. | ||
| 16 | +// | ||
| 17 | +// Versions of qpdf prior to version 7 were released under the terms | ||
| 18 | +// of version 2.0 of the Artistic License. At your option, you may | ||
| 19 | +// continue to consider qpdf to be licensed under those terms. Please | ||
| 20 | +// see the manual for additional information. | ||
| 21 | + | ||
| 22 | +// This class implements a simple writer for saving QPDF objects to | ||
| 23 | +// new PDF files. See comments through the header file for additional | ||
| 24 | +// details. | ||
| 25 | + | ||
| 26 | +#ifndef PDFVERSION_HH | ||
| 27 | +#define PDFVERSION_HH | ||
| 28 | + | ||
| 29 | +#include <qpdf/DLL.h> | ||
| 30 | +#include <string> | ||
| 31 | + | ||
| 32 | +class PDFVersion | ||
| 33 | +{ | ||
| 34 | + public: | ||
| 35 | + // Represent a PDF version. PDF versions are typically | ||
| 36 | + // major.minor, but PDF 1.7 has several extension levels as the | ||
| 37 | + // ISO 32000 spec was in progress. This class helps with | ||
| 38 | + // comparison of versions. | ||
| 39 | + QPDF_DLL | ||
| 40 | + PDFVersion(); | ||
| 41 | + QPDF_DLL | ||
| 42 | + PDFVersion(PDFVersion const&) = default; | ||
| 43 | + QPDF_DLL | ||
| 44 | + PDFVersion(int major, int minor, int extension = 0); | ||
| 45 | + QPDF_DLL | ||
| 46 | + bool operator<(PDFVersion const& rhs) const; | ||
| 47 | + QPDF_DLL | ||
| 48 | + bool operator==(PDFVersion const& rhs) const; | ||
| 49 | + | ||
| 50 | + // Replace this version with the other one if the other one is | ||
| 51 | + // greater. | ||
| 52 | + QPDF_DLL | ||
| 53 | + void updateIfGreater(PDFVersion const& other); | ||
| 54 | + | ||
| 55 | + // Initialize a string and integer suitable for passing to | ||
| 56 | + // QPDFWriter::setMinimumPDFVersion or QPDFWriter::forcePDFVersion. | ||
| 57 | + QPDF_DLL | ||
| 58 | + void getVersion(std::string& version, int& extension_level) const; | ||
| 59 | + | ||
| 60 | + QPDF_DLL | ||
| 61 | + int getMajor() const; | ||
| 62 | + QPDF_DLL | ||
| 63 | + int getMinor() const; | ||
| 64 | + QPDF_DLL | ||
| 65 | + int getExtensionLevel() const; | ||
| 66 | + | ||
| 67 | + private: | ||
| 68 | + int major; | ||
| 69 | + int minor; | ||
| 70 | + int extension; | ||
| 71 | +}; | ||
| 72 | + | ||
| 73 | +#endif // PDFVERSION_HH |
libqpdf/PDFVersion.cc
0 โ 100644
| 1 | +#include <qpdf/PDFVersion.hh> | ||
| 2 | + | ||
| 3 | +#include <qpdf/QUtil.hh> | ||
| 4 | + | ||
| 5 | +PDFVersion::PDFVersion() : | ||
| 6 | + PDFVersion(0, 0, 0) | ||
| 7 | +{ | ||
| 8 | +} | ||
| 9 | + | ||
| 10 | +PDFVersion::PDFVersion(int major, int minor, int extension) : | ||
| 11 | + major(major), | ||
| 12 | + minor(minor), | ||
| 13 | + extension(extension) | ||
| 14 | +{ | ||
| 15 | +} | ||
| 16 | + | ||
| 17 | +bool | ||
| 18 | +PDFVersion::operator<(PDFVersion const& rhs) const | ||
| 19 | +{ | ||
| 20 | + return ((this->major < rhs.major) ? true : | ||
| 21 | + (this->major > rhs.major) ? false : | ||
| 22 | + (this->minor < rhs.minor) ? true : | ||
| 23 | + (this->minor > rhs.minor) ? false : | ||
| 24 | + (this->extension < rhs.minor) ? true : | ||
| 25 | + false); | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +bool | ||
| 29 | +PDFVersion::operator==(PDFVersion const& rhs) const | ||
| 30 | +{ | ||
| 31 | + return ((this->major == rhs.major) && | ||
| 32 | + (this->minor == rhs.minor) && | ||
| 33 | + (this->extension == rhs.extension)); | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +void | ||
| 37 | +PDFVersion::updateIfGreater(PDFVersion const& other) | ||
| 38 | +{ | ||
| 39 | + if (*this < other) | ||
| 40 | + { | ||
| 41 | + *this = other; | ||
| 42 | + } | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +void | ||
| 46 | +PDFVersion::getVersion(std::string& version, int& extension_level) const | ||
| 47 | +{ | ||
| 48 | + extension_level = this->extension; | ||
| 49 | + version = QUtil::int_to_string(this->major) + "." + | ||
| 50 | + QUtil::int_to_string(this->minor); | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +int | ||
| 54 | +PDFVersion::getMajor() const | ||
| 55 | +{ | ||
| 56 | + return this->major; | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | +int | ||
| 60 | +PDFVersion::getMinor() const | ||
| 61 | +{ | ||
| 62 | + return this->minor; | ||
| 63 | +} | ||
| 64 | + | ||
| 65 | +int | ||
| 66 | +PDFVersion::getExtensionLevel() const | ||
| 67 | +{ | ||
| 68 | + return this->extension; | ||
| 69 | +} |
libqpdf/build.mk
| @@ -42,6 +42,7 @@ SRCS_libqpdf = \ | @@ -42,6 +42,7 @@ SRCS_libqpdf = \ | ||
| 42 | libqpdf/MD5.cc \ | 42 | libqpdf/MD5.cc \ |
| 43 | libqpdf/NNTree.cc \ | 43 | libqpdf/NNTree.cc \ |
| 44 | libqpdf/OffsetInputSource.cc \ | 44 | libqpdf/OffsetInputSource.cc \ |
| 45 | + libqpdf/PDFVersion.cc \ | ||
| 45 | libqpdf/Pipeline.cc \ | 46 | libqpdf/Pipeline.cc \ |
| 46 | libqpdf/Pl_AES_PDF.cc \ | 47 | libqpdf/Pl_AES_PDF.cc \ |
| 47 | libqpdf/Pl_ASCII85Decoder.cc \ | 48 | libqpdf/Pl_ASCII85Decoder.cc \ |
libtests/build.mk
libtests/pdf_version.cc
0 โ 100644
| 1 | +#include <qpdf/PDFVersion.hh> | ||
| 2 | + | ||
| 3 | +#include <cassert> | ||
| 4 | +#include <iostream> | ||
| 5 | + | ||
| 6 | +int main() | ||
| 7 | +{ | ||
| 8 | + PDFVersion v1; | ||
| 9 | + assert(v1.getMajor() == 0); | ||
| 10 | + assert(v1.getMinor() == 0); | ||
| 11 | + assert(v1.getExtensionLevel() == 0); | ||
| 12 | + v1 = PDFVersion(1, 7, 8); | ||
| 13 | + assert(v1.getMajor() == 1); | ||
| 14 | + assert(v1.getMinor() == 7); | ||
| 15 | + assert(v1.getExtensionLevel() == 8); | ||
| 16 | + std::string version; | ||
| 17 | + int extension_level = -1; | ||
| 18 | + v1.getVersion(version, extension_level); | ||
| 19 | + assert(version == "1.7"); | ||
| 20 | + assert(extension_level == 8); | ||
| 21 | + PDFVersion v2(1, 5); | ||
| 22 | + v2.getVersion(version, extension_level); | ||
| 23 | + assert(version == "1.5"); | ||
| 24 | + assert(extension_level == 0); | ||
| 25 | + assert(v2 < v1); | ||
| 26 | + PDFVersion v3 = v1; | ||
| 27 | + assert(v3 == v1); | ||
| 28 | + v1.updateIfGreater(v2); | ||
| 29 | + assert(v3 == v1); | ||
| 30 | + assert(! (v3 == v2)); | ||
| 31 | + assert(! (v2 == v1)); | ||
| 32 | + v2.updateIfGreater(v1); | ||
| 33 | + assert(v2 == v1); | ||
| 34 | + v2.getVersion(version, extension_level); | ||
| 35 | + assert(version == "1.7"); | ||
| 36 | + assert(extension_level == 8); | ||
| 37 | + std::cout << "PDFVersion assertions passed" << std::endl; | ||
| 38 | + return 0; | ||
| 39 | +} |
libtests/qtest/version.test
0 โ 100644
| 1 | +#!/usr/bin/env perl | ||
| 2 | +require 5.008; | ||
| 3 | +use warnings; | ||
| 4 | +use strict; | ||
| 5 | + | ||
| 6 | +require TestDriver; | ||
| 7 | + | ||
| 8 | +my $td = new TestDriver('pdf_version'); | ||
| 9 | + | ||
| 10 | +$td->runtest("pdf_version", | ||
| 11 | + {$td->COMMAND => "pdf_version"}, | ||
| 12 | + {$td->STRING => "PDFVersion assertions passed\n", | ||
| 13 | + $td->EXIT_STATUS => 0}, | ||
| 14 | + $td->NORMALIZE_NEWLINES); | ||
| 15 | + | ||
| 16 | +$td->report(1); |