diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2017-11-27 18:37:46 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-11-27 18:37:46 +0000 |
commit | 4f1c7f33f765b7cf5d8770d725054f74073bab39 (patch) | |
tree | ffa459a98d5fae60ffa524aa71b22d6e49301eca | |
parent | 2d950563beb291441c0e1c90cfde8227f7025327 (diff) | |
download | pdfium-4f1c7f33f765b7cf5d8770d725054f74073bab39.tar.xz |
Convert CPDF_TextObject to not use CollectionSize
This CL updates various methods in CPDF_TextObject to return or received
size_t values. Callers have been updated as needed.
Bug: pdfium:774
Change-Id: Id72511bc74637c6261add39f5414c9a4b8390b82
Reviewed-on: https://pdfium-review.googlesource.com/19430
Commit-Queue: dsinclair <dsinclair@chromium.org>
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
-rw-r--r-- | core/fpdfapi/page/cpdf_textobject.cpp | 28 | ||||
-rw-r--r-- | core/fpdfapi/page/cpdf_textobject.h | 13 | ||||
-rw-r--r-- | core/fpdftext/cpdf_textpage.cpp | 51 | ||||
-rw-r--r-- | core/fpdftext/cpdf_textpage.h | 2 | ||||
-rw-r--r-- | fxjs/cjs_document.cpp | 4 |
5 files changed, 56 insertions, 42 deletions
diff --git a/core/fpdfapi/page/cpdf_textobject.cpp b/core/fpdfapi/page/cpdf_textobject.cpp index 4baf61dd72..ac90eb1e50 100644 --- a/core/fpdfapi/page/cpdf_textobject.cpp +++ b/core/fpdfapi/page/cpdf_textobject.cpp @@ -21,13 +21,14 @@ CPDF_TextObject::CPDF_TextObject() {} CPDF_TextObject::~CPDF_TextObject() {} -int CPDF_TextObject::CountItems() const { - return pdfium::CollectionSize<int>(m_CharCodes); +size_t CPDF_TextObject::CountItems() const { + return m_CharCodes.size(); } -void CPDF_TextObject::GetItemInfo(int index, CPDF_TextObjectItem* pInfo) const { +void CPDF_TextObject::GetItemInfo(size_t index, + CPDF_TextObjectItem* pInfo) const { pInfo->m_CharCode = m_CharCodes[index]; - pInfo->m_Origin = CFX_PointF(index ? m_CharPos[index - 1] : 0, 0); + pInfo->m_Origin = CFX_PointF(index > 0 ? m_CharPos[index - 1] : 0, 0); if (pInfo->m_CharCode == CPDF_Font::kInvalidCharCode) return; @@ -49,19 +50,19 @@ void CPDF_TextObject::GetItemInfo(int index, CPDF_TextObjectItem* pInfo) const { pInfo->m_Origin.y -= fontsize * vy / 1000; } -int CPDF_TextObject::CountChars() const { - int count = 0; +size_t CPDF_TextObject::CountChars() const { + size_t count = 0; for (uint32_t charcode : m_CharCodes) { if (charcode != CPDF_Font::kInvalidCharCode) - count++; + ++count; } return count; } -void CPDF_TextObject::GetCharInfo(int index, +void CPDF_TextObject::GetCharInfo(size_t index, uint32_t* charcode, float* kerning) const { - int count = 0; + size_t count = 0; for (size_t i = 0; i < m_CharCodes.size(); ++i) { if (m_CharCodes[i] == CPDF_Font::kInvalidCharCode) continue; @@ -78,9 +79,10 @@ void CPDF_TextObject::GetCharInfo(int index, } } -void CPDF_TextObject::GetCharInfo(int index, CPDF_TextObjectItem* pInfo) const { - int count = 0; - for (int i = 0; i < pdfium::CollectionSize<int>(m_CharCodes); ++i) { +void CPDF_TextObject::GetCharInfo(size_t index, + CPDF_TextObjectItem* pInfo) const { + size_t count = 0; + for (size_t i = 0; i < m_CharCodes.size(); ++i) { uint32_t charcode = m_CharCodes[i]; if (charcode == CPDF_Font::kInvalidCharCode) continue; @@ -203,7 +205,7 @@ CFX_PointF CPDF_TextObject::CalcPositionData(float horz_scale) { bVertWriting = pCIDFont->IsVertWriting(); float fontsize = m_TextState.GetFontSize(); - for (int i = 0; i < pdfium::CollectionSize<int>(m_CharCodes); ++i) { + for (size_t i = 0; i < m_CharCodes.size(); ++i) { uint32_t charcode = m_CharCodes[i]; if (i > 0) { if (charcode == CPDF_Font::kInvalidCharCode) { diff --git a/core/fpdfapi/page/cpdf_textobject.h b/core/fpdfapi/page/cpdf_textobject.h index 02653dc2aa..a6fc62369c 100644 --- a/core/fpdfapi/page/cpdf_textobject.h +++ b/core/fpdfapi/page/cpdf_textobject.h @@ -36,12 +36,15 @@ class CPDF_TextObject : public CPDF_PageObject { const CPDF_TextObject* AsText() const override; std::unique_ptr<CPDF_TextObject> Clone() const; - int CountItems() const; - void GetItemInfo(int index, CPDF_TextObjectItem* pInfo) const; - int CountChars() const; - void GetCharInfo(int index, uint32_t* charcode, float* kerning) const; - void GetCharInfo(int index, CPDF_TextObjectItem* pInfo) const; + + size_t CountItems() const; + void GetItemInfo(size_t index, CPDF_TextObjectItem* pInfo) const; + + size_t CountChars() const; + void GetCharInfo(size_t index, uint32_t* charcode, float* kerning) const; + void GetCharInfo(size_t index, CPDF_TextObjectItem* pInfo) const; float GetCharWidth(uint32_t charcode) const; + CFX_PointF GetPos() const { return m_Pos; } CFX_Matrix GetTextMatrix() const; CPDF_Font* GetFont() const; diff --git a/core/fpdftext/cpdf_textpage.cpp b/core/fpdftext/cpdf_textpage.cpp index edd5456c1a..7ea2061c26 100644 --- a/core/fpdftext/cpdf_textpage.cpp +++ b/core/fpdftext/cpdf_textpage.cpp @@ -44,13 +44,13 @@ float NormalizeThreshold(float threshold) { float CalculateBaseSpace(const CPDF_TextObject* pTextObj, const CFX_Matrix& matrix) { float baseSpace = 0.0; - const int nItems = pTextObj->CountItems(); + const size_t nItems = pTextObj->CountItems(); if (pTextObj->m_TextState.GetCharSpace() && nItems >= 3) { bool bAllChar = true; float spacing = matrix.TransformDistance(pTextObj->m_TextState.GetCharSpace()); baseSpace = spacing; - for (int i = 0; i < nItems; i++) { + for (size_t i = 0; i < nItems; ++i) { CPDF_TextObjectItem item; pTextObj->GetItemInfo(i, &item); if (item.m_CharCode == static_cast<uint32_t>(-1)) { @@ -758,8 +758,11 @@ void CPDF_TextPage::ProcessTextObject( return; PDFTEXT_Obj prev_Obj = m_LineObj[count - 1]; + size_t nItem = prev_Obj.m_pTextObj->CountItems(); + if (nItem == 0) + return; + CPDF_TextObjectItem item; - int nItem = prev_Obj.m_pTextObj->CountItems(); prev_Obj.m_pTextObj->GetItemInfo(nItem - 1, &item); float prev_width = GetCharWidth(item.m_CharCode, prev_Obj.m_pTextObj->GetFont()) * @@ -811,7 +814,7 @@ FPDFText_MarkedContent CPDF_TextPage::PreMarkedContent(PDFTEXT_Obj Obj) { return FPDFText_MarkedContent::Pass; size_t nContentMark = pTextObj->m_ContentMark.CountItems(); - if (nContentMark < 1) + if (nContentMark == 0) return FPDFText_MarkedContent::Pass; WideString actText; @@ -939,9 +942,9 @@ void CPDF_TextPage::SwapTempTextBuf(int32_t iCharListStartAppend, bool CPDF_TextPage::IsRightToLeft(const CPDF_TextObject* pTextObj, const CPDF_Font* pFont, - int nItems) const { + size_t nItems) const { WideString str; - for (int32_t i = 0; i < nItems; i++) { + for (size_t i = 0; i < nItems; ++i) { CPDF_TextObjectItem item; pTextObj->GetItemInfo(i, &item); if (item.m_CharCode == static_cast<uint32_t>(-1)) @@ -1036,7 +1039,7 @@ void CPDF_TextPage::ProcessTextObject(PDFTEXT_Obj Obj) { } m_pPreTextObj = pTextObj; m_perMatrix = formMatrix; - int nItems = pTextObj->CountItems(); + size_t nItems = pTextObj->CountItems(); float baseSpace = CalculateBaseSpace(pTextObj, matrix); const bool bR2L = IsRightToLeft(pTextObj, pFont, nItems); @@ -1047,7 +1050,7 @@ void CPDF_TextPage::ProcessTextObject(PDFTEXT_Obj Obj) { pdfium::CollectionSize<int32_t>(m_TempCharList); float spacing = 0; - for (int i = 0; i < nItems; i++) { + for (size_t i = 0; i < nItems; ++i) { CPDF_TextObjectItem item; PAGECHAR_INFO charinfo; pTextObj->GetItemInfo(i, &item); @@ -1190,8 +1193,8 @@ void CPDF_TextPage::ProcessTextObject(PDFTEXT_Obj Obj) { CPDF_TextPage::TextOrientation CPDF_TextPage::GetTextObjectWritingMode( const CPDF_TextObject* pTextObj) const { - int32_t nChars = pTextObj->CountChars(); - if (nChars == 1) + size_t nChars = pTextObj->CountChars(); + if (nChars <= 1) return m_TextlineDir; CPDF_TextObjectItem first, last; @@ -1260,13 +1263,19 @@ CPDF_TextPage::GenerateCharacter CPDF_TextPage::ProcessInsertObject( if (WritingMode == TextOrientation::Unknown) WritingMode = GetTextObjectWritingMode(m_pPreTextObj.Get()); - CFX_FloatRect this_rect = pObj->GetRect(); - CFX_FloatRect prev_rect = m_pPreTextObj->GetRect(); + size_t nItem = m_pPreTextObj->CountItems(); + if (nItem == 0) + return GenerateCharacter::None; + CPDF_TextObjectItem PrevItem; - CPDF_TextObjectItem item; - int nItem = m_pPreTextObj->CountItems(); m_pPreTextObj->GetItemInfo(nItem - 1, &PrevItem); + + CPDF_TextObjectItem item; pObj->GetItemInfo(0, &item); + + CFX_FloatRect this_rect = pObj->GetRect(); + CFX_FloatRect prev_rect = m_pPreTextObj->GetRect(); + WideString wstrItem = pObj->GetFont()->UnicodeFromCharCode(item.m_CharCode); if (wstrItem.IsEmpty()) wstrItem += static_cast<wchar_t>(item.m_CharCode); @@ -1350,8 +1359,7 @@ CPDF_TextPage::GenerateCharacter CPDF_TextPage::ProcessInsertObject( : GenerateCharacter::LineBreak; } - int32_t nChars = pObj->CountChars(); - if (nChars == 1 && (0x2D == curChar || 0xAD == curChar) && + if (pObj->CountChars() == 1 && (0x2D == curChar || 0xAD == curChar) && IsHyphen(curChar)) { return GenerateCharacter::Hyphen; } @@ -1424,17 +1432,18 @@ bool CPDF_TextPage::IsSameTextObject(CPDF_TextObject* pTextObj1, if (pTextObj2->GetFontSize() != pTextObj1->GetFontSize()) return false; } - int nPreCount = pTextObj2->CountItems(); - int nCurCount = pTextObj1->CountItems(); - if (nPreCount != nCurCount) + + size_t nPreCount = pTextObj2->CountItems(); + if (nPreCount != pTextObj1->CountItems()) return false; + // If both objects have no items, consider them same. - if (!nPreCount) + if (nPreCount == 0) return true; CPDF_TextObjectItem itemPer; CPDF_TextObjectItem itemCur; - for (int i = 0; i < nPreCount; i++) { + for (size_t i = 0; i < nPreCount; ++i) { pTextObj2->GetItemInfo(i, &itemPer); pTextObj1->GetItemInfo(i, &itemCur); if (itemCur.m_CharCode != itemPer.m_CharCode) diff --git a/core/fpdftext/cpdf_textpage.h b/core/fpdftext/cpdf_textpage.h index 4275d82a18..e8ab82ac2c 100644 --- a/core/fpdftext/cpdf_textpage.h +++ b/core/fpdftext/cpdf_textpage.h @@ -161,7 +161,7 @@ class CPDF_TextPage { void SwapTempTextBuf(int32_t iCharListStartAppend, int32_t iBufStartAppend); bool IsRightToLeft(const CPDF_TextObject* pTextObj, const CPDF_Font* pFont, - int nItems) const; + size_t nItems) const; UnownedPtr<const CPDF_Page> const m_pPage; std::vector<uint16_t> m_CharIndex; diff --git a/fxjs/cjs_document.cpp b/fxjs/cjs_document.cpp index 4885d68e1b..46ece171b0 100644 --- a/fxjs/cjs_document.cpp +++ b/fxjs/cjs_document.cpp @@ -1335,7 +1335,7 @@ int Document::CountWords(CPDF_TextObject* pTextObj) { bool bIsLatin = false; - for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) { + for (size_t i = 0, sz = pTextObj->CountChars(); i < sz; ++i) { uint32_t charcode = CPDF_Font::kInvalidCharCode; float kerning; @@ -1367,7 +1367,7 @@ WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj, int nWordIndex) { int nWords = 0; bool bIsLatin = false; - for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) { + for (size_t i = 0, sz = pTextObj->CountChars(); i < sz; ++i) { uint32_t charcode = CPDF_Font::kInvalidCharCode; float kerning; |