blob: bd236a2f28d0c64452841b5e1fa8dd62c91a1349 [file] [log] [blame]
tsepez59601432016-08-29 14:26:57 -07001// 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
dsinclair41872fa2016-10-04 11:29:35 -07007#include "core/fpdfapi/page/cpdf_textstate.h"
tsepezfbda17d2016-08-30 10:32:36 -07008
dsinclairbc5e6d22016-10-04 11:08:49 -07009#include "core/fpdfapi/font/cpdf_font.h"
dsinclairfb505592016-10-20 06:44:03 -070010#include "core/fpdfapi/page/cpdf_docpagedata.h"
dsinclair488b7ad2016-10-04 11:55:50 -070011#include "core/fpdfapi/parser/cpdf_document.h"
tsepez59601432016-08-29 14:26:57 -070012
tsepezfbda17d2016-08-30 10:32:36 -070013CPDF_TextState::CPDF_TextState() {}
14CPDF_TextState::~CPDF_TextState() {}
15
16void CPDF_TextState::Emplace() {
17 m_Ref.Emplace();
18}
19
20CPDF_Font* CPDF_TextState::GetFont() const {
21 return m_Ref.GetObject()->m_pFont;
22}
23
tsepez59601432016-08-29 14:26:57 -070024void CPDF_TextState::SetFont(CPDF_Font* pFont) {
tsepezfbda17d2016-08-30 10:32:36 -070025 m_Ref.GetPrivateCopy()->SetFont(pFont);
26}
27
Dan Sinclair05df0752017-03-14 14:43:42 -040028float CPDF_TextState::GetFontSize() const {
tsepezfbda17d2016-08-30 10:32:36 -070029 return m_Ref.GetObject()->m_FontSize;
30}
31
Dan Sinclair05df0752017-03-14 14:43:42 -040032void CPDF_TextState::SetFontSize(float size) {
tsepezfbda17d2016-08-30 10:32:36 -070033 m_Ref.GetPrivateCopy()->m_FontSize = size;
34}
35
Dan Sinclair05df0752017-03-14 14:43:42 -040036const float* CPDF_TextState::GetMatrix() const {
tsepezfbda17d2016-08-30 10:32:36 -070037 return m_Ref.GetObject()->m_Matrix;
38}
39
Dan Sinclair05df0752017-03-14 14:43:42 -040040float* CPDF_TextState::GetMutableMatrix() {
tsepezfbda17d2016-08-30 10:32:36 -070041 return m_Ref.GetPrivateCopy()->m_Matrix;
42}
43
Dan Sinclair05df0752017-03-14 14:43:42 -040044float CPDF_TextState::GetCharSpace() const {
tsepezfbda17d2016-08-30 10:32:36 -070045 return m_Ref.GetObject()->m_CharSpace;
46}
47
Dan Sinclair05df0752017-03-14 14:43:42 -040048void CPDF_TextState::SetCharSpace(float sp) {
tsepezfbda17d2016-08-30 10:32:36 -070049 m_Ref.GetPrivateCopy()->m_CharSpace = sp;
50}
51
Dan Sinclair05df0752017-03-14 14:43:42 -040052float CPDF_TextState::GetWordSpace() const {
tsepezfbda17d2016-08-30 10:32:36 -070053 return m_Ref.GetObject()->m_WordSpace;
54}
55
Dan Sinclair05df0752017-03-14 14:43:42 -040056void CPDF_TextState::SetWordSpace(float sp) {
tsepezfbda17d2016-08-30 10:32:36 -070057 m_Ref.GetPrivateCopy()->m_WordSpace = sp;
tsepez59601432016-08-29 14:26:57 -070058}
59
Dan Sinclair05df0752017-03-14 14:43:42 -040060float CPDF_TextState::GetFontSizeV() const {
tsepezfbda17d2016-08-30 10:32:36 -070061 return m_Ref.GetObject()->GetFontSizeV();
tsepez59601432016-08-29 14:26:57 -070062}
63
Dan Sinclair05df0752017-03-14 14:43:42 -040064float CPDF_TextState::GetFontSizeH() const {
tsepezfbda17d2016-08-30 10:32:36 -070065 return m_Ref.GetObject()->GetFontSizeH();
tsepez59601432016-08-29 14:26:57 -070066}
67
Dan Sinclair05df0752017-03-14 14:43:42 -040068float CPDF_TextState::GetBaselineAngle() const {
tsepezfbda17d2016-08-30 10:32:36 -070069 return m_Ref.GetObject()->GetBaselineAngle();
tsepez59601432016-08-29 14:26:57 -070070}
71
Dan Sinclair05df0752017-03-14 14:43:42 -040072float CPDF_TextState::GetShearAngle() const {
tsepezfbda17d2016-08-30 10:32:36 -070073 return m_Ref.GetObject()->GetShearAngle();
74}
75
76TextRenderingMode CPDF_TextState::GetTextMode() const {
77 return m_Ref.GetObject()->m_TextMode;
78}
79
80void CPDF_TextState::SetTextMode(TextRenderingMode mode) {
81 m_Ref.GetPrivateCopy()->m_TextMode = mode;
82}
83
Dan Sinclair05df0752017-03-14 14:43:42 -040084const float* CPDF_TextState::GetCTM() const {
tsepezfbda17d2016-08-30 10:32:36 -070085 return m_Ref.GetObject()->m_CTM;
86}
87
Dan Sinclair05df0752017-03-14 14:43:42 -040088float* CPDF_TextState::GetMutableCTM() {
tsepezfbda17d2016-08-30 10:32:36 -070089 return m_Ref.GetPrivateCopy()->m_CTM;
tsepez59601432016-08-29 14:26:57 -070090}
tsepez0a765b82016-09-06 14:51:40 -070091
92CPDF_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
105CPDF_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
Lei Zhangb8a8c432017-05-04 14:10:50 -0700118 if (m_pDocument && m_pFont)
npme883afc2016-10-05 13:43:25 -0700119 m_pFont = m_pDocument->GetPageData()->GetFont(m_pFont->GetFontDict());
tsepez0a765b82016-09-06 14:51:40 -0700120}
121
122CPDF_TextState::TextData::~TextData() {
Lei Zhangb8a8c432017-05-04 14:10:50 -0700123 ReleaseFont();
tsepez0a765b82016-09-06 14:51:40 -0700124}
125
126void CPDF_TextState::TextData::SetFont(CPDF_Font* pFont) {
Lei Zhangb8a8c432017-05-04 14:10:50 -0700127 ReleaseFont();
Lei Zhangd74da7b2017-05-04 13:30:29 -0700128 m_pDocument = pFont ? pFont->GetDocument() : nullptr;
tsepez0a765b82016-09-06 14:51:40 -0700129 m_pFont = pFont;
130}
131
Dan Sinclair05df0752017-03-14 14:43:42 -0400132float CPDF_TextState::TextData::GetFontSizeV() const {
Dan Sinclair669a4182017-04-03 14:51:45 -0400133 return fabs(FXSYS_sqrt2(m_Matrix[1], m_Matrix[3]) * m_FontSize);
tsepez0a765b82016-09-06 14:51:40 -0700134}
135
Dan Sinclair05df0752017-03-14 14:43:42 -0400136float CPDF_TextState::TextData::GetFontSizeH() const {
Dan Sinclair669a4182017-04-03 14:51:45 -0400137 return fabs(FXSYS_sqrt2(m_Matrix[0], m_Matrix[2]) * m_FontSize);
tsepez0a765b82016-09-06 14:51:40 -0700138}
139
Dan Sinclair05df0752017-03-14 14:43:42 -0400140float CPDF_TextState::TextData::GetBaselineAngle() const {
Dan Sinclair669a4182017-04-03 14:51:45 -0400141 return atan2(m_Matrix[2], m_Matrix[0]);
tsepez0a765b82016-09-06 14:51:40 -0700142}
143
Dan Sinclair05df0752017-03-14 14:43:42 -0400144float CPDF_TextState::TextData::GetShearAngle() const {
Dan Sinclair669a4182017-04-03 14:51:45 -0400145 return GetBaselineAngle() + atan2(m_Matrix[1], m_Matrix[3]);
tsepez0a765b82016-09-06 14:51:40 -0700146}
147
Lei Zhangb8a8c432017-05-04 14:10:50 -0700148void 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
tsepez0a765b82016-09-06 14:51:40 -0700157bool 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
164bool 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
176bool 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}