summaryrefslogtreecommitdiff
path: root/xfa/fee/fx_wordbreak/fx_wordbreak_impl.cpp
blob: e77c5f14bfa31eeae89310fc04ebbb7bf88971f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "xfa/fee/fx_wordbreak/fx_wordbreak_impl.h"

FX_WordBreakProp FX_GetWordBreakProperty(FX_WCHAR wcCodePoint) {
  uint32_t dwProperty =
      (uint32_t)gs_FX_WordBreak_CodePointProperties[wcCodePoint >> 1];
  return (FX_WordBreakProp)(((wcCodePoint)&1) ? (dwProperty & 0x0F)
                                              : (dwProperty >> 4));
}
CFX_CharIter::CFX_CharIter(const CFX_WideString& wsText)
    : m_wsText(wsText), m_nIndex(0) {
  FXSYS_assert(!wsText.IsEmpty());
}
CFX_CharIter::~CFX_CharIter() {}
void CFX_CharIter::Release() {
  delete this;
}
FX_BOOL CFX_CharIter::Next(FX_BOOL bPrev) {
  if (bPrev) {
    if (m_nIndex <= 0) {
      return FALSE;
    }
    m_nIndex--;
  } else {
    if (m_nIndex + 1 >= m_wsText.GetLength()) {
      return FALSE;
    }
    m_nIndex++;
  }
  return TRUE;
}
FX_WCHAR CFX_CharIter::GetChar() {
  return m_wsText.GetAt(m_nIndex);
}
void CFX_CharIter::SetAt(int32_t nIndex) {
  if (nIndex < 0 || nIndex >= m_wsText.GetLength()) {
    return;
  }
  m_nIndex = nIndex;
}
int32_t CFX_CharIter::GetAt() const {
  return m_nIndex;
}
FX_BOOL CFX_CharIter::IsEOF(FX_BOOL bTail) const {
  return bTail ? (m_nIndex + 1 == m_wsText.GetLength()) : (m_nIndex == 0);
}
IFX_CharIter* CFX_CharIter::Clone() {
  CFX_CharIter* pIter = new CFX_CharIter(m_wsText);
  pIter->m_nIndex = m_nIndex;
  return pIter;
}
CFX_WordBreak::CFX_WordBreak() : m_pPreIter(NULL), m_pCurIter(NULL) {}
CFX_WordBreak::~CFX_WordBreak() {
  if (m_pPreIter) {
    m_pPreIter->Release();
    m_pPreIter = NULL;
  }
  if (m_pCurIter) {
    m_pCurIter->Release();
    m_pCurIter = NULL;
  }
}
void CFX_WordBreak::Release() {
  delete this;
}
void CFX_WordBreak::Attach(IFX_CharIter* pIter) {
  FXSYS_assert(pIter);
  m_pCurIter = pIter;
}
void CFX_WordBreak::Attach(const CFX_WideString& wsText) {
  m_pCurIter = new CFX_CharIter(wsText);
}
FX_BOOL CFX_WordBreak::Next(FX_BOOL bPrev) {
  IFX_CharIter* pIter = bPrev ? m_pPreIter->Clone() : m_pCurIter->Clone();
  if (pIter->IsEOF(!bPrev)) {
    return FALSE;
  }
  pIter->Next(bPrev);
  if (!FindNextBreakPos(pIter, bPrev, TRUE)) {
    pIter->Release();
    return FALSE;
  }
  if (bPrev) {
    m_pCurIter->Release();
    m_pCurIter = m_pPreIter;
    m_pCurIter->Next(TRUE);
    m_pPreIter = pIter;
  } else {
    m_pPreIter->Release();
    m_pPreIter = m_pCurIter;
    m_pPreIter->Next();
    m_pCurIter = pIter;
  }
  return TRUE;
}
void CFX_WordBreak::SetAt(int32_t nIndex) {
  if (m_pPreIter) {
    m_pPreIter->Release();
    m_pPreIter = NULL;
  }
  m_pCurIter->SetAt(nIndex);
  FindNextBreakPos(m_pCurIter, TRUE, FALSE);
  m_pPreIter = m_pCurIter;
  m_pCurIter = m_pPreIter->Clone();
  FindNextBreakPos(m_pCurIter, FALSE, FALSE);
}
int32_t CFX_WordBreak::GetWordPos() const {
  return m_pPreIter->GetAt();
}
int32_t CFX_WordBreak::GetWordLength() const {
  return m_pCurIter->GetAt() - m_pPreIter->GetAt() + 1;
}
void CFX_WordBreak::GetWord(CFX_WideString& wsWord) const {
  int32_t nWordLength = GetWordLength();
  if (nWordLength <= 0) {
    return;
  }
  FX_WCHAR* lpBuf = wsWord.GetBuffer(nWordLength);
  IFX_CharIter* pTempIter = m_pPreIter->Clone();
  int32_t i = 0;
  while (pTempIter->GetAt() <= m_pCurIter->GetAt()) {
    lpBuf[i++] = pTempIter->GetChar();
    FX_BOOL bEnd = pTempIter->Next();
    if (!bEnd) {
      break;
    }
  }
  pTempIter->Release();
  wsWord.ReleaseBuffer(nWordLength);
}
FX_BOOL CFX_WordBreak::IsEOF(FX_BOOL bTail) const {
  return m_pCurIter->IsEOF(bTail);
}
FX_BOOL CFX_WordBreak::FindNextBreakPos(IFX_CharIter* pIter,
                                        FX_BOOL bPrev,
                                        FX_BOOL bFromNext) {
  FX_WordBreakProp ePreType = FX_WordBreakProp_None;
  FX_WordBreakProp eCurType = FX_WordBreakProp_None;
  FX_WordBreakProp eNextType = FX_WordBreakProp_None;
  if (pIter->IsEOF(!bPrev)) {
    return TRUE;
  }
  if (!(bFromNext || pIter->IsEOF(bPrev))) {
    pIter->Next(!bPrev);
    FX_WCHAR wcTemp = pIter->GetChar();
    ePreType = FX_GetWordBreakProperty(wcTemp);
    pIter->Next(bPrev);
  }
  FX_WCHAR wcTemp = pIter->GetChar();
  eCurType = FX_GetWordBreakProperty(wcTemp);
  FX_BOOL bFirst = TRUE;
  do {
    pIter->Next(bPrev);
    FX_WCHAR wcTemp = pIter->GetChar();
    eNextType = FX_GetWordBreakProperty(wcTemp);
    uint16_t wBreak =
        gs_FX_WordBreak_Table[eCurType] & ((uint16_t)(1 << eNextType));
    if (wBreak) {
      if (pIter->IsEOF(!bPrev)) {
        pIter->Next(!bPrev);
        return TRUE;
      }
      if (bFirst) {
        int32_t nFlags = 0;
        if (eCurType == FX_WordBreakProp_MidLetter) {
          if (eNextType == FX_WordBreakProp_ALetter) {
            nFlags = 1;
          }
        } else if (eCurType == FX_WordBreakProp_MidNum) {
          if (eNextType == FX_WordBreakProp_Numberic) {
            nFlags = 2;
          }
        } else if (eCurType == FX_WordBreakProp_MidNumLet) {
          if (eNextType == FX_WordBreakProp_ALetter) {
            nFlags = 1;
          } else if (eNextType == FX_WordBreakProp_Numberic) {
            nFlags = 2;
          }
        }
        if (nFlags > 0) {
          FXSYS_assert(nFlags <= 2);
          if (!((nFlags == 1 && ePreType == FX_WordBreakProp_ALetter) ||
                (nFlags == 2 && ePreType == FX_WordBreakProp_Numberic))) {
            pIter->Next(!bPrev);
            return TRUE;
          }
          pIter->Next(bPrev);
          wBreak = FALSE;
        }
        bFirst = FALSE;
      }
      if (wBreak) {
        int32_t nFlags = 0;
        if (eNextType == FX_WordBreakProp_MidLetter) {
          if (eCurType == FX_WordBreakProp_ALetter) {
            nFlags = 1;
          }
        } else if (eNextType == FX_WordBreakProp_MidNum) {
          if (eCurType == FX_WordBreakProp_Numberic) {
            nFlags = 2;
          }
        } else if (eNextType == FX_WordBreakProp_MidNumLet) {
          if (eCurType == FX_WordBreakProp_ALetter) {
            nFlags = 1;
          } else if (eCurType == FX_WordBreakProp_Numberic) {
            nFlags = 2;
          }
        }
        if (nFlags <= 0) {
          pIter->Next(!bPrev);
          return TRUE;
        }
        FXSYS_assert(nFlags <= 2);
        pIter->Next(bPrev);
        wcTemp = pIter->GetChar();
        eNextType = (FX_WordBreakProp)FX_GetWordBreakProperty(wcTemp);
        if (!((nFlags == 1 && eNextType == FX_WordBreakProp_ALetter) ||
              (nFlags == 2 && eNextType == FX_WordBreakProp_Numberic))) {
          pIter->Next(!bPrev);
          pIter->Next(!bPrev);
          return TRUE;
        }
      }
    }
    ePreType = eCurType;
    eCurType = eNextType;
    bFirst = FALSE;
  } while (!pIter->IsEOF(!bPrev));
  return TRUE;
}