diff options
author | Lei Zhang <thestig@chromium.org> | 2018-06-25 22:01:07 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2018-06-25 22:01:07 +0000 |
commit | 76fb29d5ebae4fea2e5d1221f77e218a9b4b309a (patch) | |
tree | e274d6094b7850c7fc86325da790393a9bd145d8 /core | |
parent | 8c955c9d307272d43d532cd206abc4077076d212 (diff) | |
download | pdfium-chromium/3473.tar.xz |
Clean up CPDF_StreamContentParser::AddTextObject().chromium/3473
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>
Diffstat (limited to 'core')
-rw-r--r-- | core/fpdfapi/page/cpdf_streamcontentparser.cpp | 68 | ||||
-rw-r--r-- | core/fpdfapi/page/cpdf_streamcontentparser.h | 8 | ||||
-rw-r--r-- | core/fpdfapi/page/cpdf_textobject.cpp | 16 | ||||
-rw-r--r-- | core/fpdfapi/page/cpdf_textobject.h | 4 |
4 files changed, 47 insertions, 49 deletions
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<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 @@ 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<float>(), 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<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 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<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 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<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: |