From 6c06469226a127bbbca103c08d282fe2e711c89d Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Fri, 19 Dec 2014 21:54:15 -0800 Subject: Refactor some code in CPDF_TextPage::ProcessTextObject(). Fix a memory leak while we are at it. R=bo_xu@foxitsoftware.com Review URL: https://codereview.chromium.org/813153002 --- core/src/fpdftext/fpdf_text_int.cpp | 120 +++++++++++++++++++----------------- core/src/fpdftext/text_int.h | 5 ++ 2 files changed, 70 insertions(+), 55 deletions(-) (limited to 'core/src/fpdftext') diff --git a/core/src/fpdftext/fpdf_text_int.cpp b/core/src/fpdftext/fpdf_text_int.cpp index 4650272e4c..c9cfbdda2d 100644 --- a/core/src/fpdftext/fpdf_text_int.cpp +++ b/core/src/fpdftext/fpdf_text_int.cpp @@ -1690,49 +1690,9 @@ void CPDF_TextPage::ProcessTextObject(PDFTEXT_Obj Obj) int nItems = pTextObj->CountItems(); FX_FLOAT baseSpace = _CalculateBaseSpace(pTextObj, matrix); - FX_BOOL bIsBidiAndMirrosInverse = FALSE; - IFX_BidiChar* BidiChar = IFX_BidiChar::Create(); - FX_INT32 nR2L = 0; - FX_INT32 nL2R = 0; - FX_INT32 start = 0, count = 0; - CPDF_TextObjectItem item; - for (FX_INT32 i = 0; i < nItems; i++) { - pTextObj->GetItemInfo(i, &item); - if (item.m_CharCode == (FX_DWORD)-1) { - continue; - } - CFX_WideString wstrItem = pFont->UnicodeFromCharCode(item.m_CharCode); - FX_WCHAR wChar = wstrItem.GetAt(0); - if ((wstrItem.IsEmpty() || wChar == 0) && item.m_CharCode) { - wChar = (FX_WCHAR)item.m_CharCode; - } - if (!wChar) { - continue; - } - if (BidiChar && BidiChar->AppendChar(wChar)) { - FX_INT32 ret = BidiChar->GetBidiInfo(start, count); - if (ret == 2) { - nR2L++; - } - else if (ret == 1) { - nL2R++; - } - } - } - if (BidiChar && BidiChar->EndChar()) { - FX_INT32 ret = BidiChar->GetBidiInfo(start, count); - if (ret == 2) { - nR2L++; - } - else if (ret == 1) { - nL2R++; - } - } - FX_BOOL bR2L = FALSE; - if (nR2L > 0 && nR2L >= nL2R) { - bR2L = TRUE; - } - bIsBidiAndMirrosInverse = bR2L && (matrix.a * matrix.d - matrix.b * matrix.c) < 0; + const FX_BOOL bR2L = IsRightToLeft(pTextObj, pFont, nItems); + const FX_BOOL bIsBidiAndMirrorInverse = + bR2L && (matrix.a * matrix.d - matrix.b * matrix.c) < 0; FX_INT32 iBufStartAppend = m_TempTextBuf.GetLength(); FX_INT32 iCharListStartAppend = m_TempCharList.GetSize(); @@ -1877,21 +1837,71 @@ void CPDF_TextPage::ProcessTextObject(PDFTEXT_Obj Obj) } } } - if (bIsBidiAndMirrosInverse) { - FX_INT32 i, j; - i = iCharListStartAppend; - j = m_TempCharList.GetSize() - 1; - for (; i < j; i++, j--) { - std::swap(m_TempCharList[i], m_TempCharList[j]); - std::swap(m_TempCharList[i].m_Index, m_TempCharList[j].m_Index); + if (bIsBidiAndMirrorInverse) { + SwapTempTextBuf(iCharListStartAppend, iBufStartAppend); + } +} +void CPDF_TextPage::SwapTempTextBuf(FX_INT32 iCharListStartAppend, + FX_INT32 iBufStartAppend) +{ + FX_INT32 i, j; + i = iCharListStartAppend; + j = m_TempCharList.GetSize() - 1; + for (; i < j; i++, j--) { + std::swap(m_TempCharList[i], m_TempCharList[j]); + std::swap(m_TempCharList[i].m_Index, m_TempCharList[j].m_Index); + } + FX_WCHAR * pTempBuffer = m_TempTextBuf.GetBuffer(); + i = iBufStartAppend; + j = m_TempTextBuf.GetLength() - 1; + for (; i < j; i++, j--) { + std::swap(pTempBuffer[i], pTempBuffer[j]); + } +} +FX_BOOL CPDF_TextPage::IsRightToLeft(const CPDF_TextObject* pTextObj, + const CPDF_Font* pFont, + int nItems) const +{ + IFX_BidiChar* BidiChar = IFX_BidiChar::Create(); + FX_INT32 nR2L = 0; + FX_INT32 nL2R = 0; + FX_INT32 start = 0, count = 0; + CPDF_TextObjectItem item; + for (FX_INT32 i = 0; i < nItems; i++) { + pTextObj->GetItemInfo(i, &item); + if (item.m_CharCode == (FX_DWORD)-1) { + continue; + } + CFX_WideString wstrItem = pFont->UnicodeFromCharCode(item.m_CharCode); + FX_WCHAR wChar = wstrItem.GetAt(0); + if ((wstrItem.IsEmpty() || wChar == 0) && item.m_CharCode) { + wChar = (FX_WCHAR)item.m_CharCode; } - FX_WCHAR * pTempBuffer = m_TempTextBuf.GetBuffer(); - i = iBufStartAppend; - j = m_TempTextBuf.GetLength() - 1; - for (; i < j; i++, j--) { - std::swap(pTempBuffer[i], pTempBuffer[j]); + if (!wChar) { + continue; + } + if (BidiChar && BidiChar->AppendChar(wChar)) { + FX_INT32 ret = BidiChar->GetBidiInfo(start, count); + if (ret == 2) { + nR2L++; + } + else if (ret == 1) { + nL2R++; + } + } + } + if (BidiChar && BidiChar->EndChar()) { + FX_INT32 ret = BidiChar->GetBidiInfo(start, count); + if (ret == 2) { + nR2L++; + } + else if (ret == 1) { + nL2R++; } } + if (BidiChar) + BidiChar->Release(); + return (nR2L > 0 && nR2L >= nL2R); } FX_INT32 CPDF_TextPage::GetTextObjectWritingMode(const CPDF_TextObject* pTextObj) { diff --git a/core/src/fpdftext/text_int.h b/core/src/fpdftext/text_int.h index 65ffddea0a..c3f3b7108e 100644 --- a/core/src/fpdftext/text_int.h +++ b/core/src/fpdftext/text_int.h @@ -115,6 +115,11 @@ private: void AddCharInfoByRLDirection(CFX_WideString& str, int i); FX_INT32 GetTextObjectWritingMode(const CPDF_TextObject* pTextObj); FX_INT32 FindTextlineFlowDirection(); + void SwapTempTextBuf(FX_INT32 iCharListStartAppend, + FX_INT32 iBufStartAppend); + FX_BOOL IsRightToLeft(const CPDF_TextObject* pTextObj, + const CPDF_Font* pFont, + int nItems) const; protected: CPDFText_ParseOptions m_ParseOptions; CFX_WordArray m_CharIndex; -- cgit v1.2.3