1 // Copyright 2014 The PDFium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef XFA_FDE_CFDE_TEXTOUT_H_ 8 #define XFA_FDE_CFDE_TEXTOUT_H_ 9 10 #include <deque> 11 #include <memory> 12 #include <vector> 13 14 #include "core/fxcrt/retain_ptr.h" 15 #include "core/fxcrt/widestring.h" 16 #include "core/fxge/dib/fx_dib.h" 17 #include "third_party/base/span.h" 18 #include "xfa/fde/cfde_data.h" 19 #include "xfa/fgas/layout/cfgas_break.h" 20 #include "xfa/fgas/layout/cfgas_char.h" 21 22 class CFGAS_GEFont; 23 class CFGAS_TxtBreak; 24 class CFX_RenderDevice; 25 class TextCharPos; 26 27 class CFDE_TextOut { 28 public: 29 static bool DrawString(CFX_RenderDevice* device, 30 FX_ARGB color, 31 const RetainPtr<CFGAS_GEFont>& pFont, 32 pdfium::span<TextCharPos> pCharPos, 33 float fFontSize, 34 const CFX_Matrix& matrix); 35 36 CFDE_TextOut(); 37 ~CFDE_TextOut(); 38 39 void SetFont(RetainPtr<CFGAS_GEFont> pFont); 40 void SetFontSize(float fFontSize); SetTextColor(FX_ARGB color)41 void SetTextColor(FX_ARGB color) { m_TxtColor = color; } 42 void SetStyles(const FDE_TextStyle& dwStyles); 43 void SetAlignment(FDE_TextAlignment iAlignment); 44 void SetLineSpace(float fLineSpace); SetMatrix(const CFX_Matrix & matrix)45 void SetMatrix(const CFX_Matrix& matrix) { m_Matrix = matrix; } 46 void SetLineBreakTolerance(float fTolerance); 47 48 void CalcLogicSize(WideStringView str, CFX_SizeF* pSize); 49 void CalcLogicSize(WideStringView str, CFX_RectF* pRect); 50 void DrawLogicText(CFX_RenderDevice* device, 51 const WideString& str, 52 const CFX_RectF& rect); GetTotalLines()53 int32_t GetTotalLines() const { return m_iTotalLines; } 54 55 private: 56 struct Piece { 57 Piece(); 58 Piece(const Piece& that); 59 ~Piece(); 60 61 size_t start_char = 0; 62 size_t char_count = 0; 63 uint32_t char_styles = 0; 64 CFX_RectF bounds; 65 }; 66 67 class Line { 68 public: 69 Line(); 70 Line(const Line& that); 71 ~Line(); 72 new_reload()73 bool new_reload() const { return new_reload_; } set_new_reload(bool reload)74 void set_new_reload(bool reload) { new_reload_ = reload; } 75 76 size_t AddPiece(size_t index, const Piece& piece); 77 size_t GetSize() const; 78 const Piece* GetPieceAtIndex(size_t index) const; 79 Piece* GetPieceAtIndex(size_t index); 80 void RemoveLast(size_t count); 81 82 private: 83 bool new_reload_ = false; 84 std::deque<Piece> pieces_; 85 }; 86 87 bool RetrieveLineWidth(CFGAS_Char::BreakType dwBreakStatus, 88 float* pStartPos, 89 float* pWidth, 90 float* pHeight); 91 void LoadText(const WideString& str, const CFX_RectF& rect); 92 93 void Reload(const CFX_RectF& rect); 94 void ReloadLinePiece(Line* pLine, const CFX_RectF& rect); 95 bool RetrievePieces(CFGAS_Char::BreakType dwBreakStatus, 96 bool bReload, 97 const CFX_RectF& rect, 98 size_t* pStartChar, 99 int32_t* pPieceWidths); 100 void AppendPiece(const Piece& piece, bool bNeedReload, bool bEnd); 101 void DoAlignment(const CFX_RectF& rect); 102 size_t GetDisplayPos(const Piece* pPiece); 103 104 std::unique_ptr<CFGAS_TxtBreak> const m_pTxtBreak; 105 RetainPtr<CFGAS_GEFont> m_pFont; 106 float m_fFontSize = 12.0f; 107 float m_fLineSpace = 12.0f; 108 float m_fLinePos = 0.0f; 109 float m_fTolerance = 0.0f; 110 FDE_TextAlignment m_iAlignment = FDE_TextAlignment::kTopLeft; 111 FDE_TextStyle m_Styles; 112 std::vector<int32_t> m_CharWidths; 113 FX_ARGB m_TxtColor = 0xFF000000; 114 Mask<CFGAS_Break::LayoutStyle> m_dwTxtBkStyles; 115 WideString m_wsText; 116 CFX_Matrix m_Matrix; 117 std::deque<Line> m_ttoLines; 118 size_t m_iCurLine = 0; 119 size_t m_iCurPiece = 0; 120 int32_t m_iTotalLines = 0; 121 std::vector<TextCharPos> m_CharPos; 122 }; 123 124 #endif // XFA_FDE_CFDE_TEXTOUT_H_ 125