• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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 #include "core/fpdfapi/page/cpdf_textstate.h"
8 
9 #include <math.h>
10 
11 #include <utility>
12 
13 #include "core/fpdfapi/font/cpdf_font.h"
14 #include "core/fpdfapi/page/cpdf_docpagedata.h"
15 
16 CPDF_TextState::CPDF_TextState() = default;
17 
18 CPDF_TextState::~CPDF_TextState() = default;
19 
Emplace()20 void CPDF_TextState::Emplace() {
21   m_Ref.Emplace();
22 }
23 
GetFont() const24 RetainPtr<CPDF_Font> CPDF_TextState::GetFont() const {
25   return m_Ref.GetObject()->m_pFont;
26 }
27 
SetFont(RetainPtr<CPDF_Font> pFont)28 void CPDF_TextState::SetFont(RetainPtr<CPDF_Font> pFont) {
29   m_Ref.GetPrivateCopy()->SetFont(std::move(pFont));
30 }
31 
GetFontSize() const32 float CPDF_TextState::GetFontSize() const {
33   return m_Ref.GetObject()->m_FontSize;
34 }
35 
SetFontSize(float size)36 void CPDF_TextState::SetFontSize(float size) {
37   m_Ref.GetPrivateCopy()->m_FontSize = size;
38 }
39 
GetMatrix() const40 pdfium::span<const float> CPDF_TextState::GetMatrix() const {
41   return m_Ref.GetObject()->m_Matrix;
42 }
43 
GetMutableMatrix()44 pdfium::span<float> CPDF_TextState::GetMutableMatrix() {
45   return m_Ref.GetPrivateCopy()->m_Matrix;
46 }
47 
GetCharSpace() const48 float CPDF_TextState::GetCharSpace() const {
49   return m_Ref.GetObject()->m_CharSpace;
50 }
51 
SetCharSpace(float sp)52 void CPDF_TextState::SetCharSpace(float sp) {
53   m_Ref.GetPrivateCopy()->m_CharSpace = sp;
54 }
55 
GetWordSpace() const56 float CPDF_TextState::GetWordSpace() const {
57   return m_Ref.GetObject()->m_WordSpace;
58 }
59 
SetWordSpace(float sp)60 void CPDF_TextState::SetWordSpace(float sp) {
61   m_Ref.GetPrivateCopy()->m_WordSpace = sp;
62 }
63 
GetFontSizeH() const64 float CPDF_TextState::GetFontSizeH() const {
65   return m_Ref.GetObject()->GetFontSizeH();
66 }
67 
GetTextMode() const68 TextRenderingMode CPDF_TextState::GetTextMode() const {
69   return m_Ref.GetObject()->m_TextMode;
70 }
71 
SetTextMode(TextRenderingMode mode)72 void CPDF_TextState::SetTextMode(TextRenderingMode mode) {
73   m_Ref.GetPrivateCopy()->m_TextMode = mode;
74 }
75 
GetCTM() const76 pdfium::span<const float> CPDF_TextState::GetCTM() const {
77   return m_Ref.GetObject()->m_CTM;
78 }
79 
GetMutableCTM()80 pdfium::span<float> CPDF_TextState::GetMutableCTM() {
81   return m_Ref.GetPrivateCopy()->m_CTM;
82 }
83 
84 CPDF_TextState::TextData::TextData() = default;
85 
TextData(const TextData & that)86 CPDF_TextState::TextData::TextData(const TextData& that)
87     : m_pFont(that.m_pFont),
88       m_pDocument(that.m_pDocument),
89       m_FontSize(that.m_FontSize),
90       m_CharSpace(that.m_CharSpace),
91       m_WordSpace(that.m_WordSpace),
92       m_TextMode(that.m_TextMode) {
93   for (int i = 0; i < 4; ++i)
94     m_Matrix[i] = that.m_Matrix[i];
95 
96   for (int i = 0; i < 4; ++i)
97     m_CTM[i] = that.m_CTM[i];
98 
99   if (m_pDocument && m_pFont) {
100     auto* pPageData = CPDF_DocPageData::FromDocument(m_pDocument);
101     m_pFont = pPageData->GetFont(m_pFont->GetMutableFontDict());
102   }
103 }
104 
105 CPDF_TextState::TextData::~TextData() = default;
106 
Clone() const107 RetainPtr<CPDF_TextState::TextData> CPDF_TextState::TextData::Clone() const {
108   return pdfium::MakeRetain<CPDF_TextState::TextData>(*this);
109 }
110 
SetFont(RetainPtr<CPDF_Font> pFont)111 void CPDF_TextState::TextData::SetFont(RetainPtr<CPDF_Font> pFont) {
112   m_pDocument = pFont ? pFont->GetDocument() : nullptr;
113   m_pFont = std::move(pFont);
114 }
115 
GetFontSizeH() const116 float CPDF_TextState::TextData::GetFontSizeH() const {
117   return fabs(FXSYS_sqrt2(m_Matrix[0], m_Matrix[2]) * m_FontSize);
118 }
119 
SetTextRenderingModeFromInt(int iMode,TextRenderingMode * mode)120 bool SetTextRenderingModeFromInt(int iMode, TextRenderingMode* mode) {
121   if (iMode < 0 || iMode > 7)
122     return false;
123   *mode = static_cast<TextRenderingMode>(iMode);
124   return true;
125 }
126 
TextRenderingModeIsClipMode(const TextRenderingMode & mode)127 bool TextRenderingModeIsClipMode(const TextRenderingMode& mode) {
128   switch (mode) {
129     case TextRenderingMode::MODE_FILL_CLIP:
130     case TextRenderingMode::MODE_STROKE_CLIP:
131     case TextRenderingMode::MODE_FILL_STROKE_CLIP:
132     case TextRenderingMode::MODE_CLIP:
133       return true;
134     default:
135       return false;
136   }
137 }
138 
TextRenderingModeIsStrokeMode(const TextRenderingMode & mode)139 bool TextRenderingModeIsStrokeMode(const TextRenderingMode& mode) {
140   switch (mode) {
141     case TextRenderingMode::MODE_STROKE:
142     case TextRenderingMode::MODE_FILL_STROKE:
143     case TextRenderingMode::MODE_STROKE_CLIP:
144     case TextRenderingMode::MODE_FILL_STROKE_CLIP:
145       return true;
146     default:
147       return false;
148   }
149 }
150