Skip to content

Instantly share code, notes, and snippets.

@Explorer09
Last active May 16, 2024 11:14
Show Gist options
  • Save Explorer09/1da382e4b1cf3bf2e8009e60836af70b to your computer and use it in GitHub Desktop.
Save Explorer09/1da382e4b1cf3bf2e8009e60836af70b to your computer and use it in GitHub Desktop.
Unicode 16.0 "Block Octants" bits to code point / UTF-8 function
/** @file octant_bits_to_unicode.c
Unicode 16.0 "Block Octants" bits to code point / UTF-8 function
Copyright 2024 Kang-Che Sung <explorer09 @ gmail.com>
MIT License (MIT/Expat)
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* SPDX-License-Identifier: MIT */
#include <stdint.h>
#include <stdio.h>
#include <string.h>
struct unicode_octant_map_entry {
uint8_t octant_bits;
uint8_t data;
};
static const struct unicode_octant_map_entry unicode_octant_map[26] = {
{ 0x00, 0x00 /* U+00A0 */ },
{ 0x01, 0xA8 /* U+1CEA8 */ },
{ 0x02, 0xAB /* U+1CEAB */ },
{ 0x03, 0xC2 /* U+1FB82 */ },
{ 0x05, 0x98 /* U+2598 */ },
{ 0x0A, 0x9D /* U+259D */ },
{ 0x0F, 0x80 /* U+2580 */ },
{ 0x14, 0xE6 /* U+1FBE6 */ },
{ 0x28, 0xE7 /* U+1FBE7 */ },
{ 0x3F, 0xC5 /* U+1FB85 */ },
{ 0x40, 0xA3 /* U+1CEA3 */ },
{ 0x50, 0x96 /* U+2596 */ },
{ 0x55, 0x8C /* U+258C */ },
{ 0x5A, 0x9E /* U+259E */ },
{ 0x5F, 0x9B /* U+259B */ },
{ 0x80, 0xA0 /* U+1CEA0 */ },
{ 0xA0, 0x97 /* U+2597 */ },
{ 0xA5, 0x9A /* U+259A */ },
{ 0xAA, 0x90 /* U+2590 */ },
{ 0xAF, 0x9C /* U+259C */ },
{ 0xC0, 0x82 /* U+2582 */ },
{ 0xF0, 0x84 /* U+2584 */ },
{ 0xF5, 0x99 /* U+2599 */ },
{ 0xFA, 0x9F /* U+259F */ },
{ 0xFC, 0x86 /* U+2586 */ },
{ 0xFF, 0x88 /* U+2588 */ }
};
static int find_unicode_octant_map_data(uint8_t octant_bits)
{
unsigned int first = 0;
unsigned int last =
sizeof(unicode_octant_map) / sizeof(unicode_octant_map[0]);
while (first < last) {
unsigned int i = (first + last) / 2;
if (octant_bits == unicode_octant_map[i].octant_bits) {
return unicode_octant_map[i].data;
}
if (octant_bits > unicode_octant_map[i].octant_bits) {
first = i + 1;
} else {
last = i;
}
}
return -(int)first;
}
uint32_t octant_bits_to_utf32(uint8_t octant_bits)
{
int data = find_unicode_octant_map_data(octant_bits);
if (data < 0) {
return (octant_bits + (uint32_t)data) | 0x1CD00;
}
if (data == 0x00) {
return 0x00A0;
}
switch ((data >> 5) & 0x3) {
case 0:
/* "Block Elements" block */
return ((uint32_t)data & 0x1F) | 0x2580;
case 1:
/* Part of "Symbols for Legacy Computing Supplement" block */
return ((uint32_t)data & 0x1F) | 0x1CEA0;
case 2:
/* Part of "Symbols for Legacy Computing" block */
return ((uint32_t)data & 0x1F) | 0x1FB80;
case 3:
/* Part of "Symbols for Legacy Computing" block */
return ((uint32_t)data & 0x1F) | 0x1FBE6;
}
}
int octant_bits_to_utf8(uint8_t octant_bits, uint8_t out_buf[5])
{
static const uint8_t utf8_sequences[6][4] = {
{ 0xF0, 0x9C, 0xB4, 0x80 },
{ 0xC2, 0xA0, 0x00, 0x00 },
{ 0xE2, 0x96, 0x80, 0x00 },
{ 0xF0, 0x9C, 0xBA, 0xA0 },
{ 0xF0, 0x9F, 0xAE, 0x80 },
{ 0xF0, 0x9F, 0xAF, 0xA6 }
};
int data = find_unicode_octant_map_data(octant_bits);
const uint8_t (*seq_start)[4];
uint8_t code_offset;
if (data < 0) {
seq_start = &utf8_sequences[0];
code_offset = (uint8_t)(octant_bits + data);
} else if (data == 0x00) {
seq_start = &utf8_sequences[1];
code_offset = 0;
} else {
seq_start = &utf8_sequences[2 + ((data >> 5) & 0x3)];
code_offset = (uint8_t)data & 0x1F;
}
int length = ((*seq_start)[0] >> 4) > 0xE ? 4 :
(((*seq_start)[0] >> 4) == 0xE ? 3 : 2);
if (out_buf != NULL) {
memcpy(out_buf, seq_start, sizeof(*seq_start));
out_buf[4] = 0x00;
out_buf[length - 1] |= (code_offset & 0x3F);
out_buf[length - 2] |= (code_offset >> 6);
}
return length;
}
int octant_bits_to_utf16(uint8_t octant_bits, uint16_t out_buf[3])
{
static const uint16_t utf16_sequences[6][2] = {
{ 0xD833, 0xDD00 },
{ 0x00A0, 0x0000 },
{ 0x2580, 0x0000 },
{ 0xD833, 0xDEA0 },
{ 0xD83E, 0xDF80 },
{ 0xD83E, 0xDFE6 }
};
int data = find_unicode_octant_map_data(octant_bits);
const uint16_t (*seq_start)[2];
uint8_t code_offset;
if (data < 0) {
seq_start = &utf16_sequences[0];
code_offset = (uint8_t)(octant_bits + data);
} else if (data == 0x00) {
seq_start = &utf16_sequences[1];
code_offset = 0;
} else {
seq_start = &utf16_sequences[2 + ((data >> 5) & 0x3)];
code_offset = (uint8_t)data & 0x1F;
}
int length = ((*seq_start)[0] & 0xFC00) == 0xD800 ? 2 : 1;
if (out_buf != NULL) {
memcpy(out_buf, seq_start, sizeof(*seq_start));
out_buf[2] = 0x0000;
out_buf[length - 1] |= code_offset;
}
return length;
}
int main(int argc, char *argv[])
{
uint8_t utf8_buf[5];
uint16_t utf16_buf[3];
for (unsigned int i = 0; i < 256; i++) {
uint32_t code_point = octant_bits_to_utf32((uint8_t)i);
octant_bits_to_utf8((uint8_t)i, utf8_buf);
octant_bits_to_utf16((uint8_t)i, utf16_buf);
printf("%02X: %sU+%04lX, %02X %02X %02X %02X, %04X %04X, %s\n",
i,
code_point <= 0xFFFF ? " " : "",
(unsigned long)code_point,
utf8_buf[0], utf8_buf[1], utf8_buf[2], utf8_buf[3],
utf16_buf[0], utf16_buf[1],
utf8_buf);
}
printf("\n");
for (unsigned int i = 0; i < 256; i++) {
octant_bits_to_utf8((uint8_t)i, utf8_buf);
printf("%s%s", utf8_buf, i % 16 == 15 ? "\n" : "");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment