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(const CPDF_TextState&) = default;
19
20 CPDF_TextState& CPDF_TextState::operator=(const CPDF_TextState&) = default;
21
22 CPDF_TextState::~CPDF_TextState() = default;
23
Emplace()24 void CPDF_TextState::Emplace() {
25 m_Ref.Emplace();
26 }
27
GetFont() const28 RetainPtr<CPDF_Font> CPDF_TextState::GetFont() const {
29 return m_Ref.GetObject()->font_;
30 }
31
SetFont(RetainPtr<CPDF_Font> pFont)32 void CPDF_TextState::SetFont(RetainPtr<CPDF_Font> pFont) {
33 m_Ref.GetPrivateCopy()->SetFont(std::move(pFont));
34 }
35
GetFontSize() const36 float CPDF_TextState::GetFontSize() const {
37 return m_Ref.GetObject()->font_size_;
38 }
39
SetFontSize(float size)40 void CPDF_TextState::SetFontSize(float size) {
41 if (!m_Ref || GetFontSize() != size) {
42 m_Ref.GetPrivateCopy()->font_size_ = size;
43 }
44 }
45
GetMatrix() const46 pdfium::span<const float> CPDF_TextState::GetMatrix() const {
47 return m_Ref.GetObject()->matrix_;
48 }
49
GetMutableMatrix()50 pdfium::span<float> CPDF_TextState::GetMutableMatrix() {
51 return m_Ref.GetPrivateCopy()->matrix_;
52 }
53
GetCharSpace() const54 float CPDF_TextState::GetCharSpace() const {
55 return m_Ref.GetObject()->char_space_;
56 }
57
SetCharSpace(float sp)58 void CPDF_TextState::SetCharSpace(float sp) {
59 if (!m_Ref || GetCharSpace() != sp) {
60 m_Ref.GetPrivateCopy()->char_space_ = sp;
61 }
62 }
63
GetWordSpace() const64 float CPDF_TextState::GetWordSpace() const {
65 return m_Ref.GetObject()->word_space_;
66 }
67
SetWordSpace(float sp)68 void CPDF_TextState::SetWordSpace(float sp) {
69 if (!m_Ref || GetWordSpace() != sp) {
70 m_Ref.GetPrivateCopy()->word_space_ = sp;
71 }
72 }
73
GetFontSizeH() const74 float CPDF_TextState::GetFontSizeH() const {
75 return m_Ref.GetObject()->GetFontSizeH();
76 }
77
GetTextMode() const78 TextRenderingMode CPDF_TextState::GetTextMode() const {
79 return m_Ref.GetObject()->text_rendering_mode_;
80 }
81
SetTextMode(TextRenderingMode mode)82 void CPDF_TextState::SetTextMode(TextRenderingMode mode) {
83 if (!m_Ref || GetTextMode() != mode) {
84 m_Ref.GetPrivateCopy()->text_rendering_mode_ = mode;
85 }
86 }
87
GetCTM() const88 pdfium::span<const float> CPDF_TextState::GetCTM() const {
89 return m_Ref.GetObject()->ctm_;
90 }
91
GetMutableCTM()92 pdfium::span<float> CPDF_TextState::GetMutableCTM() {
93 return m_Ref.GetPrivateCopy()->ctm_;
94 }
95
96 CPDF_TextState::TextData::TextData() = default;
97
TextData(const TextData & that)98 CPDF_TextState::TextData::TextData(const TextData& that)
99 : font_(that.font_),
100 document_(that.document_),
101 font_size_(that.font_size_),
102 char_space_(that.char_space_),
103 word_space_(that.word_space_),
104 text_rendering_mode_(that.text_rendering_mode_),
105 matrix_(that.matrix_),
106 ctm_(that.ctm_) {
107 if (document_ && font_) {
108 auto* page_data = CPDF_DocPageData::FromDocument(document_);
109 font_ = page_data->GetFont(font_->GetMutableFontDict());
110 }
111 }
112
113 CPDF_TextState::TextData::~TextData() = default;
114
Clone() const115 RetainPtr<CPDF_TextState::TextData> CPDF_TextState::TextData::Clone() const {
116 return pdfium::MakeRetain<CPDF_TextState::TextData>(*this);
117 }
118
SetFont(RetainPtr<CPDF_Font> pFont)119 void CPDF_TextState::TextData::SetFont(RetainPtr<CPDF_Font> pFont) {
120 document_ = pFont ? pFont->GetDocument() : nullptr;
121 font_ = std::move(pFont);
122 }
123
GetFontSizeH() const124 float CPDF_TextState::TextData::GetFontSizeH() const {
125 return fabs(hypotf(matrix_[0], matrix_[2]) * font_size_);
126 }
127
SetTextRenderingModeFromInt(int iMode,TextRenderingMode * mode)128 bool SetTextRenderingModeFromInt(int iMode, TextRenderingMode* mode) {
129 if (iMode < 0 || iMode > 7)
130 return false;
131 *mode = static_cast<TextRenderingMode>(iMode);
132 return true;
133 }
134
TextRenderingModeIsClipMode(const TextRenderingMode & mode)135 bool TextRenderingModeIsClipMode(const TextRenderingMode& mode) {
136 switch (mode) {
137 case TextRenderingMode::MODE_FILL_CLIP:
138 case TextRenderingMode::MODE_STROKE_CLIP:
139 case TextRenderingMode::MODE_FILL_STROKE_CLIP:
140 case TextRenderingMode::MODE_CLIP:
141 return true;
142 default:
143 return false;
144 }
145 }
146
TextRenderingModeIsStrokeMode(const TextRenderingMode & mode)147 bool TextRenderingModeIsStrokeMode(const TextRenderingMode& mode) {
148 switch (mode) {
149 case TextRenderingMode::MODE_STROKE:
150 case TextRenderingMode::MODE_FILL_STROKE:
151 case TextRenderingMode::MODE_STROKE_CLIP:
152 case TextRenderingMode::MODE_FILL_STROKE_CLIP:
153 return true;
154 default:
155 return false;
156 }
157 }
158