Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / pdfium / core / src / fpdftext / fpdf_text_int.cpp
index de4c703..5b174f0 100644 (file)
@@ -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) {\r
+        FX_INT32 i, j;\r
+        i = iCharListStartAppend;\r
+        j = m_TempCharList.GetSize() - 1;\r
+        PAGECHAR_INFO tempCharInfo;\r
+        FX_INT32 tempIndex = 0;\r
+        for (; i < j; i++, j--) {\r
+            tempCharInfo = m_TempCharList[i];\r
+            m_TempCharList[i] = m_TempCharList[j];\r
+            m_TempCharList[j] = tempCharInfo;\r
+            tempIndex = m_TempCharList[i].m_Index;\r
+            m_TempCharList[i].m_Index = m_TempCharList[j].m_Index;\r
+            m_TempCharList[j].m_Index = tempIndex;\r
+        }\r
+        FX_WCHAR * pTempBuffer = m_TempTextBuf.GetBuffer();\r
+        i = iBufStartAppend;\r
+        j = m_TempTextBuf.GetLength() - 1;\r
+        FX_WCHAR wTemp;\r
+        for (; i < j; i++, j--) {\r
+            wTemp = pTempBuffer[i];\r
+            pTempBuffer[i] = pTempBuffer[j];\r
+            pTempBuffer[j] = wTemp;\r
+        }\r
+    }\r
 }
 FX_INT32 CPDF_TextPage::GetTextObjectWritingMode(const CPDF_TextObject* pTextObj)
 {