OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "xfa/fde/cfx_wordbreak.h" | 7 #include "xfa/fde/cfx_wordbreak.h" |
8 | 8 |
9 #include "xfa/fde/cfx_chariter.h" | 9 #include "xfa/fde/cfx_chariter.h" |
10 | 10 |
(...skipping 2771 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2782 | 2782 |
2783 void CFX_WordBreak::Attach(IFX_CharIter* pIter) { | 2783 void CFX_WordBreak::Attach(IFX_CharIter* pIter) { |
2784 ASSERT(pIter); | 2784 ASSERT(pIter); |
2785 m_pCurIter.reset(pIter); | 2785 m_pCurIter.reset(pIter); |
2786 } | 2786 } |
2787 | 2787 |
2788 void CFX_WordBreak::Attach(const CFX_WideString& wsText) { | 2788 void CFX_WordBreak::Attach(const CFX_WideString& wsText) { |
2789 m_pCurIter.reset(new CFX_CharIter(wsText)); | 2789 m_pCurIter.reset(new CFX_CharIter(wsText)); |
2790 } | 2790 } |
2791 | 2791 |
2792 FX_BOOL CFX_WordBreak::Next(FX_BOOL bPrev) { | 2792 bool CFX_WordBreak::Next(bool bPrev) { |
2793 std::unique_ptr<IFX_CharIter> pIter( | 2793 std::unique_ptr<IFX_CharIter> pIter( |
2794 (bPrev ? m_pPreIter : m_pCurIter)->Clone()); | 2794 (bPrev ? m_pPreIter : m_pCurIter)->Clone()); |
2795 if (pIter->IsEOF(!bPrev)) | 2795 if (pIter->IsEOF(!bPrev)) |
2796 return FALSE; | 2796 return false; |
2797 | 2797 |
2798 pIter->Next(bPrev); | 2798 pIter->Next(bPrev); |
2799 if (!FindNextBreakPos(pIter.get(), bPrev, TRUE)) | 2799 if (!FindNextBreakPos(pIter.get(), bPrev, true)) |
2800 return FALSE; | 2800 return false; |
2801 | 2801 |
2802 if (bPrev) { | 2802 if (bPrev) { |
2803 m_pCurIter = std::move(m_pPreIter); | 2803 m_pCurIter = std::move(m_pPreIter); |
2804 m_pCurIter->Next(TRUE); | 2804 m_pCurIter->Next(true); |
2805 m_pPreIter = std::move(pIter); | 2805 m_pPreIter = std::move(pIter); |
2806 } else { | 2806 } else { |
2807 m_pPreIter = std::move(m_pCurIter); | 2807 m_pPreIter = std::move(m_pCurIter); |
2808 m_pPreIter->Next(); | 2808 m_pPreIter->Next(); |
2809 m_pCurIter = std::move(pIter); | 2809 m_pCurIter = std::move(pIter); |
2810 } | 2810 } |
2811 return TRUE; | 2811 return true; |
2812 } | 2812 } |
2813 | 2813 |
2814 void CFX_WordBreak::SetAt(int32_t nIndex) { | 2814 void CFX_WordBreak::SetAt(int32_t nIndex) { |
2815 m_pPreIter.reset(); | 2815 m_pPreIter.reset(); |
2816 m_pCurIter->SetAt(nIndex); | 2816 m_pCurIter->SetAt(nIndex); |
2817 FindNextBreakPos(m_pCurIter.get(), TRUE, FALSE); | 2817 FindNextBreakPos(m_pCurIter.get(), true, false); |
2818 m_pPreIter = std::move(m_pCurIter); | 2818 m_pPreIter = std::move(m_pCurIter); |
2819 m_pCurIter.reset(m_pPreIter->Clone()); | 2819 m_pCurIter.reset(m_pPreIter->Clone()); |
2820 FindNextBreakPos(m_pCurIter.get(), FALSE, FALSE); | 2820 FindNextBreakPos(m_pCurIter.get(), false, false); |
2821 } | 2821 } |
2822 | 2822 |
2823 int32_t CFX_WordBreak::GetWordPos() const { | 2823 int32_t CFX_WordBreak::GetWordPos() const { |
2824 return m_pPreIter->GetAt(); | 2824 return m_pPreIter->GetAt(); |
2825 } | 2825 } |
2826 | 2826 |
2827 int32_t CFX_WordBreak::GetWordLength() const { | 2827 int32_t CFX_WordBreak::GetWordLength() const { |
2828 return m_pCurIter->GetAt() - m_pPreIter->GetAt() + 1; | 2828 return m_pCurIter->GetAt() - m_pPreIter->GetAt() + 1; |
2829 } | 2829 } |
2830 | 2830 |
2831 void CFX_WordBreak::GetWord(CFX_WideString& wsWord) const { | 2831 void CFX_WordBreak::GetWord(CFX_WideString& wsWord) const { |
2832 int32_t nWordLength = GetWordLength(); | 2832 int32_t nWordLength = GetWordLength(); |
2833 if (nWordLength <= 0) { | 2833 if (nWordLength <= 0) { |
2834 return; | 2834 return; |
2835 } | 2835 } |
2836 FX_WCHAR* lpBuf = wsWord.GetBuffer(nWordLength); | 2836 FX_WCHAR* lpBuf = wsWord.GetBuffer(nWordLength); |
2837 std::unique_ptr<IFX_CharIter> pTempIter(m_pPreIter->Clone()); | 2837 std::unique_ptr<IFX_CharIter> pTempIter(m_pPreIter->Clone()); |
2838 int32_t i = 0; | 2838 int32_t i = 0; |
2839 while (pTempIter->GetAt() <= m_pCurIter->GetAt()) { | 2839 while (pTempIter->GetAt() <= m_pCurIter->GetAt()) { |
2840 lpBuf[i++] = pTempIter->GetChar(); | 2840 lpBuf[i++] = pTempIter->GetChar(); |
2841 if (!pTempIter->Next()) | 2841 if (!pTempIter->Next()) |
2842 break; | 2842 break; |
2843 } | 2843 } |
2844 wsWord.ReleaseBuffer(nWordLength); | 2844 wsWord.ReleaseBuffer(nWordLength); |
2845 } | 2845 } |
2846 | 2846 |
2847 FX_BOOL CFX_WordBreak::IsEOF(FX_BOOL bTail) const { | 2847 bool CFX_WordBreak::IsEOF(bool bTail) const { |
2848 return m_pCurIter->IsEOF(bTail); | 2848 return m_pCurIter->IsEOF(bTail); |
2849 } | 2849 } |
2850 | 2850 |
2851 FX_BOOL CFX_WordBreak::FindNextBreakPos(IFX_CharIter* pIter, | 2851 bool CFX_WordBreak::FindNextBreakPos(IFX_CharIter* pIter, |
2852 FX_BOOL bPrev, | 2852 bool bPrev, |
2853 FX_BOOL bFromNext) { | 2853 bool bFromNext) { |
2854 FX_WordBreakProp ePreType = FX_WordBreakProp_None; | 2854 FX_WordBreakProp ePreType = FX_WordBreakProp_None; |
2855 FX_WordBreakProp eCurType = FX_WordBreakProp_None; | 2855 FX_WordBreakProp eCurType = FX_WordBreakProp_None; |
2856 FX_WordBreakProp eNextType = FX_WordBreakProp_None; | 2856 FX_WordBreakProp eNextType = FX_WordBreakProp_None; |
2857 if (pIter->IsEOF(!bPrev)) { | 2857 if (pIter->IsEOF(!bPrev)) { |
2858 return TRUE; | 2858 return true; |
2859 } | 2859 } |
2860 if (!(bFromNext || pIter->IsEOF(bPrev))) { | 2860 if (!(bFromNext || pIter->IsEOF(bPrev))) { |
2861 pIter->Next(!bPrev); | 2861 pIter->Next(!bPrev); |
2862 ePreType = GetWordBreakProperty(pIter->GetChar()); | 2862 ePreType = GetWordBreakProperty(pIter->GetChar()); |
2863 pIter->Next(bPrev); | 2863 pIter->Next(bPrev); |
2864 } | 2864 } |
2865 eCurType = GetWordBreakProperty(pIter->GetChar()); | 2865 eCurType = GetWordBreakProperty(pIter->GetChar()); |
2866 FX_BOOL bFirst = TRUE; | 2866 bool bFirst = true; |
2867 do { | 2867 do { |
2868 pIter->Next(bPrev); | 2868 pIter->Next(bPrev); |
2869 eNextType = GetWordBreakProperty(pIter->GetChar()); | 2869 eNextType = GetWordBreakProperty(pIter->GetChar()); |
2870 uint16_t wBreak = | 2870 uint16_t wBreak = |
2871 gs_FX_WordBreak_Table[eCurType] & ((uint16_t)(1 << eNextType)); | 2871 gs_FX_WordBreak_Table[eCurType] & ((uint16_t)(1 << eNextType)); |
2872 if (wBreak) { | 2872 if (wBreak) { |
2873 if (pIter->IsEOF(!bPrev)) { | 2873 if (pIter->IsEOF(!bPrev)) { |
2874 pIter->Next(!bPrev); | 2874 pIter->Next(!bPrev); |
2875 return TRUE; | 2875 return true; |
2876 } | 2876 } |
2877 if (bFirst) { | 2877 if (bFirst) { |
2878 int32_t nFlags = 0; | 2878 int32_t nFlags = 0; |
2879 if (eCurType == FX_WordBreakProp_MidLetter) { | 2879 if (eCurType == FX_WordBreakProp_MidLetter) { |
2880 if (eNextType == FX_WordBreakProp_ALetter) { | 2880 if (eNextType == FX_WordBreakProp_ALetter) { |
2881 nFlags = 1; | 2881 nFlags = 1; |
2882 } | 2882 } |
2883 } else if (eCurType == FX_WordBreakProp_MidNum) { | 2883 } else if (eCurType == FX_WordBreakProp_MidNum) { |
2884 if (eNextType == FX_WordBreakProp_Numberic) { | 2884 if (eNextType == FX_WordBreakProp_Numberic) { |
2885 nFlags = 2; | 2885 nFlags = 2; |
2886 } | 2886 } |
2887 } else if (eCurType == FX_WordBreakProp_MidNumLet) { | 2887 } else if (eCurType == FX_WordBreakProp_MidNumLet) { |
2888 if (eNextType == FX_WordBreakProp_ALetter) { | 2888 if (eNextType == FX_WordBreakProp_ALetter) { |
2889 nFlags = 1; | 2889 nFlags = 1; |
2890 } else if (eNextType == FX_WordBreakProp_Numberic) { | 2890 } else if (eNextType == FX_WordBreakProp_Numberic) { |
2891 nFlags = 2; | 2891 nFlags = 2; |
2892 } | 2892 } |
2893 } | 2893 } |
2894 if (nFlags > 0) { | 2894 if (nFlags > 0) { |
2895 ASSERT(nFlags <= 2); | 2895 ASSERT(nFlags <= 2); |
2896 if (!((nFlags == 1 && ePreType == FX_WordBreakProp_ALetter) || | 2896 if (!((nFlags == 1 && ePreType == FX_WordBreakProp_ALetter) || |
2897 (nFlags == 2 && ePreType == FX_WordBreakProp_Numberic))) { | 2897 (nFlags == 2 && ePreType == FX_WordBreakProp_Numberic))) { |
2898 pIter->Next(!bPrev); | 2898 pIter->Next(!bPrev); |
2899 return TRUE; | 2899 return true; |
2900 } | 2900 } |
2901 pIter->Next(bPrev); | 2901 pIter->Next(bPrev); |
2902 wBreak = FALSE; | 2902 wBreak = false; |
2903 } | 2903 } |
2904 bFirst = FALSE; | 2904 bFirst = false; |
2905 } | 2905 } |
2906 if (wBreak) { | 2906 if (wBreak) { |
2907 int32_t nFlags = 0; | 2907 int32_t nFlags = 0; |
2908 if (eNextType == FX_WordBreakProp_MidLetter) { | 2908 if (eNextType == FX_WordBreakProp_MidLetter) { |
2909 if (eCurType == FX_WordBreakProp_ALetter) { | 2909 if (eCurType == FX_WordBreakProp_ALetter) { |
2910 nFlags = 1; | 2910 nFlags = 1; |
2911 } | 2911 } |
2912 } else if (eNextType == FX_WordBreakProp_MidNum) { | 2912 } else if (eNextType == FX_WordBreakProp_MidNum) { |
2913 if (eCurType == FX_WordBreakProp_Numberic) { | 2913 if (eCurType == FX_WordBreakProp_Numberic) { |
2914 nFlags = 2; | 2914 nFlags = 2; |
2915 } | 2915 } |
2916 } else if (eNextType == FX_WordBreakProp_MidNumLet) { | 2916 } else if (eNextType == FX_WordBreakProp_MidNumLet) { |
2917 if (eCurType == FX_WordBreakProp_ALetter) { | 2917 if (eCurType == FX_WordBreakProp_ALetter) { |
2918 nFlags = 1; | 2918 nFlags = 1; |
2919 } else if (eCurType == FX_WordBreakProp_Numberic) { | 2919 } else if (eCurType == FX_WordBreakProp_Numberic) { |
2920 nFlags = 2; | 2920 nFlags = 2; |
2921 } | 2921 } |
2922 } | 2922 } |
2923 if (nFlags <= 0) { | 2923 if (nFlags <= 0) { |
2924 pIter->Next(!bPrev); | 2924 pIter->Next(!bPrev); |
2925 return TRUE; | 2925 return true; |
2926 } | 2926 } |
2927 ASSERT(nFlags <= 2); | 2927 ASSERT(nFlags <= 2); |
2928 pIter->Next(bPrev); | 2928 pIter->Next(bPrev); |
2929 eNextType = (FX_WordBreakProp)GetWordBreakProperty(pIter->GetChar()); | 2929 eNextType = (FX_WordBreakProp)GetWordBreakProperty(pIter->GetChar()); |
2930 if (!((nFlags == 1 && eNextType == FX_WordBreakProp_ALetter) || | 2930 if (!((nFlags == 1 && eNextType == FX_WordBreakProp_ALetter) || |
2931 (nFlags == 2 && eNextType == FX_WordBreakProp_Numberic))) { | 2931 (nFlags == 2 && eNextType == FX_WordBreakProp_Numberic))) { |
2932 pIter->Next(!bPrev); | 2932 pIter->Next(!bPrev); |
2933 pIter->Next(!bPrev); | 2933 pIter->Next(!bPrev); |
2934 return TRUE; | 2934 return true; |
2935 } | 2935 } |
2936 } | 2936 } |
2937 } | 2937 } |
2938 ePreType = eCurType; | 2938 ePreType = eCurType; |
2939 eCurType = eNextType; | 2939 eCurType = eNextType; |
2940 bFirst = FALSE; | 2940 bFirst = false; |
2941 } while (!pIter->IsEOF(!bPrev)); | 2941 } while (!pIter->IsEOF(!bPrev)); |
2942 return TRUE; | 2942 return true; |
2943 } | 2943 } |
OLD | NEW |