Index: core/fpdfdoc/csection.cpp |
diff --git a/core/fpdfdoc/csection.cpp b/core/fpdfdoc/csection.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6778005ccab7fa228c27fac8df2181a83e98613b |
--- /dev/null |
+++ b/core/fpdfdoc/csection.cpp |
@@ -0,0 +1,295 @@ |
+// Copyright 2016 PDFium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
+ |
+#include "core/fpdfdoc/csection.h" |
+ |
+#include <algorithm> |
+ |
+#include "core/fpdfdoc/cpvt_wordinfo.h" |
+#include "core/fpdfdoc/ctypeset.h" |
+#include "third_party/base/stl_util.h" |
+ |
+CSection::CSection(CPDF_VariableText* pVT) : m_pVT(pVT) {} |
+ |
+CSection::~CSection() { |
+ ResetAll(); |
+} |
+ |
+void CSection::ResetAll() { |
dsinclair
2016/05/16 14:12:27
nit: Just Reset()?
|
+ ResetWordArray(); |
+ ResetLineArray(); |
+} |
+ |
+void CSection::ResetLineArray() { |
dsinclair
2016/05/16 14:12:27
nit: ResetLines(), Array is an implmentation detai
|
+ m_LineArray.RemoveAll(); |
+} |
+ |
+void CSection::ResetWordArray() { |
dsinclair
2016/05/16 14:12:27
ditto
|
+ m_WordArray.clear(); |
+} |
+ |
+void CSection::ResetLinePlace() { |
+ for (int32_t i = 0, sz = m_LineArray.GetSize(); i < sz; i++) { |
+ if (CLine* pLine = m_LineArray.GetAt(i)) { |
+ pLine->LinePlace = CPVT_WordPlace(SecPlace.nSecIndex, i, -1); |
+ } |
+ } |
+} |
+ |
+CPVT_WordPlace CSection::AddWord(const CPVT_WordPlace& place, |
+ const CPVT_WordInfo& wordinfo) { |
+ std::unique_ptr<CPVT_WordInfo> pWord(new CPVT_WordInfo(wordinfo)); |
+ int32_t nSize = GetNumberOfWords(); |
+ int32_t nWordIndex = std::max(std::min(place.nWordIndex, nSize), 0); |
+ if (nWordIndex == nSize) |
+ m_WordArray.push_back(std::move(pWord)); |
+ else |
+ m_WordArray.insert(m_WordArray.begin() + nWordIndex, std::move(pWord)); |
+ return place; |
+} |
+ |
+CPVT_WordPlace CSection::AddLine(const CPVT_LineInfo& lineinfo) { |
+ return CPVT_WordPlace(SecPlace.nSecIndex, m_LineArray.Add(lineinfo), -1); |
+} |
+ |
+CPVT_FloatRect CSection::Rearrange() { |
+ if (m_pVT->m_nCharArray > 0) |
+ return CTypeset(this).CharArray(); |
+ return CTypeset(this).Typeset(); |
+} |
+ |
+CFX_PointF CSection::GetSectionSize(FX_FLOAT fFontSize) { |
+ return CTypeset(this).GetEditSize(fFontSize); |
+} |
+ |
+CPVT_WordPlace CSection::GetBeginWordPlace() const { |
+ if (CLine* pLine = m_LineArray.GetAt(0)) |
+ return pLine->GetBeginWordPlace(); |
+ return SecPlace; |
+} |
+ |
+CPVT_WordPlace CSection::GetEndWordPlace() const { |
+ if (CLine* pLine = m_LineArray.GetAt(m_LineArray.GetSize() - 1)) |
+ return pLine->GetEndWordPlace(); |
+ return SecPlace; |
+} |
+ |
+CPVT_WordPlace CSection::GetPrevWordPlace(const CPVT_WordPlace& place) const { |
+ if (place.nLineIndex < 0) |
+ return GetBeginWordPlace(); |
+ |
+ if (place.nLineIndex >= m_LineArray.GetSize()) |
+ return GetEndWordPlace(); |
+ |
+ if (CLine* pLine = m_LineArray.GetAt(place.nLineIndex)) { |
+ if (place.nWordIndex == pLine->m_LineInfo.nBeginWordIndex) |
+ return CPVT_WordPlace(place.nSecIndex, place.nLineIndex, -1); |
+ |
+ if (place.nWordIndex >= pLine->m_LineInfo.nBeginWordIndex) |
+ return pLine->GetPrevWordPlace(place); |
+ |
+ if (CLine* pPrevLine = m_LineArray.GetAt(place.nLineIndex - 1)) |
+ return pPrevLine->GetEndWordPlace(); |
+ } |
+ return place; |
+} |
+ |
+CPVT_WordPlace CSection::GetNextWordPlace(const CPVT_WordPlace& place) const { |
+ if (place.nLineIndex < 0) |
+ return GetBeginWordPlace(); |
+ |
+ if (place.nLineIndex >= m_LineArray.GetSize()) |
+ return GetEndWordPlace(); |
+ |
+ if (CLine* pLine = m_LineArray.GetAt(place.nLineIndex)) { |
+ if (place.nWordIndex < pLine->m_LineInfo.nEndWordIndex) |
+ return pLine->GetNextWordPlace(place); |
+ |
+ if (CLine* pNextLine = m_LineArray.GetAt(place.nLineIndex + 1)) |
+ return pNextLine->GetBeginWordPlace(); |
+ } |
+ return place; |
+} |
+ |
+void CSection::UpdateWordPlace(CPVT_WordPlace& place) const { |
+ int32_t nLeft = 0; |
+ int32_t nRight = m_LineArray.GetSize() - 1; |
+ int32_t nMid = (nLeft + nRight) / 2; |
+ while (nLeft <= nRight) { |
+ CLine* pLine = m_LineArray.GetAt(nMid); |
+ if (!pLine) |
+ return; |
+ |
+ if (place.nWordIndex == pLine->m_LineInfo.nBeginWordIndex) { |
+ place.nLineIndex = nMid; |
+ return; |
+ } |
+ |
+ if (place.nWordIndex < pLine->m_LineInfo.nBeginWordIndex) |
+ nRight = nMid - 1; |
+ else |
+ nLeft = nMid + 1; |
+ nMid = (nLeft + nRight) / 2; |
+ } |
+} |
+ |
+CPVT_WordPlace CSection::SearchWordPlace(const CFX_FloatPoint& point) const { |
+ CPVT_WordPlace place = GetBeginWordPlace(); |
+ FX_BOOL bUp = TRUE; |
+ FX_BOOL bDown = TRUE; |
+ int32_t nLeft = 0; |
+ int32_t nRight = m_LineArray.GetSize() - 1; |
+ int32_t nMid = m_LineArray.GetSize() / 2; |
+ FX_FLOAT fTop = 0; |
+ FX_FLOAT fBottom = 0; |
+ while (nLeft <= nRight) { |
+ if (CLine* pLine = m_LineArray.GetAt(nMid)) { |
+ fTop = pLine->m_LineInfo.fLineY - pLine->m_LineInfo.fLineAscent - |
+ m_pVT->GetLineLeading(m_SecInfo); |
+ fBottom = pLine->m_LineInfo.fLineY - pLine->m_LineInfo.fLineDescent; |
+ if (IsFloatBigger(point.y, fTop)) |
+ bUp = FALSE; |
+ |
+ if (IsFloatSmaller(point.y, fBottom)) |
+ bDown = FALSE; |
+ |
+ if (IsFloatSmaller(point.y, fTop)) { |
+ nRight = nMid - 1; |
+ nMid = (nLeft + nRight) / 2; |
+ continue; |
+ } |
+ if (IsFloatBigger(point.y, fBottom)) { |
+ nLeft = nMid + 1; |
+ nMid = (nLeft + nRight) / 2; |
+ continue; |
+ } |
+ |
+ place = SearchWordPlace( |
+ point.x, |
+ CPVT_WordRange(pLine->GetNextWordPlace(pLine->GetBeginWordPlace()), |
+ pLine->GetEndWordPlace())); |
+ place.nLineIndex = nMid; |
+ return place; |
+ } |
+ } |
+ if (bUp) |
+ place = GetBeginWordPlace(); |
+ if (bDown) |
+ place = GetEndWordPlace(); |
+ return place; |
+} |
+ |
+CPVT_WordPlace CSection::SearchWordPlace( |
+ FX_FLOAT fx, |
+ const CPVT_WordPlace& lineplace) const { |
+ CLine* pLine = m_LineArray.GetAt(lineplace.nLineIndex); |
+ if (!pLine) |
+ return GetBeginWordPlace(); |
+ return SearchWordPlace( |
+ fx - m_SecInfo.rcSection.left, |
+ CPVT_WordRange(pLine->GetNextWordPlace(pLine->GetBeginWordPlace()), |
+ pLine->GetEndWordPlace())); |
+} |
+ |
+CPVT_WordPlace CSection::SearchWordPlace(FX_FLOAT fx, |
+ const CPVT_WordRange& range) const { |
+ CPVT_WordPlace wordplace = range.BeginPos; |
+ wordplace.nWordIndex = -1; |
+ if (!m_pVT) |
+ return wordplace; |
+ |
+ int32_t nLeft = range.BeginPos.nWordIndex; |
+ int32_t nRight = range.EndPos.nWordIndex + 1; |
+ int32_t nMid = (nLeft + nRight) / 2; |
+ while (nLeft < nRight) { |
+ if (nMid == nLeft) |
+ break; |
+ |
+ if (nMid == nRight) { |
+ nMid--; |
+ break; |
+ } |
+ |
+ const CPVT_WordInfo* pWord = GetWord(nMid); |
+ if (!pWord) |
+ break; |
+ |
+ if (fx > pWord->fWordX + m_pVT->GetWordWidth(*pWord) * VARIABLETEXT_HALF) |
+ nLeft = nMid; |
+ else |
+ nRight = nMid; |
+ nMid = (nLeft + nRight) / 2; |
+ } |
+ const CPVT_WordInfo* pWord = GetWord(nMid); |
+ if (pWord && |
+ fx > pWord->fWordX + m_pVT->GetWordWidth(*pWord) * VARIABLETEXT_HALF) { |
+ wordplace.nWordIndex = nMid; |
+ } |
+ return wordplace; |
+} |
+ |
+void CSection::ClearLeftWords(int32_t nWordIndex) { |
+ if (IsWordArrayEmpty()) |
+ return; |
+ |
+ m_WordArray.erase(m_WordArray.begin(), m_WordArray.begin() + nWordIndex + 1); |
+} |
+ |
+void CSection::ClearRightWords(int32_t nWordIndex) { |
+ if (IsWordArrayEmpty()) |
+ return; |
+ |
+ m_WordArray.erase(m_WordArray.begin() + nWordIndex, m_WordArray.end()); |
+} |
+ |
+void CSection::ClearMidWords(int32_t nBeginIndex, int32_t nEndIndex) { |
+ if (IsWordArrayEmpty()) |
+ return; |
+ |
+ m_WordArray.erase(m_WordArray.begin() + nBeginIndex + 1, |
+ m_WordArray.begin() + nEndIndex + 1); |
+} |
+ |
+void CSection::ClearWords(const CPVT_WordRange& PlaceRange) { |
+ CPVT_WordPlace SecBeginPos = GetBeginWordPlace(); |
+ CPVT_WordPlace SecEndPos = GetEndWordPlace(); |
+ if (PlaceRange.BeginPos.WordCmp(SecBeginPos) >= 0) { |
+ if (PlaceRange.EndPos.WordCmp(SecEndPos) <= 0) { |
+ ClearMidWords(PlaceRange.BeginPos.nWordIndex, |
+ PlaceRange.EndPos.nWordIndex); |
+ } else { |
+ ClearRightWords(PlaceRange.BeginPos.nWordIndex); |
+ } |
+ } else if (PlaceRange.EndPos.WordCmp(SecEndPos) <= 0) { |
+ ClearLeftWords(PlaceRange.EndPos.nWordIndex); |
+ } else { |
+ ResetWordArray(); |
+ } |
+} |
+ |
+void CSection::ClearWord(const CPVT_WordPlace& place) { |
+ m_WordArray.erase(m_WordArray.begin() + place.nWordIndex); |
+} |
+ |
+bool CSection::IsWordArrayEmpty() const { |
+ return m_WordArray.empty(); |
+} |
+ |
+int32_t CSection::GetNumberOfWords() const { |
+ return pdfium::CollectionSize<int32_t>(m_WordArray); |
+} |
+ |
+const CPVT_WordInfo* CSection::GetWord(int32_t index) const { |
+ if (index < 0 || index >= GetNumberOfWords()) |
+ return nullptr; |
+ return m_WordArray[index].get(); |
+} |
+ |
+CPVT_WordInfo* CSection::GetWord(int32_t index) { |
+ if (index < 0 || index >= GetNumberOfWords()) |
+ return nullptr; |
+ return m_WordArray[index].get(); |
+} |