| OLD | NEW |
| 1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "core/fpdfdoc/include/cpdf_variabletext.h" | |
| 8 | |
| 9 #include "core/fpdfapi/fpdf_font/include/cpdf_font.h" | 7 #include "core/fpdfapi/fpdf_font/include/cpdf_font.h" |
| 10 #include "core/fpdfdoc/cpvt_wordinfo.h" | 8 #include "core/fpdfdoc/cpvt_wordinfo.h" |
| 11 #include "core/fpdfdoc/csection.h" | 9 #include "core/fpdfdoc/csection.h" |
| 10 #include "core/fpdfdoc/include/cpdf_variabletext.h" |
| 12 #include "core/fpdfdoc/include/cpvt_section.h" | 11 #include "core/fpdfdoc/include/cpvt_section.h" |
| 13 #include "core/fpdfdoc/include/cpvt_word.h" | 12 #include "core/fpdfdoc/include/cpvt_word.h" |
| 14 #include "core/fpdfdoc/include/ipvt_fontmap.h" | 13 #include "core/fpdfdoc/include/ipvt_fontmap.h" |
| 15 | 14 |
| 16 namespace { | 15 namespace { |
| 17 | 16 |
| 18 const float kDefaultFontSize = 18.0f; | 17 const float kDefaultFontSize = 18.0f; |
| 19 const float kFontScale = 0.001f; | 18 const float kFontScale = 0.001f; |
| 20 const uint8_t kReturnLength = 1; | 19 const uint8_t kReturnLength = 1; |
| 21 const float kScalePercent = 0.01f; | 20 const float kScalePercent = 0.01f; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 return 0; | 63 return 0; |
| 65 } | 64 } |
| 66 if (CPDF_Font* pSysFont = m_pFontMap->GetPDFFont(1)) { | 65 if (CPDF_Font* pSysFont = m_pFontMap->GetPDFFont(1)) { |
| 67 if (pSysFont->CharCodeFromUnicode(word) != CPDF_Font::kInvalidCharCode) | 66 if (pSysFont->CharCodeFromUnicode(word) != CPDF_Font::kInvalidCharCode) |
| 68 return 1; | 67 return 1; |
| 69 } | 68 } |
| 70 return -1; | 69 return -1; |
| 71 } | 70 } |
| 72 | 71 |
| 73 FX_BOOL CPDF_VariableText::Provider::IsLatinWord(uint16_t word) { | 72 FX_BOOL CPDF_VariableText::Provider::IsLatinWord(uint16_t word) { |
| 74 return (word >= 0x61 && word <= 0x7A) || (word >= 0x41 && word <= 0x5A) || | 73 if ((word >= 0x61 && word <= 0x7A) || (word >= 0x41 && word <= 0x5A) || |
| 75 word == 0x2D || word == 0x27; | 74 word == 0x2D || word == 0x27) { |
| 75 return TRUE; |
| 76 } |
| 77 return FALSE; |
| 76 } | 78 } |
| 77 | 79 |
| 78 int32_t CPDF_VariableText::Provider::GetDefaultFontIndex() { | 80 int32_t CPDF_VariableText::Provider::GetDefaultFontIndex() { |
| 79 return 0; | 81 return 0; |
| 80 } | 82 } |
| 81 | 83 |
| 82 CPDF_VariableText::Iterator::Iterator(CPDF_VariableText* pVT) | 84 CPDF_VariableText::Iterator::Iterator(CPDF_VariableText* pVT) |
| 83 : m_CurPos(-1, -1, -1), m_pVT(pVT) {} | 85 : m_CurPos(-1, -1, -1), m_pVT(pVT) {} |
| 84 | 86 |
| 85 CPDF_VariableText::Iterator::~Iterator() {} | 87 CPDF_VariableText::Iterator::~Iterator() {} |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 ASSERT(m_pVT); | 157 ASSERT(m_pVT); |
| 156 if (m_CurPos.nSecIndex > 0) { | 158 if (m_CurPos.nSecIndex > 0) { |
| 157 m_CurPos = CPVT_WordPlace(m_CurPos.nSecIndex - 1, 0, -1); | 159 m_CurPos = CPVT_WordPlace(m_CurPos.nSecIndex - 1, 0, -1); |
| 158 return TRUE; | 160 return TRUE; |
| 159 } | 161 } |
| 160 return FALSE; | 162 return FALSE; |
| 161 } | 163 } |
| 162 | 164 |
| 163 FX_BOOL CPDF_VariableText::Iterator::GetWord(CPVT_Word& word) const { | 165 FX_BOOL CPDF_VariableText::Iterator::GetWord(CPVT_Word& word) const { |
| 164 word.WordPlace = m_CurPos; | 166 word.WordPlace = m_CurPos; |
| 165 CSection* pSection = m_pVT->m_SectionArray.GetAt(m_CurPos.nSecIndex); | 167 if (CSection* pSection = m_pVT->m_SectionArray.GetAt(m_CurPos.nSecIndex)) { |
| 166 if (!pSection) | 168 if (pSection->m_LineArray.GetAt(m_CurPos.nLineIndex)) { |
| 167 return FALSE; | 169 if (CPVT_WordInfo* pWord = |
| 170 pSection->m_WordArray.GetAt(m_CurPos.nWordIndex)) { |
| 171 word.Word = pWord->Word; |
| 172 word.nCharset = pWord->nCharset; |
| 173 word.fWidth = m_pVT->GetWordWidth(*pWord); |
| 174 word.ptWord = m_pVT->InToOut( |
| 175 CFX_FloatPoint(pWord->fWordX + pSection->m_SecInfo.rcSection.left, |
| 176 pWord->fWordY + pSection->m_SecInfo.rcSection.top)); |
| 177 word.fAscent = m_pVT->GetWordAscent(*pWord); |
| 178 word.fDescent = m_pVT->GetWordDescent(*pWord); |
| 179 if (pWord->pWordProps) |
| 180 word.WordProps = *pWord->pWordProps; |
| 168 | 181 |
| 169 if (!pSection->m_LineArray.GetAt(m_CurPos.nLineIndex)) | 182 word.nFontIndex = m_pVT->GetWordFontIndex(*pWord); |
| 170 return FALSE; | 183 word.fFontSize = m_pVT->GetWordFontSize(*pWord); |
| 171 | 184 return TRUE; |
| 172 if (m_CurPos.nWordIndex < 0 || | 185 } |
| 173 m_CurPos.nWordIndex >= pSection->m_WordArray.GetSize()) { | 186 } |
| 174 return FALSE; | |
| 175 } | 187 } |
| 176 | 188 return FALSE; |
| 177 CPVT_WordInfo* pWord = pSection->m_WordArray.GetAt(m_CurPos.nWordIndex); | |
| 178 if (!pWord) | |
| 179 return FALSE; | |
| 180 | |
| 181 word.Word = pWord->Word; | |
| 182 word.nCharset = pWord->nCharset; | |
| 183 word.fWidth = m_pVT->GetWordWidth(*pWord); | |
| 184 word.ptWord = m_pVT->InToOut( | |
| 185 CFX_FloatPoint(pWord->fWordX + pSection->m_SecInfo.rcSection.left, | |
| 186 pWord->fWordY + pSection->m_SecInfo.rcSection.top)); | |
| 187 word.fAscent = m_pVT->GetWordAscent(*pWord); | |
| 188 word.fDescent = m_pVT->GetWordDescent(*pWord); | |
| 189 if (pWord->pWordProps) | |
| 190 word.WordProps = *pWord->pWordProps; | |
| 191 | |
| 192 word.nFontIndex = m_pVT->GetWordFontIndex(*pWord); | |
| 193 word.fFontSize = m_pVT->GetWordFontSize(*pWord); | |
| 194 return TRUE; | |
| 195 } | 189 } |
| 196 | 190 |
| 197 FX_BOOL CPDF_VariableText::Iterator::SetWord(const CPVT_Word& word) { | 191 FX_BOOL CPDF_VariableText::Iterator::SetWord(const CPVT_Word& word) { |
| 198 if (CSection* pSection = m_pVT->m_SectionArray.GetAt(m_CurPos.nSecIndex)) { | 192 if (CSection* pSection = m_pVT->m_SectionArray.GetAt(m_CurPos.nSecIndex)) { |
| 199 if (CPVT_WordInfo* pWord = | 193 if (CPVT_WordInfo* pWord = |
| 200 pSection->m_WordArray.GetAt(m_CurPos.nWordIndex)) { | 194 pSection->m_WordArray.GetAt(m_CurPos.nWordIndex)) { |
| 201 if (pWord->pWordProps) | 195 if (pWord->pWordProps) |
| 202 *pWord->pWordProps = word.WordProps; | 196 *pWord->pWordProps = word.WordProps; |
| 203 return TRUE; | 197 return TRUE; |
| 204 } | 198 } |
| (...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 728 if (nSecIndex == m_SectionArray.GetSize()) | 722 if (nSecIndex == m_SectionArray.GetSize()) |
| 729 m_SectionArray.Add(pSection); | 723 m_SectionArray.Add(pSection); |
| 730 else | 724 else |
| 731 m_SectionArray.InsertAt(nSecIndex, pSection); | 725 m_SectionArray.InsertAt(nSecIndex, pSection); |
| 732 | 726 |
| 733 return place; | 727 return place; |
| 734 } | 728 } |
| 735 | 729 |
| 736 CPVT_WordPlace CPDF_VariableText::AddLine(const CPVT_WordPlace& place, | 730 CPVT_WordPlace CPDF_VariableText::AddLine(const CPVT_WordPlace& place, |
| 737 const CPVT_LineInfo& lineinfo) { | 731 const CPVT_LineInfo& lineinfo) { |
| 738 if (m_SectionArray.GetSize() == 0) | 732 if (m_SectionArray.IsEmpty()) |
| 739 return place; | 733 return place; |
| 740 if (CSection* pSection = m_SectionArray.GetAt(place.nSecIndex)) | 734 if (CSection* pSection = m_SectionArray.GetAt(place.nSecIndex)) |
| 741 return pSection->AddLine(lineinfo); | 735 return pSection->AddLine(lineinfo); |
| 742 return place; | 736 return place; |
| 743 } | 737 } |
| 744 | 738 |
| 745 CPVT_WordPlace CPDF_VariableText::AddWord(const CPVT_WordPlace& place, | 739 CPVT_WordPlace CPDF_VariableText::AddWord(const CPVT_WordPlace& place, |
| 746 const CPVT_WordInfo& wordinfo) { | 740 const CPVT_WordInfo& wordinfo) { |
| 747 if (m_SectionArray.GetSize() <= 0) | 741 if (m_SectionArray.GetSize() <= 0) { |
| 748 return place; | 742 return place; |
| 749 | 743 } |
| 750 CPVT_WordPlace newplace = place; | 744 CPVT_WordPlace newplace = place; |
| 751 newplace.nSecIndex = | 745 newplace.nSecIndex = |
| 752 std::max(std::min(newplace.nSecIndex, m_SectionArray.GetSize() - 1), 0); | 746 std::max(std::min(newplace.nSecIndex, m_SectionArray.GetSize() - 1), 0); |
| 753 if (CSection* pSection = m_SectionArray.GetAt(newplace.nSecIndex)) | 747 if (CSection* pSection = m_SectionArray.GetAt(newplace.nSecIndex)) |
| 754 return pSection->AddWord(newplace, wordinfo); | 748 return pSection->AddWord(newplace, wordinfo); |
| 755 return place; | 749 return place; |
| 756 } | 750 } |
| 757 | 751 |
| 758 FX_BOOL CPDF_VariableText::GetWordInfo(const CPVT_WordPlace& place, | 752 FX_BOOL CPDF_VariableText::GetWordInfo(const CPVT_WordPlace& place, |
| 759 CPVT_WordInfo& wordinfo) { | 753 CPVT_WordInfo& wordinfo) { |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1052 continue; | 1046 continue; |
| 1053 } else { | 1047 } else { |
| 1054 nLeft = nMid + 1; | 1048 nLeft = nMid + 1; |
| 1055 nMid = (nLeft + nRight) / 2; | 1049 nMid = (nLeft + nRight) / 2; |
| 1056 continue; | 1050 continue; |
| 1057 } | 1051 } |
| 1058 } | 1052 } |
| 1059 return (FX_FLOAT)gFontSizeSteps[nMid]; | 1053 return (FX_FLOAT)gFontSizeSteps[nMid]; |
| 1060 } | 1054 } |
| 1061 | 1055 |
| 1062 bool CPDF_VariableText::IsBigger(FX_FLOAT fFontSize) const { | 1056 FX_BOOL CPDF_VariableText::IsBigger(FX_FLOAT fFontSize) { |
| 1063 CFX_PointF szTotal; | 1057 FX_BOOL bBigger = FALSE; |
| 1058 CPVT_Size szTotal; |
| 1064 for (int32_t s = 0, sz = m_SectionArray.GetSize(); s < sz; s++) { | 1059 for (int32_t s = 0, sz = m_SectionArray.GetSize(); s < sz; s++) { |
| 1065 if (CSection* pSection = m_SectionArray.GetAt(s)) { | 1060 if (CSection* pSection = m_SectionArray.GetAt(s)) { |
| 1066 CFX_PointF size = pSection->GetSectionSize(fFontSize); | 1061 CPVT_Size size = pSection->GetSectionSize(fFontSize); |
| 1067 szTotal.x = std::max(size.x, szTotal.x); | 1062 szTotal.x = std::max(size.x, szTotal.x); |
| 1068 szTotal.y += size.y; | 1063 szTotal.y += size.y; |
| 1069 if (IsFloatBigger(szTotal.x, GetPlateWidth()) || | 1064 if (IsFloatBigger(szTotal.x, GetPlateWidth()) || |
| 1070 IsFloatBigger(szTotal.y, GetPlateHeight())) { | 1065 IsFloatBigger(szTotal.y, GetPlateHeight())) { |
| 1071 return true; | 1066 bBigger = TRUE; |
| 1067 break; |
| 1072 } | 1068 } |
| 1073 } | 1069 } |
| 1074 } | 1070 } |
| 1075 return false; | 1071 return bBigger; |
| 1076 } | 1072 } |
| 1077 | 1073 |
| 1078 CPVT_FloatRect CPDF_VariableText::RearrangeSections( | 1074 CPVT_FloatRect CPDF_VariableText::RearrangeSections( |
| 1079 const CPVT_WordRange& PlaceRange) { | 1075 const CPVT_WordRange& PlaceRange) { |
| 1080 CPVT_WordPlace place; | 1076 CPVT_WordPlace place; |
| 1081 FX_FLOAT fPosY = 0; | 1077 FX_FLOAT fPosY = 0; |
| 1082 FX_FLOAT fOldHeight; | 1078 FX_FLOAT fOldHeight; |
| 1083 int32_t nSSecIndex = PlaceRange.BeginPos.nSecIndex; | 1079 int32_t nSSecIndex = PlaceRange.BeginPos.nSecIndex; |
| 1084 int32_t nESecIndex = PlaceRange.EndPos.nSecIndex; | 1080 int32_t nESecIndex = PlaceRange.EndPos.nSecIndex; |
| 1085 CPVT_FloatRect rcRet; | 1081 CPVT_FloatRect rcRet; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1156 m_pVTIterator = new CPDF_VariableText::Iterator(this); | 1152 m_pVTIterator = new CPDF_VariableText::Iterator(this); |
| 1157 return m_pVTIterator; | 1153 return m_pVTIterator; |
| 1158 } | 1154 } |
| 1159 | 1155 |
| 1160 CPDF_VariableText::Provider* CPDF_VariableText::SetProvider( | 1156 CPDF_VariableText::Provider* CPDF_VariableText::SetProvider( |
| 1161 CPDF_VariableText::Provider* pProvider) { | 1157 CPDF_VariableText::Provider* pProvider) { |
| 1162 CPDF_VariableText::Provider* pOld = m_pVTProvider; | 1158 CPDF_VariableText::Provider* pOld = m_pVTProvider; |
| 1163 m_pVTProvider = pProvider; | 1159 m_pVTProvider = pProvider; |
| 1164 return pOld; | 1160 return pOld; |
| 1165 } | 1161 } |
| OLD | NEW |