Generating code

This commit is contained in:
Jakub Melka
2020-03-21 16:36:27 +01:00
parent 96d2e33692
commit bc6ca3fc46
15 changed files with 966 additions and 9 deletions

View File

@ -16,6 +16,7 @@
// along with PDFForQt. If not, see <https://www.gnu.org/licenses/>.
#include "pdfdocumentbuilder.h"
#include "pdfencoding.h"
namespace pdf
{
@ -62,6 +63,82 @@ void PDFObjectFactory::endDictionaryItem()
std::get<PDFDictionary>(dictionaryItem.object).addEntry(qMove(topItem.itemName), qMove(std::get<PDFObject>(topItem.object)));
}
PDFObjectFactory& PDFObjectFactory::operator<<(QString textString)
{
if (!PDFEncoding::canConvertToEncoding(textString, PDFEncoding::Encoding::PDFDoc))
{
// Use unicode encoding
QByteArray ba;
{
QTextStream textStream(&ba, QIODevice::WriteOnly);
textStream.setCodec("UTF-16BE");
textStream.setGenerateByteOrderMark(true);
textStream << textString;
}
addObject(PDFObject::createString(std::make_shared<PDFString>(qMove(ba))));
}
else
{
// Use PDF document encoding
addObject(PDFObject::createString(std::make_shared<PDFString>(PDFEncoding::convertToEncoding(textString, PDFEncoding::Encoding::PDFDoc))));
}
return *this;
}
PDFObjectFactory& PDFObjectFactory::operator<<(WrapAnnotationColor color)
{
if (color.color.isValid())
{
// Jakub Melka: we will decide, if we have gray/rgb/cmyk color
QColor value = color.color;
if (value.spec() == QColor::Cmyk)
{
*this << std::initializer_list<PDFReal>{ value.cyanF(), value.magentaF(), value.yellowF(), value.blackF() };
}
else if (qIsGray(value.rgb()))
{
*this << std::initializer_list<PDFReal>{ value.redF() };
}
else
{
*this << std::initializer_list<PDFReal>{ value.redF(), value.greenF(), value.blueF() };
}
}
else
{
addObject(PDFObject::createNull());
}
return *this;
}
PDFObjectFactory& PDFObjectFactory::operator<<(WrapCurrentDateTime)
{
addObject(PDFObject::createString(std::make_shared<PDFString>(PDFEncoding::converDateTimeToString(QDateTime::currentDateTime()))));
return *this;
}
PDFObjectFactory& PDFObjectFactory::operator<<(const QRectF& value)
{
*this << std::initializer_list<PDFReal>{ value.left(), value.top(), value.right(), value.bottom() };
return *this;
}
PDFObjectFactory& PDFObjectFactory::operator<<(int value)
{
*this << PDFInteger(value);
return *this;
}
PDFObjectFactory& PDFObjectFactory::operator<<(WrapName wrapName)
{
addObject(PDFObject::createName(std::make_shared<PDFString>(qMove(wrapName.name))));
return *this;
}
PDFObject PDFObjectFactory::takeObject()
{
Q_ASSERT(m_items.size() == 1);
@ -154,8 +231,111 @@ PDFDocument PDFDocumentBuilder::build() const
return PDFDocument(PDFObjectStorage(m_storage), m_version);
}
PDFObjectReference PDFDocumentBuilder::addObject(PDFObject object)
{
return m_storage.addObject(PDFObjectManipulator::removeNullObjects(object));
}
void PDFDocumentBuilder::mergeTo(PDFObjectReference reference, PDFObject object)
{
m_storage.setObject(reference, PDFObjectManipulator::merge(m_storage.getObject(reference), qMove(object), PDFObjectManipulator::RemoveNullObjects));
}
QRectF PDFDocumentBuilder::getPopupWindowRect(const QRectF& rectangle) const
{
return rectangle.translated(rectangle.width() * 1.25, 0);
}
/* START GENERATED CODE */
PDFObjectReference PDFDocumentBuilder::createAnnotationSquare(PDFObjectReference page,
QRectF rectangle,
PDFReal borderWidth,
QColor fillColor,
QColor strokeColor,
QString title,
QString subject,
QString contents)
{
PDFObjectFactory objectBuilder;
objectBuilder.beginDictionary();
objectBuilder.beginDictionaryItem("Subtype");
objectBuilder << WrapName("Square");
objectBuilder.endDictionaryItem();
objectBuilder.beginDictionaryItem("Rect");
objectBuilder << rectangle;
objectBuilder.endDictionaryItem();
objectBuilder.beginDictionaryItem("F");
objectBuilder << 4;
objectBuilder.endDictionaryItem();
objectBuilder.beginDictionaryItem("P");
objectBuilder << page;
objectBuilder.endDictionaryItem();
objectBuilder.beginDictionaryItem("M");
objectBuilder << WrapCurrentDateTime();
objectBuilder.endDictionaryItem();
objectBuilder.beginDictionaryItem("CreationDate");
objectBuilder << WrapCurrentDateTime();
objectBuilder.endDictionaryItem();
objectBuilder.beginDictionaryItem("Border");
objectBuilder << std::initializer_list<PDFReal>{ 0.0, 0.0, borderWidth };
objectBuilder.endDictionaryItem();
objectBuilder.beginDictionaryItem("C");
objectBuilder << WrapAnnotationColor(strokeColor);
objectBuilder.endDictionaryItem();
objectBuilder.beginDictionaryItem("IC");
objectBuilder << WrapAnnotationColor(fillColor);
objectBuilder.endDictionaryItem();
objectBuilder.beginDictionaryItem("T");
objectBuilder << title;
objectBuilder.endDictionaryItem();
objectBuilder.beginDictionaryItem("Contents");
objectBuilder << contents;
objectBuilder.endDictionaryItem();
objectBuilder.beginDictionaryItem("Subj");
objectBuilder << subject;
objectBuilder.endDictionaryItem();
objectBuilder.endDictionary();
PDFObjectReference annotationObject = addObject(objectBuilder.takeObject());
PDFObjectReference popupAnnotation = createAnnotationPopup(page, annotationObject, getPopupWindowRect(rectangle), false);
objectBuilder.beginDictionaryItem("Popup");
objectBuilder << popupAnnotation;
objectBuilder.endDictionaryItem();
PDFObject updateAnnotationPopup = objectBuilder.takeObject();
mergeTo(annotationObject, updateAnnotationPopup);
return PDFObjectReference();
}
PDFObjectReference PDFDocumentBuilder::createAnnotationPopup(PDFObjectReference page,
PDFObjectReference parentAnnotation,
QRectF rectangle,
bool opened)
{
PDFObjectFactory objectBuilder;
objectBuilder.beginDictionary();
objectBuilder.beginDictionaryItem("Subtype");
objectBuilder << WrapName("Popup");
objectBuilder.endDictionaryItem();
objectBuilder.beginDictionaryItem("Rect");
objectBuilder << rectangle;
objectBuilder.endDictionaryItem();
objectBuilder.beginDictionaryItem("P");
objectBuilder << page;
objectBuilder.endDictionaryItem();
objectBuilder.beginDictionaryItem("Parent");
objectBuilder << parentAnnotation;
objectBuilder.endDictionaryItem();
objectBuilder.beginDictionaryItem("Open");
objectBuilder << opened;
objectBuilder.endDictionaryItem();
objectBuilder.endDictionary();
PDFObjectReference popupAnnotation = addObject(objectBuilder.takeObject());
return popupAnnotation;
}
/* END GENERATED CODE */
} // namespace pdf