PoDoFo 1.0.0-dev
Loading...
Searching...
No Matches
PdfPainter.h
1
7#ifndef PDF_PAINTER_H
8#define PDF_PAINTER_H
9
10#include "PdfCanvas.h"
11#include "PdfTextState.h"
12#include "PdfGraphicsState.h"
13#include "PdfPainterPath.h"
14#include "PdfPainterTextObject.h"
15#include "PdfColorSpace.h"
16#include "PdfPattern.h"
17#include "PdfContentStreamOperators.h"
18
19#include <podofo/auxiliary/StateStack.h>
20
21namespace PoDoFo {
22
23class PdfExtGState;
24class PdfFont;
25class PdfImage;
26class PdfObjectStream;
27class PdfXObject;
28class PdfPainter;
29
31{
32 None = 0,
33 Prepend = 1,
34 NoSaveRestorePrior = 2,
35 NoSaveRestore = 4,
36 RawCoordinates = 8,
37};
38
43{
44 Stroke = 1,
45 Fill = 2,
46 StrokeFill = 3,
47 FillEvenOdd = 4,
49};
50
51enum class PdfDrawTextStyle : uint8_t
52{
53 Regular = 0,
54 StrikeThrough = 1,
55 Underline = 2,
56};
57
58struct PODOFO_API PdfDrawTextMultiLineParams final
59{
60 PdfDrawTextStyle Style = PdfDrawTextStyle::Regular;
61 PdfHorizontalAlignment HorizontalAlignment = PdfHorizontalAlignment::Left;
62 PdfVerticalAlignment VerticalAlignment = PdfVerticalAlignment::Top;
63 bool SkipClip = false;
64 bool PreserveTrailingSpaces = false;
65};
66
67struct PODOFO_API PdfPainterState final
68{
69 PdfPainterState();
70
71 PdfGraphicsState GraphicsState;
72 PdfTextState TextState;
73 nullable<Vector2> FirstPoint;
74 nullable<Vector2> CurrentPoint;
75private:
76 friend class PdfPainter;
77private:
78 PdfTextState EmittedTextState;
79};
80
81using PdfPainterStateStack = StateStack<PdfPainterState>;
82
83class PODOFO_API PdfGraphicsStateWrapper final
84{
85 friend class PdfPainter;
86
87private:
88 PdfGraphicsStateWrapper(PdfPainter& painter, PdfGraphicsState& state);
89
90public:
94 void ConcatenateTransformationMatrix(const Matrix& matrix);
95 void SetLineWidth(double lineWidth);
96 void SetMiterLevel(double value);
97 void SetLineCapStyle(PdfLineCapStyle capStyle);
98 void SetLineJoinStyle(PdfLineJoinStyle joinStyle);
99 void SetRenderingIntent(const std::string_view& intent);
102 void SetNonStrokingColorSpace(PdfColorSpaceInitializer&& colorSpace);
105 void SetStrokingColorSpace(PdfColorSpaceInitializer&& color);
108 void SetNonStrokingColor(const PdfColor& color);
111 void SetStrokingColor(const PdfColor& color);
114 void SetNonStrokingColor(const PdfColorRaw& color);
117 void SetStrokingColor(const PdfColorRaw& color);
118 void SetExtGState(const PdfExtGState& extGState);
121 void SetStrokingUncolouredTilingPattern(const PdfUncolouredTilingPattern& pattern, const PdfColorRaw& color);
122
125 void SetNonStrokingUncolouredTilingPattern(const PdfUncolouredTilingPattern& pattern, const PdfColorRaw& color);
128 void SetStrokingPattern(const PdfPattern& pattern);
131 void SetNonStrokingPattern(const PdfPattern& pattern);
134 void SetShadingDictionary(const PdfShadingDictionary& shading);
135
136public:
139 const Matrix& GetCurrentMatrix() { return m_state->CTM; }
140 double GetLineWidth() const { return m_state->LineWidth; }
141 double GetMiterLevel() const { return m_state->MiterLimit; }
142 PdfLineCapStyle GetLineCapStyle() const { return m_state->LineCapStyle; }
143 PdfLineJoinStyle GetLineJoinStyle() const { return m_state->LineJoinStyle; }
144 const std::string& GetRenderingIntent() const { return m_state->RenderingIntent; }
145 const PdfColorRaw& GetNonStrokingColor() const { return m_state->NonStrokingColor; }
146 const PdfColorRaw& GetStrokingColor() const { return m_state->StrokingColor; }
147 PdfColorSpaceFilterPtr GetNonStrokingColorSpace() const { return m_state->NonStrokingColorSpaceFilter; }
148 PdfColorSpaceFilterPtr GetStrokingColorSpace() const { return m_state->StrokingColorSpaceFilter; }
149
150public:
151 operator const PdfGraphicsState&() const { return *m_state; }
152
153private:
154 void SetState(PdfGraphicsState& state) { m_state = &state; }
155
156private:
157 PdfPainter* m_painter;
158 PdfGraphicsState* m_state;
159};
160
161class PODOFO_API PdfTextStateWrapper final
162{
163 friend class PdfPainter;
164
165private:
166 PdfTextStateWrapper(PdfPainter& painter, PdfTextState& state);
167
168public:
169 void SetFont(const PdfFont& font, double fontSize);
170
175 void SetFontScale(double scale);
176
180 void SetCharSpacing(double charSpacing);
181
185 void SetWordSpacing(double wordSpacing);
186
187 void SetRenderingMode(PdfTextRenderingMode mode);
188
192 void SetMatrix(const Matrix& matrix);
193
194public:
195 inline const PdfFont* GetFont() const { return m_State->Font; }
196
200 inline double GetFontSize() const { return m_State->FontSize; }
201
205 inline double GetFontScale() const { return m_State->FontScale; }
206
210 inline double GetCharSpacing() const { return m_State->CharSpacing; }
211
215 inline double GetWordSpacing() const { return m_State->WordSpacing; }
216
217 inline PdfTextRenderingMode GetRenderingMode() const { return m_State->RenderingMode; }
218
219public:
220 const PdfTextState& GetState() const { return *m_State; }
221 operator const PdfTextState&() const { return *m_State; }
222
223private:
224 void SetState(PdfTextState& state) { m_State = &state; }
225
226private:
227 PdfPainter* m_painter;
228 PdfTextState* m_State;
229};
230
244{
245 friend class PdfGraphicsStateWrapper;
246 friend class PdfTextStateWrapper;
247 friend class PdfPainterPathContext;
248 friend class PdfPainterTextObject;
249
250public:
255 PdfPainter();
256
257 ~PdfPainter() noexcept(false);
258
270 void SetCanvas(PdfCanvas& canvas, PdfPainterFlags flags = PdfPainterFlags::None);
271
276 void FinishDrawing();
277
299 void SetStrokeStyle(PdfStrokeStyle strokeStyle, bool inverted = false, double scale = 1.0, bool subtractJoinCap = false);
300
301 void SetStrokeStyle(const cspan<double>& dashArray, double phase);
302
310 void SetClipRect(double x, double y, double width, double height);
311
316 void SetClipRect(const Rect& rect);
317
324 void DrawLine(double x1, double y1, double x2, double y2);
325
336 void DrawCubicBezier(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4);
337
347 void DrawArc(double x, double y, double radius, double startAngle, double endAngle, bool clockwise = false);
348
354 void DrawCircle(double x, double y, double radius, PdfPathDrawMode mode = PdfPathDrawMode::Stroke);
355
362 void DrawEllipse(double x, double y, double width, double height,
363 PdfPathDrawMode mode = PdfPathDrawMode::Stroke);
364
373 void DrawRectangle(double x, double y, double width, double height,
374 PdfPathDrawMode mode = PdfPathDrawMode::Stroke, double roundX = 0.0, double roundY = 0.0);
375
381 void DrawRectangle(const Rect& rect, PdfPathDrawMode mode = PdfPathDrawMode::Stroke,
382 double roundX = 0.0, double roundY = 0.0);
383
390 void DrawText(const std::string_view& str, double x, double y,
391 PdfDrawTextStyle style = PdfDrawTextStyle::Regular);
392
404 void DrawTextMultiLine(const std::string_view& str, double x, double y, double width, double height,
405 const PdfDrawTextMultiLineParams& params = { });
406
415 void DrawTextMultiLine(const std::string_view& str, const Rect& rect,
416 const PdfDrawTextMultiLineParams& params = { });
417
426 void DrawTextAligned(const std::string_view& str, double x, double y,
428 PdfDrawTextStyle style = PdfDrawTextStyle::Regular);
429
437 void DrawImage(const PdfImage& obj, double x, double y, double scaleX = 1.0, double scaleY = 1.0);
438
449 void DrawXObject(const PdfXObject& obj, double x, double y, double scaleX = 1.0, double scaleY = 1.0);
450
454 void DrawPath(const PdfPainterPath& path, PdfPathDrawMode drawMode = PdfPathDrawMode::Stroke);
455
459 void ClipPath(const PdfPainterPath& path, bool useEvenOddRule = false);
460
464 void BeginMarkedContent(const std::string_view& tag);
465
469 void EndMarkedContent();
470
478 void Save();
479
487 void Restore();
488
493 void SetPrecision(unsigned short precision);
494
498 unsigned short GetPrecision() const;
499
503 std::string_view GetContent() const;
504
505public:
506 inline const PdfPainterStateStack& GetStateStack() const { return m_StateStack; }
507
517 inline void SetTabWidth(unsigned short tabWidth) { m_TabWidth = tabWidth; }
518
525 inline unsigned short GetTabWidth() const { return m_TabWidth; }
526
531 inline PdfCanvas* GetCanvas() const { return m_canvas; }
532
537 inline PdfObjectStream* GetStream() const { return m_objStream; }
538
539private:
540 // To be called by PdfPainterTextObject
541 void BeginText();
542 void EndText();
543 void TextMoveTo(double x, double y);
544 void AddText(const std::string_view& str);
545
546private:
547 // To be called by state wrappers
548 void SetLineWidth(double value);
549 void SetMiterLimit(double miterLimit);
550 void SetLineCapStyle(PdfLineCapStyle style);
551 void SetLineJoinStyle(PdfLineJoinStyle style);
552 void SetNonStrokingColor(const PdfColor& color);
553 void SetStrokingColor(const PdfColor& color);
554 void SetNonStrokingColor(const PdfColorRaw& color, const PdfColorSpaceFilter& colorSpace);
555 void SetStrokingColor(const PdfColorRaw& color, const PdfColorSpaceFilter& colorSpace);
556 void SetNonStrokingColorSpace(const PdfVariant& expVar);
557 void SetStrokingColorSpace(const PdfVariant& expVar);
558 void SetStrokingPattern(const PdfPattern& pattern, const PdfColorRaw* color, const PdfColorSpaceFilter* colorSpace);
559 void SetNonStrokingPattern(const PdfPattern& pattern, const PdfColorRaw* color, const PdfColorSpaceFilter* colorSpace);
560 void SetShadingDictionary(const PdfShadingDictionary& shading);
561 void SetRenderingIntent(const std::string_view& intent);
562 void SetTransformationMatrix(const Matrix& matrix);
563 void SetFont(const PdfFont& font, double fontSize);
564 void SetFontScale(double value);
565 void SetCharSpacing(double value);
566 void SetWordSpacing(double value);
567 void SetTextRenderingMode(PdfTextRenderingMode value);
568 void SetTextMatrix(const Matrix& matrix);
569 void SetExtGState(const PdfExtGState& extGState);
570
571private:
572 void writeTextState();
573 void setFont(const PdfFont& font, double fontSize);
574 void setFontScale(double value);
575 void setCharSpacing(double value);
576 void setWordSpacing(double value);
577 void setTextRenderingMode(PdfTextRenderingMode value);
578 void setTextMatrix(const Matrix& value);
579 void save();
580 void restore();
581 void reset();
582 void drawRectangle(double x, double y, double width, double height, PdfPathDrawMode mode, double roundX, double roundY);
583 void drawPath(PdfPathDrawMode mode);
584 void stroke();
585 void fill(bool useEvenOddRule);
586 void strokeAndFill(bool useEvenOddRule);
587 PdfName tryAddResource(const PdfObject& obj, PdfResourceType type);
588 PdfName tryAddResource(const PdfReference& ref, PdfResourceType type);
589 void drawLines(const std::vector<std::array<double, 4>>& lines);
590
591private:
592 // PdfContentStreamOperators implementation
593 void re_Operator(double x, double y, double width, double height) override;
594 void m_Operator(double x, double y) override;
595 void l_Operator(double x, double y) override;
596 void c_Operator(double c1x, double c1y, double c2x, double c2y, double x, double y) override;
597 void v_Operator(double cx, double cy, double x, double y) override;
598 void y_Operator(double cx, double cy, double x, double y) override;
599 void n_Operator() override;
600 void h_Operator() override;
601 void b_Operator() override;
602 void B_Operator() override;
603 void bStar_Operator() override;
604 void BStar_Operator() override;
605 void s_Operator() override;
606 void S_Operator() override;
607 void f_Operator() override;
608 void fStar_Operator() override;
609 void W_Operator() override;
610 void WStar_Operator() override;
611 void MP_Operator(const std::string_view& tag) override;
612 void DP_Operator(const std::string_view& tag, const PdfDictionary& properties) override;
613 void DP_Operator(const std::string_view& tag, const std::string_view& propertyDictName) override;
614 void BMC_Operator(const std::string_view& tag) override;
615 void BDC_Operator(const std::string_view& tag, const PdfDictionary& properties) override;
616 void BDC_Operator(const std::string_view& tag, const std::string_view& propertyDictName) override;
617 void EMC_Operator() override;
618 void q_Operator() override;
619 void Q_Operator() override;
620 void BT_Operator() override;
621 void ET_Operator() override;
622 void Td_Operator(double tx, double ty) override;
623 void TD_Operator(double tx, double ty) override;
624 void Tm_Operator(double a, double b, double c, double d, double e, double f) override;
625 void Tr_Operator(PdfTextRenderingMode mode) override;
626 void Ts_Operator(double rise) override;
627 void Tc_Operator(double charSpace) override;
628 void TL_Operator(double leading) override;
629 void Tf_Operator(const std::string_view& fontName, double fontSize) override;
630 void Tw_Operator(double wordSpace) override;
631 void Tz_Operator(double scale) override;
632 void Tj_Operator(const std::string_view& encoded, bool hex) override;
633 void TJ_Operator_Begin() override;
634 void TJ_Operator_Delta(double delta) override;
635 void TJ_Operator_Glyphs(const std::string_view& encoded, bool hex) override;
636 void TJ_Operator_End() override;
637 void cm_Operator(double a, double b, double c, double d, double e, double f) override;
638 void w_Operator(double lineWidth) override;
639 void J_Operator(PdfLineCapStyle style) override;
640 void j_Operator(PdfLineJoinStyle style) override;
641 void M_Operator(double miterLimit) override;
642 void d_Operator(const cspan<double>& dashArray, double fase) override;
643 void ri_Operator(const std::string_view& intent) override;
644 void i_Operator(double flatness) override;
645 void gs_Operator(const std::string_view& dictName) override;
646 void Do_Operator(const std::string_view& xobjname) override;
647 void cs_Operator(PdfColorSpaceType colorSpace) override;
648 void cs_Operator(const std::string_view& name) override;
649 void CS_Operator(PdfColorSpaceType colorSpace) override;
650 void CS_Operator(const std::string_view& name) override;
651 void sc_Operator(const cspan<double>& components) override;
652 void SC_Operator(const cspan<double>& components) override;
653 void scn_Operator(const cspan<double>& components) override;
654 void SCN_Operator(const cspan<double>& components) override;
655 void scn_Operator(const cspan<double>& components, const std::string_view& patternName) override;
656 void SCN_Operator(const cspan<double>& components, const std::string_view& patternName) override;
657 void scn_Operator(const std::string_view& patternName) override;
658 void SCN_Operator(const std::string_view& patternName) override;
659 void G_Operator(double gray) override;
660 void g_Operator(double gray) override;
661 void RG_Operator(double red, double green, double blue) override;
662 void rg_Operator(double red, double green, double blue) override;
663 void K_Operator(double cyan, double magenta, double yellow, double black) override;
664 void k_Operator(double cyan, double magenta, double yellow, double black) override;
665 void sh_Operator(const std::string_view& shadingDictName) override;
666 void BX_Operator() override;
667 void EX_Operator() override;
668 void Extension_Operator(const std::string_view& opName, const cspan<PdfVariant>& operands) override;
669
670private:
671 enum PainterStatus
672 {
673 StatusDefault = 1,
674 StatusTextObject = 2,
675 StatusTextArray = 4,
676 StatusExtension = 8,
677 };
678
679private:
680 void drawTextAligned(const std::string_view& str, double x, double y, double width,
681 PdfHorizontalAlignment hAlignment, PdfDrawTextStyle style, std::vector<std::array<double, 4>>& linesToDraw);
682
683 void drawText(const std::string_view& str, double x, double y,
684 bool isUnderline, bool isStrikeThrough, std::vector<std::array<double, 4>>& linesToDraw);
685
686 void drawMultiLineText(const std::string_view& str, double x, double y, double width, double height,
687 PdfHorizontalAlignment hAlignment, PdfVerticalAlignment vAlignment, bool skipClip, bool preserveTrailingSpaces,
688 PdfDrawTextStyle style);
689
690 void setLineWidth(double width);
691
701 std::string expandTabs(const std::string_view& str) const;
702 void checkStream();
703 void openPath(double x, double y);
704 void resetPath();
705 void checkPathOpened() const;
706 void checkFont() const;
707 void finishDrawing();
708 void checkStatus(int expectedStatus);
709 void enterTextObject();
710 void exitTextObject();
711
712private:
713 PdfPainter(const PdfPainter&) = delete;
714 PdfPainter& operator=(const PdfPainter&) = delete;
715
716private:
717 PdfPainterFlags m_flags;
718 PainterStatus m_painterStatus;
719 PdfPainterStateStack m_StateStack;
720 unsigned m_textStackCount;
721
722public:
723 PdfGraphicsStateWrapper GraphicsState;
724 PdfTextStateWrapper TextState;
725 PdfPainterTextObject TextObject;
726
727private:
732 PdfObjectStream* m_objStream;
733
737 PdfCanvas* m_canvas;
738
742 unsigned short m_TabWidth;
743
746 PdfStringStream m_stream;
747
748 std::unordered_map<PdfReference, PdfName> m_resNameCache;
749};
750
751}
752
753ENABLE_BITMASK_OPERATORS(PoDoFo::PdfPainterFlags);
754ENABLE_BITMASK_OPERATORS(PoDoFo::PdfDrawTextStyle);
755
756#endif // PDF_PAINTER_H
A interface that provides the necessary features for a painter to draw onto a PdfObject.
Definition PdfCanvas.h:28
A class that implements methods to sample colors from a scanline buffer.
Definition PdfColorSpaceFilter.h:32
A color object can represent either a grayscale value, a RGB color, a CMYK color.
Definition PdfColor.h:23
Pdf content stream callble operator interface ISO 32000 - 1:2008 "A.2 PDF Content Stream Operators".
Definition PdfContentStreamOperators.h:22
The PDF dictionary data type of PoDoFo (inherits from PdfDataContainer, the base class for such repre...
Definition PdfDictionary.h:82
This class wraps the ExtGState object used in the Resource Dictionary of a Content-supporting element...
Definition PdfExtGState.h:23
Before you can draw text on a PDF document, you have to create a font object first.
Definition PdfFont.h:45
A PdfImage object is needed when ever you want to embed an image file into a PDF document.
Definition PdfImage.h:78
This class represents a PdfName.
Definition PdfName.h:24
A PDF stream can be appended to any PdfObject and can contain arbitrary data.
Definition PdfObjectStream.h:87
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:35
This class describes PDF paths being written to a PdfPainter.
Definition PdfPainterPath.h:19
This class describes a manually handled PDF text object (content stream operators surrounded by BT .
Definition PdfPainterTextObject.h:22
This class provides an easy to use painter object which allows you to draw on a PDF page object.
Definition PdfPainter.h:244
void SetTabWidth(unsigned short tabWidth)
Set the tab width for the DrawText operation.
Definition PdfPainter.h:517
void DrawTextMultiLine(const std::string_view &str, const Rect &rect, const PdfDrawTextMultiLineParams &params={ })
Draw multiline text into a rectangle doing automatic wordwrapping.
PdfCanvas * GetCanvas() const
Return the current page that is that on the painter.
Definition PdfPainter.h:531
unsigned short GetTabWidth() const
Get the currently set tab width.
Definition PdfPainter.h:525
void DrawTextMultiLine(const std::string_view &str, double x, double y, double width, double height, const PdfDrawTextMultiLineParams &params={ })
Draw multiline text into a rectangle doing automatic wordwrapping.
PdfObjectStream * GetStream() const
Return the current canvas stream that is set on the painter.
Definition PdfPainter.h:537
A reference is a pointer to a object in the PDF file of the form "4 0 R", where 4 is the object numbe...
Definition PdfReference.h:24
A variant data type which supports all data types supported by the PDF standard.
Definition PdfVariant.h:33
A XObject is a content stream with several drawing commands and data which can be used throughout a P...
Definition PdfXObject.h:31
An normalized rectangle defined by position (left-bottom) and size.
Definition Rect.h:20
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:38
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition basetypes.h:16
PdfLineJoinStyle
Enum for line join styles when drawing.
Definition PdfDeclarations.h:442
PdfStrokeStyle
Enum for the different stroke styles that can be set when drawing to a PDF file (mostly for line draw...
Definition PdfDeclarations.h:408
PdfPathDrawMode
An enum describing modes to draw paths and figures.
Definition PdfPainter.h:43
@ StrokeFill
Stroke and fill using the the even-odd rule to determine the region to fill.
@ StrokeFillEvenOdd
Stroke and fill using the the even-odd rule to determine the region to fill.
@ FillEvenOdd
Fill using the the even-odd rule to determine the region to fill.
std::shared_ptr< const PdfColorSpaceFilter > PdfColorSpaceFilterPtr
Convenience alias for a constant PdfColorSpaceFilter shared ptr.
Definition PdfColorSpaceFilter.h:85
PdfPainterFlags
Definition PdfPainter.h:31
@ NoSaveRestore
Do not perform a Save/Restore of added content in this painting session.
PdfHorizontalAlignment
Enum for text alignment.
Definition PdfDeclarations.h:462
@ None
Do not add a default appearrance.
PdfLineCapStyle
Enum for line cap styles when drawing.
Definition PdfDeclarations.h:432
PdfTextRenderingMode
Enum for text rendering mode (Tr)
Definition PdfDeclarations.h:392
@ Fill
Default mode, fill text.
PdfColorSpaceType
Enum for the colorspaces supported by PDF.
Definition PdfDeclarations.h:359
PdfVerticalAlignment
Enum for vertical text alignment.
Definition PdfDeclarations.h:452