Clean up CPDF_StreamContentParser::AddTextObject(). Change-Id: I1794848607f2db3f1ef39dbd221b7219feb9254c Reviewed-on: https://pdfium-review.googlesource.com/35990 Reviewed-by: Henrique Nakashima <hnakashima@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.cpp b/core/fpdfapi/page/cpdf_streamcontentparser.cpp index 2fbb73b..9d6e86d 100644 --- a/core/fpdfapi/page/cpdf_streamcontentparser.cpp +++ b/core/fpdfapi/page/cpdf_streamcontentparser.cpp
@@ -1200,28 +1200,23 @@ m_pCurStates->m_ParentMatrix); } -void CPDF_StreamContentParser::AddTextObject(ByteString* pStrs, +void CPDF_StreamContentParser::AddTextObject(const ByteString* pStrs, float fInitKerning, - float* pKerning, - int nsegs) { + const std::vector<float>& kernings, + size_t nSegs) { CPDF_Font* pFont = m_pCurStates->m_TextState.GetFont(); - if (!pFont) { + if (!pFont) return; - } + if (fInitKerning != 0) { - if (!pFont->IsVertWriting()) { - m_pCurStates->m_TextPos.x -= - (fInitKerning * m_pCurStates->m_TextState.GetFontSize() * - m_pCurStates->m_TextHorzScale) / - 1000; - } else { - m_pCurStates->m_TextPos.y -= - (fInitKerning * m_pCurStates->m_TextState.GetFontSize()) / 1000; - } + if (pFont->IsVertWriting()) + m_pCurStates->m_TextPos.y -= GetVerticalTextSize(fInitKerning); + else + m_pCurStates->m_TextPos.x -= GetHorizontalTextSize(fInitKerning); } - if (nsegs == 0) { + if (nSegs == 0) return; - } + const TextRenderingMode text_mode = pFont->IsType3Font() ? TextRenderingMode::MODE_FILL : m_pCurStates->m_TextState.GetTextMode(); @@ -1236,7 +1231,7 @@ pCTM[2] = m_pCurStates->m_CTM.b; pCTM[3] = m_pCurStates->m_CTM.d; } - pText->SetSegments(pStrs, pKerning, nsegs); + pText->SetSegments(pStrs, kernings, nSegs); pText->SetPosition( m_mtContentToUser.Transform(m_pCurStates->m_CTM.Transform( m_pCurStates->m_TextMatrix.Transform(CFX_PointF( @@ -1251,20 +1246,22 @@ } m_pObjectHolder->AppendPageObject(std::move(pText)); } - if (pKerning && pKerning[nsegs - 1] != 0) { - if (!pFont->IsVertWriting()) { - m_pCurStates->m_TextPos.x -= - (pKerning[nsegs - 1] * m_pCurStates->m_TextState.GetFontSize() * - m_pCurStates->m_TextHorzScale) / - 1000; - } else { - m_pCurStates->m_TextPos.y -= - (pKerning[nsegs - 1] * m_pCurStates->m_TextState.GetFontSize()) / - 1000; - } + if (!kernings.empty() && kernings[nSegs - 1] != 0) { + if (pFont->IsVertWriting()) + m_pCurStates->m_TextPos.y -= GetVerticalTextSize(kernings[nSegs - 1]); + else + m_pCurStates->m_TextPos.x -= GetHorizontalTextSize(kernings[nSegs - 1]); } } +float CPDF_StreamContentParser::GetHorizontalTextSize(float fKerning) const { + return GetVerticalTextSize(fKerning) * m_pCurStates->m_TextHorzScale; +} + +float CPDF_StreamContentParser::GetVerticalTextSize(float fKerning) const { + return fKerning * m_pCurStates->m_TextState.GetFontSize() / 1000; +} + int32_t CPDF_StreamContentParser::GetCurrentStreamIndex() { auto it = std::upper_bound(m_StreamStartOffsets.begin(), m_StreamStartOffsets.end(), m_pSyntax->GetPos()); @@ -1273,10 +1270,8 @@ void CPDF_StreamContentParser::Handle_ShowText() { ByteString str = GetString(0); - if (str.IsEmpty()) { - return; - } - AddTextObject(&str, 0, nullptr, 1); + if (!str.IsEmpty()) + AddTextObject(&str, 0, std::vector<float>(), 1); } void CPDF_StreamContentParser::Handle_ShowText_Positioning() { @@ -1292,10 +1287,9 @@ } if (nsegs == 0) { for (size_t i = 0; i < n; i++) { - m_pCurStates->m_TextPos.x -= - (pArray->GetNumberAt(i) * m_pCurStates->m_TextState.GetFontSize() * - m_pCurStates->m_TextHorzScale) / - 1000; + float fKerning = pArray->GetNumberAt(i); + if (fKerning != 0) + m_pCurStates->m_TextPos.x -= GetHorizontalTextSize(fKerning); } return; } @@ -1319,7 +1313,7 @@ kernings[iSegment - 1] += num; } } - AddTextObject(strs.data(), fInitKerning, kernings.data(), iSegment); + AddTextObject(strs.data(), fInitKerning, kernings, iSegment); } void CPDF_StreamContentParser::Handle_SetTextLeading() {
diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.h b/core/fpdfapi/page/cpdf_streamcontentparser.h index adcb2a5..a5efe48 100644 --- a/core/fpdfapi/page/cpdf_streamcontentparser.h +++ b/core/fpdfapi/page/cpdf_streamcontentparser.h
@@ -102,10 +102,12 @@ return static_cast<int>(GetNumber(index)); } void OnOperator(const ByteStringView& op); - void AddTextObject(ByteString* pText, + void AddTextObject(const ByteString* pStrs, float fInitKerning, - float* pKerning, - int count); + const std::vector<float>& kernings, + size_t nSegs); + float GetHorizontalTextSize(float fKerning) const; + float GetVerticalTextSize(float fKerning) const; void OnChangeTextMatrix(); void ParsePathObject();
diff --git a/core/fpdfapi/page/cpdf_textobject.cpp b/core/fpdfapi/page/cpdf_textobject.cpp index e678d5f..9da96f5 100644 --- a/core/fpdfapi/page/cpdf_textobject.cpp +++ b/core/fpdfapi/page/cpdf_textobject.cpp
@@ -148,34 +148,34 @@ } void CPDF_TextObject::SetSegments(const ByteString* pStrs, - const float* pKerning, - int nsegs) { + const std::vector<float>& kernings, + size_t nSegs) { m_CharCodes.clear(); m_CharPos.clear(); CPDF_Font* pFont = m_TextState.GetFont(); int nChars = 0; - for (int i = 0; i < nsegs; ++i) + for (size_t i = 0; i < nSegs; ++i) nChars += pFont->CountChar(pStrs[i].AsStringView()); - nChars += nsegs - 1; + nChars += nSegs - 1; m_CharCodes.resize(nChars); m_CharPos.resize(nChars - 1); size_t index = 0; - for (int i = 0; i < nsegs; ++i) { + for (size_t i = 0; i < nSegs; ++i) { ByteStringView segment = pStrs[i].AsStringView(); size_t offset = 0; while (offset < segment.GetLength()) { ASSERT(index < m_CharCodes.size()); m_CharCodes[index++] = pFont->GetNextChar(segment, offset); } - if (i != nsegs - 1) { - m_CharPos[index - 1] = pKerning[i]; + if (i != nSegs - 1) { + m_CharPos[index - 1] = kernings[i]; m_CharCodes[index++] = CPDF_Font::kInvalidCharCode; } } } void CPDF_TextObject::SetText(const ByteString& str) { - SetSegments(&str, nullptr, 1); + SetSegments(&str, std::vector<float>(), 1); RecalcPositionData(); SetDirty(true); }
diff --git a/core/fpdfapi/page/cpdf_textobject.h b/core/fpdfapi/page/cpdf_textobject.h index d3b6dcc..ac17c6d 100644 --- a/core/fpdfapi/page/cpdf_textobject.h +++ b/core/fpdfapi/page/cpdf_textobject.h
@@ -60,7 +60,9 @@ const std::vector<uint32_t>& GetCharCodes() const { return m_CharCodes; } const std::vector<float>& GetCharPositions() const { return m_CharPos; } - void SetSegments(const ByteString* pStrs, const float* pKerning, int nSegs); + void SetSegments(const ByteString* pStrs, + const std::vector<float>& kernings, + size_t nSegs); CFX_PointF CalcPositionData(float horz_scale); private: