| 1 |
%% |
| 2 |
%% Copyright (C) 2020, 2021 by Xiangdong Zeng <[email protected]> |
| 3 |
%% |
| 4 |
%% This work may be distributed and/or modified under the |
| 5 |
%% conditions of the LaTeX Project Public License, either |
| 6 |
%% version 1.3c of this license or (at your option) any later |
| 7 |
%% version. The latest version of this license is in: |
| 8 |
%% |
| 9 |
%% http://www.latex-project.org/lppl.txt |
| 10 |
%% |
| 11 |
%% and version 1.3 or later is part of all distributions of |
| 12 |
%% LaTeX version 2005/12/01 or later. |
| 13 |
%% |
| 14 |
%% This work has the LPPL maintenance status `maintained'. |
| 15 |
%% |
| 16 |
%% The Current Maintainer of this work is Xiangdong Zeng. |
| 17 |
%% |
| 18 |
\NeedsTeXFormat{LaTeX2e}[2020/02/02] |
| 19 |
\ProvidesExplPackage{emoji}{2021/07/17}{0.2.2}{Emoji support in (Lua)LaTeX} |
| 20 |
|
| 21 |
\msg_new:nnn { emoji } { require-luatex } |
| 22 |
{ The~'emoji'~package~requires~LuaTeX. } |
| 23 |
\sys_if_engine_luatex:F |
| 24 |
{ \msg_critical:nn { emoji } { require-luatex } } |
| 25 |
|
| 26 |
\RequirePackage { fontspec } |
| 27 |
|
| 28 |
% Set emoji font. When used in preamble, it will be delayed until the |
| 29 |
% `\AtBeginDocument` hook. |
| 30 |
% #1: name |
| 31 |
% #2: option |
| 32 |
\NewDocumentCommand \setemojifont { m o } |
| 33 |
{ |
| 34 |
\__emoji_if_preamble:TF |
| 35 |
{ |
| 36 |
% The actual `\emoji_font:` command will be defined in the |
| 37 |
% `\AtBeginDocument` hook, but it will precede our checking. |
| 38 |
% So we need to "pretend" that it's defined. |
| 39 |
\cs_set_eq:NN \emoji_font: \prg_do_nothing: |
| 40 |
\AtBeginDocument |
| 41 |
} |
| 42 |
{ \use:n } |
| 43 |
{ |
| 44 |
\IfValueTF {#2} |
| 45 |
{ \emoji_set_font:nn {#1} {#2} } |
| 46 |
{ \emoji_set_font:n {#1} } |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
% Check if in the preamble. |
| 51 |
\prg_set_conditional:Npnn \__emoji_if_preamble: { TF } |
| 52 |
{ |
| 53 |
\cs_if_eq:NNTF \@onlypreamble \@notprerr |
| 54 |
{ \prg_return_false: } |
| 55 |
{ \prg_return_true: } |
| 56 |
} |
| 57 |
|
| 58 |
% Define `\emoji_font:` when used. |
| 59 |
% #1: name |
| 60 |
% #2: option |
| 61 |
\cs_new_protected:Npn \emoji_set_font:nn #1#2 |
| 62 |
{ \setfontface \emoji_font: {#1} [ Renderer = HarfBuzz, #2 ] } |
| 63 |
\cs_new_protected:Npn \emoji_set_font:n #1 |
| 64 |
{ \emoji_set_font:nn {#1} {} } |
| 65 |
|
| 66 |
% Detect available emoji fonts from OS or TEXMF tree. Currently support: |
| 67 |
% - Apple Color Emoji |
| 68 |
% - Segoe UI Emoji |
| 69 |
% - Noto Color Emoji |
| 70 |
% Remarks: |
| 71 |
% - `\file_if_exist:nTF` is much faster than `\fontspec_font_if_exist:nTF`. |
| 72 |
% - We use environment variable `$WINDIR` for path on Windows. |
| 73 |
\cs_new_protected:Npn \__emoji_detect_font: |
| 74 |
{ |
| 75 |
\file_if_exist:nTF { /System/Library/Fonts/Apple~Color~Emoji.ttc } |
| 76 |
{ \emoji_set_font:n { Apple~Color~Emoji } } |
| 77 |
{ |
| 78 |
\file_if_exist:nTF { \c_dollar_str WINDIR/Fonts/seguiemj.ttf } |
| 79 |
{ \emoji_set_font:n { Segoe~UI~Emoji } } |
| 80 |
{ |
| 81 |
\fontspec_font_if_exist:nTF { Noto~Color~Emoji } |
| 82 |
{ \emoji_set_font:n { Noto~Color~Emoji } } |
| 83 |
{ \msg_warning:nn { emoji } { no-emoji-font } } |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
\msg_new:nnn { emoji } { no-emoji-font } |
| 88 |
{ |
| 89 |
It~seems~that~you~have~not~declare~an~emoji~font. \\ |
| 90 |
You~should~use~"\setemojifont"~to~set~a~font. |
| 91 |
} |
| 92 |
|
| 93 |
% The main command for use emoji. |
| 94 |
% #1: name |
| 95 |
\NewDocumentCommand \emoji { m } |
| 96 |
{ |
| 97 |
% TODO: options |
| 98 |
\emoji_if_name_exist:nTF {#1} |
| 99 |
{ \emoji_print:n {#1} } |
| 100 |
{ \msg_error:nnn { emoji } { emoji-not-exist } {#1} } |
| 101 |
} |
| 102 |
\msg_new:nnn { emoji } { emoji-not-exist } |
| 103 |
{ |
| 104 |
The~emoji~name~"#1"~can't~be~found. \\ |
| 105 |
Please~check~your~spelling~or~try~another~one. |
| 106 |
} |
| 107 |
|
| 108 |
% Check if an emoji name exists or not. |
| 109 |
% #1: name |
| 110 |
\prg_new_protected_conditional:Npnn \emoji_if_name_exist:n #1 { T, F, TF } |
| 111 |
{ |
| 112 |
\tl_if_exist:cTF { c__emoji_ #1 _tl } |
| 113 |
{ \prg_return_true: } |
| 114 |
{ \prg_return_false: } |
| 115 |
} |
| 116 |
|
| 117 |
% In a group, change to the emoji font then use corresponding tl constant. |
| 118 |
\cs_new_protected:Npn \emoji_print:n #1 |
| 119 |
{ |
| 120 |
\group_begin: |
| 121 |
\exp_args:Nv \__emoji_ltj_set_range:n { c__emoji_ #1 _tl } |
| 122 |
\emoji_font: |
| 123 |
\tl_use:c { c__emoji_ #1 _tl } |
| 124 |
\group_end: |
| 125 |
} |
| 126 |
\cs_new_eq:NN \__emoji_ltj_set_range:n \use_none:n |
| 127 |
|
| 128 |
% Define new emoji. They are stored in tl constants internally. |
| 129 |
% For special characters (#), we first change their catcode to 12 (other), |
| 130 |
% then define the tl constant. |
| 131 |
% This function is mainly used in `emoji-table.def`, but will be changed to |
| 132 |
% print the table in the documentation. |
| 133 |
% #1: Code points |
| 134 |
% #2: Name |
| 135 |
% #3: Aliases |
| 136 |
% #4: Description |
| 137 |
% #5: Version |
| 138 |
\cs_new_protected:Npn \__emoji_def:nnnnn |
| 139 |
{ |
| 140 |
\group_begin: |
| 141 |
\char_set_catcode_other:N \# |
| 142 |
\__emoji_def_aux:nnnnn |
| 143 |
} |
| 144 |
\cs_new_protected:Npn \__emoji_def_aux:nnnnn #1#2#3#4#5 |
| 145 |
{ |
| 146 |
\clist_map_inline:nn { #2, #3 } |
| 147 |
{ \tl_const:cn { c__emoji_ ##1 _tl } {#1} } |
| 148 |
\group_end: |
| 149 |
} |
| 150 |
|
| 151 |
% These two commands will be used in the documentation. Set to be empty |
| 152 |
% commands as placeholders. |
| 153 |
\cs_set:Npn \__emoji_group:n #1 {} |
| 154 |
\cs_set:Npn \__emoji_subgroup:n #1 {} |
| 155 |
|
| 156 |
% Input the emoji definition file. |
| 157 |
% This file is generated by a Python script from Unicode and GitHub data. |
| 158 |
\file_input:n { emoji-table.def } |
| 159 |
|
| 160 |
\cs_new_protected:Npn \__emoji_if_package_loaded:nT #1#2 |
| 161 |
{ \@ifpackageloaded {#1} {#2} {} } |
| 162 |
|
| 163 |
\AtBeginDocument |
| 164 |
{ |
| 165 |
% Compatibility with hyperref. |
| 166 |
% `\emoji{...}` will be turned to a normal character sequence. |
| 167 |
\__emoji_if_package_loaded:nT { hyperref } |
| 168 |
{ |
| 169 |
\pdfstringdefDisableCommands |
| 170 |
{ |
| 171 |
\cs_set_nopar:Npn \emoji #1 |
| 172 |
{ \tl_use:c { c__emoji_ #1 _tl } } |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
% Compatibility with luatexja. |
| 177 |
% Emoji should be "ALchar" so that the correct font can be used. |
| 178 |
\__emoji_if_package_loaded:nT { luatexja } |
| 179 |
{ |
| 180 |
\cs_set_protected:Npn \__emoji_ltj_set_range:n #1 |
| 181 |
{ |
| 182 |
\tl_set:Nx \l_tmpa_tl |
| 183 |
{ \tl_map_function:nN {#1} \__emoji_encode_from_char:n } |
| 184 |
% Range 4 means "characters usually not in Japanese fonts". |
| 185 |
% `\ltjdefcharrange` does not accespt empty value, so we need to |
| 186 |
% remove extra comma. |
| 187 |
\exp_args:Nnx \ltjdefcharrange {4} { \tl_tail:N \l_tmpa_tl } |
| 188 |
} |
| 189 |
% 0-7F are always treated as an ALchar and can't be customized. |
| 190 |
\cs_new:Npn \__emoji_encode_from_char:n #1 |
| 191 |
{ \int_compare:nNnF {`#1} < {"80} {,`#1} } |
| 192 |
} |
| 193 |
|
| 194 |
% If the user doesn't set emoji font explicitly, then detect and set default |
| 195 |
% fonts automatically. |
| 196 |
\cs_if_exist:NF \emoji_font: |
| 197 |
{ \__emoji_detect_font: } |
| 198 |
} |
| 199 |
|
| 200 |
\endinput |