| tsepez | 5960143 | 2016-08-29 14:26:57 -0700 | [diff] [blame] | 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 | |
| dsinclair | 41872fa | 2016-10-04 11:29:35 -0700 | [diff] [blame] | 7 | #include "core/fpdfapi/page/cpdf_textstate.h" |
| tsepez | fbda17d | 2016-08-30 10:32:36 -0700 | [diff] [blame] | 8 | |
| dsinclair | bc5e6d2 | 2016-10-04 11:08:49 -0700 | [diff] [blame] | 9 | #include "core/fpdfapi/font/cpdf_font.h" |
| dsinclair | fb50559 | 2016-10-20 06:44:03 -0700 | [diff] [blame] | 10 | #include "core/fpdfapi/page/cpdf_docpagedata.h" |
| dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 11 | #include "core/fpdfapi/parser/cpdf_document.h" |
| tsepez | 5960143 | 2016-08-29 14:26:57 -0700 | [diff] [blame] | 12 | |
| tsepez | fbda17d | 2016-08-30 10:32:36 -0700 | [diff] [blame] | 13 | CPDF_TextState::CPDF_TextState() {} |
| 14 | CPDF_TextState::~CPDF_TextState() {} |
| 15 | |
| 16 | void CPDF_TextState::Emplace() { |
| 17 | m_Ref.Emplace(); |
| 18 | } |
| 19 | |
| 20 | CPDF_Font* CPDF_TextState::GetFont() const { |
| 21 | return m_Ref.GetObject()->m_pFont; |
| 22 | } |
| 23 | |
| tsepez | 5960143 | 2016-08-29 14:26:57 -0700 | [diff] [blame] | 24 | void CPDF_TextState::SetFont(CPDF_Font* pFont) { |
| tsepez | fbda17d | 2016-08-30 10:32:36 -0700 | [diff] [blame] | 25 | m_Ref.GetPrivateCopy()->SetFont(pFont); |
| 26 | } |
| 27 | |
| Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 28 | float CPDF_TextState::GetFontSize() const { |
| tsepez | fbda17d | 2016-08-30 10:32:36 -0700 | [diff] [blame] | 29 | return m_Ref.GetObject()->m_FontSize; |
| 30 | } |
| 31 | |
| Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 32 | void CPDF_TextState::SetFontSize(float size) { |
| tsepez | fbda17d | 2016-08-30 10:32:36 -0700 | [diff] [blame] | 33 | m_Ref.GetPrivateCopy()->m_FontSize = size; |
| 34 | } |
| 35 | |
| Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 36 | const float* CPDF_TextState::GetMatrix() const { |
| tsepez | fbda17d | 2016-08-30 10:32:36 -0700 | [diff] [blame] | 37 | return m_Ref.GetObject()->m_Matrix; |
| 38 | } |
| 39 | |
| Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 40 | float* CPDF_TextState::GetMutableMatrix() { |
| tsepez | fbda17d | 2016-08-30 10:32:36 -0700 | [diff] [blame] | 41 | return m_Ref.GetPrivateCopy()->m_Matrix; |
| 42 | } |
| 43 | |
| Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 44 | float CPDF_TextState::GetCharSpace() const { |
| tsepez | fbda17d | 2016-08-30 10:32:36 -0700 | [diff] [blame] | 45 | return m_Ref.GetObject()->m_CharSpace; |
| 46 | } |
| 47 | |
| Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 48 | void CPDF_TextState::SetCharSpace(float sp) { |
| tsepez | fbda17d | 2016-08-30 10:32:36 -0700 | [diff] [blame] | 49 | m_Ref.GetPrivateCopy()->m_CharSpace = sp; |
| 50 | } |
| 51 | |
| Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 52 | float CPDF_TextState::GetWordSpace() const { |
| tsepez | fbda17d | 2016-08-30 10:32:36 -0700 | [diff] [blame] | 53 | return m_Ref.GetObject()->m_WordSpace; |
| 54 | } |
| 55 | |
| Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 56 | void CPDF_TextState::SetWordSpace(float sp) { |
| tsepez | fbda17d | 2016-08-30 10:32:36 -0700 | [diff] [blame] | 57 | m_Ref.GetPrivateCopy()->m_WordSpace = sp; |
| tsepez | 5960143 | 2016-08-29 14:26:57 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 60 | float CPDF_TextState::GetFontSizeV() const { |
| tsepez | fbda17d | 2016-08-30 10:32:36 -0700 | [diff] [blame] | 61 | return m_Ref.GetObject()->GetFontSizeV(); |
| tsepez | 5960143 | 2016-08-29 14:26:57 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 64 | float CPDF_TextState::GetFontSizeH() const { |
| tsepez | fbda17d | 2016-08-30 10:32:36 -0700 | [diff] [blame] | 65 | return m_Ref.GetObject()->GetFontSizeH(); |
| tsepez | 5960143 | 2016-08-29 14:26:57 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 68 | float CPDF_TextState::GetBaselineAngle() const { |
| tsepez | fbda17d | 2016-08-30 10:32:36 -0700 | [diff] [blame] | 69 | return m_Ref.GetObject()->GetBaselineAngle(); |
| tsepez | 5960143 | 2016-08-29 14:26:57 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 72 | float CPDF_TextState::GetShearAngle() const { |
| tsepez | fbda17d | 2016-08-30 10:32:36 -0700 | [diff] [blame] | 73 | return m_Ref.GetObject()->GetShearAngle(); |
| 74 | } |
| 75 | |
| 76 | TextRenderingMode CPDF_TextState::GetTextMode() const { |
| 77 | return m_Ref.GetObject()->m_TextMode; |
| 78 | } |
| 79 | |
| 80 | void CPDF_TextState::SetTextMode(TextRenderingMode mode) { |
| 81 | m_Ref.GetPrivateCopy()->m_TextMode = mode; |
| 82 | } |
| 83 | |
| Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 84 | const float* CPDF_TextState::GetCTM() const { |
| tsepez | fbda17d | 2016-08-30 10:32:36 -0700 | [diff] [blame] | 85 | return m_Ref.GetObject()->m_CTM; |
| 86 | } |
| 87 | |
| Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 88 | float* CPDF_TextState::GetMutableCTM() { |
| tsepez | fbda17d | 2016-08-30 10:32:36 -0700 | [diff] [blame] | 89 | return m_Ref.GetPrivateCopy()->m_CTM; |
| tsepez | 5960143 | 2016-08-29 14:26:57 -0700 | [diff] [blame] | 90 | } |
| tsepez | 0a765b8 | 2016-09-06 14:51:40 -0700 | [diff] [blame] | 91 | |
| 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 | |
| 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 | |
| Lei Zhang | b8a8c43 | 2017-05-04 14:10:50 -0700 | [diff] [blame] | 118 | if (m_pDocument && m_pFont) |
| npm | e883afc | 2016-10-05 13:43:25 -0700 | [diff] [blame] | 119 | m_pFont = m_pDocument->GetPageData()->GetFont(m_pFont->GetFontDict()); |
| tsepez | 0a765b8 | 2016-09-06 14:51:40 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | CPDF_TextState::TextData::~TextData() { |
| Lei Zhang | b8a8c43 | 2017-05-04 14:10:50 -0700 | [diff] [blame] | 123 | ReleaseFont(); |
| tsepez | 0a765b8 | 2016-09-06 14:51:40 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void CPDF_TextState::TextData::SetFont(CPDF_Font* pFont) { |
| Lei Zhang | b8a8c43 | 2017-05-04 14:10:50 -0700 | [diff] [blame] | 127 | ReleaseFont(); |
| Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 128 | m_pDocument = pFont ? pFont->GetDocument() : nullptr; |
| tsepez | 0a765b8 | 2016-09-06 14:51:40 -0700 | [diff] [blame] | 129 | m_pFont = pFont; |
| 130 | } |
| 131 | |
| Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 132 | float CPDF_TextState::TextData::GetFontSizeV() const { |
| Dan Sinclair | 669a418 | 2017-04-03 14:51:45 -0400 | [diff] [blame] | 133 | return fabs(FXSYS_sqrt2(m_Matrix[1], m_Matrix[3]) * m_FontSize); |
| tsepez | 0a765b8 | 2016-09-06 14:51:40 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 136 | float CPDF_TextState::TextData::GetFontSizeH() const { |
| Dan Sinclair | 669a418 | 2017-04-03 14:51:45 -0400 | [diff] [blame] | 137 | return fabs(FXSYS_sqrt2(m_Matrix[0], m_Matrix[2]) * m_FontSize); |
| tsepez | 0a765b8 | 2016-09-06 14:51:40 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 140 | float CPDF_TextState::TextData::GetBaselineAngle() const { |
| Dan Sinclair | 669a418 | 2017-04-03 14:51:45 -0400 | [diff] [blame] | 141 | return atan2(m_Matrix[2], m_Matrix[0]); |
| tsepez | 0a765b8 | 2016-09-06 14:51:40 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 144 | float CPDF_TextState::TextData::GetShearAngle() const { |
| Dan Sinclair | 669a418 | 2017-04-03 14:51:45 -0400 | [diff] [blame] | 145 | return GetBaselineAngle() + atan2(m_Matrix[1], m_Matrix[3]); |
| tsepez | 0a765b8 | 2016-09-06 14:51:40 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| Lei Zhang | b8a8c43 | 2017-05-04 14:10:50 -0700 | [diff] [blame] | 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 | |
| tsepez | 0a765b8 | 2016-09-06 14:51:40 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 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 | } |