From 76fb29d5ebae4fea2e5d1221f77e218a9b4b309a Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Mon, 25 Jun 2018 22:01:07 +0000 Subject: Clean up CPDF_StreamContentParser::AddTextObject(). Change-Id: I1794848607f2db3f1ef39dbd221b7219feb9254c Reviewed-on: https://pdfium-review.googlesource.com/35990 Reviewed-by: Henrique Nakashima Commit-Queue: Lei Zhang --- core/fpdfapi/page/cpdf_streamcontentparser.cpp | 68 ++++++++++++-------------- core/fpdfapi/page/cpdf_streamcontentparser.h | 8 +-- core/fpdfapi/page/cpdf_textobject.cpp | 16 +++--- core/fpdfapi/page/cpdf_textobject.h | 4 +- 4 files changed, 47 insertions(+), 49 deletions(-) (limited to 'core/fpdfapi/page') diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.cpp b/core/fpdfapi/page/cpdf_streamcontentparser.cpp index 2fbb73b6b2..9d6e86def0 100644 --- a/core/fpdfapi/page/cpdf_streamcontentparser.cpp +++ b/core/fpdfapi/page/cpdf_streamcontentparser.cpp @@ -1200,28 +1200,23 @@ CPDF_Pattern* CPDF_StreamContentParser::FindPattern(const ByteString& name, 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& 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 @@ void CPDF_StreamContentParser::AddTextObject(ByteString* pStrs, 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 @@ void CPDF_StreamContentParser::AddTextObject(ByteString* pStrs, } 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 @@ int32_t CPDF_StreamContentParser::GetCurrentStreamIndex() { 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(), 1); } void CPDF_StreamContentParser::Handle_ShowText_Positioning() { @@ -1292,10 +1287,9 @@ void CPDF_StreamContentParser::Handle_ShowText_Positioning() { } 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 @@ void CPDF_StreamContentParser::Handle_ShowText_Positioning() { 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 adcb2a5b47..a5efe48d46 100644 --- a/core/fpdfapi/page/cpdf_streamcontentparser.h +++ b/core/fpdfapi/page/cpdf_streamcontentparser.h @@ -102,10 +102,12 @@ class CPDF_StreamContentParser { return static_cast(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& 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 e678d5fc10..9da96f54f7 100644 --- a/core/fpdfapi/page/cpdf_textobject.cpp +++ b/core/fpdfapi/page/cpdf_textobject.cpp @@ -148,34 +148,34 @@ CFX_Matrix CPDF_TextObject::GetTextMatrix() const { } void CPDF_TextObject::SetSegments(const ByteString* pStrs, - const float* pKerning, - int nsegs) { + const std::vector& 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(), 1); RecalcPositionData(); SetDirty(true); } diff --git a/core/fpdfapi/page/cpdf_textobject.h b/core/fpdfapi/page/cpdf_textobject.h index d3b6dcc3de..ac17c6df68 100644 --- a/core/fpdfapi/page/cpdf_textobject.h +++ b/core/fpdfapi/page/cpdf_textobject.h @@ -60,7 +60,9 @@ class CPDF_TextObject : public CPDF_PageObject { const std::vector& GetCharCodes() const { return m_CharCodes; } const std::vector& GetCharPositions() const { return m_CharPos; } - void SetSegments(const ByteString* pStrs, const float* pKerning, int nSegs); + void SetSegments(const ByteString* pStrs, + const std::vector& kernings, + size_t nSegs); CFX_PointF CalcPositionData(float horz_scale); private: -- cgit v1.2.3