Text annotations graphics

This commit is contained in:
Jakub Melka 2020-03-31 19:38:43 +02:00
parent 629559bb53
commit 8d242e752f
5 changed files with 86 additions and 4 deletions

View File

@ -1,4 +1,4 @@
QT += gui
QT += gui widgets
CONFIG += c++11 console
CONFIG -= app_bundle

View File

@ -15,12 +15,12 @@
// You should have received a copy of the GNU Lesser General Public License
// along with PDFForQt. If not, see <https://www.gnu.org/licenses/>.
#include <QCoreApplication>
#include <QApplication>
#include "pdfexamplesgenerator.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QApplication a(argc, argv);
PDFExamplesGenerator::generateAnnotationsExample();
}

View File

@ -160,36 +160,42 @@ void PDFExamplesGenerator::generateAnnotationsExample()
pdf::PDFObjectReference annotation = builder.createAnnotationSquare(page5, QRectF(50, 50, 50, 50), 3.0, Qt::green, Qt::red, "Title1", "Subject1", "Contents - green filling, red boundary");
builder.setAnnotationBorderStyle(annotation, pdf::PDFAnnotationBorder::Style::Solid, 2.718);
builder.setAnnotationColor(annotation, Qt::black);
builder.updateAnnotationAppearanceStreams(annotation);
}
{
pdf::PDFObjectReference annotation = builder.createAnnotationSquare(page5, QRectF(50, 150, 50, 50), 3.0, Qt::green, Qt::red, "Title1", "Subject1", "Contents - green filling, red boundary");
builder.setAnnotationBorderStyle(annotation, pdf::PDFAnnotationBorder::Style::Underline, 2.718);
builder.setAnnotationColor(annotation, Qt::black);
builder.updateAnnotationAppearanceStreams(annotation);
}
{
pdf::PDFObjectReference annotation = builder.createAnnotationSquare(page5, QRectF(50, 250, 50, 50), 3.0, Qt::green, Qt::red, "Title1", "Subject1", "Contents - green filling, red boundary");
builder.setAnnotationBorderStyle(annotation, pdf::PDFAnnotationBorder::Style::Inset, 2.718);
builder.setAnnotationColor(annotation, Qt::black);
builder.updateAnnotationAppearanceStreams(annotation);
}
{
pdf::PDFObjectReference annotation = builder.createAnnotationSquare(page5, QRectF(150, 50, 50, 50), 3.0, Qt::green, Qt::red, "Title1", "Subject1", "Contents - green filling, red boundary");
builder.setAnnotationBorderStyle(annotation, pdf::PDFAnnotationBorder::Style::Beveled, 2.718);
builder.setAnnotationColor(annotation, Qt::black);
builder.updateAnnotationAppearanceStreams(annotation);
}
{
pdf::PDFObjectReference annotation = builder.createAnnotationSquare(page5, QRectF(150, 150, 50, 50), 3.0, Qt::green, Qt::red, "Title1", "Subject1", "Contents - green filling, red boundary");
builder.setAnnotationBorderStyle(annotation, pdf::PDFAnnotationBorder::Style::Dashed, 2.718);
builder.setAnnotationColor(annotation, Qt::black);
builder.updateAnnotationAppearanceStreams(annotation);
}
{
pdf::PDFObjectReference annotation = builder.createAnnotationSquare(page5, QRectF(150, 250, 50, 50), 3.0, Qt::green, Qt::red, "Title1", "Subject1", "Contents - green filling, red boundary");
builder.setAnnotationBorder(annotation, 5.0, 3.0, 2.0);
builder.setAnnotationColor(annotation, Qt::black);
builder.updateAnnotationAppearanceStreams(annotation);
}
}

View File

@ -24,6 +24,8 @@
#include "pdfwidgetutils.h"
#include "pdfpagecontentprocessor.h"
#include <QApplication>
namespace pdf
{
@ -1079,7 +1081,11 @@ void PDFSimpleGeometryAnnotation::draw(AnnotationDrawParameters& parameters) con
case AnnotationType::Circle:
{
painter.drawEllipse(getRectangle());
const PDFAnnotationBorder& border = getBorder();
const PDFReal width = border.getWidth();
QRectF rectangle = getRectangle();
rectangle.adjust(width, width, -width, -width);
painter.drawEllipse(rectangle);
break;
}
@ -1121,4 +1127,73 @@ QColor PDFMarkupAnnotation::getFillColor() const
return color;
}
void PDFTextAnnotation::draw(AnnotationDrawParameters& parameters) const
{
const PDFReal opacity = getOpacity();
QColor strokeColor = QColor::fromRgbF(0.0, 0.0, 0.0, opacity);
QColor fillColor = QColor::fromRgbF(1.0, 1.0, 0.0, opacity);
constexpr const PDFReal rectSize = 32.0;
constexpr const PDFReal penWidth = 2.0;
QPainter& painter = *parameters.painter;
QRectF rectangle = getRectangle();
rectangle.setSize(QSizeF(rectSize, rectSize));
QPen pen(strokeColor);
pen.setWidthF(penWidth);
painter.setPen(pen);
painter.setBrush(QBrush(fillColor, Qt::SolidPattern));
QRectF ellipseRectangle = rectangle;
ellipseRectangle.adjust(penWidth, penWidth, -penWidth, -penWidth);
// Draw the ellipse
painter.drawEllipse(ellipseRectangle);
QFont font = QApplication::font();
font.setPixelSize(16.0);
QString text = "?";
if (m_iconName == "Comment")
{
text = QString::fromUtf16(u"\U0001F4AC");
}
else if (m_iconName == "Help")
{
text = "?";
}
else if (m_iconName == "Insert")
{
text = QString::fromUtf16(u"\u2380");
}
else if (m_iconName == "Key")
{
text = QString::fromUtf16(u"\U0001F511");
}
else if (m_iconName == "NewParagraph")
{
text = QString::fromUtf16(u"\u2606");
}
else if (m_iconName == "Note")
{
text = QString::fromUtf16(u"\u266A");
}
else if (m_iconName == "Paragraph")
{
text = QString::fromUtf16(u"\u00B6");
}
QPainterPath textPath;
textPath.addText(0.0, 0.0, font, text);
textPath = QMatrix(1.0, 0.0, 0.0, -1.0, 0.0, 0.0).map(textPath);
QRectF textBoundingRect = textPath.boundingRect();
QPointF offset = rectangle.center() - textBoundingRect.center();
textPath.translate(offset);
painter.fillPath(textPath, QBrush(strokeColor, Qt::SolidPattern));
parameters.boundingRectangle = rectangle;
}
} // namespace pdf

View File

@ -580,6 +580,7 @@ public:
inline explicit PDFTextAnnotation() = default;
virtual AnnotationType getType() const override { return AnnotationType::Text; }
virtual void draw(AnnotationDrawParameters& parameters) const override;
bool isOpen() const { return m_open; }
const QByteArray& getIconName() const { return m_iconName; }