From 290b64e230b0759900342be4e0b07054028cfcb9 Mon Sep 17 00:00:00 2001 From: James Grogan Date: Fri, 2 Dec 2022 08:44:04 +0000 Subject: [PATCH] Clean up som pdf code. --- src/core/Dictionary.cpp | 20 ++--- src/core/Dictionary.h | 19 +++-- src/publishing/pdf/PdfDictionary.cpp | 18 ++--- src/publishing/pdf/PdfDictionary.h | 3 +- src/publishing/pdf/PdfDocument.cpp | 40 +++++----- src/publishing/pdf/PdfDocument.h | 16 ++-- src/publishing/pdf/PdfDocumentCatalog.cpp | 30 +++---- src/publishing/pdf/PdfDocumentCatalog.h | 7 +- src/publishing/pdf/PdfObject.cpp | 28 +++---- src/publishing/pdf/PdfObject.h | 19 +++-- src/publishing/pdf/PdfOutline.cpp | 34 ++++---- src/publishing/pdf/PdfOutline.h | 8 +- src/publishing/pdf/PdfPage.cpp | 95 +++++++++++++++++++++++ src/publishing/pdf/PdfPage.h | 93 ++-------------------- src/publishing/pdf/PdfPageTree.cpp | 28 +++---- src/publishing/pdf/PdfPageTree.h | 7 +- src/publishing/pdf/PdfStream.cpp | 18 ++--- src/publishing/pdf/PdfStream.h | 7 +- src/publishing/pdf/PdfWriter.cpp | 4 +- src/publishing/pdf/PdfWriter.h | 2 +- src/publishing/pdf/PdfXRefTable.cpp | 10 +-- src/publishing/pdf/PdfXRefTable.h | 9 +-- test/publishing/TestDocumentConverter.cpp | 2 +- test/publishing/TestPdfWriter.cpp | 3 +- 24 files changed, 264 insertions(+), 256 deletions(-) diff --git a/src/core/Dictionary.cpp b/src/core/Dictionary.cpp index 720a75a..4268458 100644 --- a/src/core/Dictionary.cpp +++ b/src/core/Dictionary.cpp @@ -1,21 +1,21 @@ #include "Dictionary.h" -bool Dictionary::HasKey(const std::string& key) const +bool Dictionary::hasKey(const std::string& key) const { - return HasStringKey(key) || HasDictKey(key); + return hasStringKey(key) || hasDictKey(key); } -bool Dictionary::HasStringKey(const std::string& key) const +bool Dictionary::hasStringKey(const std::string& key) const { return mStringData.count(key) > 0; } -bool Dictionary::HasDictKey(const std::string& key) const +bool Dictionary::hasDictKey(const std::string& key) const { return mDictData.count(key) > 0; } -std::vector Dictionary::GetStringKeys() const +std::vector Dictionary::getStringKeys() const { std::vector keys; for (const auto& item : mStringData) @@ -25,7 +25,7 @@ std::vector Dictionary::GetStringKeys() const return keys; } -std::vector Dictionary::GetDictKeys() const +std::vector Dictionary::getDictKeys() const { std::vector keys; for (const auto& item : mDictData) @@ -35,22 +35,22 @@ std::vector Dictionary::GetDictKeys() const return keys; } -Dictionary* Dictionary::GetDict(const std::string& key) const +Dictionary* Dictionary::getDict(const std::string& key) const { return mDictData.at(key).get(); } -std::string Dictionary::GetItem(const std::string& key) const +std::string Dictionary::getItem(const std::string& key) const { return mStringData.at(key); } -void Dictionary::AddStringItem(const std::string& key, const std::string& item) +void Dictionary::addStringItem(const std::string& key, const std::string& item) { mStringData[key] = item; } -void Dictionary::AddDictItem(const std::string& key, std::unique_ptr dict) +void Dictionary::addDictItem(const std::string& key, std::unique_ptr dict) { mDictData[key] = std::move(dict); } diff --git a/src/core/Dictionary.h b/src/core/Dictionary.h index 8399608..c26468e 100644 --- a/src/core/Dictionary.h +++ b/src/core/Dictionary.h @@ -10,24 +10,23 @@ public: Dictionary() = default; virtual ~Dictionary() = default; - bool HasKey(const std::string& key) const; + void addStringItem(const std::string& key, const std::string& item); - bool HasStringKey(const std::string& key) const; + void addDictItem(const std::string& key, std::unique_ptr dict); - bool HasDictKey(const std::string& key) const; + Dictionary* getDict(const std::string& key) const; - Dictionary* GetDict(const std::string& key) const; + std::vector getDictKeys() const; - std::vector GetDictKeys() const; + std::vector getStringKeys() const; - std::vector GetStringKeys() const; + std::string getItem(const std::string& key) const; - std::string GetItem(const std::string& key) const; + bool hasKey(const std::string& key) const; - void AddStringItem(const std::string& key, const std::string& item); - - void AddDictItem(const std::string& key, std::unique_ptr dict); + bool hasStringKey(const std::string& key) const; + bool hasDictKey(const std::string& key) const; protected: std::map mStringData; diff --git a/src/publishing/pdf/PdfDictionary.cpp b/src/publishing/pdf/PdfDictionary.cpp index d5da237..f34b24c 100644 --- a/src/publishing/pdf/PdfDictionary.cpp +++ b/src/publishing/pdf/PdfDictionary.cpp @@ -1,8 +1,8 @@ #include "PdfDictionary.h" -std::string PdfDictionary::ToString() const +std::string PdfDictionary::toString() const { - const auto keys = GetStringKeys(); + const auto keys = getStringKeys(); if (keys.empty()) { return ""; @@ -15,28 +15,28 @@ std::string PdfDictionary::ToString() const { if (first) { - content += " /" + key + " " + GetItem(key) + ending; + content += " /" + key + " " + getItem(key) + ending; first = false; } else { - content += " /" + key + " " + GetItem(key) + ending; + content += " /" + key + " " + getItem(key) + ending; } } - const auto dictKeys = GetDictKeys(); + const auto dictKeys = getDictKeys(); ending = dictKeys.size() == 1 ? "" : "\n"; - for (const auto& key : GetDictKeys()) + for (const auto& key : getDictKeys()) { - auto pdfDict = dynamic_cast(GetDict(key)); + auto pdfDict = dynamic_cast(getDict(key)); if (first) { - content += " /" + key + " " + pdfDict->ToString() + ending; + content += " /" + key + " " + pdfDict->toString() + ending; first = false; } else { - content += " /" + key + " " + pdfDict->ToString() + ending; + content += " /" + key + " " + pdfDict->toString() + ending; } } diff --git a/src/publishing/pdf/PdfDictionary.h b/src/publishing/pdf/PdfDictionary.h index 9987ac8..bf9a65c 100644 --- a/src/publishing/pdf/PdfDictionary.h +++ b/src/publishing/pdf/PdfDictionary.h @@ -5,6 +5,5 @@ class PdfDictionary : public Dictionary { public: - - std::string ToString() const; + std::string toString() const; }; diff --git a/src/publishing/pdf/PdfDocument.cpp b/src/publishing/pdf/PdfDocument.cpp index eb18bea..4da607b 100644 --- a/src/publishing/pdf/PdfDocument.cpp +++ b/src/publishing/pdf/PdfDocument.cpp @@ -7,6 +7,8 @@ #include "PdfOutline.h" #include "PdfXRefTable.h" +#include "StringUtils.h" + PdfDocument::PdfDocument() { mXRefTable = std::make_unique(); @@ -18,56 +20,56 @@ PdfDocument::~PdfDocument() } -std::string PdfDocument::ToString() +std::string PdfDocument::toString() { - IndexObjects(); - auto content = GetHeaderString(); - content += GetBodyString(); + indexObjects(); + auto content = getHeaderString(); + content += getBodyString(); - content += GetXRefString(); + content += getXRefString(); - content += GetTrailerString(); + content += getTrailerString(); content += "%%EOF\n"; return content; } -std::string PdfDocument::GetHeaderString() +std::string PdfDocument::getHeaderString() { auto content = "%PDF-" + mVersion + "\n"; mXRefOffset = content.size(); - mXRefTable->AddRecord(content.size(), 65535, true); + mXRefTable->addRecord(content.size(), 65535, true); return content; } -std::string PdfDocument::GetBodyString() +std::string PdfDocument::getBodyString() { - const auto content = mCatalog->ToString(mXRefTable.get()); + const auto content = mCatalog->toString(mXRefTable.get()); mXRefOffset += content.size(); return content; } -std::string PdfDocument::GetXRefString() +std::string PdfDocument::getXRefString() { - return mXRefTable->ToString(); + return mXRefTable->toString(); } -std::string PdfDocument::GetTrailerString() +std::string PdfDocument::getTrailerString() { - const auto numObjects = mXRefTable->GetNumEntries(); - mTrailer.AddStringItem("Size", std::to_string(numObjects)); - mTrailer.AddStringItem("Root", mCatalog->GetRefString()); + const auto numObjects = mXRefTable->getNumEntries(); + mTrailer.addStringItem("Size", std::to_string(numObjects)); + mTrailer.addStringItem("Root", mCatalog->getRefString()); std::string content = "trailer\n"; - content += mTrailer.ToString(); + content += mTrailer.toString(); content += "startxref\n"; content += std::to_string(mXRefOffset) + "\n"; return content; } -void PdfDocument::IndexObjects() +void PdfDocument::indexObjects() { - mCatalog->IndexObjects(0); + mCatalog->indexObjects(0); } diff --git a/src/publishing/pdf/PdfDocument.h b/src/publishing/pdf/PdfDocument.h index 5139c0d..f9a4779 100644 --- a/src/publishing/pdf/PdfDocument.h +++ b/src/publishing/pdf/PdfDocument.h @@ -1,7 +1,6 @@ #pragma once #include "PdfDictionary.h" -#include "StringUtils.h" #include #include @@ -15,26 +14,25 @@ using PdfXRefTablePtr = std::unique_ptr; class PdfDocument { - public: PdfDocument(); ~PdfDocument(); - std::string ToString(); - + std::string toString(); private: - std::string GetHeaderString(); + std::string getHeaderString(); - std::string GetTrailerString(); + std::string getTrailerString(); - std::string GetBodyString(); + std::string getBodyString(); - std::string GetXRefString(); + std::string getXRefString(); - void IndexObjects(); + void indexObjects(); private: PdfDictionary mTrailer; + std::string mVersion {"1.7"}; PdfXRefTablePtr mXRefTable; unsigned mXRefOffset{0}; diff --git a/src/publishing/pdf/PdfDocumentCatalog.cpp b/src/publishing/pdf/PdfDocumentCatalog.cpp index 6a9bdd3..e6db0fa 100644 --- a/src/publishing/pdf/PdfDocumentCatalog.cpp +++ b/src/publishing/pdf/PdfDocumentCatalog.cpp @@ -12,33 +12,33 @@ PdfDocumentCatalog::PdfDocumentCatalog() mPages = std::make_unique(); } -unsigned PdfDocumentCatalog::IndexObjects(unsigned count) +unsigned PdfDocumentCatalog::indexObjects(unsigned count) { auto newCount = count + 1; mObjectNumber = newCount; - auto objectCount = mOutlines->IndexObjects(mObjectNumber); - objectCount = mPages->IndexObjects(objectCount); + auto objectCount = mOutlines->indexObjects(mObjectNumber); + objectCount = mPages->indexObjects(objectCount); return objectCount; } -std::string PdfDocumentCatalog::ToString(PdfXRefTable* xRefTable) +std::string PdfDocumentCatalog::toString(PdfXRefTable* xRefTable) { - UpdateDictionary(); - auto content = GetStringPrefix(); - content += mDictionary.ToString(); - content += GetStringSuffix(); - xRefTable->AddRecord(content.size(), mGenerationNumber, mIsFree); + updateDictionary(); + auto content = getStringPrefix(); + content += mDictionary.toString(); + content += getStringSuffix(); + xRefTable->addRecord(content.size(), mGenerationNumber, mIsFree); - content += mOutlines->ToString(xRefTable); + content += mOutlines->toString(xRefTable); - content += mPages->ToString(xRefTable); + content += mPages->toString(xRefTable); return content; } -void PdfDocumentCatalog::UpdateDictionary() +void PdfDocumentCatalog::updateDictionary() { - mDictionary.AddStringItem("Type", "/Catalog"); - mDictionary.AddStringItem("Outlines", mOutlines->GetRefString()); - mDictionary.AddStringItem("Pages", mPages->GetRefString()); + mDictionary.addStringItem("Type", "/Catalog"); + mDictionary.addStringItem("Outlines", mOutlines->getRefString()); + mDictionary.addStringItem("Pages", mPages->getRefString()); } diff --git a/src/publishing/pdf/PdfDocumentCatalog.h b/src/publishing/pdf/PdfDocumentCatalog.h index d2665d6..a9208c7 100644 --- a/src/publishing/pdf/PdfDocumentCatalog.h +++ b/src/publishing/pdf/PdfDocumentCatalog.h @@ -8,15 +8,14 @@ class PdfOutlineCollection; class PdfDocumentCatalog : public PdfObject { public: - PdfDocumentCatalog(); - unsigned IndexObjects(unsigned count) override; + unsigned indexObjects(unsigned count) override; - std::string ToString(PdfXRefTable* xRefTable) override; + std::string toString(PdfXRefTable* xRefTable) override; private: - void UpdateDictionary() override; + void updateDictionary() override; std::unique_ptr mOutlines; std::unique_ptr mPages; diff --git a/src/publishing/pdf/PdfObject.cpp b/src/publishing/pdf/PdfObject.cpp index 26ba0ca..d217834 100644 --- a/src/publishing/pdf/PdfObject.cpp +++ b/src/publishing/pdf/PdfObject.cpp @@ -3,56 +3,56 @@ #include "PdfDictionary.h" #include "PdfXRefTable.h" -std::string PdfObject::ToString(PdfXRefTable* xRefTable) +std::string PdfObject::toString(PdfXRefTable* xRefTable) { - UpdateDictionary(); + updateDictionary(); - auto content = GetStringPrefix(); - content += mDictionary.ToString(); - content += GetStringSuffix(); + auto content = getStringPrefix(); + content += mDictionary.toString(); + content += getStringSuffix(); - xRefTable->AddRecord(content.size(), mGenerationNumber, mIsFree); + xRefTable->addRecord(content.size(), mGenerationNumber, mIsFree); return content; } -void PdfObject::UpdateDictionary() +void PdfObject::updateDictionary() { } -std::string PdfObject::GetStringSuffix() const +std::string PdfObject::getStringSuffix() const { return "endobj\n\n"; } -unsigned PdfObject::IndexObjects(unsigned count) +unsigned PdfObject::indexObjects(unsigned count) { const auto newCount = count + 1; mObjectNumber = newCount; return newCount; } -void PdfObject::SetObjectNumber(unsigned num) +void PdfObject::setObjectNumber(unsigned num) { mObjectNumber = num; } -void PdfObject::SetGenerationNumber(unsigned num) +void PdfObject::setGenerationNumber(unsigned num) { mGenerationNumber = num; } -std::string PdfObject::GetStringPrefix() const +std::string PdfObject::getStringPrefix() const { return std::to_string(mObjectNumber) + " " + std::to_string(mGenerationNumber) + " obj\n"; } -std::string PdfObject::GetRefString() const +std::string PdfObject::getRefString() const { return std::to_string(mObjectNumber) + " " + std::to_string(mGenerationNumber) + " R"; } -bool PdfObject::IsFree() const +bool PdfObject::isFree() const { return mIsFree; } diff --git a/src/publishing/pdf/PdfObject.h b/src/publishing/pdf/PdfObject.h index 67be2c6..a92562d 100644 --- a/src/publishing/pdf/PdfObject.h +++ b/src/publishing/pdf/PdfObject.h @@ -12,25 +12,24 @@ class PdfObject public: virtual ~PdfObject() = default; - std::string GetStringPrefix() const; + std::string getStringPrefix() const; - std::string GetStringSuffix() const; + std::string getStringSuffix() const; - std::string GetRefString() const; + std::string getRefString() const; - virtual unsigned IndexObjects(unsigned count); + virtual unsigned indexObjects(unsigned count); - bool IsFree() const; + bool isFree() const; - virtual std::string ToString(PdfXRefTable* xRefTable); + virtual std::string toString(PdfXRefTable* xRefTable); - void SetObjectNumber(unsigned num); + void setObjectNumber(unsigned num); - void SetGenerationNumber(unsigned num); + void setGenerationNumber(unsigned num); protected: - - virtual void UpdateDictionary(); + virtual void updateDictionary(); unsigned mObjectNumber{0}; unsigned mGenerationNumber{0}; diff --git a/src/publishing/pdf/PdfOutline.cpp b/src/publishing/pdf/PdfOutline.cpp index 5b83595..cbf09a0 100644 --- a/src/publishing/pdf/PdfOutline.cpp +++ b/src/publishing/pdf/PdfOutline.cpp @@ -2,39 +2,39 @@ #include "PdfXRefTable.h" -std::string PdfOutline::ToString(PdfXRefTable* xRefTable) +std::string PdfOutline::toString(PdfXRefTable* xRefTable) { - mDictionary.AddStringItem("Type", "/Outline"); - auto content = GetStringPrefix(); - content += mDictionary.ToString(); - content += GetStringSuffix(); - xRefTable->AddRecord(content.size(), mGenerationNumber, mIsFree); + mDictionary.addStringItem("Type", "/Outline"); + auto content = getStringPrefix(); + content += mDictionary.toString(); + content += getStringSuffix(); + xRefTable->addRecord(content.size(), mGenerationNumber, mIsFree); return content; } -unsigned PdfOutlineCollection::IndexObjects(unsigned count) +unsigned PdfOutlineCollection::indexObjects(unsigned count) { auto newCount = count + 1; mObjectNumber = newCount; for (const auto& outline : mOutlines) { - newCount = outline->IndexObjects(newCount); + newCount = outline->indexObjects(newCount); } return newCount; } -std::string PdfOutlineCollection::ToString(PdfXRefTable* xRefTable) +std::string PdfOutlineCollection::toString(PdfXRefTable* xRefTable) { - UpdateDictionary(); - std::string content = GetStringPrefix(); - content += mDictionary.ToString(); - content += GetStringSuffix(); - xRefTable->AddRecord(content.size(), mGenerationNumber, mIsFree); + updateDictionary(); + std::string content = getStringPrefix(); + content += mDictionary.toString(); + content += getStringSuffix(); + xRefTable->addRecord(content.size(), mGenerationNumber, mIsFree); return content; } -void PdfOutlineCollection::UpdateDictionary() +void PdfOutlineCollection::updateDictionary() { - mDictionary.AddStringItem("Type", "/Outlines"); - mDictionary.AddStringItem("Count", std::to_string(mOutlines.size())); + mDictionary.addStringItem("Type", "/Outlines"); + mDictionary.addStringItem("Count", std::to_string(mOutlines.size())); } diff --git a/src/publishing/pdf/PdfOutline.h b/src/publishing/pdf/PdfOutline.h index 1d5e257..b691492 100644 --- a/src/publishing/pdf/PdfOutline.h +++ b/src/publishing/pdf/PdfOutline.h @@ -7,7 +7,7 @@ class PdfOutline : public PdfObject { public: - std::string ToString(PdfXRefTable* xRefTable) override; + std::string toString(PdfXRefTable* xRefTable) override; }; using PdfOutlinePtr = std::unique_ptr; @@ -15,11 +15,11 @@ class PdfOutlineCollection : public PdfObject { public: - unsigned IndexObjects(unsigned count) override; + unsigned indexObjects(unsigned count) override; - std::string ToString(PdfXRefTable* xRefTable) override; + std::string toString(PdfXRefTable* xRefTable) override; - void UpdateDictionary(); + void updateDictionary(); private: std::vector mOutlines; diff --git a/src/publishing/pdf/PdfPage.cpp b/src/publishing/pdf/PdfPage.cpp index e69de29..72a47f6 100644 --- a/src/publishing/pdf/PdfPage.cpp +++ b/src/publishing/pdf/PdfPage.cpp @@ -0,0 +1,95 @@ +#include "PdfPage.h" + +#include "PdfObject.h" +#include "PdfStream.h" +#include "PdfXRefTable.h" +#include "PdfPageTree.h" + +std::string PdfProcSet::toString(PdfXRefTable* xRefTable) +{ + updateDictionary(); + auto content = getStringPrefix(); + content += mDictionary.toString(); + content += "[/PDF]\n"; + content += getStringSuffix(); + xRefTable->addRecord(content.size(), mGenerationNumber, mIsFree); + return content; +} + +std::string PdfFont::toString(PdfXRefTable* xRefTable) +{ + updateDictionary(); + auto content = getStringPrefix(); + content += mDictionary.toString(); + content += getStringSuffix(); + xRefTable->addRecord(content.size(), mGenerationNumber, mIsFree); + return content; +} + +void PdfFont::updateDictionary() +{ + mDictionary.addStringItem("Type", "/Font"); + mDictionary.addStringItem("Subtype", "/Type1"); + mDictionary.addStringItem("Name", "/F1"); + mDictionary.addStringItem("BaseFont", "/Helvetica"); + mDictionary.addStringItem("Encoding", "/MacRomanEncoding"); +} + +PdfPage::PdfPage(PdfPageTree* parent) + : mParent(parent) +{ + mContent = std::make_unique(); + + std::string pageContent = "BT\n"; + pageContent += "/F1 24 Tf\n"; + pageContent += "100 100 Td\n"; + pageContent += "(Hello World) Tj\n"; + pageContent += "ET"; + + mContent->setContent(pageContent); + mProcSet = std::make_unique(); + + mDefaultFont = std::make_unique(); +} + +unsigned PdfPage::indexObjects(unsigned count) +{ + auto newCount = count + 1; + mObjectNumber = newCount; + newCount = mContent->indexObjects(newCount); + newCount = mProcSet->indexObjects(newCount); + newCount = mDefaultFont->indexObjects(newCount); + return newCount; +} + +std::string PdfPage::toString(PdfXRefTable* xRefTable) +{ + updateDictionary(); + auto content = getStringPrefix(); + content += mDictionary.toString(); + content += getStringSuffix(); + xRefTable->addRecord(content.size(), mGenerationNumber, mIsFree); + + content += mContent->toString(xRefTable); + content += mProcSet->toString(xRefTable); + content += mDefaultFont->toString(xRefTable); + return content; +} + +void PdfPage::updateDictionary() +{ + std::string mediaBox = "[0 0 " + std::to_string(mWidth) + " " + std::to_string(mHeight) + "]"; + mDictionary.addStringItem("Type", "/Page"); + mDictionary.addStringItem("MediaBox", mediaBox); + mDictionary.addStringItem("Parent", mParent->getRefString()); + mDictionary.addStringItem("Contents", mContent->getRefString()); + + auto resourcesDict = std::make_unique(); + resourcesDict->addStringItem("ProcSet", mProcSet->getRefString()); + + auto fontDict = std::make_unique(); + fontDict->addStringItem("F1", mDefaultFont->getRefString()); + resourcesDict->addDictItem("Font", std::move(fontDict)); + + mDictionary.addDictItem("Resources", std::move(resourcesDict)); +} diff --git a/src/publishing/pdf/PdfPage.h b/src/publishing/pdf/PdfPage.h index e022c76..888caff 100644 --- a/src/publishing/pdf/PdfPage.h +++ b/src/publishing/pdf/PdfPage.h @@ -9,106 +9,27 @@ class PdfPageTree; class PdfProcSet : public PdfObject { public: - std::string ToString(PdfXRefTable* xRefTable) override - { - UpdateDictionary(); - auto content = GetStringPrefix(); - content += mDictionary.ToString(); - content += "[/PDF]\n"; - content += GetStringSuffix(); - xRefTable->AddRecord(content.size(), mGenerationNumber, mIsFree); - - return content; - } + std::string toString(PdfXRefTable* xRefTable) override; }; class PdfFont : public PdfObject { public: - std::string ToString(PdfXRefTable* xRefTable) override - { - UpdateDictionary(); - auto content = GetStringPrefix(); - content += mDictionary.ToString(); - content += GetStringSuffix(); - xRefTable->AddRecord(content.size(), mGenerationNumber, mIsFree); - return content; - } + std::string toString(PdfXRefTable* xRefTable) override; - void UpdateDictionary() - { - mDictionary.AddStringItem("Type", "/Font"); - mDictionary.AddStringItem("Subtype", "/Type1"); - mDictionary.AddStringItem("Name", "/F1"); - mDictionary.AddStringItem("BaseFont", "/Helvetica"); - mDictionary.AddStringItem("Encoding", "/MacRomanEncoding"); - } + void updateDictionary(); }; class PdfPage : public PdfObject { public: - PdfPage(PdfPageTree* parent) - : mParent(parent) - { - mContent = std::make_unique(); + PdfPage(PdfPageTree* parent); - std::string pageContent = "BT\n"; - pageContent += "/F1 24 Tf\n"; - pageContent += "100 100 Td\n"; - pageContent += "(Hello World) Tj\n"; - pageContent += "ET"; + unsigned indexObjects(unsigned count); - mContent->SetContent(pageContent); - mProcSet = std::make_unique(); + std::string toString(PdfXRefTable* xRefTable) override; - mDefaultFont = std::make_unique(); - } - - unsigned IndexObjects(unsigned count) - { - auto newCount = count + 1; - mObjectNumber = newCount; - newCount = mContent->IndexObjects(newCount); - newCount = mProcSet->IndexObjects(newCount); - newCount = mDefaultFont->IndexObjects(newCount); - return newCount; - } - - std::string ToString(PdfXRefTable* xRefTable) override - { - UpdateDictionary(); - auto content = GetStringPrefix(); - content += mDictionary.ToString(); - content += GetStringSuffix(); - xRefTable->AddRecord(content.size(), mGenerationNumber, mIsFree); - - content += mContent->ToString(xRefTable); - - content += mProcSet->ToString(xRefTable); - - content += mDefaultFont->ToString(xRefTable); - - return content; - } - - void UpdateDictionary() - { - std::string mediaBox = "[0 0 " + std::to_string(mWidth) + " " + std::to_string(mHeight) + "]"; - mDictionary.AddStringItem("Type", "/Page"); - mDictionary.AddStringItem("MediaBox", mediaBox); - mDictionary.AddStringItem("Parent", mParent->GetRefString()); - mDictionary.AddStringItem("Contents", mContent->GetRefString()); - - auto resourcesDict = std::make_unique(); - resourcesDict->AddStringItem("ProcSet", mProcSet->GetRefString()); - - auto fontDict = std::make_unique(); - fontDict->AddStringItem("F1", mDefaultFont->GetRefString()); - resourcesDict->AddDictItem("Font", std::move(fontDict)); - - mDictionary.AddDictItem("Resources", std::move(resourcesDict)); - } + void updateDictionary(); private: unsigned mWidth{612}; diff --git a/src/publishing/pdf/PdfPageTree.cpp b/src/publishing/pdf/PdfPageTree.cpp index c62b946..b534caa 100644 --- a/src/publishing/pdf/PdfPageTree.cpp +++ b/src/publishing/pdf/PdfPageTree.cpp @@ -8,30 +8,30 @@ PdfPageTree::PdfPageTree() mRootPage = std::make_unique(this); } -unsigned PdfPageTree::IndexObjects(unsigned count) +unsigned PdfPageTree::indexObjects(unsigned count) { auto newCount = count + 1; mObjectNumber = newCount; - newCount = mRootPage->IndexObjects(newCount); + newCount = mRootPage->indexObjects(newCount); return newCount; } -std::string PdfPageTree::ToString(PdfXRefTable* xRefTable) +std::string PdfPageTree::toString(PdfXRefTable* xRefTable) { - UpdateDictionary(); - std::string content = GetStringPrefix(); - content += mDictionary.ToString(); - content += GetStringSuffix(); - xRefTable->AddRecord(content.size(), mGenerationNumber, mIsFree); + updateDictionary(); + std::string content = getStringPrefix(); + content += mDictionary.toString(); + content += getStringSuffix(); + xRefTable->addRecord(content.size(), mGenerationNumber, mIsFree); - content += mRootPage->ToString(xRefTable); + content += mRootPage->toString(xRefTable); return content; } -void PdfPageTree::UpdateDictionary() +void PdfPageTree::updateDictionary() { - mDictionary.AddStringItem("Type", "/Pages"); - std::string kids = "[" + mRootPage->GetRefString() + "]"; - mDictionary.AddStringItem("Kids", kids); - mDictionary.AddStringItem("Count", "1"); + mDictionary.addStringItem("Type", "/Pages"); + std::string kids = "[" + mRootPage->getRefString() + "]"; + mDictionary.addStringItem("Kids", kids); + mDictionary.addStringItem("Count", "1"); } diff --git a/src/publishing/pdf/PdfPageTree.h b/src/publishing/pdf/PdfPageTree.h index 897db9b..41f659f 100644 --- a/src/publishing/pdf/PdfPageTree.h +++ b/src/publishing/pdf/PdfPageTree.h @@ -8,14 +8,13 @@ using PdfPagePtr = std::unique_ptr; class PdfPageTree : public PdfObject { public: - PdfPageTree(); - unsigned IndexObjects(unsigned count) override; + unsigned indexObjects(unsigned count) override; - std::string ToString(PdfXRefTable* xRefTable) override; + std::string toString(PdfXRefTable* xRefTable) override; - void UpdateDictionary() override; + void updateDictionary() override; private: diff --git a/src/publishing/pdf/PdfStream.cpp b/src/publishing/pdf/PdfStream.cpp index 0fc34f7..6008fa0 100644 --- a/src/publishing/pdf/PdfStream.cpp +++ b/src/publishing/pdf/PdfStream.cpp @@ -5,32 +5,32 @@ PdfStream::PdfStream() { - mDictionary.AddStringItem("Length", "0"); + mDictionary.addStringItem("Length", "0"); } -void PdfStream::SetContent(const std::string& content) +void PdfStream::setContent(const std::string& content) { mContent = content; - Update(); + update(); } -void PdfStream::Update() +void PdfStream::update() { auto length = mContent.size(); - mDictionary.AddStringItem("Length", std::to_string(length)); + mDictionary.addStringItem("Length", std::to_string(length)); } -std::string PdfStream::ToString(PdfXRefTable* xRefTable) +std::string PdfStream::toString(PdfXRefTable* xRefTable) { - std::string content = GetStringPrefix(); + std::string content = getStringPrefix(); - content += mDictionary.ToString(); + content += mDictionary.toString(); content += "stream\n"; content += mContent; content += "\nendstream\n"; content += "endobj\n\n"; - xRefTable->AddRecord(content.size(), mGenerationNumber, mIsFree); + xRefTable->addRecord(content.size(), mGenerationNumber, mIsFree); return content; } diff --git a/src/publishing/pdf/PdfStream.h b/src/publishing/pdf/PdfStream.h index 595dc5a..36f8df2 100644 --- a/src/publishing/pdf/PdfStream.h +++ b/src/publishing/pdf/PdfStream.h @@ -5,14 +5,13 @@ class PdfStream : public PdfObject { public: - PdfStream(); - void Update(); + void update(); - void SetContent(const std::string& content); + void setContent(const std::string& content); - std::string ToString(PdfXRefTable* xRefTable) override; + std::string toString(PdfXRefTable* xRefTable) override; private: std::string mContent; diff --git a/src/publishing/pdf/PdfWriter.cpp b/src/publishing/pdf/PdfWriter.cpp index 2735658..672ef58 100644 --- a/src/publishing/pdf/PdfWriter.cpp +++ b/src/publishing/pdf/PdfWriter.cpp @@ -2,11 +2,11 @@ #include "PdfDocument.h" -std::string PdfWriter::ToString(const std::unique_ptr& document) const +std::string PdfWriter::toString(PdfDocument* document) const { std::string content; - content += document->ToString(); + content += document->toString(); return content; } diff --git a/src/publishing/pdf/PdfWriter.h b/src/publishing/pdf/PdfWriter.h index 0b03765..a946af1 100644 --- a/src/publishing/pdf/PdfWriter.h +++ b/src/publishing/pdf/PdfWriter.h @@ -9,5 +9,5 @@ class PdfWriter { public: - std::string ToString(const std::unique_ptr& document) const; + std::string toString(PdfDocument* document) const; }; diff --git a/src/publishing/pdf/PdfXRefTable.cpp b/src/publishing/pdf/PdfXRefTable.cpp index 827ce62..1dbec09 100644 --- a/src/publishing/pdf/PdfXRefTable.cpp +++ b/src/publishing/pdf/PdfXRefTable.cpp @@ -7,7 +7,7 @@ PdfXRefTable::PdfXRefTable() mSections.push_back(TableSubSection()); } -std::string PdfXRefTable::ToString() +std::string PdfXRefTable::toString() { std::string content; for (const auto& section : mSections) @@ -27,7 +27,7 @@ std::string PdfXRefTable::ToString() return content; } -unsigned PdfXRefTable::GetNextOffset() +unsigned PdfXRefTable::getNextOffset() { auto lastNumRecords = mSections[mSections.size() - 1].mRecords.size(); if (lastNumRecords > 0) @@ -45,18 +45,18 @@ unsigned PdfXRefTable::GetNextOffset() } } -void PdfXRefTable::AddRecord(unsigned numBytes, unsigned generation, unsigned isFree) +void PdfXRefTable::addRecord(unsigned numBytes, unsigned generation, unsigned isFree) { XRefRecord record; - record.mOffsetBytes = GetNextOffset(); + record.mOffsetBytes = getNextOffset(); record.mGenerationNumber = generation; record.mIsFree = isFree; mSections[mSections.size()-1].mRecords.push_back(record); mLastAddedBytes = numBytes; } -unsigned PdfXRefTable::GetNumEntries() +unsigned PdfXRefTable::getNumEntries() { unsigned count = 0; for (const auto& section : mSections) diff --git a/src/publishing/pdf/PdfXRefTable.h b/src/publishing/pdf/PdfXRefTable.h index 9bd8a51..f27ec1f 100644 --- a/src/publishing/pdf/PdfXRefTable.h +++ b/src/publishing/pdf/PdfXRefTable.h @@ -20,16 +20,15 @@ struct TableSubSection class PdfXRefTable { public: - PdfXRefTable(); - std::string ToString(); + std::string toString(); - unsigned GetNextOffset(); + unsigned getNextOffset(); - void AddRecord(unsigned numBytes, unsigned generation, unsigned isFree); + void addRecord(unsigned numBytes, unsigned generation, unsigned isFree); - unsigned GetNumEntries(); + unsigned getNumEntries(); private: unsigned mLastAddedBytes{0}; diff --git a/test/publishing/TestDocumentConverter.cpp b/test/publishing/TestDocumentConverter.cpp index 53164d4..546e6ca 100644 --- a/test/publishing/TestDocumentConverter.cpp +++ b/test/publishing/TestDocumentConverter.cpp @@ -11,7 +11,7 @@ TEST_CASE(TestDocumentConverterMarkdownToPdf, "publishing") auto document = std::make_unique(); PdfWriter writer; - const auto output = writer.ToString(document); + const auto output = writer.toString(document.get()); File pdf_file(TestUtils::getTestOutputDir() / "TestDocumentConverterMarkdownToPdf.pdf"); pdf_file.open(File::AccessMode::Write); diff --git a/test/publishing/TestPdfWriter.cpp b/test/publishing/TestPdfWriter.cpp index e7793cc..5efdce3 100644 --- a/test/publishing/TestPdfWriter.cpp +++ b/test/publishing/TestPdfWriter.cpp @@ -11,9 +11,8 @@ TEST_CASE(TestPdfWriter, "publishing") auto document = std::make_unique(); PdfWriter writer; - const auto output = writer.ToString(document); + const auto output = writer.toString(document.get()); File pdf_file(TestUtils::getTestOutputDir() / "TestPdfWriter.pdf"); - pdf_file.open(File::AccessMode::Write); pdf_file.writeText(output); }