Debian Codegraph @

texmf-dist/tex/luatex/lua-uni-algos/lua-uni-graphemes.lua

raw
1 -- lua-uni-graphemes.lua
2 -- Copyright 2020--2025 Marcel Krüger
3 --
4 -- This work may be distributed and/or modified under the
5 -- conditions of the LaTeX Project Public License, either version 1.3
6 -- of this license or (at your option) any later version.
7 -- The latest version of this license is in
8 -- http://www.latex-project.org/lppl.txt
9 -- and version 1.3 or later is part of all distributions of LaTeX
10 -- version 2005/12/01 or later.
11 --
12 -- This work has the LPPL maintenance status `maintained'.
13 --
14 -- The Current Maintainer of this work is Marcel Krüger
15
16 local property do
17 local p = require'lua-uni-parse'
18 local l = lpeg or require'lpeg'
19
20 property = p.parse_file('emoji-data',
21 l.Cg(p.fields(p.codepoint_range, l.C'Extended_Pictographic')) + p.ignore_line,
22 p.multiset)
23
24 property = p.parse_file('GraphemeBreakProperty', l.Cf(
25 l.Carg(1)
26 * (l.Cg(p.fields(p.codepoint_range, l.C(l.R('az', 'AZ', '__')^1))) + p.ignore_line)^0
27 * -1, p.multiset),
28 nil,
29 property)
30 if not property then
31 error[[Break Property matching failed]]
32 end
33 end
34
35 local controls = { CR = true, LF = true, Control = true, }
36 local precore_lookup = {
37 Prepend = "PRECORE",
38 L = "L",
39 V = "V",
40 LV = "V",
41 LVT = "T",
42 T = "T",
43 Regional_Indicator = "RI",
44 Extended_Pictographic = "POST_PICTO",
45 }
46 local l_lookup = {
47 L = "L",
48 V = "V",
49 LV = "V",
50 LVT = "T",
51 }
52 local postcore_map = { Extend = true, ZWJ = true, SpacingMark = true, }
53 local state_map state_map = {
54 START = function(prop)
55 if prop == 'CR' then
56 return 'CR', true
57 end
58 if prop == 'LF' or prop == 'Control' then
59 return 'START', true
60 end
61 return state_map.PRECORE(prop), true
62 end,
63 PRECORE = function(prop)
64 if controls[prop] then
65 return state_map.START(prop)
66 end
67 return precore_lookup[prop] or 'POSTCORE'
68 end,
69 POSTCORE = function(prop)
70 if postcore_map[prop] then
71 return 'POSTCORE'
72 end
73 return state_map.START(prop)
74 end,
75 RI = function(prop)
76 if prop == 'Regional_Indicator' then
77 return 'POSTCORE'
78 end
79 return state_map.POSTCORE(prop)
80 end,
81 PRE_PICTO = function(prop)
82 if prop == "Extended_Pictographic" then
83 return "POST_PICTO"
84 end
85 return state_map.POSTCORE(prop)
86 end,
87 POST_PICTO = function(prop)
88 if prop == "Extend" then
89 return "POST_PICTO"
90 end
91 if prop == "ZWJ" then
92 return "PRE_PICTO"
93 end
94 return state_map.POSTCORE(prop)
95 end,
96 L = function(prop)
97 local nextstate = l_lookup[prop]
98 if nextstate then
99 return nextstate
100 end
101 return state_map.POSTCORE(prop)
102 end,
103 V = function(prop)
104 if prop == 'V' then
105 return 'V'
106 end
107 return state_map.T(prop)
108 end,
109 T = function(prop)
110 if prop == 'T' then
111 return 'T'
112 end
113 return state_map.POSTCORE(prop)
114 end,
115 CR = function(prop)
116 if prop == 'LF' then
117 return 'START'
118 else
119 return state_map.START(prop)
120 end
121 end,
122 }
123
124 -- The value of "state" is considered internal and should not be relied upon.
125 -- Just pass it to the function as is or pass nil. `nil` should only be passed when the passed codepoint starts a new cluster
126 local function read_codepoint(cp, state)
127 local new_cluster
128 state, new_cluster = state_map[state or 'START'](property[cp])
129 return new_cluster, state
130 end
131
132 -- A Lua iterator for strings -- Only reporting the beginning of every grapheme cluster
133 local function graphemes_start(str)
134 local nextcode, str, i = utf8.codes(str)
135 local state = "START"
136 return function()
137 local new_cluster, code
138 repeat
139 i, code = nextcode(str, i)
140 if not i then return end
141 new_cluster, state = read_codepoint(code, state)
142 until new_cluster
143 return i, code
144 end
145 end
146 -- A more useful iterator: returns the byterange of the graphemecluster in reverse order followed by a string with te cluster
147 local function graphemes(str)
148 local iter = graphemes_start(str)
149 return function(_, cur)
150 if cur == #str then return end
151 local new = iter()
152 if not new then return #str, cur + 1, str:sub(cur + 1) end
153 return new - 1, cur + 1, str:sub(cur + 1, new - 1)
154 end, nil, iter() - 1
155 end
156 return {
157 read_codepoint = read_codepoint,
158 graphemes_start = graphemes_start,
159 graphemes = graphemes,
160 }
161 --[[
162 for i, c in graphemes_start'äbcdef' do
163 print(i, utf8.char(c))
164 end
165 for i, j, s in graphemes'Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞' do
166 print(j, i, s)
167 end
168 ]]