1 // Copyright 2018 PDFium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #include "core/fxcrt/cfx_utf8encoder.h" 8 9 CFX_UTF8Encoder::CFX_UTF8Encoder() = default; 10 11 CFX_UTF8Encoder::~CFX_UTF8Encoder() = default; 12 Input(wchar_t unicodeAsWchar)13void CFX_UTF8Encoder::Input(wchar_t unicodeAsWchar) { 14 uint32_t unicode = static_cast<uint32_t>(unicodeAsWchar); 15 if (unicode < 0x80) { 16 m_Buffer.push_back(unicode); 17 } else { 18 if (unicode >= 0x80000000) 19 return; 20 21 int nbytes = 0; 22 if (unicode < 0x800) 23 nbytes = 2; 24 else if (unicode < 0x10000) 25 nbytes = 3; 26 else if (unicode < 0x200000) 27 nbytes = 4; 28 else if (unicode < 0x4000000) 29 nbytes = 5; 30 else 31 nbytes = 6; 32 33 static const uint8_t prefix[] = {0xc0, 0xe0, 0xf0, 0xf8, 0xfc}; 34 int order = 1 << ((nbytes - 1) * 6); 35 int code = unicodeAsWchar; 36 m_Buffer.push_back(prefix[nbytes - 2] | (code / order)); 37 for (int i = 0; i < nbytes - 1; i++) { 38 code = code % order; 39 order >>= 6; 40 m_Buffer.push_back(0x80 | (code / order)); 41 } 42 } 43 } 44