symb-emj.lmt /size: 2075 b    last modification: 2023-12-21 09:44
1if not modules then modules = { } end modules ['symb-emj'] = {
2    version   = 1.001,
3    comment   = "companion to symb-emj.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local symbols         = fonts.symbols
10
11local resolvedemoji   = characters.emoji.resolve
12local processfeatures = fonts.handlers.otf.featuresprocessor
13local injectspacing   = nodes.injections.handler
14local protectglyphs   = nodes.handlers.protectglyphs
15local tonodes         = nodes.tonodes
16local currentfont     = font.current
17
18local nuts            = nodes.nuts
19local tonode          = nuts.tonode
20local tonut           = nuts.tonut
21local remove_node     = nuts.remove
22local isglyph         = nuts.isglyph
23local getnext         = nuts.getnext
24
25local function removemodifiers(head)
26    local head    = tonut(head)
27    local current = head
28    while current do
29        local char = isglyph(current)
30        if char and (char == 0x200D or (char >= 0x1F3FB and char <= 0x1F3FF)) then
31            head, current = remove_node(head,current,true)
32        else
33            current = getnext(current)
34        end
35    end
36    return tonode(head)
37end
38
39-- fast enough, no need to memoize, maybe use attributes
40
41local function checkedemoji(name,id)
42    local str = resolvedemoji(name)
43    if str then
44        if not id then
45            id = currentfont()
46        end
47        local head = tonodes(str,id,nil,nil,true) -- use current attributes
48        head = processfeatures(head,id,false)
49        if head then
50            head = injectspacing(head)
51            protectglyphs(head)
52            return removemodifiers(head)
53        end
54    end
55end
56
57symbols.emoji = {
58    resolved = resolvedemoji,
59    checked  = checkedemoji,
60}
61
62interfaces.implement {
63    name      = "resolvedemoji",
64    actions   = { resolvedemoji, context.escaped },
65    arguments = "string",
66}
67
68interfaces.implement {
69    name      = "checkedemoji",
70    actions   = { checkedemoji, context },
71    arguments = "string",
72}
73
74
75