diff options
author | Tom Sepez <tsepez@chromium.org> | 2017-03-17 14:17:25 -0700 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-03-17 21:58:51 +0000 |
commit | 7a1220dc1c0051f2a6bf50f3f38419ae51ecb9a1 (patch) | |
tree | 72d186d4898da33e9f582a791aaa2562f74628e4 /core | |
parent | f8b15f46e69fdec04a03afeb6f2bf60f90dff433 (diff) | |
download | pdfium-7a1220dc1c0051f2a6bf50f3f38419ae51ecb9a1.tar.xz |
Replace CLines class with std::vector<Cline>.
m_LineArray only grows via push_back of non-null pointer, so
remove some null checks.
Another little idiosyncrasy is CLines::Clear() doesn't clear the
items in CLines, only the things that were once in CLines but aren't
anymore. So don't call it.
Change-Id: Icc434be94b1b0522533c7533b8f6b2736bb864c4
Reviewed-on: https://pdfium-review.googlesource.com/3099
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'core')
-rw-r--r-- | core/fpdfdoc/cline.cpp | 2 | ||||
-rw-r--r-- | core/fpdfdoc/cline.h | 3 | ||||
-rw-r--r-- | core/fpdfdoc/clines.cpp | 52 | ||||
-rw-r--r-- | core/fpdfdoc/clines.h | 35 | ||||
-rw-r--r-- | core/fpdfdoc/cpdf_variabletext.cpp | 202 | ||||
-rw-r--r-- | core/fpdfdoc/csection.cpp | 186 | ||||
-rw-r--r-- | core/fpdfdoc/csection.h | 7 | ||||
-rw-r--r-- | core/fpdfdoc/ctypeset.cpp | 209 |
8 files changed, 310 insertions, 386 deletions
diff --git a/core/fpdfdoc/cline.cpp b/core/fpdfdoc/cline.cpp index 2e8477cfe7..a24b81950a 100644 --- a/core/fpdfdoc/cline.cpp +++ b/core/fpdfdoc/cline.cpp @@ -8,6 +8,8 @@ CLine::CLine() {} +CLine::CLine(const CPVT_LineInfo& lineinfo) : m_LineInfo(lineinfo) {} + CLine::~CLine() {} CPVT_WordPlace CLine::GetBeginWordPlace() const { diff --git a/core/fpdfdoc/cline.h b/core/fpdfdoc/cline.h index 6f95beb546..67bf06b649 100644 --- a/core/fpdfdoc/cline.h +++ b/core/fpdfdoc/cline.h @@ -10,9 +10,10 @@ #include "core/fpdfdoc/cpvt_lineinfo.h" #include "core/fpdfdoc/cpvt_wordplace.h" -class CLine final { +class CLine { public: CLine(); + explicit CLine(const CPVT_LineInfo& lineinfo); ~CLine(); CPVT_WordPlace GetBeginWordPlace() const; diff --git a/core/fpdfdoc/clines.cpp b/core/fpdfdoc/clines.cpp deleted file mode 100644 index 1e425eab93..0000000000 --- a/core/fpdfdoc/clines.cpp +++ /dev/null @@ -1,52 +0,0 @@ -// 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/clines.h" - -#include "core/fpdfdoc/cline.h" - -CLines::CLines() : m_nTotal(0) {} - -CLines::~CLines() { - RemoveAll(); -} - -int32_t CLines::GetSize() const { - return m_Lines.GetSize(); -} - -CLine* CLines::GetAt(int32_t nIndex) const { - return m_Lines.GetAt(nIndex); -} - -void CLines::Empty() { - m_nTotal = 0; -} - -void CLines::RemoveAll() { - for (int32_t i = 0, sz = GetSize(); i < sz; i++) - delete GetAt(i); - m_Lines.RemoveAll(); - m_nTotal = 0; -} - -int32_t CLines::Add(const CPVT_LineInfo& lineinfo) { - if (m_nTotal >= GetSize()) { - CLine* pLine = new CLine; - pLine->m_LineInfo = lineinfo; - m_Lines.Add(pLine); - } else if (CLine* pLine = GetAt(m_nTotal)) { - pLine->m_LineInfo = lineinfo; - } - return m_nTotal++; -} - -void CLines::Clear() { - for (int32_t i = GetSize() - 1; i >= m_nTotal; i--) { - delete GetAt(i); - m_Lines.RemoveAt(i); - } -} diff --git a/core/fpdfdoc/clines.h b/core/fpdfdoc/clines.h deleted file mode 100644 index b4db0e3966..0000000000 --- a/core/fpdfdoc/clines.h +++ /dev/null @@ -1,35 +0,0 @@ -// 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 - -#ifndef CORE_FPDFDOC_CLINES_H_ -#define CORE_FPDFDOC_CLINES_H_ - -#include <stdint.h> - -#include "core/fpdfdoc/cpvt_arraytemplate.h" -#include "core/fpdfdoc/cpvt_lineinfo.h" - -class CLine; - -class CLines final { - public: - CLines(); - ~CLines(); - - int32_t GetSize() const; - CLine* GetAt(int32_t nIndex) const; - - void Empty(); - void RemoveAll(); - int32_t Add(const CPVT_LineInfo& lineinfo); - void Clear(); - - private: - CPVT_ArrayTemplate<CLine*> m_Lines; - int32_t m_nTotal; -}; - -#endif // CORE_FPDFDOC_CLINES_H_ diff --git a/core/fpdfdoc/cpdf_variabletext.cpp b/core/fpdfdoc/cpdf_variabletext.cpp index 5dabf8db0c..d5333192f5 100644 --- a/core/fpdfdoc/cpdf_variabletext.cpp +++ b/core/fpdfdoc/cpdf_variabletext.cpp @@ -16,6 +16,7 @@ #include "core/fpdfdoc/csection.h" #include "core/fpdfdoc/ipvt_fontmap.h" #include "third_party/base/ptr_util.h" +#include "third_party/base/stl_util.h" namespace { @@ -112,37 +113,41 @@ bool CPDF_VariableText::Iterator::PrevWord() { } bool CPDF_VariableText::Iterator::NextLine() { - if (CSection* pSection = m_pVT->m_SectionArray.GetAt(m_CurPos.nSecIndex)) { - if (m_CurPos.nLineIndex < pSection->m_LineArray.GetSize() - 1) { - m_CurPos = - CPVT_WordPlace(m_CurPos.nSecIndex, m_CurPos.nLineIndex + 1, -1); - return true; - } - if (m_CurPos.nSecIndex < m_pVT->m_SectionArray.GetSize() - 1) { - m_CurPos = CPVT_WordPlace(m_CurPos.nSecIndex + 1, 0, -1); - return true; - } + CSection* pSection = m_pVT->m_SectionArray.GetAt(m_CurPos.nSecIndex); + if (!pSection) + return false; + + if (m_CurPos.nLineIndex < + pdfium::CollectionSize<int32_t>(pSection->m_LineArray) - 1) { + m_CurPos = CPVT_WordPlace(m_CurPos.nSecIndex, m_CurPos.nLineIndex + 1, -1); + return true; + } + if (m_CurPos.nSecIndex < m_pVT->m_SectionArray.GetSize() - 1) { + m_CurPos = CPVT_WordPlace(m_CurPos.nSecIndex + 1, 0, -1); + return true; } return false; } bool CPDF_VariableText::Iterator::PrevLine() { - if (m_pVT->m_SectionArray.GetAt(m_CurPos.nSecIndex)) { - if (m_CurPos.nLineIndex > 0) { - m_CurPos = - CPVT_WordPlace(m_CurPos.nSecIndex, m_CurPos.nLineIndex - 1, -1); - return true; - } - if (m_CurPos.nSecIndex > 0) { - if (CSection* pLastSection = - m_pVT->m_SectionArray.GetAt(m_CurPos.nSecIndex - 1)) { - m_CurPos = CPVT_WordPlace(m_CurPos.nSecIndex - 1, - pLastSection->m_LineArray.GetSize() - 1, -1); - return true; - } - } + if (!m_pVT->m_SectionArray.GetAt(m_CurPos.nSecIndex)) + return false; + + if (m_CurPos.nLineIndex > 0) { + m_CurPos = CPVT_WordPlace(m_CurPos.nSecIndex, m_CurPos.nLineIndex - 1, -1); + return true; } - return false; + if (m_CurPos.nSecIndex <= 0) + return false; + + CSection* pLastSection = m_pVT->m_SectionArray.GetAt(m_CurPos.nSecIndex - 1); + if (!pLastSection) + return false; + + m_CurPos = CPVT_WordPlace( + m_CurPos.nSecIndex - 1, + pdfium::CollectionSize<int32_t>(pLastSection->m_LineArray) - 1, -1); + return true; } bool CPDF_VariableText::Iterator::NextSection() { @@ -164,58 +169,65 @@ bool CPDF_VariableText::Iterator::PrevSection() { bool CPDF_VariableText::Iterator::GetWord(CPVT_Word& word) const { word.WordPlace = m_CurPos; - if (CSection* pSection = m_pVT->m_SectionArray.GetAt(m_CurPos.nSecIndex)) { - if (pSection->m_LineArray.GetAt(m_CurPos.nLineIndex)) { - if (CPVT_WordInfo* pWord = - pSection->m_WordArray.GetAt(m_CurPos.nWordIndex)) { - word.Word = pWord->Word; - word.nCharset = pWord->nCharset; - word.fWidth = m_pVT->GetWordWidth(*pWord); - word.ptWord = m_pVT->InToOut( - CFX_PointF(pWord->fWordX + pSection->m_SecInfo.rcSection.left, - pWord->fWordY + pSection->m_SecInfo.rcSection.top)); - word.fAscent = m_pVT->GetWordAscent(*pWord); - word.fDescent = m_pVT->GetWordDescent(*pWord); - if (pWord->pWordProps) - word.WordProps = *pWord->pWordProps; - - word.nFontIndex = m_pVT->GetWordFontIndex(*pWord); - word.fFontSize = m_pVT->GetWordFontSize(*pWord); - return true; - } - } - } - return false; + CSection* pSection = m_pVT->m_SectionArray.GetAt(m_CurPos.nSecIndex); + if (!pSection) + return false; + + if (!pdfium::IndexInBounds(pSection->m_LineArray, m_CurPos.nLineIndex)) + return false; + + CPVT_WordInfo* pWord = pSection->m_WordArray.GetAt(m_CurPos.nWordIndex); + if (!pWord) + return false; + + word.Word = pWord->Word; + word.nCharset = pWord->nCharset; + word.fWidth = m_pVT->GetWordWidth(*pWord); + word.ptWord = m_pVT->InToOut( + CFX_PointF(pWord->fWordX + pSection->m_SecInfo.rcSection.left, + pWord->fWordY + pSection->m_SecInfo.rcSection.top)); + word.fAscent = m_pVT->GetWordAscent(*pWord); + word.fDescent = m_pVT->GetWordDescent(*pWord); + if (pWord->pWordProps) + word.WordProps = *pWord->pWordProps; + word.nFontIndex = m_pVT->GetWordFontIndex(*pWord); + word.fFontSize = m_pVT->GetWordFontSize(*pWord); + return true; } bool CPDF_VariableText::Iterator::SetWord(const CPVT_Word& word) { - if (CSection* pSection = m_pVT->m_SectionArray.GetAt(m_CurPos.nSecIndex)) { - if (CPVT_WordInfo* pWord = - pSection->m_WordArray.GetAt(m_CurPos.nWordIndex)) { - if (pWord->pWordProps) - *pWord->pWordProps = word.WordProps; - return true; - } - } - return false; + CSection* pSection = m_pVT->m_SectionArray.GetAt(m_CurPos.nSecIndex); + if (!pSection) + return false; + + CPVT_WordInfo* pWord = pSection->m_WordArray.GetAt(m_CurPos.nWordIndex); + if (!pWord) + return false; + + if (pWord->pWordProps) + *pWord->pWordProps = word.WordProps; + return true; } bool CPDF_VariableText::Iterator::GetLine(CPVT_Line& line) const { ASSERT(m_pVT); line.lineplace = CPVT_WordPlace(m_CurPos.nSecIndex, m_CurPos.nLineIndex, -1); - if (CSection* pSection = m_pVT->m_SectionArray.GetAt(m_CurPos.nSecIndex)) { - if (CLine* pLine = pSection->m_LineArray.GetAt(m_CurPos.nLineIndex)) { - line.ptLine = m_pVT->InToOut(CFX_PointF( - pLine->m_LineInfo.fLineX + pSection->m_SecInfo.rcSection.left, - pLine->m_LineInfo.fLineY + pSection->m_SecInfo.rcSection.top)); - line.fLineWidth = pLine->m_LineInfo.fLineWidth; - line.fLineAscent = pLine->m_LineInfo.fLineAscent; - line.fLineDescent = pLine->m_LineInfo.fLineDescent; - line.lineEnd = pLine->GetEndWordPlace(); - return true; - } - } - return false; + CSection* pSection = m_pVT->m_SectionArray.GetAt(m_CurPos.nSecIndex); + if (!pSection) + return false; + + if (!pdfium::IndexInBounds(pSection->m_LineArray, m_CurPos.nLineIndex)) + return false; + + CLine* pLine = pSection->m_LineArray[m_CurPos.nLineIndex].get(); + line.ptLine = m_pVT->InToOut( + CFX_PointF(pLine->m_LineInfo.fLineX + pSection->m_SecInfo.rcSection.left, + pLine->m_LineInfo.fLineY + pSection->m_SecInfo.rcSection.top)); + line.fLineWidth = pLine->m_LineInfo.fLineWidth; + line.fLineAscent = pLine->m_LineInfo.fLineAscent; + line.fLineDescent = pLine->m_LineInfo.fLineDescent; + line.lineEnd = pLine->GetEndWordPlace(); + return true; } bool CPDF_VariableText::Iterator::GetSection(CPVT_Section& section) const { @@ -232,14 +244,15 @@ bool CPDF_VariableText::Iterator::GetSection(CPVT_Section& section) const { } bool CPDF_VariableText::Iterator::SetSection(const CPVT_Section& section) { - if (CSection* pSection = m_pVT->m_SectionArray.GetAt(m_CurPos.nSecIndex)) { - if (pSection->m_SecInfo.pSecProps) - *pSection->m_SecInfo.pSecProps = section.SecProps; - if (pSection->m_SecInfo.pWordProps) - *pSection->m_SecInfo.pWordProps = section.WordProps; - return true; - } - return false; + CSection* pSection = m_pVT->m_SectionArray.GetAt(m_CurPos.nSecIndex); + if (!pSection) + return false; + + if (pSection->m_SecInfo.pSecProps) + *pSection->m_SecInfo.pSecProps = section.SecProps; + if (pSection->m_SecInfo.pWordProps) + *pSection->m_SecInfo.pWordProps = section.WordProps; + return true; } CPDF_VariableText::CPDF_VariableText() @@ -602,7 +615,8 @@ CPVT_WordPlace CPDF_VariableText::GetUpWordPlace( } if (temp.nSecIndex-- > 0) { if (CSection* pLastSection = m_SectionArray.GetAt(temp.nSecIndex)) { - temp.nLineIndex = pLastSection->m_LineArray.GetSize() - 1; + temp.nLineIndex = + pdfium::CollectionSize<int32_t>(pLastSection->m_LineArray) - 1; return pLastSection->SearchWordPlace( pt.x - pLastSection->m_SecInfo.rcSection.left, temp); } @@ -617,7 +631,8 @@ CPVT_WordPlace CPDF_VariableText::GetDownWordPlace( if (CSection* pSection = m_SectionArray.GetAt(place.nSecIndex)) { CPVT_WordPlace temp = place; CFX_PointF pt = OutToIn(point); - if (temp.nLineIndex++ < pSection->m_LineArray.GetSize() - 1) { + if (temp.nLineIndex++ < + pdfium::CollectionSize<int32_t>(pSection->m_LineArray) - 1) { return pSection->SearchWordPlace( pt.x - pSection->m_SecInfo.rcSection.left, temp); } @@ -639,11 +654,14 @@ CPVT_WordPlace CPDF_VariableText::GetLineBeginPlace( CPVT_WordPlace CPDF_VariableText::GetLineEndPlace( const CPVT_WordPlace& place) const { - if (CSection* pSection = m_SectionArray.GetAt(place.nSecIndex)) { - if (CLine* pLine = pSection->m_LineArray.GetAt(place.nLineIndex)) - return pLine->GetEndWordPlace(); - } - return place; + CSection* pSection = m_SectionArray.GetAt(place.nSecIndex); + if (!pSection) + return place; + + if (!pdfium::IndexInBounds(pSection->m_LineArray, place.nLineIndex)) + return place; + + return pSection->m_LineArray[place.nLineIndex]->GetEndWordPlace(); } CPVT_WordPlace CPDF_VariableText::GetSectionBeginPlace( @@ -739,13 +757,15 @@ bool CPDF_VariableText::SetWordInfo(const CPVT_WordPlace& place, bool CPDF_VariableText::GetLineInfo(const CPVT_WordPlace& place, CPVT_LineInfo& lineinfo) { - if (CSection* pSection = m_SectionArray.GetAt(place.nSecIndex)) { - if (CLine* pLine = pSection->m_LineArray.GetAt(place.nLineIndex)) { - lineinfo = pLine->m_LineInfo; - return true; - } - } - return false; + CSection* pSection = m_SectionArray.GetAt(place.nSecIndex); + if (!pSection) + return false; + + if (!pdfium::IndexInBounds(pSection->m_LineArray, place.nLineIndex)) + return false; + + lineinfo = pSection->m_LineArray[place.nLineIndex]->m_LineInfo; + return true; } bool CPDF_VariableText::GetSectionInfo(const CPVT_WordPlace& place, diff --git a/core/fpdfdoc/csection.cpp b/core/fpdfdoc/csection.cpp index ce78418dc5..981f6d0b54 100644 --- a/core/fpdfdoc/csection.cpp +++ b/core/fpdfdoc/csection.cpp @@ -10,6 +10,8 @@ #include "core/fpdfdoc/cline.h" #include "core/fpdfdoc/cpvt_wordinfo.h" +#include "third_party/base/ptr_util.h" +#include "third_party/base/stl_util.h" CSection::CSection(CPDF_VariableText* pVT) : m_pVT(pVT) {} @@ -23,7 +25,7 @@ void CSection::ResetAll() { } void CSection::ResetLineArray() { - m_LineArray.RemoveAll(); + m_LineArray.clear(); } void CSection::ResetWordArray() { @@ -34,10 +36,10 @@ void CSection::ResetWordArray() { } 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); - } + int32_t i = 0; + for (auto& pLine : m_LineArray) { + pLine->LinePlace = CPVT_WordPlace(SecPlace.nSecIndex, i, -1); + ++i; } } @@ -55,7 +57,8 @@ CPVT_WordPlace CSection::AddWord(const CPVT_WordPlace& place, } CPVT_WordPlace CSection::AddLine(const CPVT_LineInfo& lineinfo) { - return CPVT_WordPlace(SecPlace.nSecIndex, m_LineArray.Add(lineinfo), -1); + m_LineArray.push_back(pdfium::MakeUnique<CLine>(lineinfo)); + return CPVT_WordPlace(SecPlace.nSecIndex, m_LineArray.size() - 1, -1); } CPVT_FloatRect CSection::Rearrange() { @@ -70,78 +73,69 @@ CFX_SizeF CSection::GetSectionSize(float fFontSize) { } CPVT_WordPlace CSection::GetBeginWordPlace() const { - if (CLine* pLine = m_LineArray.GetAt(0)) { - return pLine->GetBeginWordPlace(); - } - return SecPlace; + if (m_LineArray.empty()) + return SecPlace; + return m_LineArray.front()->GetBeginWordPlace(); } CPVT_WordPlace CSection::GetEndWordPlace() const { - if (CLine* pLine = m_LineArray.GetAt(m_LineArray.GetSize() - 1)) { - return pLine->GetEndWordPlace(); - } - return SecPlace; + if (m_LineArray.empty()) + return SecPlace; + return m_LineArray.back()->GetEndWordPlace(); } CPVT_WordPlace CSection::GetPrevWordPlace(const CPVT_WordPlace& place) const { - if (place.nLineIndex < 0) { + if (place.nLineIndex < 0) return GetBeginWordPlace(); - } - if (place.nLineIndex >= m_LineArray.GetSize()) { + + if (place.nLineIndex >= pdfium::CollectionSize<int32_t>(m_LineArray)) 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) { - if (CLine* pPrevLine = m_LineArray.GetAt(place.nLineIndex - 1)) { - return pPrevLine->GetEndWordPlace(); - } - } else { - return pLine->GetPrevWordPlace(place); - } - } - return place; + + CLine* pLine = m_LineArray[place.nLineIndex].get(); + 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 (!pdfium::IndexInBounds(m_LineArray, place.nLineIndex - 1)) + return place; + + return m_LineArray[place.nLineIndex - 1]->GetEndWordPlace(); } CPVT_WordPlace CSection::GetNextWordPlace(const CPVT_WordPlace& place) const { - if (place.nLineIndex < 0) { + if (place.nLineIndex < 0) return GetBeginWordPlace(); - } - if (place.nLineIndex >= m_LineArray.GetSize()) { + + if (place.nLineIndex >= pdfium::CollectionSize<int32_t>(m_LineArray)) return GetEndWordPlace(); - } - if (CLine* pLine = m_LineArray.GetAt(place.nLineIndex)) { - if (place.nWordIndex >= pLine->m_LineInfo.nEndWordIndex) { - if (CLine* pNextLine = m_LineArray.GetAt(place.nLineIndex + 1)) { - return pNextLine->GetBeginWordPlace(); - } - } else { - return pLine->GetNextWordPlace(place); - } - } - return place; + + CLine* pLine = m_LineArray[place.nLineIndex].get(); + if (place.nWordIndex < pLine->m_LineInfo.nEndWordIndex) + return pLine->GetNextWordPlace(place); + + if (!pdfium::IndexInBounds(m_LineArray, place.nLineIndex + 1)) + return place; + + return m_LineArray[place.nLineIndex + 1]->GetBeginWordPlace(); } void CSection::UpdateWordPlace(CPVT_WordPlace& place) const { int32_t nLeft = 0; - int32_t nRight = m_LineArray.GetSize() - 1; + int32_t nRight = pdfium::CollectionSize<int32_t>(m_LineArray) - 1; int32_t nMid = (nLeft + nRight) / 2; while (nLeft <= nRight) { - if (CLine* pLine = m_LineArray.GetAt(nMid)) { - if (place.nWordIndex < pLine->m_LineInfo.nBeginWordIndex) { - nRight = nMid - 1; - nMid = (nLeft + nRight) / 2; - } else if (place.nWordIndex > pLine->m_LineInfo.nEndWordIndex) { - nLeft = nMid + 1; - nMid = (nLeft + nRight) / 2; - } else { - place.nLineIndex = nMid; - return; - } + CLine* pLine = m_LineArray[nMid].get(); + if (place.nWordIndex < pLine->m_LineInfo.nBeginWordIndex) { + nRight = nMid - 1; + nMid = (nLeft + nRight) / 2; + } else if (place.nWordIndex > pLine->m_LineInfo.nEndWordIndex) { + nLeft = nMid + 1; + nMid = (nLeft + nRight) / 2; } else { - break; + place.nLineIndex = nMid; + return; } } } @@ -152,58 +146,52 @@ CPVT_WordPlace CSection::SearchWordPlace(const CFX_PointF& point) const { bool bUp = true; bool bDown = true; int32_t nLeft = 0; - int32_t nRight = m_LineArray.GetSize() - 1; - int32_t nMid = m_LineArray.GetSize() / 2; - float fTop = 0; - float fBottom = 0; + int32_t nRight = pdfium::CollectionSize<int32_t>(m_LineArray) - 1; + int32_t nMid = pdfium::CollectionSize<int32_t>(m_LineArray) / 2; 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; - } else if (IsFloatBigger(point.y, fBottom)) { - nLeft = nMid + 1; - nMid = (nLeft + nRight) / 2; - continue; - } else { - place = SearchWordPlace( - point.x, - CPVT_WordRange(pLine->GetNextWordPlace(pLine->GetBeginWordPlace()), - pLine->GetEndWordPlace())); - place.nLineIndex = nMid; - return place; - } + CLine* pLine = m_LineArray[nMid].get(); + float fTop = pLine->m_LineInfo.fLineY - pLine->m_LineInfo.fLineAscent - + m_pVT->GetLineLeading(m_SecInfo); + float 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) { + if (bUp) place = GetBeginWordPlace(); - } - if (bDown) { + if (bDown) place = GetEndWordPlace(); - } return place; } CPVT_WordPlace CSection::SearchWordPlace( float fx, const CPVT_WordPlace& lineplace) const { - if (CLine* pLine = m_LineArray.GetAt(lineplace.nLineIndex)) { - return SearchWordPlace( - fx - m_SecInfo.rcSection.left, - CPVT_WordRange(pLine->GetNextWordPlace(pLine->GetBeginWordPlace()), - pLine->GetEndWordPlace())); - } - return GetBeginWordPlace(); + if (!pdfium::IndexInBounds(m_LineArray, lineplace.nLineIndex)) + return GetBeginWordPlace(); + + CLine* pLine = m_LineArray[lineplace.nLineIndex].get(); + return SearchWordPlace( + fx - m_SecInfo.rcSection.left, + CPVT_WordRange(pLine->GetNextWordPlace(pLine->GetBeginWordPlace()), + pLine->GetEndWordPlace())); } CPVT_WordPlace CSection::SearchWordPlace(float fx, diff --git a/core/fpdfdoc/csection.h b/core/fpdfdoc/csection.h index b82409dc6f..6907b3dc45 100644 --- a/core/fpdfdoc/csection.h +++ b/core/fpdfdoc/csection.h @@ -7,7 +7,10 @@ #ifndef CORE_FPDFDOC_CSECTION_H_ #define CORE_FPDFDOC_CSECTION_H_ -#include "core/fpdfdoc/clines.h" +#include <memory> +#include <vector> + +#include "core/fpdfdoc/cline.h" #include "core/fpdfdoc/cpvt_sectioninfo.h" #include "core/fpdfdoc/ctypeset.h" #include "core/fxcrt/fx_coordinates.h" @@ -46,7 +49,7 @@ class CSection final { CPVT_WordPlace SecPlace; CPVT_SectionInfo m_SecInfo; - CLines m_LineArray; + std::vector<std::unique_ptr<CLine>> m_LineArray; CPVT_ArrayTemplate<CPVT_WordInfo*> m_WordArray; private: diff --git a/core/fpdfdoc/ctypeset.cpp b/core/fpdfdoc/ctypeset.cpp index 6cfaff5cc8..34deb216b2 100644 --- a/core/fpdfdoc/ctypeset.cpp +++ b/core/fpdfdoc/ctypeset.cpp @@ -11,6 +11,7 @@ #include "core/fpdfdoc/cline.h" #include "core/fpdfdoc/cpvt_wordinfo.h" #include "core/fpdfdoc/csection.h" +#include "third_party/base/stl_util.h" namespace { @@ -179,80 +180,77 @@ CTypeset::CTypeset(CSection* pSection) CTypeset::~CTypeset() {} CPVT_FloatRect CTypeset::CharArray() { - ASSERT(m_pSection); + m_rcRet = CPVT_FloatRect(0, 0, 0, 0); + if (m_pSection->m_LineArray.empty()) + return m_rcRet; + + float fNodeWidth = m_pVT->GetPlateWidth() / + (m_pVT->m_nCharArray <= 0 ? 1 : m_pVT->m_nCharArray); float fLineAscent = m_pVT->GetFontAscent(m_pVT->GetDefaultFontIndex(), m_pVT->GetFontSize()); float fLineDescent = m_pVT->GetFontDescent(m_pVT->GetDefaultFontIndex(), m_pVT->GetFontSize()); - m_rcRet.Default(); - float x = 0.0f, y = 0.0f; - float fNextWidth; + float x = 0.0f; + float y = m_pVT->GetLineLeading(m_pSection->m_SecInfo) + fLineAscent; int32_t nStart = 0; - float fNodeWidth = m_pVT->GetPlateWidth() / - (m_pVT->m_nCharArray <= 0 ? 1 : m_pVT->m_nCharArray); - if (CLine* pLine = m_pSection->m_LineArray.GetAt(0)) { - x = 0.0f; - y += m_pVT->GetLineLeading(m_pSection->m_SecInfo); - y += fLineAscent; - nStart = 0; - switch (m_pVT->GetAlignment(m_pSection->m_SecInfo)) { - case 0: - pLine->m_LineInfo.fLineX = fNodeWidth * VARIABLETEXT_HALF; - break; - case 1: - nStart = (m_pVT->m_nCharArray - m_pSection->m_WordArray.GetSize()) / 2; - pLine->m_LineInfo.fLineX = - fNodeWidth * nStart - fNodeWidth * VARIABLETEXT_HALF; - break; - case 2: - nStart = m_pVT->m_nCharArray - m_pSection->m_WordArray.GetSize(); - pLine->m_LineInfo.fLineX = - fNodeWidth * nStart - fNodeWidth * VARIABLETEXT_HALF; - break; + CLine* pLine = m_pSection->m_LineArray.front().get(); + switch (m_pVT->GetAlignment(m_pSection->m_SecInfo)) { + case 0: + pLine->m_LineInfo.fLineX = fNodeWidth * VARIABLETEXT_HALF; + break; + case 1: + nStart = (m_pVT->m_nCharArray - m_pSection->m_WordArray.GetSize()) / 2; + pLine->m_LineInfo.fLineX = + fNodeWidth * nStart - fNodeWidth * VARIABLETEXT_HALF; + break; + case 2: + nStart = m_pVT->m_nCharArray - m_pSection->m_WordArray.GetSize(); + pLine->m_LineInfo.fLineX = + fNodeWidth * nStart - fNodeWidth * VARIABLETEXT_HALF; + break; + } + for (int32_t w = 0, sz = m_pSection->m_WordArray.GetSize(); w < sz; w++) { + if (w >= m_pVT->m_nCharArray) + break; + + float fNextWidth = 0; + if (CPVT_WordInfo* pNextWord = m_pSection->m_WordArray.GetAt(w + 1)) { + pNextWord->fWordTail = 0; + fNextWidth = m_pVT->GetWordWidth(*pNextWord); } - for (int32_t w = 0, sz = m_pSection->m_WordArray.GetSize(); w < sz; w++) { - if (w >= m_pVT->m_nCharArray) { - break; - } - fNextWidth = 0; - if (CPVT_WordInfo* pNextWord = m_pSection->m_WordArray.GetAt(w + 1)) { - pNextWord->fWordTail = 0; - fNextWidth = m_pVT->GetWordWidth(*pNextWord); + if (CPVT_WordInfo* pWord = m_pSection->m_WordArray.GetAt(w)) { + pWord->fWordTail = 0; + float fWordWidth = m_pVT->GetWordWidth(*pWord); + float fWordAscent = m_pVT->GetWordAscent(*pWord); + float fWordDescent = m_pVT->GetWordDescent(*pWord); + x = (float)(fNodeWidth * (w + nStart + 0.5) - + fWordWidth * VARIABLETEXT_HALF); + pWord->fWordX = x; + pWord->fWordY = y; + if (w == 0) { + pLine->m_LineInfo.fLineX = x; } - if (CPVT_WordInfo* pWord = m_pSection->m_WordArray.GetAt(w)) { + if (w != m_pSection->m_WordArray.GetSize() - 1) { + pWord->fWordTail = + (fNodeWidth - (fWordWidth + fNextWidth) * VARIABLETEXT_HALF > 0 + ? fNodeWidth - (fWordWidth + fNextWidth) * VARIABLETEXT_HALF + : 0); + } else { pWord->fWordTail = 0; - float fWordWidth = m_pVT->GetWordWidth(*pWord); - float fWordAscent = m_pVT->GetWordAscent(*pWord); - float fWordDescent = m_pVT->GetWordDescent(*pWord); - x = (float)(fNodeWidth * (w + nStart + 0.5) - - fWordWidth * VARIABLETEXT_HALF); - pWord->fWordX = x; - pWord->fWordY = y; - if (w == 0) { - pLine->m_LineInfo.fLineX = x; - } - if (w != m_pSection->m_WordArray.GetSize() - 1) { - pWord->fWordTail = - (fNodeWidth - (fWordWidth + fNextWidth) * VARIABLETEXT_HALF > 0 - ? fNodeWidth - (fWordWidth + fNextWidth) * VARIABLETEXT_HALF - : 0); - } else { - pWord->fWordTail = 0; - } - x += fWordWidth; - fLineAscent = std::max(fLineAscent, fWordAscent); - fLineDescent = std::min(fLineDescent, fWordDescent); } + x += fWordWidth; + fLineAscent = std::max(fLineAscent, fWordAscent); + fLineDescent = std::min(fLineDescent, fWordDescent); } - pLine->m_LineInfo.nBeginWordIndex = 0; - pLine->m_LineInfo.nEndWordIndex = m_pSection->m_WordArray.GetSize() - 1; - pLine->m_LineInfo.fLineY = y; - pLine->m_LineInfo.fLineWidth = x - pLine->m_LineInfo.fLineX; - pLine->m_LineInfo.fLineAscent = fLineAscent; - pLine->m_LineInfo.fLineDescent = fLineDescent; - y -= fLineDescent; } - return m_rcRet = CPVT_FloatRect(0, 0, x, y); + pLine->m_LineInfo.nBeginWordIndex = 0; + pLine->m_LineInfo.nEndWordIndex = m_pSection->m_WordArray.GetSize() - 1; + pLine->m_LineInfo.fLineY = y; + pLine->m_LineInfo.fLineWidth = x - pLine->m_LineInfo.fLineX; + pLine->m_LineInfo.fLineAscent = fLineAscent; + pLine->m_LineInfo.fLineDescent = fLineDescent; + m_rcRet = CPVT_FloatRect(0, 0, x, y - fLineDescent); + return m_rcRet; } CFX_SizeF CTypeset::GetEditSize(float fFontSize) { @@ -264,9 +262,8 @@ CFX_SizeF CTypeset::GetEditSize(float fFontSize) { CPVT_FloatRect CTypeset::Typeset() { ASSERT(m_pVT); - m_pSection->m_LineArray.Empty(); + m_pSection->m_LineArray.clear(); SplitLines(true, 0.0f); - m_pSection->m_LineArray.Clear(); OutputLines(); return m_rcRet; } @@ -439,54 +436,54 @@ void CTypeset::OutputLines() { fMaxX = fMinX + m_rcRet.Width(); fMinY = 0.0f; fMaxY = m_rcRet.Height(); - int32_t nTotalLines = m_pSection->m_LineArray.GetSize(); + int32_t nTotalLines = + pdfium::CollectionSize<int32_t>(m_pSection->m_LineArray); if (nTotalLines > 0) { m_pSection->m_SecInfo.nTotalLine = nTotalLines; for (int32_t l = 0; l < nTotalLines; l++) { - if (CLine* pLine = m_pSection->m_LineArray.GetAt(l)) { - switch (m_pVT->GetAlignment(m_pSection->m_SecInfo)) { - default: - case 0: - fPosX = 0; - break; - case 1: - fPosX = (fTypesetWidth - pLine->m_LineInfo.fLineWidth) * - VARIABLETEXT_HALF; - break; - case 2: - fPosX = fTypesetWidth - pLine->m_LineInfo.fLineWidth; - break; - } - fPosX += fLineIndent; - fPosY += m_pVT->GetLineLeading(m_pSection->m_SecInfo); - fPosY += pLine->m_LineInfo.fLineAscent; - pLine->m_LineInfo.fLineX = fPosX - fMinX; - pLine->m_LineInfo.fLineY = fPosY - fMinY; - for (int32_t w = pLine->m_LineInfo.nBeginWordIndex; - w <= pLine->m_LineInfo.nEndWordIndex; w++) { - if (CPVT_WordInfo* pWord = m_pSection->m_WordArray.GetAt(w)) { - pWord->fWordX = fPosX - fMinX; - if (pWord->pWordProps) { - switch (pWord->pWordProps->nScriptType) { - default: - case CPDF_VariableText::ScriptType::Normal: - pWord->fWordY = fPosY - fMinY; - break; - case CPDF_VariableText::ScriptType::Super: - pWord->fWordY = fPosY - m_pVT->GetWordAscent(*pWord) - fMinY; - break; - case CPDF_VariableText::ScriptType::Sub: - pWord->fWordY = fPosY - m_pVT->GetWordDescent(*pWord) - fMinY; - break; - } - } else { - pWord->fWordY = fPosY - fMinY; + CLine* pLine = m_pSection->m_LineArray[l].get(); + switch (m_pVT->GetAlignment(m_pSection->m_SecInfo)) { + default: + case 0: + fPosX = 0; + break; + case 1: + fPosX = (fTypesetWidth - pLine->m_LineInfo.fLineWidth) * + VARIABLETEXT_HALF; + break; + case 2: + fPosX = fTypesetWidth - pLine->m_LineInfo.fLineWidth; + break; + } + fPosX += fLineIndent; + fPosY += m_pVT->GetLineLeading(m_pSection->m_SecInfo); + fPosY += pLine->m_LineInfo.fLineAscent; + pLine->m_LineInfo.fLineX = fPosX - fMinX; + pLine->m_LineInfo.fLineY = fPosY - fMinY; + for (int32_t w = pLine->m_LineInfo.nBeginWordIndex; + w <= pLine->m_LineInfo.nEndWordIndex; w++) { + if (CPVT_WordInfo* pWord = m_pSection->m_WordArray.GetAt(w)) { + pWord->fWordX = fPosX - fMinX; + if (pWord->pWordProps) { + switch (pWord->pWordProps->nScriptType) { + default: + case CPDF_VariableText::ScriptType::Normal: + pWord->fWordY = fPosY - fMinY; + break; + case CPDF_VariableText::ScriptType::Super: + pWord->fWordY = fPosY - m_pVT->GetWordAscent(*pWord) - fMinY; + break; + case CPDF_VariableText::ScriptType::Sub: + pWord->fWordY = fPosY - m_pVT->GetWordDescent(*pWord) - fMinY; + break; } - fPosX += m_pVT->GetWordWidth(*pWord); + } else { + pWord->fWordY = fPosY - fMinY; } + fPosX += m_pVT->GetWordWidth(*pWord); } - fPosY -= pLine->m_LineInfo.fLineDescent; } + fPosY -= pLine->m_LineInfo.fLineDescent; } } m_rcRet = CPVT_FloatRect(fMinX, fMinY, fMaxX, fMaxY); |