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