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
13CPDF_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
24void CPDF_TextState::SetFont(CPDF_Font* pFont) {
25  m_Ref.GetPrivateCopy()->SetFont(pFont);
26}
27
28FX_FLOAT CPDF_TextState::GetFontSize() const {
29  return m_Ref.GetObject()->m_FontSize;
30}
31
32void CPDF_TextState::SetFontSize(FX_FLOAT size) {
33  m_Ref.GetPrivateCopy()->m_FontSize = size;
34}
35
36const FX_FLOAT* CPDF_TextState::GetMatrix() const {
37  return m_Ref.GetObject()->m_Matrix;
38}
39
40FX_FLOAT* CPDF_TextState::GetMutableMatrix() {
41  return m_Ref.GetPrivateCopy()->m_Matrix;
42}
43
44FX_FLOAT CPDF_TextState::GetCharSpace() const {
45  return m_Ref.GetObject()->m_CharSpace;
46}
47
48void CPDF_TextState::SetCharSpace(FX_FLOAT sp) {
49  m_Ref.GetPrivateCopy()->m_CharSpace = sp;
50}
51
52FX_FLOAT CPDF_TextState::GetWordSpace() const {
53  return m_Ref.GetObject()->m_WordSpace;
54}
55
56void CPDF_TextState::SetWordSpace(FX_FLOAT sp) {
57  m_Ref.GetPrivateCopy()->m_WordSpace = sp;
58}
59
60FX_FLOAT CPDF_TextState::GetFontSizeV() const {
61  return m_Ref.GetObject()->GetFontSizeV();
62}
63
64FX_FLOAT CPDF_TextState::GetFontSizeH() const {
65  return m_Ref.GetObject()->GetFontSizeH();
66}
67
68FX_FLOAT CPDF_TextState::GetBaselineAngle() const {
69  return m_Ref.GetObject()->GetBaselineAngle();
70}
71
72FX_FLOAT CPDF_TextState::GetShearAngle() const {
73  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
84const FX_FLOAT* CPDF_TextState::GetCTM() const {
85  return m_Ref.GetObject()->m_CTM;
86}
87
88FX_FLOAT* CPDF_TextState::GetMutableCTM() {
89  return m_Ref.GetPrivateCopy()->m_CTM;
90}
91
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
118  if (m_pDocument && m_pFont) {
119    m_pFont = m_pDocument->GetPageData()->GetFont(m_pFont->GetFontDict());
120  }
121}
122
123CPDF_TextState::TextData::~TextData() {
124  if (m_pDocument && m_pFont) {
125    CPDF_DocPageData* pPageData = m_pDocument->GetPageData();
126    if (pPageData && !pPageData->IsForceClear())
127      pPageData->ReleaseFont(m_pFont->GetFontDict());
128  }
129}
130
131void CPDF_TextState::TextData::SetFont(CPDF_Font* pFont) {
132  CPDF_Document* pDoc = m_pDocument;
133  CPDF_DocPageData* pPageData = pDoc ? pDoc->GetPageData() : nullptr;
134  if (pPageData && m_pFont && !pPageData->IsForceClear())
135    pPageData->ReleaseFont(m_pFont->GetFontDict());
136
137  m_pDocument = pFont ? pFont->m_pDocument : nullptr;
138  m_pFont = pFont;
139}
140
141FX_FLOAT CPDF_TextState::TextData::GetFontSizeV() const {
142  return FXSYS_fabs(FXSYS_sqrt2(m_Matrix[1], m_Matrix[3]) * m_FontSize);
143}
144
145FX_FLOAT CPDF_TextState::TextData::GetFontSizeH() const {
146  return FXSYS_fabs(FXSYS_sqrt2(m_Matrix[0], m_Matrix[2]) * m_FontSize);
147}
148
149FX_FLOAT CPDF_TextState::TextData::GetBaselineAngle() const {
150  return FXSYS_atan2(m_Matrix[2], m_Matrix[0]);
151}
152
153FX_FLOAT CPDF_TextState::TextData::GetShearAngle() const {
154  return GetBaselineAngle() + FXSYS_atan2(m_Matrix[1], m_Matrix[3]);
155}
156
157bool 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}
187