summaryrefslogtreecommitdiff
path: root/core/fxcrt/fx_basic_utf.cpp
blob: fcfd5934e49af9648d311bb521a1c58c367a5f46 (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
// 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 "core/fxcrt/fx_basic.h"

void CFX_UTF8Decoder::Clear() {
  m_Buffer.Clear();
  m_PendingBytes = 0;
}

void CFX_UTF8Decoder::AppendChar(uint32_t ch) {
  m_Buffer.AppendChar((wchar_t)ch);
}

void CFX_UTF8Decoder::Input(uint8_t byte) {
  if (byte < 0x80) {
    m_PendingBytes = 0;
    m_Buffer.AppendChar(byte);
  } else if (byte < 0xc0) {
    if (m_PendingBytes == 0) {
      return;
    }
    m_PendingBytes--;
    m_PendingChar |= (byte & 0x3f) << (m_PendingBytes * 6);
    if (m_PendingBytes == 0) {
      AppendChar(m_PendingChar);
    }
  } else if (byte < 0xe0) {
    m_PendingBytes = 1;
    m_PendingChar = (byte & 0x1f) << 6;
  } else if (byte < 0xf0) {
    m_PendingBytes = 2;
    m_PendingChar = (byte & 0x0f) << 12;
  } else if (byte < 0xf8) {
    m_PendingBytes = 3;
    m_PendingChar = (byte & 0x07) << 18;
  } else if (byte < 0xfc) {
    m_PendingBytes = 4;
    m_PendingChar = (byte & 0x03) << 24;
  } else if (byte < 0xfe) {
    m_PendingBytes = 5;
    m_PendingChar = (byte & 0x01) << 30;
  }
}

CFX_UTF8Encoder::CFX_UTF8Encoder() {}

CFX_UTF8Encoder::~CFX_UTF8Encoder() {}

void CFX_UTF8Encoder::Input(wchar_t unicodeAsWchar) {
  uint32_t unicode = static_cast<uint32_t>(unicodeAsWchar);
  if (unicode < 0x80) {
    m_Buffer.push_back(unicode);
  } else {
    if (unicode >= 0x80000000) {
      return;
    }
    int nbytes = 0;
    if (unicode < 0x800) {
      nbytes = 2;
    } else if (unicode < 0x10000) {
      nbytes = 3;
    } else if (unicode < 0x200000) {
      nbytes = 4;
    } else if (unicode < 0x4000000) {
      nbytes = 5;
    } else {
      nbytes = 6;
    }
    static uint8_t prefix[] = {0xc0, 0xe0, 0xf0, 0xf8, 0xfc};
    int order = 1 << ((nbytes - 1) * 6);
    int code = unicodeAsWchar;
    m_Buffer.push_back(prefix[nbytes - 2] | (code / order));
    for (int i = 0; i < nbytes - 1; i++) {
      code = code % order;
      order >>= 6;
      m_Buffer.push_back(0x80 | (code / order));
    }
  }
}

void CFX_UTF8Encoder::AppendStr(const CFX_ByteStringC& str) {
  m_Buffer.insert(m_Buffer.end(), str.begin(), str.end());
}

CFX_ByteStringC CFX_UTF8Encoder::GetResult() const {
  return CFX_ByteStringC(m_Buffer.data(), m_Buffer.size());
}

CFX_ByteString FX_UTF8Encode(const CFX_WideStringC& wsStr) {
  FX_STRSIZE len = wsStr.GetLength();
  const wchar_t* pStr = wsStr.unterminated_c_str();
  CFX_UTF8Encoder encoder;
  while (len-- > 0)
    encoder.Input(*pStr++);

  return CFX_ByteString(encoder.GetResult());
}