Skip to content

Instantly share code, notes, and snippets.

@WhiteAbeLincoln
Created September 15, 2018 22:11
Show Gist options
  • Save WhiteAbeLincoln/7b87169b8f4e1b2759597ef18c9ea542 to your computer and use it in GitHub Desktop.
Save WhiteAbeLincoln/7b87169b8f4e1b2759597ef18c9ea542 to your computer and use it in GitHub Desktop.
Generate mathematical symbol XCompose mappings
#!/usr/bin/env python3
import unicodedata
import sys
#
# Helper script to generate XCompose files
#
# Usage:
#
# ./gen_x_compose.py >Compose
#
# Output in file 'Compose' then has to be manually adjusted
#
# How to use XCompose instructions on askubuntu.com:
# https://askubuntu.com/a/877344/99330
#
# adapted from https://github.com/rybak/scripts/blob/master/extra/gen_x_compose.py
# and made to work with arbitrary unicode ranges and prefixes
def debug(x):
print(x, file=sys.stderr)
def print_alphabet(starters, latin_start, unicode_start, size):
for i in range(0, size):
latin_letter = chr(latin_start + i)
unicode_letter = chr(unicode_start + i)
prefix = ' '.join([f'<{s}>' for s in starters])
try:
comment = unicodedata.name(unicode_letter)
print("<Multi_key> " + prefix + " <{}> : \"{}\" U{:04X} # {}".format(
latin_letter,
unicode_letter,
ord(unicode_letter),
comment
))
except:
# undefined Unicode code point
debug("Skipping...")
pass
print('# MATHEMATICAL BOLD')
BOLD_A = ord('𝐀')
BOLD_Z = ord('𝐙')
print_alphabet(['m', 'b'], ord('A'), BOLD_A, BOLD_Z - BOLD_A + 1)
BOLD_a = ord('𝐚')
BOLD_z = ord('𝐳')
print_alphabet(['m', 'b'], ord('a'), BOLD_a, BOLD_z - BOLD_a + 1)
print('# MATHEMATICAL ITALIC')
ITALIC_A = ord('𝐴')
ITALIC_Z = ord('𝑍')
print_alphabet(['m', 'i'], ord('A'), ITALIC_A, ITALIC_Z - ITALIC_A + 1)
ITALIC_a = ord('π‘Ž')
ITALIC_z = ord('𝑧')
print_alphabet(['m', 'i'], ord('a'), ITALIC_a, ITALIC_z - ITALIC_a + 1)
print('# MATHEMATICAL BOLD ITALIC')
ITALIC_B_A = ord('𝑨')
ITALIC_B_Z = ord('𝒁')
print_alphabet(['m', 'I'], ord('A'), ITALIC_B_A, ITALIC_B_Z - ITALIC_B_A + 1)
ITALIC_B_a = ord('𝒂')
ITALIC_B_z = ord('𝒛')
print_alphabet(['m', 'I'], ord('a'), ITALIC_B_a, ITALIC_B_z - ITALIC_B_a + 1)
print('# MATHEMATICAL SCRIPT')
SCRIPT_A = ord('π’œ')
SCRIPT_Z = ord('𝒡')
print_alphabet(['m', 's'], ord('A'), SCRIPT_A, SCRIPT_Z - SCRIPT_A + 1)
SCRIPT_a = ord('𝒢')
SCRIPT_z = ord('𝓏')
print_alphabet(['m', 's'], ord('a'), SCRIPT_a, SCRIPT_z - SCRIPT_a + 1)
print('# MATHEMATICAL BOLD SCRIPT')
SCRIPT_B_A = ord('𝓐')
SCRIPT_B_Z = ord('𝓩')
print_alphabet(['m', 'S'], ord('A'), SCRIPT_B_A, SCRIPT_B_Z - SCRIPT_B_A + 1)
SCRIPT_B_a = ord('π“ͺ')
SCRIPT_B_z = ord('𝔃')
print_alphabet(['m', 'S'], ord('a'), SCRIPT_B_a, SCRIPT_B_z - SCRIPT_B_a + 1)
print('# MATHEMATICAL FRACTUR')
FRACTUR_A = ord('𝔄')
FRACTUR_Z = ord('π”œ') # not actually Z, since Z is in a different symbols block
print_alphabet(['m', 'f'], ord('A'), FRACTUR_A, FRACTUR_Z - FRACTUR_A + 1)
FRACTUR_a = ord('𝒢')
FRACTUR_z = ord('𝓏')
print_alphabet(['m', 'f'], ord('a'), FRACTUR_a, FRACTUR_z - FRACTUR_a + 1)
print('# MATHEMATICAL DOUBLE-STRUCK')
DOUBLE_A = ord('𝔸')
DOUBLE_Z = ord('𝕐') # not actually Z, since Z is in a different symbols block
print_alphabet(['m', 'd'], ord('A'), DOUBLE_A, DOUBLE_Z - DOUBLE_A + 1)
DOUBLE_a = ord('𝕒')
DOUBLE_z = ord('𝕫')
print_alphabet(['m', 'd'], ord('a'), DOUBLE_a, DOUBLE_z - DOUBLE_a + 1)
print('# MATHEMATICAL BOLD FRACTUR')
FRACTUR_B_A = ord('𝕬')
FRACTUR_B_Z = ord('𝖅')
print_alphabet(['m', 'F'], ord('A'), FRACTUR_B_A, FRACTUR_B_Z - FRACTUR_B_A + 1)
FRACTUR_B_a = ord('𝖆')
FRACTUR_B_z = ord('π–Ÿ')
print_alphabet(['m', 'F'], ord('a'), FRACTUR_B_a, FRACTUR_B_z - FRACTUR_B_a + 1)
print('# MATHEMATICAL LETTER-LIKE SYMBOLS')
# plank's constant, or italic h
print_alphabet(['m', 'i'], ord('h'), ord('β„Ž'), 1)
# script
print_alphabet(['m', 's'], ord('B'), ord('ℬ'), 1)
print_alphabet(['m', 's'], ord('E'), ord('β„°'), 1)
print_alphabet(['m', 's'], ord('F'), ord('β„±'), 1)
print_alphabet(['m', 's'], ord('H'), ord('β„‹'), 1)
print_alphabet(['m', 's'], ord('I'), ord('ℐ'), 1)
print_alphabet(['m', 's'], ord('L'), ord('β„’'), 1)
print_alphabet(['m', 's'], ord('M'), ord('β„³'), 1)
print_alphabet(['m', 's'], ord('R'), ord('β„›'), 1)
# script lower
print_alphabet(['m', 's'], ord('e'), ord('β„―'), 1)
print_alphabet(['m', 's'], ord('g'), ord('β„Š'), 1)
print_alphabet(['m', 's'], ord('o'), ord('β„΄'), 1)
# fractur
print_alphabet(['m', 'f'], ord('C'), ord('β„­'), 1)
print_alphabet(['m', 'f'], ord('H'), ord('β„Œ'), 1)
print_alphabet(['m', 'f'], ord('I'), ord('β„‘'), 1)
print_alphabet(['m', 'f'], ord('R'), ord('β„œ'), 1)
print_alphabet(['m', 'f'], ord('Z'), ord('ℨ'), 1)
# double-struck
print_alphabet(['m', 'd'], ord('C'), ord('β„‚'), 1)
print_alphabet(['m', 'd'], ord('H'), ord('ℍ'), 1)
print_alphabet(['m', 'd'], ord('N'), ord('β„•'), 1)
print_alphabet(['m', 'd'], ord('P'), ord('β„™'), 1)
print_alphabet(['m', 'd'], ord('Q'), ord('β„š'), 1)
print_alphabet(['m', 'd'], ord('R'), ord('ℝ'), 1)
print_alphabet(['m', 'd'], ord('Z'), ord('β„€'), 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment