summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/emoji/emoji.sty
blob: 75b669e5116a2c545357ee15870ff3610bf6c838 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
%%
%%  Copyright (C) 2020 by Xiangdong Zeng <xdzeng96@gmail.com>
%%
%%  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/03/16}{0.2}{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