OXYGENHUB
# 0adc2e74f5cf x16-emulator / src / iso_8859_15.c
1.7 KiB · C · last changed chore(): initial commit 16 days ago source blame history raw
1
// Commander X16 Emulator
2
// Copyright (c) 2019,2022 Michael Steil
3
// All rights reserved. License: 2-clause BSD
4
5
#include <stdint.h>
6
#include <stdio.h>
7
#include "utf8_encode.h"
8
9
uint8_t
10
iso8859_15_from_unicode(uint32_t c)
11
{
12
	// line feed -> carriage return
13
	if (c == '\n') {
14
		return '\r';
15
	}
16
17
	// translate Unicode characters not part of Latin-1 but part of Latin-15
18
	switch (c) {
19
		case 0x20ac: // '€'
20
			return 0xa4;
21
		case 0x160: // 'Š'
22
			return 0xa6;
23
		case 0x161: // 'š'
24
			return 0xa8;
25
		case 0x17d: // 'Ž'
26
			return 0xb4;
27
		case 0x17e: // 'ž'
28
			return 0xb8;
29
		case 0x152: // 'Œ'
30
			return 0xbc;
31
		case 0x153: // 'œ'
32
			return 0xbd;
33
		case 0x178: // 'Ÿ'
34
			return 0xbe;
35
	}
36
37
	// remove Unicode characters part of Latin-1 but not part of Latin-15
38
	switch (c) {
39
		case 0xa4: // '¤'
40
		case 0xa6: // '¦'
41
		case 0xa8: // '¨'
42
		case 0xb4: // '´'
43
		case 0xb8: // '¸'
44
		case 0xbc: // '¼'
45
		case 0xbd: // '½'
46
		case 0xbe: // '¾'
47
			return '?';
48
	}
49
50
	// all other Unicode characters are also unsupported
51
	if (c >= 256) {
52
		return '?';
53
	}
54
55
	// everything else is Latin-15 already
56
	return c;
57
}
58
59
uint32_t
60
unicode_from_iso8859_15(uint8_t c)
61
{
62
	// translate Latin-15 characters not part of Latin-1
63
	switch (c) {
64
		case 0xa4:
65
			return 0x20ac; // '€'
66
		case 0xa6:
67
			return 0x160; // 'Š'
68
		case 0xa8:
69
			return 0x161; // 'š'
70
		case 0xb4:
71
			return 0x17d; // 'Ž'
72
		case 0xb8:
73
			return 0x17e; // 'ž'
74
		case 0xbc:
75
			return 0x152; // 'Œ'
76
		case 0xbd:
77
			return 0x153; // 'œ'
78
		case 0xbe:
79
			return 0x178; // 'Ÿ'
80
		default:
81
			return c;
82
	}
83
}
84
85
// converts the character to UTF-8 and prints it
86
void
87
print_iso8859_15_char(char c)
88
{
89
	char utf8[5];
90
	utf8_encode(utf8, unicode_from_iso8859_15(c));
91
	printf("%s", utf8);
92
}