Encapsulate CPVT_Word Encapsulates CPVT_Word member variables by making them private and exposing them via getter methods. Updates all call sites. Introduces `AscentY()` and `DescentY()` to handle coordinate calculations. Removes unused WordPlace member variable and respective constructor parameter. Bug: 40115028 Change-Id: I49137130432993a71bb41a6d1e7ad338092fb781 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/150130 Reviewed-by: Lei Zhang <thestig@chromium.org> Commit-Queue: Seung Hyun Jin <seunghyunjin@google.com>
diff --git a/core/fpdfdoc/cpdf_generateap.cpp b/core/fpdfdoc/cpdf_generateap.cpp index d39980e..11d1564 100644 --- a/core/fpdfdoc/cpdf_generateap.cpp +++ b/core/fpdfdoc/cpdf_generateap.cpp
@@ -332,8 +332,8 @@ } CPVT_Word word; if (vt_iterator->GetWord(word)) { - new_point = - CFX_PointF(word.ptWord.x + offset.x, word.ptWord.y + offset.y); + new_point = CFX_PointF(word.location().x + offset.x, + word.location().y + offset.y); } else { CPVT_Line line; vt_iterator->GetLine(line); @@ -347,35 +347,36 @@ } CPVT_Word word; if (vt_iterator->GetWord(word)) { - if (word.nFontIndex != current_font_index) { + if (word.font_index() != current_font_index) { if (!words.IsEmpty()) { line_stream << GetWordRenderString(words.AsStringView()); words.clear(); } - line_stream << GetFontSetString(font_map, word.nFontIndex, - word.fFontSize); - current_font_index = word.nFontIndex; + line_stream << GetFontSetString(font_map, word.font_index(), + word.font_size()); + current_font_index = word.font_index(); } - words += - GetPDFWordString(font_map, current_font_index, word.Word, sub_word); + words += GetPDFWordString(font_map, current_font_index, word.word(), + sub_word); } oldplace = place; } else { CPVT_Word word; if (vt_iterator->GetWord(word)) { - new_point = - CFX_PointF(word.ptWord.x + offset.x, word.ptWord.y + offset.y); + new_point = CFX_PointF(word.location().x + offset.x, + word.location().y + offset.y); if (new_point != old_point) { WritePoint(edit_stream, new_point - old_point) << " Td\n"; old_point = new_point; } - if (word.nFontIndex != current_font_index) { - edit_stream << GetFontSetString(font_map, word.nFontIndex, - word.fFontSize); - current_font_index = word.nFontIndex; + if (word.font_index() != current_font_index) { + edit_stream << GetFontSetString(font_map, word.font_index(), + word.font_size()); + current_font_index = word.font_index(); } edit_stream << GetWordRenderString( - GetPDFWordString(font_map, current_font_index, word.Word, sub_word) + GetPDFWordString(font_map, current_font_index, word.word(), + sub_word) .AsStringView()); } }
diff --git a/core/fpdfdoc/cpvt_variabletext.cpp b/core/fpdfdoc/cpvt_variabletext.cpp index 64fcdc0..2a881b3 100644 --- a/core/fpdfdoc/cpvt_variabletext.cpp +++ b/core/fpdfdoc/cpvt_variabletext.cpp
@@ -127,7 +127,6 @@ } bool CPVT_VariableText::Iterator::GetWord(CPVT_Word& word) const { - word.WordPlace = cur_pos_; if (!fxcrt::IndexInBounds(vt_->section_array_, cur_pos_.nSecIndex)) { return false; } @@ -142,16 +141,12 @@ return false; } - word.Word = pInfo->Word; - word.nCharset = pInfo->nCharset; - word.fWidth = vt_->GetWordWidth(*pInfo); - word.ptWord = + word = CPVT_Word( + pInfo->Word, pInfo->nCharset, vt_->InToOut(CFX_PointF(pInfo->fWordX + pSection->GetRect().left, - pInfo->fWordY + pSection->GetRect().top)); - word.fAscent = vt_->GetWordAscent(*pInfo); - word.fDescent = vt_->GetWordDescent(*pInfo); - word.nFontIndex = pInfo->nFontIndex; - word.fFontSize = vt_->GetWordFontSize(); + pInfo->fWordY + pSection->GetRect().top)), + vt_->GetWordAscent(*pInfo), vt_->GetWordDescent(*pInfo), + vt_->GetWordWidth(*pInfo), pInfo->nFontIndex, vt_->GetWordFontSize()); return true; }
diff --git a/core/fpdfdoc/cpvt_variabletext_unittest.cpp b/core/fpdfdoc/cpvt_variabletext_unittest.cpp index fbf9a5d..37e95aa 100644 --- a/core/fpdfdoc/cpvt_variabletext_unittest.cpp +++ b/core/fpdfdoc/cpvt_variabletext_unittest.cpp
@@ -76,28 +76,28 @@ it->SetAt(1); // Set to first word index (Place(0, 0, 0)) CPVT_Word word; ASSERT_TRUE(it->GetWord(word)); - EXPECT_EQ('h', word.Word); - float first_x = word.ptWord.x; + EXPECT_EQ('h', word.word()); + float first_x = word.location().x; ASSERT_TRUE(it->NextWord()); ASSERT_TRUE(it->GetWord(word)); - EXPECT_EQ('e', word.Word); - float second_x = word.ptWord.x; + EXPECT_EQ('e', word.word()); + float second_x = word.location().x; ASSERT_TRUE(it->NextWord()); ASSERT_TRUE(it->GetWord(word)); - EXPECT_EQ('l', word.Word); - float third_x = word.ptWord.x; + EXPECT_EQ('l', word.word()); + float third_x = word.location().x; ASSERT_TRUE(it->NextWord()); ASSERT_TRUE(it->GetWord(word)); - EXPECT_EQ('l', word.Word); - float fourth_x = word.ptWord.x; + EXPECT_EQ('l', word.word()); + float fourth_x = word.location().x; ASSERT_TRUE(it->NextWord()); ASSERT_TRUE(it->GetWord(word)); - EXPECT_EQ('o', word.Word); - float fifth_x = word.ptWord.x; + EXPECT_EQ('o', word.word()); + float fifth_x = word.location().x; EXPECT_FALSE(it->NextWord()); @@ -125,23 +125,23 @@ it->SetAt(1); // Set to first word index (Place(0, 0, 0)) CPVT_Word word; ASSERT_TRUE(it->GetWord(word)); - EXPECT_EQ(0x05E9, word.Word); - float first_x = word.ptWord.x; + EXPECT_EQ(0x05E9, word.word()); + float first_x = word.location().x; ASSERT_TRUE(it->NextWord()); ASSERT_TRUE(it->GetWord(word)); - EXPECT_EQ(0x05DC, word.Word); - float second_x = word.ptWord.x; + EXPECT_EQ(0x05DC, word.word()); + float second_x = word.location().x; ASSERT_TRUE(it->NextWord()); ASSERT_TRUE(it->GetWord(word)); - EXPECT_EQ(0x05D5, word.Word); - float third_x = word.ptWord.x; + EXPECT_EQ(0x05D5, word.word()); + float third_x = word.location().x; ASSERT_TRUE(it->NextWord()); ASSERT_TRUE(it->GetWord(word)); - EXPECT_EQ(0x05DD, word.Word); - float fourth_x = word.ptWord.x; + EXPECT_EQ(0x05DD, word.word()); + float fourth_x = word.location().x; EXPECT_FALSE(it->NextWord());
diff --git a/core/fpdfdoc/cpvt_word.cpp b/core/fpdfdoc/cpvt_word.cpp index fe29b15..103ea7e 100644 --- a/core/fpdfdoc/cpvt_word.cpp +++ b/core/fpdfdoc/cpvt_word.cpp
@@ -5,6 +5,24 @@ #include "core/fpdfdoc/cpvt_word.h" CPVT_Word::CPVT_Word() = default; + +CPVT_Word::CPVT_Word(uint16_t word, + FX_Charset charset, + const CFX_PointF& location, + float ascent, + float descent, + float width, + int32_t font_index, + float font_size) + : Word(word), + nCharset(charset), + ptWord(location), + fAscent(ascent), + fDescent(descent), + fWidth(width), + nFontIndex(font_index), + fFontSize(font_size) {} + CPVT_Word::CPVT_Word(const CPVT_Word&) = default; CPVT_Word& CPVT_Word::operator=(const CPVT_Word&) = default; CPVT_Word::~CPVT_Word() = default;
diff --git a/core/fpdfdoc/cpvt_word.h b/core/fpdfdoc/cpvt_word.h index 5c8bb44..3e3555e 100644 --- a/core/fpdfdoc/cpvt_word.h +++ b/core/fpdfdoc/cpvt_word.h
@@ -9,20 +9,40 @@ #include <stdint.h> -#include "core/fpdfdoc/cpvt_wordplace.h" #include "core/fxcrt/fx_codepage.h" #include "core/fxcrt/fx_coordinates.h" class CPVT_Word { public: CPVT_Word(); + CPVT_Word(uint16_t word, + FX_Charset charset, + const CFX_PointF& location, + float ascent, + float descent, + float width, + int32_t font_index, + float font_size); CPVT_Word(const CPVT_Word&); CPVT_Word& operator=(const CPVT_Word&); ~CPVT_Word(); + uint16_t word() const { return Word; } + FX_Charset charset() const { return nCharset; } + int32_t font_index() const { return nFontIndex; } + const CFX_PointF& location() const { return ptWord; } + void set_location(const CFX_PointF& location) { ptWord = location; } + float width() const { return fWidth; } + float font_size() const { return fFontSize; } + float ascent() const { return fAscent; } + float descent() const { return fDescent; } + + float AscentY() const { return ptWord.y + fAscent; } + float DescentY() const { return ptWord.y + fDescent; } + + private: uint16_t Word = 0; FX_Charset nCharset = FX_Charset::kANSI; - CPVT_WordPlace WordPlace; CFX_PointF ptWord; float fAscent = 0.0f; float fDescent = 0.0f;
diff --git a/fpdfsdk/cpdfsdk_appstream.cpp b/fpdfsdk/cpdfsdk_appstream.cpp index 55b3e2f..483d0b7 100644 --- a/fpdfsdk/cpdfsdk_appstream.cpp +++ b/fpdfsdk/cpdfsdk_appstream.cpp
@@ -660,8 +660,8 @@ CPVT_Word word; if (pIterator->GetWord(word)) { - ptNew = CFX_PointF(word.ptWord.x + ptOffset.x, - word.ptWord.y + ptOffset.y); + ptNew = CFX_PointF(word.location().x + ptOffset.x, + word.location().y + ptOffset.y); } else { CPVT_Line line; pIterator->GetLine(line); @@ -679,37 +679,37 @@ CPVT_Word word; if (pIterator->GetWord(word)) { - if (word.nFontIndex != nCurFontIndex) { + if (word.font_index() != nCurFontIndex) { if (!sWords.IsEmpty()) { sEditStream << GetWordRenderString(sWords.AsStringView()); sWords.clear(); } - sEditStream << GetFontSetString(pEdit->GetFontMap(), word.nFontIndex, - word.fFontSize); - nCurFontIndex = word.nFontIndex; + sEditStream << GetFontSetString(pEdit->GetFontMap(), + word.font_index(), word.font_size()); + nCurFontIndex = word.font_index(); } - sWords += pEdit->GetPDFWordString(nCurFontIndex, word.Word, SubWord); + sWords += pEdit->GetPDFWordString(nCurFontIndex, word.word(), SubWord); } oldplace = place; } else { CPVT_Word word; if (pIterator->GetWord(word)) { - ptNew = - CFX_PointF(word.ptWord.x + ptOffset.x, word.ptWord.y + ptOffset.y); + ptNew = CFX_PointF(word.location().x + ptOffset.x, + word.location().y + ptOffset.y); if (ptNew.x != ptOld.x || ptNew.y != ptOld.y) { WritePoint(sEditStream, {ptNew.x - ptOld.x, ptNew.y - ptOld.y}) << " " << kMoveTextPositionOperator << "\n"; ptOld = ptNew; } - if (word.nFontIndex != nCurFontIndex) { - sEditStream << GetFontSetString(pEdit->GetFontMap(), word.nFontIndex, - word.fFontSize); - nCurFontIndex = word.nFontIndex; + if (word.font_index() != nCurFontIndex) { + sEditStream << GetFontSetString(pEdit->GetFontMap(), + word.font_index(), word.font_size()); + nCurFontIndex = word.font_index(); } sEditStream << GetWordRenderString( - pEdit->GetPDFWordString(nCurFontIndex, word.Word, SubWord) + pEdit->GetPDFWordString(nCurFontIndex, word.word(), SubWord) .AsStringView()); } }
diff --git a/fpdfsdk/pwl/cpwl_edit.cpp b/fpdfsdk/pwl/cpwl_edit.cpp index 4f688d8..ef21e17 100644 --- a/fpdfsdk/pwl/cpwl_edit.cpp +++ b/fpdfsdk/pwl/cpwl_edit.cpp
@@ -673,10 +673,10 @@ CPVT_Word word; CPVT_Line line; if (pIterator->GetWord(word)) { - ptHead->x = word.ptWord.x + word.fWidth; - ptHead->y = word.ptWord.y + word.fAscent; - ptFoot->x = word.ptWord.x + word.fWidth; - ptFoot->y = word.ptWord.y + word.fDescent; + ptHead->x = word.location().x + word.width(); + ptHead->y = word.AscentY(); + ptFoot->x = word.location().x + word.width(); + ptFoot->y = word.DescentY(); } else if (pIterator->GetLine(line)) { ptHead->x = line.ptLine.x; ptHead->y = line.ptLine.y + line.fLineAscent;
diff --git a/fpdfsdk/pwl/cpwl_edit_impl.cpp b/fpdfsdk/pwl/cpwl_edit_impl.cpp index 5d176d0..bcff631 100644 --- a/fpdfsdk/pwl/cpwl_edit_impl.cpp +++ b/fpdfsdk/pwl/cpwl_edit_impl.cpp
@@ -65,7 +65,7 @@ CHECK(edit_); if (vt_iterator_->GetWord(word)) { - word.ptWord = edit_->VTToEdit(word.ptWord); + word.set_location(edit_->VTToEdit(word.location())); return true; } return false; @@ -650,15 +650,16 @@ CPVT_Line line; pIterator->GetLine(line); if (pFillerNotify->IsSelectionImplemented()) { - CFX_FloatRect rc(word.ptWord.x, line.ptLine.y + line.fLineDescent, - word.ptWord.x + word.fWidth, + CFX_FloatRect rc(word.location().x, line.ptLine.y + line.fLineDescent, + word.location().x + word.width(), line.ptLine.y + line.fLineAscent); rc.Intersect(rcClip); pFillerNotify->OutputSelectedRect(pSystemData, rc); } else { CFX_Path pathSelBK; - pathSelBK.AppendRect(word.ptWord.x, line.ptLine.y + line.fLineDescent, - word.ptWord.x + word.fWidth, + pathSelBK.AppendRect(word.location().x, + line.ptLine.y + line.fLineDescent, + word.location().x + word.width(), line.ptLine.y + line.fLineAscent); pDevice->DrawPath(pathSelBK, &mtUser2Device, nullptr, crSelBK, 0, @@ -666,7 +667,7 @@ } } if (bContinuous) { - if (place.LineCmp(oldplace) != 0 || word.nFontIndex != nFontIndex || + if (place.LineCmp(oldplace) != 0 || word.font_index() != nFontIndex || crOldFill != crCurFill) { if (!sTextBuf.IsEmpty()) { DrawTextString(pDevice, @@ -675,18 +676,20 @@ mtUser2Device, sTextBuf, crOldFill); sTextBuf.clear(); } - nFontIndex = word.nFontIndex; - ptBT = word.ptWord; + nFontIndex = word.font_index(); + ptBT = word.location(); crOldFill = crCurFill; } - sTextBuf += GetPDFWordString(word.nFontIndex, word.Word, SubWord); + sTextBuf += GetPDFWordString(word.font_index(), word.word(), SubWord); } else { DrawTextString( pDevice, - CFX_PointF(word.ptWord.x + ptOffset.x, word.ptWord.y + ptOffset.y), - font_map->GetPDFFont(word.nFontIndex).Get(), fFontSize, + CFX_PointF(word.location().x + ptOffset.x, + word.location().y + ptOffset.y), + font_map->GetPDFFont(word.font_index()).Get(), fFontSize, mtUser2Device, - GetPDFWordString(word.nFontIndex, word.Word, SubWord), crCurFill); + GetPDFWordString(word.font_index(), word.word(), SubWord), + crCurFill); } oldplace = place; } @@ -856,7 +859,7 @@ while (pIterator->NextWord()) { CPVT_WordPlace place = pIterator->GetWordPlace(); if (pIterator->GetWord(wordinfo)) { - swRet += wordinfo.Word; + swRet += wordinfo.word(); } if (oldplace.nSecIndex != place.nSecIndex) { swRet += L"\r\n"; @@ -886,7 +889,7 @@ break; } if (pIterator->GetWord(wordinfo)) { - swRet += wordinfo.Word; + swRet += wordinfo.word(); } if (oldplace.nSecIndex != place.nSecIndex) { swRet += L"\r\n"; @@ -1224,10 +1227,10 @@ CPVT_Word word; CPVT_Line line; if (pIterator->GetWord(word)) { - ptHead.x = word.ptWord.x + word.fWidth; - ptHead.y = word.ptWord.y + word.fAscent; - ptFoot.x = word.ptWord.x + word.fWidth; - ptFoot.y = word.ptWord.y + word.fDescent; + ptHead.x = word.location().x + word.width(); + ptHead.y = word.AscentY(); + ptFoot.x = word.location().x + word.width(); + ptFoot.y = word.DescentY(); } else if (pIterator->GetLine(line)) { ptHead.x = line.ptLine.x; ptHead.y = line.ptLine.y + line.fLineAscent; @@ -1340,9 +1343,9 @@ pIterator->GetLine(lineinfo); if (place.LineCmp(wrTemp.BeginPos) == 0 || place.LineCmp(wrTemp.EndPos) == 0) { - CFX_FloatRect rcWord(wordinfo.ptWord.x, + CFX_FloatRect rcWord(wordinfo.location().x, lineinfo.ptLine.y + lineinfo.fLineDescent, - wordinfo.ptWord.x + wordinfo.fWidth, + wordinfo.location().x + wordinfo.width(), lineinfo.ptLine.y + lineinfo.fLineAscent); if (notify_) { @@ -1393,10 +1396,10 @@ CPVT_Word word; CPVT_Line line; if (pIterator->GetWord(word)) { - ptHead.x = word.ptWord.x + word.fWidth; - ptHead.y = word.ptWord.y + word.fAscent; - ptFoot.x = word.ptWord.x + word.fWidth; - ptFoot.y = word.ptWord.y + word.fDescent; + ptHead.x = word.location().x + word.width(); + ptHead.y = word.AscentY(); + ptFoot.x = word.location().x + word.width(); + ptFoot.y = word.DescentY(); } else if (pIterator->GetLine(line)) { ptHead.x = line.ptLine.x; ptHead.y = line.ptLine.y + line.fLineAscent; @@ -1737,7 +1740,7 @@ if (bAddUndo && enable_undo_) { AddEditUndoItem(std::make_unique<UndoBackspace>( - this, wp_old_caret_, wp_caret_, word.Word, word.nCharset)); + this, wp_old_caret_, wp_caret_, word.word(), word.charset())); } RearrangePart(CPVT_WordRange(wp_caret_, wp_old_caret_)); ScrollToCaret(); @@ -1763,11 +1766,13 @@ sel_state_.Set(wp_caret_, wp_caret_); if (bAddUndo && enable_undo_) { if (bSecEnd) { - AddEditUndoItem(std::make_unique<UndoDelete>( - this, wp_old_caret_, wp_caret_, word.Word, word.nCharset, bSecEnd)); + AddEditUndoItem(std::make_unique<UndoDelete>(this, wp_old_caret_, + wp_caret_, word.word(), + word.charset(), bSecEnd)); } else { - AddEditUndoItem(std::make_unique<UndoDelete>( - this, wp_old_caret_, wp_caret_, word.Word, word.nCharset, bSecEnd)); + AddEditUndoItem(std::make_unique<UndoDelete>(this, wp_old_caret_, + wp_caret_, word.word(), + word.charset(), bSecEnd)); } } RearrangePart(CPVT_WordRange(wp_old_caret_, wp_caret_)); @@ -1933,8 +1938,8 @@ CPVT_Word word; CPVT_Line line; if (pIterator->GetWord(word)) { - caret_point_.x = word.ptWord.x + word.fWidth; - caret_point_.y = word.ptWord.y; + caret_point_.x = word.location().x + word.width(); + caret_point_.y = word.location().y; } else if (pIterator->GetLine(line)) { caret_point_.x = line.ptLine.x; caret_point_.y = line.ptLine.y;
diff --git a/fpdfsdk/pwl/cpwl_list_ctrl.cpp b/fpdfsdk/pwl/cpwl_list_ctrl.cpp index 75f910b..d239740 100644 --- a/fpdfsdk/pwl/cpwl_list_ctrl.cpp +++ b/fpdfsdk/pwl/cpwl_list_ctrl.cpp
@@ -50,7 +50,7 @@ CPWL_EditImpl::Iterator* pIterator = edit_->GetIterator(); pIterator->SetAt(1); pIterator->GetWord(word); - return word.Word; + return word.word(); } WideString CPWL_ListCtrl::Item::GetText() const {