%% %% Copyright (C) 2020 by Xiangdong Zeng %% %% This work may be distributed and/or modified under the %% conditions of the LaTeX Project Public License, either %% version 1.3c of this license or (at your option) any later %% version. The latest version of this license is in: %% %% http://www.latex-project.org/lppl.txt %% %% and version 1.3 or later is part of all distributions of %% LaTeX version 2005/12/01 or later. %% %% This work has the LPPL maintenance status `maintained'. %% %% The Current Maintainer of this work is Xiangdong Zeng. %% \NeedsTeXFormat{LaTeX2e}[2020/02/02] \ProvidesExplPackage{emoji}{2020/06/27}{0.2.1}{Emoji support in (Lua)LaTeX} \msg_new:nnn { emoji } { require-luatex } { The~'emoji'~package~requires~LuaTeX. } \sys_if_engine_luatex:F { \msg_critical:nn { emoji } { require-luatex } } \RequirePackage { fontspec } % Set emoji font. Internally use fontspec's `\setfontface`. % #1: option % #2: name \NewDocumentCommand \setemojifont { m o } { \__emoji_if_preamble:TF { \AtBeginDocument } { \use:n } { \IfValueTF {#2} { \emoji_set_font:nn {#1} {#2} } { \emoji_set_font:n {#1} } } } % Check if in the preamble. \prg_set_conditional:Npnn \__emoji_if_preamble: { TF } { \cs_if_eq:NNTF \@onlypreamble \@notprerr { \prg_return_false: } { \prg_return_true: } } % Define `\emoji_font:` when used. \cs_new_protected:Npn \emoji_set_font:nn #1#2 { \setfontface \emoji_font: {#1} [ Renderer = HarfBuzz, #2 ] } \cs_new_protected:Npn \emoji_set_font:n #1 { \emoji_set_font:nn {#1} {} } % Detect available emoji fonts from OS or TEXMF tree. Currently support: % - Apple Color Emoji % - Segoe UI Emoji % - Noto Color Emoji % Remarks: % - `\file_if_exist:nTF` is much faster than `\fontspec_font_if_exist:nTF`. % - We use environment variable `$WINDIR` for path on Windows. \cs_new_protected:Npn \__emoji_detect_font: { \file_if_exist:nTF { /System/Library/Fonts/Apple~Color~Emoji.ttc } { \emoji_set_font:n { Apple~Color~Emoji } } { \file_if_exist:nTF { \c_dollar_str WINDIR/Fonts/seguiemj.ttf } { \emoji_set_font:n { Segoe~UI~Emoji } } { \fontspec_font_if_exist:nTF { Noto~Color~Emoji } { \emoji_set_font:n { Noto~Color~Emoji } } { \msg_warning:nn { emoji } { no-emoji-font } } } } } \msg_new:nnn { emoji } { no-emoji-font } { It~seems~that~you~have~not~declare~an~emoji~font. \\ You~should~use~"\setemojifont"~to~set~a~font. } % If the user doesn't set emoji font explicitly, then detect and set default % fonts automatically. \AtBeginDocument { \cs_if_exist_use:NF \emoji_font: { \__emoji_detect_font: } } % The main command for use emoji. % #1: name \NewDocumentCommand \emoji { m } { % TODO: options \emoji_if_name_exist:nTF {#1} { \emoji_print:n {#1} } { \msg_error:nnn { emoji } { emoji-not-exist } {#1} } } \msg_new:nnn { emoji } { emoji-not-exist } { The~emoji~name~"#1"~can't~be~found. \\ Please~check~your~spelling~or~try~another~one. } % Check if an emoji name exists or not. % #1: name \prg_new_protected_conditional:Npnn \emoji_if_name_exist:n #1 { T, F, TF } { \tl_if_exist:cTF { c__emoji_ #1 _tl } { \prg_return_true: } { \prg_return_false: } } % In a group, change to the emoji font then use corresponding tl constant. \cs_new_protected:Npn \emoji_print:n #1 { \group_begin: \emoji_font: \tl_use:c { c__emoji_ #1 _tl } \group_end: } % Define new emoji. They are stored in tl constants internally. % For special characters (#), we first change their catcode to 12 (other), % then define the tl constant. % This function is mainly used in `emoji-table.def`, but will be changed to % print the table in the documentation. % #1: Code points % #2: Name % #3: Aliases % #4: Description % #5: Version \cs_new_protected:Npn \__emoji_def:nnnnn { \group_begin: \char_set_catcode_other:N \# \__emoji_def_aux:nnnnn } \cs_new_protected:Npn \__emoji_def_aux:nnnnn #1#2#3#4#5 { \clist_map_inline:nn { #2, #3 } { \tl_const:cn { c__emoji_ ##1 _tl } {#1} } \group_end: } % These two commands will be used in the documentation. Set to be empty % commands as placeholders. \cs_set:Npn \__emoji_group:n #1 {} \cs_set:Npn \__emoji_subgroup:n #1 {} % Input the emoji definition file. % This file is generated by a Python scripts from Unicode and GitHub data. \file_input:n { emoji-table.def } % Compatibility with hyperref. % `\emoji{...}` will be turned to a normal character sequence. \AtBeginDocument { \@ifpackageloaded { hyperref } { \pdfstringdefDisableCommands { \cs_set_nopar:Npn \emoji #1 { \tl_use:c { c__emoji_ #1 _tl } } } } {} } \endinput