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"
19#include <podofo/auxiliary/StateStack.h>
34 NoSaveRestorePrior = 2,
51enum class PdfDrawTextStyle : uint8_t
58struct PODOFO_API PdfDrawTextMultiLineParams final
60 PdfDrawTextStyle Style = PdfDrawTextStyle::Regular;
63 bool SkipClip =
false;
64 bool PreserveTrailingSpaces =
false;
67struct PODOFO_API PdfPainterState final
71 PdfGraphicsState GraphicsState;
72 PdfTextState TextState;
73 nullable<Vector2> FirstPoint;
74 nullable<Vector2> CurrentPoint;
76 friend class PdfPainter;
78 PdfTextState EmittedTextState;
81using PdfPainterStateStack = StateStack<PdfPainterState>;
83class PODOFO_API PdfGraphicsStateWrapper final
85 friend class PdfPainter;
88 PdfGraphicsStateWrapper(PdfPainter& painter, PdfGraphicsState& state);
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);
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);
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; }
151 operator const PdfGraphicsState&()
const {
return *m_state; }
154 void SetState(PdfGraphicsState& state) { m_state = &state; }
157 PdfPainter* m_painter;
158 PdfGraphicsState* m_state;
161class PODOFO_API PdfTextStateWrapper final
163 friend class PdfPainter;
166 PdfTextStateWrapper(PdfPainter& painter, PdfTextState& state);
169 void SetFont(
const PdfFont& font,
double fontSize);
175 void SetFontScale(
double scale);
180 void SetCharSpacing(
double charSpacing);
185 void SetWordSpacing(
double wordSpacing);
187 void SetRenderingMode(PdfTextRenderingMode mode);
192 void SetMatrix(
const Matrix& matrix);
195 inline const PdfFont* GetFont()
const {
return m_State->Font; }
200 inline double GetFontSize()
const {
return m_State->FontSize; }
205 inline double GetFontScale()
const {
return m_State->FontScale; }
210 inline double GetCharSpacing()
const {
return m_State->CharSpacing; }
215 inline double GetWordSpacing()
const {
return m_State->WordSpacing; }
220 const PdfTextState& GetState()
const {
return *m_State; }
221 operator const PdfTextState&()
const {
return *m_State; }
224 void SetState(PdfTextState& state) { m_State = &state; }
227 PdfPainter* m_painter;
228 PdfTextState* m_State;
245 friend class PdfGraphicsStateWrapper;
246 friend class PdfTextStateWrapper;
247 friend class PdfPainterPathContext;
276 void FinishDrawing();
310 void SetClipRect(
double x,
double y,
double width,
double height);
324 void DrawLine(
double x1,
double y1,
double x2,
double y2);
336 void DrawCubicBezier(
double x1,
double y1,
double x2,
double y2,
double x3,
double y3,
double x4,
double y4);
362 void DrawEllipse(
double x,
double y,
double width,
double height,
373 void DrawRectangle(
double x,
double y,
double width,
double height,
390 void DrawText(
const std::string_view&
str,
double x,
double y,
391 PdfDrawTextStyle
style = PdfDrawTextStyle::Regular);
405 const PdfDrawTextMultiLineParams&
params = { });
416 const PdfDrawTextMultiLineParams&
params = { });
426 void DrawTextAligned(
const std::string_view&
str,
double x,
double y,
428 PdfDrawTextStyle
style = PdfDrawTextStyle::Regular);
464 void BeginMarkedContent(
const std::string_view&
tag);
469 void EndMarkedContent();
493 void SetPrecision(
unsigned short precision);
498 unsigned short GetPrecision()
const;
503 std::string_view GetContent()
const;
506 inline const PdfPainterStateStack& GetStateStack()
const {
return m_StateStack; }
543 void TextMoveTo(
double x,
double y);
544 void AddText(
const std::string_view&
str);
548 void SetLineWidth(
double value);
560 void SetShadingDictionary(
const PdfShadingDictionary&
shading);
561 void SetRenderingIntent(
const std::string_view&
intent);
562 void SetTransformationMatrix(
const Matrix&
matrix);
564 void SetFontScale(
double value);
565 void SetCharSpacing(
double value);
566 void SetWordSpacing(
double value);
568 void SetTextMatrix(
const Matrix&
matrix);
572 void writeTextState();
574 void setFontScale(
double value);
575 void setCharSpacing(
double value);
576 void setWordSpacing(
double value);
578 void setTextMatrix(
const Matrix& value);
589 void drawLines(
const std::vector<std::array<double, 4>>&
lines);
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;
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;
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;
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;
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;
648 void cs_Operator(
const std::string_view& name)
override;
650 void CS_Operator(
const std::string_view& name)
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;
666 void BX_Operator()
override;
667 void EX_Operator()
override;
674 StatusTextObject = 2,
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);
683 void drawText(
const std::string_view& str,
double x,
double y,
684 bool isUnderline,
bool isStrikeThrough, std::vector<std::array<double, 4>>& linesToDraw);
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);
690 void setLineWidth(
double width);
701 std::string expandTabs(
const std::string_view& str)
const;
703 void openPath(
double x,
double y);
705 void checkPathOpened()
const;
706 void checkFont()
const;
707 void finishDrawing();
708 void checkStatus(
int expectedStatus);
709 void enterTextObject();
710 void exitTextObject();
713 PdfPainter(
const PdfPainter&) =
delete;
714 PdfPainter& operator=(
const PdfPainter&) =
delete;
717 PdfPainterFlags m_flags;
718 PainterStatus m_painterStatus;
719 PdfPainterStateStack m_StateStack;
720 unsigned m_textStackCount;
723 PdfGraphicsStateWrapper GraphicsState;
724 PdfTextStateWrapper TextState;
725 PdfPainterTextObject TextObject;
732 PdfObjectStream* m_objStream;
742 unsigned short m_TabWidth;
746 PdfStringStream m_stream;
748 std::unordered_map<PdfReference, PdfName> m_resNameCache;
754ENABLE_BITMASK_OPERATORS(PoDoFo::PdfDrawTextStyle);
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 ¶ms={ })
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 ¶ms={ })
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