summaryrefslogtreecommitdiffstatshomepage
path: root/src/tools/imgtool/charconv.cpp
blob: 0dadc8acb88dfe606bca1389d406935cc84a0bf7 (plain) (blame)
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
// license:BSD-3-Clause
// copyright-holders:Nathan Woods
/***************************************************************************

    charconv.cpp

    Imgtool character set conversion routines.

***************************************************************************/

#include "corestr.h"
#include "charconv.h"
#include "unicode.h"
#include "coretmpl.h"

imgtool::simple_charconverter imgtool::charconverter_iso_8859_1(nullptr, nullptr);


//-------------------------------------------------
//  from_utf8
//-------------------------------------------------

void imgtool::charconverter::from_utf8(std::ostream &dest, const std::string &src) const
{
	from_utf8(dest, src.c_str(), src.size());
}


//-------------------------------------------------
//  to_utf8
//-------------------------------------------------

void imgtool::charconverter::to_utf8(std::ostream &dest, const std::string &src) const
{
	to_utf8(dest, src.c_str(), src.size());
}


//-------------------------------------------------
//  simple_charconverter::simple_charconverter
//-------------------------------------------------

imgtool::simple_charconverter::simple_charconverter(const char32_t lowpage[0x80], const char32_t highpage[0x80], unicode_normalization_form norm)
	: m_norm(norm), m_lowpage(lowpage), m_highpage(highpage)
{
	// build the reverse lookup table
	for (int i = 0; i < 256; i++)
	{
		const char32_t *page = i >= 128 ? m_highpage : m_lowpage;
		char32_t unicode_char = page ? page[i % 128] : i;
		m_reverse_lookup.emplace_back(unicode_char, (char)i);
	}

	// and sort it
	std::sort(m_reverse_lookup.begin(), m_reverse_lookup.end(), [](const std::pair<char32_t, char> &a, const std::pair<char32_t, char> &b)
	{
		return b.first > a.first;
	});
}


//-------------------------------------------------
//  from_utf8
//-------------------------------------------------

void imgtool::simple_charconverter::from_utf8(std::ostream &dest, const char *src, size_t src_length) const
{
	// normalize the incoming unicode
	std::string normalized_src = normalize_unicode(src, src_length, m_norm);

	auto iter = normalized_src.begin();
	while (iter != normalized_src.end())
	{
		// get the next character
		char32_t ch;
		int rc = uchar_from_utf8(&ch, &*iter, normalized_src.end() - iter);
		if (rc < 0)
		{
			ch = 0xFFFD;
			rc = 1;
		}
		iter += rc;

		// do the reverse lookup
		auto lookup = std::lower_bound(m_reverse_lookup.begin(), m_reverse_lookup.end(), ch, [](const std::pair<char32_t, char> &a, const char32_t &b)
		{
			return a.first < b;
		});
		if (lookup == m_reverse_lookup.end())
			throw charconverter_exception();

		// and output the results
		dest << lookup->second;
	}
}


//-------------------------------------------------
//  to_utf8
//-------------------------------------------------

void imgtool::simple_charconverter::to_utf8(std::ostream &dest, const char *src, size_t src_length) const
{
	for (size_t i = 0; i < src_length; i++)
	{
		// which page is this in?
		const char32_t *page = ((src[i] & 0x80) == 0) ? m_lowpage : m_highpage;

		// is this page present?
		if ((src[i] & 0x80) == 0)
		{
			// no - pass it on
			dest << src[i];
		}
		else
		{
			// yes - we need to do a lookup
			size_t base = ((src[i] & 0x80) == 0) ? 0x00 : 0x80;
			char32_t ch = page[((unsigned char)(src[i])) - base];
			if (ch == 0)
				throw charconverter_exception();

			dest << utf8_from_uchar(ch);
		}
	}
}