Skip to content

Instantly share code, notes, and snippets.

@michal-h21
Last active October 1, 2018 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michal-h21/5922487ed74f3c1b6a34c49904971b0d to your computer and use it in GitHub Desktop.
Save michal-h21/5922487ed74f3c1b6a34c49904971b0d to your computer and use it in GitHub Desktop.
\documentclass{book}
\usepackage{fontspec}
\setmainfont{Coelacanth}
\usepackage{luacode}
\begin{luacode*}
-- table with mappings from unicode to char codes
unicode_chars = {}
function load_unicode_chars(current_font)
local chars = {}
-- process all characters in the font
for i,v in pairs(fonts.hashes.identifiers[current_font].characters or {}) do
local univalue = v.unicode
-- save the mapping from unicode to the character index
if univalue then chars[v.unicode] = i end
end
-- cache the character table
unicode_chars[current_font] = chars
return chars
end
function get_unicode_char(character)
local current_font = font.current()
-- get the saved character table for the current font, or generate a new one
local current_font_chars = unicode_chars[current_font] or load_unicode_chars(current_font)
return current_font_chars[character]
end
local utf_char = utf.char
function print_unicode_char(character)
local num
if character:match('^"') then
character = character:sub(2)
num = tonumber(character, 16)
else
num = tonumber(character)
end
tex.print(utf_char(get_unicode_char(num)))
end
\end{luacode*}
% \newcommand\byunicode[1]{\directlua{tex.print(utf.char(get_unicode_char(tonumber("#1"))))}}
\newcommand\byunicode[1]{\directlua{print_unicode_char("\luatexluaescapestring{#1}")}}
\let\altbyunicode\byunicode
\begin{document}
\byunicode{62860} \byunicode{\number"F58C}
\byunicode{62860} \byunicode{"F58C}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment