From 56ef173042d786281edcbbc9f1c38c8f97ef10d5 Mon Sep 17 00:00:00 2001 From: Bo Xu Date: Thu, 11 Sep 2014 14:26:42 -0700 Subject: Fix hebrew character highlight issue in a special document There is an image object and text objects in this document, but the character in each text object is reversed. When rendering, the image object is shown. However, when highlighting, the text object is selected, resulting in text index issue. Moreover, the character in the document is in reading order, which is different from normal document. BUG=pdfium:43 R=jbreiden@google.com Review URL: https://codereview.chromium.org/484503002 --- core/src/fpdftext/fpdf_text.cpp | 7 +++- core/src/fpdftext/fpdf_text_int.cpp | 71 +++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/core/src/fpdftext/fpdf_text.cpp b/core/src/fpdftext/fpdf_text.cpp index defad925b9..f633e19517 100644 --- a/core/src/fpdftext/fpdf_text.cpp +++ b/core/src/fpdftext/fpdf_text.cpp @@ -184,7 +184,12 @@ CTextBaseLine* CTextPage::InsertTextBox(CTextBaseLine* pBaseLine, FX_FLOAT basey while (offset < len) { FX_DWORD ch = pFont->GetNextChar(pStr, offset); CFX_WideString unicode_str = pFont->UnicodeFromCharCode(ch); - text += unicode_str; + if (unicode_str.IsEmpty()) { + text += (FX_WCHAR)ch; + } + else { + text += unicode_str; + } } pBaseLine->InsertTextBox(leftx, rightx, topy, bottomy, spacew, fontsize_v, text); return pBaseLine; diff --git a/core/src/fpdftext/fpdf_text_int.cpp b/core/src/fpdftext/fpdf_text_int.cpp index de4c703a04..5b174f0187 100644 --- a/core/src/fpdftext/fpdf_text_int.cpp +++ b/core/src/fpdftext/fpdf_text_int.cpp @@ -1682,6 +1682,53 @@ void CPDF_TextPage::ProcessTextObject(PDFTEXT_Obj Obj) baseSpace = 0.0; } } + + 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; + FX_INT32 iBufStartAppend = m_TempTextBuf.GetLength(); + FX_INT32 iCharListStartAppend = m_TempCharList.GetSize(); + for (int i = 0; i < nItems; i++) { CPDF_TextObjectItem item; PAGECHAR_INFO charinfo; @@ -1828,6 +1875,30 @@ void CPDF_TextPage::ProcessTextObject(PDFTEXT_Obj Obj) } } } + if (bIsBidiAndMirrosInverse) { + FX_INT32 i, j; + i = iCharListStartAppend; + j = m_TempCharList.GetSize() - 1; + PAGECHAR_INFO tempCharInfo; + FX_INT32 tempIndex = 0; + for (; i < j; i++, j--) { + tempCharInfo = m_TempCharList[i]; + m_TempCharList[i] = m_TempCharList[j]; + m_TempCharList[j] = tempCharInfo; + tempIndex = m_TempCharList[i].m_Index; + m_TempCharList[i].m_Index = m_TempCharList[j].m_Index; + m_TempCharList[j].m_Index = tempIndex; + } + FX_WCHAR * pTempBuffer = m_TempTextBuf.GetBuffer(); + i = iBufStartAppend; + j = m_TempTextBuf.GetLength() - 1; + FX_WCHAR wTemp; + for (; i < j; i++, j--) { + wTemp = pTempBuffer[i]; + pTempBuffer[i] = pTempBuffer[j]; + pTempBuffer[j] = wTemp; + } + } } FX_INT32 CPDF_TextPage::GetTextObjectWritingMode(const CPDF_TextObject* pTextObj) { -- cgit v1.2.3