summaryrefslogtreecommitdiff
path: root/xfa/fxfa/parser/xfa_basic_imp.cpp
blob: ae5e5e893925e8ae62e880448f84708220358731 (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
// 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/fxfa/parser/xfa_basic_imp.h"

#include "core/fxcrt/include/fx_ext.h"
#include "xfa/fgas/crt/fgas_algorithm.h"
#include "xfa/fgas/crt/fgas_codepage.h"
#include "xfa/fgas/crt/fgas_system.h"
#include "xfa/fxfa/fm2js/xfa_fm2jsapi.h"
#include "xfa/fxfa/parser/xfa_basic_data.h"
#include "xfa/fxfa/parser/xfa_docdata.h"
#include "xfa/fxfa/parser/xfa_doclayout.h"
#include "xfa/fxfa/parser/xfa_document.h"
#include "xfa/fxfa/parser/xfa_localemgr.h"
#include "xfa/fxfa/parser/xfa_object.h"
#include "xfa/fxfa/parser/xfa_parser.h"
#include "xfa/fxfa/parser/xfa_script.h"
#include "xfa/fxfa/parser/xfa_utils.h"

const XFA_PACKETINFO* XFA_GetPacketByName(const CFX_WideStringC& wsName) {
  int32_t iLength = wsName.GetLength();
  if (iLength == 0) {
    return NULL;
  }
  uint32_t uHash = FX_HashCode_String_GetW(wsName.GetPtr(), iLength);
  int32_t iStart = 0, iEnd = g_iXFAPacketCount - 1;
  do {
    int32_t iMid = (iStart + iEnd) / 2;
    const XFA_PACKETINFO* pInfo = g_XFAPacketData + iMid;
    if (uHash == pInfo->uHash) {
      return pInfo;
    } else if (uHash < pInfo->uHash) {
      iEnd = iMid - 1;
    } else {
      iStart = iMid + 1;
    }
  } while (iStart <= iEnd);
  return NULL;
}

const XFA_PACKETINFO* XFA_GetPacketByID(FX_DWORD dwPacket) {
  int32_t iStart = 0, iEnd = g_iXFAPacketCount - 1;
  do {
    int32_t iMid = (iStart + iEnd) / 2;
    FX_DWORD dwFind = (g_XFAPacketData + iMid)->eName;
    if (dwPacket == dwFind) {
      return g_XFAPacketData + iMid;
    } else if (dwPacket < dwFind) {
      iEnd = iMid - 1;
    } else {
      iStart = iMid + 1;
    }
  } while (iStart <= iEnd);
  return NULL;
}

const XFA_PACKETINFO* XFA_GetPacketByIndex(XFA_PACKET ePacket) {
  return g_XFAPacketData + ePacket;
}

const XFA_ATTRIBUTEENUMINFO* XFA_GetAttributeEnumByName(
    const CFX_WideStringC& wsName) {
  int32_t iLength = wsName.GetLength();
  if (iLength == 0) {
    return NULL;
  }
  uint32_t uHash = FX_HashCode_String_GetW(wsName.GetPtr(), iLength);
  int32_t iStart = 0, iEnd = g_iXFAEnumCount - 1;
  do {
    int32_t iMid = (iStart + iEnd) / 2;
    const XFA_ATTRIBUTEENUMINFO* pInfo = g_XFAEnumData + iMid;
    if (uHash == pInfo->uHash) {
      return pInfo;
    } else if (uHash < pInfo->uHash) {
      iEnd = iMid - 1;
    } else {
      iStart = iMid + 1;
    }
  } while (iStart <= iEnd);
  return NULL;
}
const XFA_ATTRIBUTEENUMINFO* XFA_GetAttributeEnumByID(XFA_ATTRIBUTEENUM eName) {
  return g_XFAEnumData + eName;
}
int32_t XFA_GetAttributeCount() {
  return g_iXFAAttributeCount;
}
const XFA_ATTRIBUTEINFO* XFA_GetAttributeByName(const CFX_WideStringC& wsName) {
  int32_t iLength = wsName.GetLength();
  if (iLength == 0) {
    return NULL;
  }
  uint32_t uHash = FX_HashCode_String_GetW(wsName.GetPtr(), iLength);
  int32_t iStart = 0, iEnd = g_iXFAAttributeCount - 1;
  do {
    int32_t iMid = (iStart + iEnd) / 2;
    const XFA_ATTRIBUTEINFO* pInfo = g_XFAAttributeData + iMid;
    if (uHash == pInfo->uHash) {
      return pInfo;
    } else if (uHash < pInfo->uHash) {
      iEnd = iMid - 1;
    } else {
      iStart = iMid + 1;
    }
  } while (iStart <= iEnd);
  return NULL;
}
const XFA_ATTRIBUTEINFO* XFA_GetAttributeByID(XFA_ATTRIBUTE eName) {
  return (eName < g_iXFAAttributeCount) ? (g_XFAAttributeData + eName) : NULL;
}
FX_BOOL XFA_GetAttributeDefaultValue(void*& pValue,
                                     XFA_ELEMENT eElement,
                                     XFA_ATTRIBUTE eAttribute,
                                     XFA_ATTRIBUTETYPE eType,
                                     FX_DWORD dwPacket) {
  const XFA_ATTRIBUTEINFO* pInfo = XFA_GetAttributeByID(eAttribute);
  if (pInfo == NULL) {
    return FALSE;
  }
  if (dwPacket && (dwPacket & pInfo->dwPackets) == 0) {
    return FALSE;
  }
  if (pInfo->eType == eType) {
    pValue = pInfo->pDefValue;
    return TRUE;
  } else if (pInfo->eType == XFA_ATTRIBUTETYPE_NOTSURE) {
    const XFA_NOTSUREATTRIBUTE* pAttr =
        XFA_GetNotsureAttribute(eElement, eAttribute, eType);
    if (pAttr) {
      pValue = pAttr->pValue;
      return TRUE;
    }
  }
  return FALSE;
}
XFA_ATTRIBUTEENUM XFA_GetAttributeDefaultValue_Enum(XFA_ELEMENT eElement,
                                                    XFA_ATTRIBUTE eAttribute,
                                                    FX_DWORD dwPacket) {
  void* pValue;
  if (XFA_GetAttributeDefaultValue(pValue, eElement, eAttribute,
                                   XFA_ATTRIBUTETYPE_Enum, dwPacket)) {
    return (XFA_ATTRIBUTEENUM)(uintptr_t)pValue;
  }
  return XFA_ATTRIBUTEENUM_Unknown;
}
CFX_WideStringC XFA_GetAttributeDefaultValue_Cdata(XFA_ELEMENT eElement,
                                                   XFA_ATTRIBUTE eAttribute,
                                                   FX_DWORD dwPacket) {
  void* pValue;
  if (XFA_GetAttributeDefaultValue(pValue, eElement, eAttribute,
                                   XFA_ATTRIBUTETYPE_Cdata, dwPacket)) {
    return (const FX_WCHAR*)pValue;
  }
  return NULL;
}
FX_BOOL XFA_GetAttributeDefaultValue_Boolean(XFA_ELEMENT eElement,
                                             XFA_ATTRIBUTE eAttribute,
                                             FX_DWORD dwPacket) {
  void* pValue;
  if (XFA_GetAttributeDefaultValue(pValue, eElement, eAttribute,
                                   XFA_ATTRIBUTETYPE_Boolean, dwPacket)) {
    return (FX_BOOL)(uintptr_t)pValue;
  }
  return FALSE;
}
int32_t XFA_GetAttributeDefaultValue_Integer(XFA_ELEMENT eElement,
                                             XFA_ATTRIBUTE eAttribute,
                                             FX_DWORD dwPacket) {
  void* pValue;
  if (XFA_GetAttributeDefaultValue(pValue, eElement, eAttribute,
                                   XFA_ATTRIBUTETYPE_Integer, dwPacket)) {
    return (int32_t)(uintptr_t)pValue;
  }
  return 0;
}
CXFA_Measurement XFA_GetAttributeDefaultValue_Measure(XFA_ELEMENT eElement,
                                                      XFA_ATTRIBUTE eAttribute,
                                                      FX_DWORD dwPacket) {
  void* pValue;
  if (XFA_GetAttributeDefaultValue(pValue, eElement, eAttribute,
                                   XFA_ATTRIBUTETYPE_Measure, dwPacket)) {
    return *(CXFA_Measurement*)pValue;
  }
  return CXFA_Measurement();
}
int32_t XFA_GetElementCount() {
  return g_iXFAElementCount;
}
const XFA_ELEMENTINFO* XFA_GetElementByName(const CFX_WideStringC& wsName) {
  int32_t iLength = wsName.GetLength();
  if (iLength == 0) {
    return NULL;
  }
  uint32_t uHash = FX_HashCode_String_GetW(wsName.GetPtr(), iLength);
  int32_t iStart = 0, iEnd = g_iXFAElementCount - 1;
  do {
    int32_t iMid = (iStart + iEnd) / 2;
    const XFA_ELEMENTINFO* pInfo = g_XFAElementData + iMid;
    if (uHash == pInfo->uHash) {
      return pInfo;
    } else if (uHash < pInfo->uHash) {
      iEnd = iMid - 1;
    } else {
      iStart = iMid + 1;
    }
  } while (iStart <= iEnd);
  return NULL;
}
const XFA_ELEMENTINFO* XFA_GetElementByID(XFA_ELEMENT eName) {
  return (eName < g_iXFAElementCount) ? (g_XFAElementData + eName) : NULL;
}
const uint16_t* XFA_GetElementChildren(XFA_ELEMENT eElement, int32_t& iCount) {
  if (eElement >= g_iXFAElementCount) {
    return NULL;
  }
  const XFA_ELEMENTHIERARCHY* pElement = g_XFAElementChildrenIndex + eElement;
  iCount = pElement->wCount;
  return g_XFAElementChildrenData + pElement->wStart;
}
const uint8_t* XFA_GetElementAttributes(XFA_ELEMENT eElement, int32_t& iCount) {
  if (eElement >= g_iXFAElementCount) {
    return NULL;
  }
  const XFA_ELEMENTHIERARCHY* pElement = g_XFAElementAttributeIndex + eElement;
  iCount = pElement->wCount;
  return g_XFAElementAttributeData + pElement->wStart;
}
const XFA_ATTRIBUTEINFO* XFA_GetAttributeOfElement(XFA_ELEMENT eElement,
                                                   XFA_ATTRIBUTE eAttribute,
                                                   FX_DWORD dwPacket) {
  int32_t iCount = 0;
  const uint8_t* pAttr = XFA_GetElementAttributes(eElement, iCount);
  if (pAttr == NULL || iCount < 1) {
    return NULL;
  }
  CFX_DSPATemplate<uint8_t> search;
  int32_t index = search.Lookup(eAttribute, pAttr, iCount);
  if (index < 0) {
    return NULL;
  }
  const XFA_ATTRIBUTEINFO* pInfo = XFA_GetAttributeByID(eAttribute);
  ASSERT(pInfo);
  if (dwPacket == XFA_XDPPACKET_UNKNOWN)
    return pInfo;
  return (dwPacket & pInfo->dwPackets) ? pInfo : NULL;
}
const XFA_ELEMENTINFO* XFA_GetChildOfElement(XFA_ELEMENT eElement,
                                             XFA_ELEMENT eChild,
                                             FX_DWORD dwPacket) {
  int32_t iCount = 0;
  const uint16_t* pChild = XFA_GetElementChildren(eElement, iCount);
  if (pChild == NULL || iCount < 1) {
    return NULL;
  }
  CFX_DSPATemplate<uint16_t> search;
  int32_t index = search.Lookup(eChild, pChild, iCount);
  if (index < 0) {
    return NULL;
  }
  const XFA_ELEMENTINFO* pInfo = XFA_GetElementByID(eChild);
  ASSERT(pInfo);
  if (dwPacket == XFA_XDPPACKET_UNKNOWN)
    return pInfo;
  return (dwPacket & pInfo->dwPackets) ? pInfo : NULL;
}
const XFA_PROPERTY* XFA_GetElementProperties(XFA_ELEMENT eElement,
                                             int32_t& iCount) {
  if (eElement >= g_iXFAElementCount) {
    return NULL;
  }
  const XFA_ELEMENTHIERARCHY* pElement = g_XFAElementPropertyIndex + eElement;
  iCount = pElement->wCount;
  return g_XFAElementPropertyData + pElement->wStart;
}
const XFA_PROPERTY* XFA_GetPropertyOfElement(XFA_ELEMENT eElement,
                                             XFA_ELEMENT eProperty,
                                             FX_DWORD dwPacket) {
  int32_t iCount = 0;
  const XFA_PROPERTY* pProperty = XFA_GetElementProperties(eElement, iCount);
  if (pProperty == NULL || iCount < 1) {
    return NULL;
  }
  int32_t iStart = 0, iEnd = iCount - 1, iMid;
  do {
    iMid = (iStart + iEnd) / 2;
    XFA_ELEMENT eName = (XFA_ELEMENT)pProperty[iMid].eName;
    if (eProperty == eName) {
      break;
    } else if (eProperty < eName) {
      iEnd = iMid - 1;
    } else {
      iStart = iMid + 1;
    }
  } while (iStart <= iEnd);
  if (iStart > iEnd) {
    return NULL;
  }
  const XFA_ELEMENTINFO* pInfo = XFA_GetElementByID(eProperty);
  ASSERT(pInfo);
  if (dwPacket == XFA_XDPPACKET_UNKNOWN)
    return pProperty + iMid;
  return (dwPacket & pInfo->dwPackets) ? (pProperty + iMid) : NULL;
}
const XFA_NOTSUREATTRIBUTE* XFA_GetNotsureAttribute(XFA_ELEMENT eElement,
                                                    XFA_ATTRIBUTE eAttribute,
                                                    XFA_ATTRIBUTETYPE eType) {
  int32_t iStart = 0, iEnd = g_iXFANotsureCount - 1;
  do {
    int32_t iMid = (iStart + iEnd) / 2;
    const XFA_NOTSUREATTRIBUTE* pAttr = g_XFANotsureAttributes + iMid;
    if (eElement == pAttr->eElement) {
      if (pAttr->eAttribute == eAttribute) {
        if (eType == XFA_ATTRIBUTETYPE_NOTSURE || eType == pAttr->eType) {
          return pAttr;
        }
        return NULL;
      } else {
        int32_t iBefore = iMid - 1;
        if (iBefore >= 0) {
          pAttr = g_XFANotsureAttributes + iBefore;
          while (eElement == pAttr->eElement) {
            if (pAttr->eAttribute == eAttribute) {
              if (eType == XFA_ATTRIBUTETYPE_NOTSURE || eType == pAttr->eType) {
                return pAttr;
              }
              return NULL;
            }
            iBefore--;
            if (iBefore < 0) {
              break;
            }
            pAttr = g_XFANotsureAttributes + iBefore;
          }
        }
        int32_t iAfter = iMid + 1;
        if (iAfter <= g_iXFANotsureCount - 1) {
          pAttr = g_XFANotsureAttributes + iAfter;
          while (eElement == pAttr->eElement) {
            if (pAttr->eAttribute == eAttribute) {
              if (eType == XFA_ATTRIBUTETYPE_NOTSURE || eType == pAttr->eType) {
                return pAttr;
              }
              return NULL;
            }
            iAfter++;
            if (iAfter > g_iXFANotsureCount - 1) {
              break;
            }
            pAttr = g_XFANotsureAttributes + iAfter;
          }
        }
        return NULL;
      }
    } else if (eElement < pAttr->eElement) {
      iEnd = iMid - 1;
    } else {
      iStart = iMid + 1;
    }
  } while (iStart <= iEnd);
  return NULL;
}
int32_t XFA_GetMethodCount() {
  return g_iSomMethodCount;
}
const XFA_METHODINFO* XFA_GetMethodByName(XFA_ELEMENT eElement,
                                          const CFX_WideStringC& wsMethodName) {
  int32_t iLength = wsMethodName.GetLength();
  if (iLength == 0) {
    return NULL;
  }
  int32_t iElementIndex = eElement;
  while (iElementIndex != -1) {
    XFA_LPCSCRIPTHIERARCHY scriptIndex = g_XFAScriptIndex + iElementIndex;
    int32_t icount = scriptIndex->wMethodCount;
    if (icount == 0) {
      iElementIndex = scriptIndex->wParentIndex;
      continue;
    }
    uint32_t uHash = FX_HashCode_String_GetW(wsMethodName.GetPtr(), iLength);
    int32_t iStart = scriptIndex->wMethodStart, iEnd = iStart + icount - 1;
    do {
      int32_t iMid = (iStart + iEnd) / 2;
      const XFA_METHODINFO* pInfo = g_SomMethodData + iMid;
      if (uHash == pInfo->uHash) {
        return pInfo;
      } else if (uHash < pInfo->uHash) {
        iEnd = iMid - 1;
      } else {
        iStart = iMid + 1;
      }
    } while (iStart <= iEnd);
    iElementIndex = scriptIndex->wParentIndex;
  }
  return NULL;
}
const XFA_SCRIPTATTRIBUTEINFO* XFA_GetScriptAttributeByName(
    XFA_ELEMENT eElement,
    const CFX_WideStringC& wsAttributeName) {
  int32_t iLength = wsAttributeName.GetLength();
  if (iLength == 0) {
    return NULL;
  }
  int32_t iElementIndex = eElement;
  while (iElementIndex != -1) {
    XFA_LPCSCRIPTHIERARCHY scriptIndex = g_XFAScriptIndex + iElementIndex;
    int32_t icount = scriptIndex->wAttributeCount;
    if (icount == 0) {
      iElementIndex = scriptIndex->wParentIndex;
      continue;
    }
    uint32_t uHash = FX_HashCode_String_GetW(wsAttributeName.GetPtr(), iLength);
    int32_t iStart = scriptIndex->wAttributeStart, iEnd = iStart + icount - 1;
    do {
      int32_t iMid = (iStart + iEnd) / 2;
      const XFA_SCRIPTATTRIBUTEINFO* pInfo = g_SomAttributeData + iMid;
      if (uHash == pInfo->uHash) {
        return pInfo;
      } else if (uHash < pInfo->uHash) {
        iEnd = iMid - 1;
      } else {
        iStart = iMid + 1;
      }
    } while (iStart <= iEnd);
    iElementIndex = scriptIndex->wParentIndex;
  }
  return NULL;
}
void CXFA_Measurement::Set(const CFX_WideStringC& wsMeasure) {
  if (wsMeasure.IsEmpty()) {
    m_fValue = 0;
    m_eUnit = XFA_UNIT_Unknown;
    return;
  }
  int32_t iUsedLen = 0;
  int32_t iOffset = (wsMeasure.GetAt(0) == L'=') ? 1 : 0;
  FX_FLOAT fValue = FX_wcstof(wsMeasure.GetPtr() + iOffset,
                              wsMeasure.GetLength() - iOffset, &iUsedLen);
  XFA_UNIT eUnit = GetUnit(wsMeasure.Mid(iOffset + iUsedLen));
  Set(fValue, eUnit);
}
FX_BOOL CXFA_Measurement::ToString(CFX_WideString& wsMeasure) const {
  switch (GetUnit()) {
    case XFA_UNIT_Mm:
      wsMeasure.Format(L"%.8gmm", GetValue());
      return TRUE;
    case XFA_UNIT_Pt:
      wsMeasure.Format(L"%.8gpt", GetValue());
      return TRUE;
    case XFA_UNIT_In:
      wsMeasure.Format(L"%.8gin", GetValue());
      return TRUE;
    case XFA_UNIT_Cm:
      wsMeasure.Format(L"%.8gcm", GetValue());
      return TRUE;
    case XFA_UNIT_Mp:
      wsMeasure.Format(L"%.8gmp", GetValue());
      return TRUE;
    case XFA_UNIT_Pc:
      wsMeasure.Format(L"%.8gpc", GetValue());
      return TRUE;
    case XFA_UNIT_Em:
      wsMeasure.Format(L"%.8gem", GetValue());
      return TRUE;
    case XFA_UNIT_Percent:
      wsMeasure.Format(L"%.8g%%", GetValue());
      return TRUE;
    default:
      wsMeasure.Format(L"%.8g", GetValue());
      return FALSE;
  }
}
FX_BOOL CXFA_Measurement::ToUnit(XFA_UNIT eUnit, FX_FLOAT& fValue) const {
  fValue = GetValue();
  XFA_UNIT eFrom = GetUnit();
  if (eFrom == eUnit) {
    return TRUE;
  }
  switch (eFrom) {
    case XFA_UNIT_Pt:
      break;
    case XFA_UNIT_Mm:
      fValue *= 72 / 2.54f / 10;
      break;
    case XFA_UNIT_In:
      fValue *= 72;
      break;
    case XFA_UNIT_Cm:
      fValue *= 72 / 2.54f;
      break;
    case XFA_UNIT_Mp:
      fValue *= 0.001f;
      break;
    case XFA_UNIT_Pc:
      fValue *= 12.0f;
      break;
    default:
      fValue = 0;
      return FALSE;
  }
  switch (eUnit) {
    case XFA_UNIT_Pt:
      return TRUE;
    case XFA_UNIT_Mm:
      fValue /= 72 / 2.54f / 10;
      return TRUE;
    case XFA_UNIT_In:
      fValue /= 72;
      return TRUE;
    case XFA_UNIT_Cm:
      fValue /= 72 / 2.54f;
      return TRUE;
    case XFA_UNIT_Mp:
      fValue /= 0.001f;
      return TRUE;
    case XFA_UNIT_Pc:
      fValue /= 12.0f;
      return TRUE;
    default:
      fValue = 0;
      return FALSE;
  }
  return FALSE;
}
XFA_UNIT CXFA_Measurement::GetUnit(const CFX_WideStringC& wsUnit) {
  if (wsUnit == FX_WSTRC(L"mm")) {
    return XFA_UNIT_Mm;
  } else if (wsUnit == FX_WSTRC(L"pt")) {
    return XFA_UNIT_Pt;
  } else if (wsUnit == FX_WSTRC(L"in")) {
    return XFA_UNIT_In;
  } else if (wsUnit == FX_WSTRC(L"cm")) {
    return XFA_UNIT_Cm;
  } else if (wsUnit == FX_WSTRC(L"pc")) {
    return XFA_UNIT_Pc;
  } else if (wsUnit == FX_WSTRC(L"mp")) {
    return XFA_UNIT_Mp;
  } else if (wsUnit == FX_WSTRC(L"em")) {
    return XFA_UNIT_Em;
  } else if (wsUnit == FX_WSTRC(L"%")) {
    return XFA_UNIT_Percent;
  } else {
    return XFA_UNIT_Unknown;
  }
}
IFX_Stream* XFA_CreateWideTextRead(const CFX_WideString& wsBuffer) {
  return new CXFA_WideTextRead(wsBuffer);
}
CXFA_WideTextRead::CXFA_WideTextRead(const CFX_WideString& wsBuffer)
    : m_wsBuffer(wsBuffer), m_iPosition(0), m_iRefCount(1) {}
void CXFA_WideTextRead::Release() {
  if (--m_iRefCount < 1) {
    delete this;
  }
}
IFX_Stream* CXFA_WideTextRead::Retain() {
  m_iRefCount++;
  return this;
}
FX_DWORD CXFA_WideTextRead::GetAccessModes() const {
  return FX_STREAMACCESS_Read | FX_STREAMACCESS_Text;
}
int32_t CXFA_WideTextRead::GetLength() const {
  return m_wsBuffer.GetLength() * sizeof(FX_WCHAR);
}
int32_t CXFA_WideTextRead::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) {
  switch (eSeek) {
    case FX_STREAMSEEK_Begin:
      m_iPosition = iOffset;
      break;
    case FX_STREAMSEEK_Current:
      m_iPosition += iOffset;
      break;
    case FX_STREAMSEEK_End:
      m_iPosition = m_wsBuffer.GetLength() + iOffset;
      break;
  }
  if (m_iPosition < 0) {
    m_iPosition = 0;
  }
  if (m_iPosition > m_wsBuffer.GetLength()) {
    m_iPosition = m_wsBuffer.GetLength();
  }
  return GetPosition();
}
int32_t CXFA_WideTextRead::GetPosition() {
  return m_iPosition * sizeof(FX_WCHAR);
}
FX_BOOL CXFA_WideTextRead::IsEOF() const {
  return m_iPosition >= m_wsBuffer.GetLength();
}
int32_t CXFA_WideTextRead::ReadString(FX_WCHAR* pStr,
                                      int32_t iMaxLength,
                                      FX_BOOL& bEOS,
                                      int32_t const* pByteSize) {
  if (iMaxLength > m_wsBuffer.GetLength() - m_iPosition) {
    iMaxLength = m_wsBuffer.GetLength() - m_iPosition;
  }
  FXSYS_wcsncpy(pStr, (const FX_WCHAR*)m_wsBuffer + m_iPosition, iMaxLength);
  m_iPosition += iMaxLength;
  bEOS = IsEOF();
  return iMaxLength;
}
uint16_t CXFA_WideTextRead::GetCodePage() const {
  return (sizeof(FX_WCHAR) == 2) ? FX_CODEPAGE_UTF16LE : FX_CODEPAGE_UTF32LE;
}
uint16_t CXFA_WideTextRead::SetCodePage(uint16_t wCodePage) {
  return GetCodePage();
}