1 // Copyright 2016 PDFium Authors. All rights reserved.
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 "core/fpdfapi/font/cpdf_font.h"
10 #include "core/fpdfapi/page/cpdf_docpagedata.h"
11 #include "core/fpdfapi/parser/cpdf_document.h"
12
CPDF_TextState()13 CPDF_TextState::CPDF_TextState() {}
~CPDF_TextState()14 CPDF_TextState::~CPDF_TextState() {}
15
Emplace()16 void CPDF_TextState::Emplace() {
17 m_Ref.Emplace();
18 }
19
GetFont() const20 CPDF_Font* CPDF_TextState::GetFont() const {
21 return m_Ref.GetObject()->m_pFont;
22 }
23
SetFont(CPDF_Font * pFont)24 void CPDF_TextState::SetFont(CPDF_Font* pFont) {
25 m_Ref.GetPrivateCopy()->SetFont(pFont);
26 }
27
GetFontSize() const28 float CPDF_TextState::GetFontSize() const {
29 return m_Ref.GetObject()->m_FontSize;
30 }
31
SetFontSize(float size)32 void CPDF_TextState::SetFontSize(float size) {
33 m_Ref.GetPrivateCopy()->m_FontSize = size;
34 }
35
GetMatrix() const36 const float* CPDF_TextState::GetMatrix() const {
37 return m_Ref.GetObject()->m_Matrix;
38 }
39
GetMutableMatrix()40 float* CPDF_TextState::GetMutableMatrix() {
41 return m_Ref.GetPrivateCopy()->m_Matrix;
42 }
43
GetCharSpace() const44 float CPDF_TextState::GetCharSpace() const {
45 return m_Ref.GetObject()->m_CharSpace;
46 }
47
SetCharSpace(float sp)48 void CPDF_TextState::SetCharSpace(float sp) {
49 m_Ref.GetPrivateCopy()->m_CharSpace = sp;
50 }
51
GetWordSpace() const52 float CPDF_TextState::GetWordSpace() const {
53 return m_Ref.GetObject()->m_WordSpace;
54 }
55
SetWordSpace(float sp)56 void CPDF_TextState::SetWordSpace(float sp) {
57 m_Ref.GetPrivateCopy()->m_WordSpace = sp;
58 }
59
GetFontSizeV() const60 float CPDF_TextState::GetFontSizeV() const {
61 return m_Ref.GetObject()->GetFontSizeV();
62 }
63
GetFontSizeH() const64 float CPDF_TextState::GetFontSizeH() const {
65 return m_Ref.GetObject()->GetFontSizeH();
66 }
67
GetBaselineAngle() const68 float CPDF_TextState::GetBaselineAngle() const {
69 return m_Ref.GetObject()->GetBaselineAngle();
70 }
71
GetShearAngle() const72 float CPDF_TextState::GetShearAngle() const {
73 return m_Ref.GetObject()->GetShearAngle();
74 }
75
GetTextMode() const76 TextRenderingMode CPDF_TextState::GetTextMode() const {
77 return m_Ref.GetObject()->m_TextMode;
78 }
79
SetTextMode(TextRenderingMode mode)80 void CPDF_TextState::SetTextMode(TextRenderingMode mode) {
81 m_Ref.GetPrivateCopy()->m_TextMode = mode;
82 }
83
GetCTM() const84 const float* CPDF_TextState::GetCTM() const {
85 return m_Ref.GetObject()->m_CTM;
86 }
87
GetMutableCTM()88 float* CPDF_TextState::GetMutableCTM() {
89 return m_Ref.GetPrivateCopy()->m_CTM;
90 }
91
TextData()92 CPDF_TextState::TextData::TextData()
93 : m_pFont(nullptr),
94 m_pDocument(nullptr),
95 m_FontSize(1.0f),
96 m_CharSpace(0),
97 m_WordSpace(0),
98 m_TextMode(TextRenderingMode::MODE_FILL) {
99 m_Matrix[0] = m_Matrix[3] = 1.0f;
100 m_Matrix[1] = m_Matrix[2] = 0;
101 m_CTM[0] = m_CTM[3] = 1.0f;
102 m_CTM[1] = m_CTM[2] = 0;
103 }
104
TextData(const TextData & that)105 CPDF_TextState::TextData::TextData(const TextData& that)
106 : m_pFont(that.m_pFont),
107 m_pDocument(that.m_pDocument),
108 m_FontSize(that.m_FontSize),
109 m_CharSpace(that.m_CharSpace),
110 m_WordSpace(that.m_WordSpace),
111 m_TextMode(that.m_TextMode) {
112 for (int i = 0; i < 4; ++i)
113 m_Matrix[i] = that.m_Matrix[i];
114
115 for (int i = 0; i < 4; ++i)
116 m_CTM[i] = that.m_CTM[i];
117
118 if (m_pDocument && m_pFont)
119 m_pFont = m_pDocument->GetPageData()->GetFont(m_pFont->GetFontDict());
120 }
121
~TextData()122 CPDF_TextState::TextData::~TextData() {
123 ReleaseFont();
124 }
125
SetFont(CPDF_Font * pFont)126 void CPDF_TextState::TextData::SetFont(CPDF_Font* pFont) {
127 ReleaseFont();
128 m_pDocument = pFont ? pFont->GetDocument() : nullptr;
129 m_pFont = pFont;
130 }
131
GetFontSizeV() const132 float CPDF_TextState::TextData::GetFontSizeV() const {
133 return fabs(FXSYS_sqrt2(m_Matrix[1], m_Matrix[3]) * m_FontSize);
134 }
135
GetFontSizeH() const136 float CPDF_TextState::TextData::GetFontSizeH() const {
137 return fabs(FXSYS_sqrt2(m_Matrix[0], m_Matrix[2]) * m_FontSize);
138 }
139
GetBaselineAngle() const140 float CPDF_TextState::TextData::GetBaselineAngle() const {
141 return atan2(m_Matrix[2], m_Matrix[0]);
142 }
143
GetShearAngle() const144 float CPDF_TextState::TextData::GetShearAngle() const {
145 return GetBaselineAngle() + atan2(m_Matrix[1], m_Matrix[3]);
146 }
147
ReleaseFont()148 void CPDF_TextState::TextData::ReleaseFont() {
149 if (!m_pDocument || !m_pFont)
150 return;
151
152 CPDF_DocPageData* pPageData = m_pDocument->GetPageData();
153 if (pPageData && !pPageData->IsForceClear())
154 pPageData->ReleaseFont(m_pFont->GetFontDict());
155 }
156
SetTextRenderingModeFromInt(int iMode,TextRenderingMode * mode)157 bool SetTextRenderingModeFromInt(int iMode, TextRenderingMode* mode) {
158 if (iMode < 0 || iMode > 7)
159 return false;
160 *mode = static_cast<TextRenderingMode>(iMode);
161 return true;
162 }
163
TextRenderingModeIsClipMode(const TextRenderingMode & mode)164 bool TextRenderingModeIsClipMode(const TextRenderingMode& mode) {
165 switch (mode) {
166 case TextRenderingMode::MODE_FILL_CLIP:
167 case TextRenderingMode::MODE_STROKE_CLIP:
168 case TextRenderingMode::MODE_FILL_STROKE_CLIP:
169 case TextRenderingMode::MODE_CLIP:
170 return true;
171 default:
172 return false;
173 }
174 }
175
TextRenderingModeIsStrokeMode(const TextRenderingMode & mode)176 bool TextRenderingModeIsStrokeMode(const TextRenderingMode& mode) {
177 switch (mode) {
178 case TextRenderingMode::MODE_STROKE:
179 case TextRenderingMode::MODE_FILL_STROKE:
180 case TextRenderingMode::MODE_STROKE_CLIP:
181 case TextRenderingMode::MODE_FILL_STROKE_CLIP:
182 return true;
183 default:
184 return false;
185 }
186 }
187