Debian Codegraph @

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

raw
1 -- lua-uni-words.lua
2 -- Copyright 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 extended_pictographic, property do
17 local p = require'lua-uni-parse'
18 local l = lpeg or require'lpeg'
19
20 extended_pictographic = p.parse_file('emoji-data',
21 l.Cg(p.fields(p.codepoint_range, 'Extended_Pictographic' * l.Cc(true))) + p.ignore_line,
22 p.multiset)
23 if not extended_pictographic then
24 error[[Break Property matching failed]]
25 end
26
27 property = p.parse_file('WordBreakProperty',
28 l.Cg(p.fields(p.codepoint_range, l.C(l.R('az', 'AZ', '__')^1))) + p.ignore_line,
29 p.multiset)
30 if not property then
31 error[[Break Property matching failed]]
32 end
33 end
34
35 local ignorable = { Extend = true, Format = true, ZWJ = true, }
36 local controls = { CR = true, LF = true, Newline = true, }
37
38 local function context_AHLetter_Mid(cp)
39 local prop = property[cp]
40 if ignorable[prop] then
41 return nil, context_AHLetter_Mid
42 end
43 if prop == 'ALetter' then
44 return false, 'ASTARTED'
45 end
46 if prop == 'Hebrew_Letter' then
47 return false, 'HSTARTED'
48 end
49 return true, 'PRE'
50 end
51
52 local function context_HLetter_Double(cp)
53 local prop = property[cp]
54 if ignorable[prop] then
55 return nil, context_HLetter_Double
56 end
57 if prop == 'Hebrew_Letter' then
58 return false, 'HSTARTED'
59 end
60 return true, 'PRE'
61 end
62
63 local function context_Numeric_Mid(cp)
64 local prop = property[cp]
65 if ignorable[prop] then
66 return nil, context_Numeric_Mid
67 end
68 if prop == 'Numeric' then
69 return false, 'NSTARTED'
70 end
71 return true, 'PRE'
72 end
73
74 local state_map state_map = {
75 START = function(prop)
76 if prop == 'CR' then
77 return 'CR', true
78 end
79 if prop == 'LF' or prop == 'Newline' then
80 return 'START', true
81 end
82 return state_map.PRE(prop), true
83 end,
84 PRE = function(prop)
85 if controls[prop] then
86 return state_map.START(prop)
87 end
88 if ignorable[prop] then
89 return 'PRE', false
90 end
91 if prop == 'WSegSpace' then
92 return 'WHITE', true
93 end
94 if prop == 'ALetter' then
95 return 'ASTARTED', true
96 end
97 if prop == 'Hebrew_Letter' then
98 return 'HSTARTED', true
99 end
100 if prop == 'Numeric' then
101 return 'NSTARTED', true
102 end
103 if prop == 'Katakana' then
104 return 'KSTARTED', true
105 end
106 if prop == 'ExtendNumLet' then
107 return 'EXTEND', true
108 end
109 if prop == 'Regional_Indicator' then
110 return 'RI', true
111 end
112 return 'PRE', true
113 end,
114 CR = function(prop)
115 if prop == 'LF' then
116 return 'START', false
117 else
118 return state_map.START(prop)
119 end
120 end,
121 WHITE = function(prop)
122 if prop == 'WSegSpace' then
123 return 'WHITE', false
124 else
125 return state_map.PRE(prop)
126 end
127 end,
128 EXTEND = function(prop)
129 if ignorable[prop] then
130 return 'EXTEND', false
131 end
132 if prop == 'ALetter' then
133 return 'ASTARTED', false
134 end
135 if prop == 'Hebrew_Letter' then
136 return 'HSTARTED', false
137 end
138 if prop == 'Katakana' then
139 return 'KSTARTED', false
140 end
141 if prop == 'Numeric' then
142 return 'NSTARTED', false
143 end
144 if prop == 'ExtendNumLet' then
145 return 'EXTEND', false
146 end
147 return state_map.PRE(prop)
148 end,
149 KSTARTED = function(prop)
150 if ignorable[prop] then
151 return 'KSTARTED', false
152 end
153 if prop == 'Katakana' then
154 return 'Katakana', false
155 end
156 if prop == 'ExtendNumLet' then
157 return 'EXTEND', false
158 end
159 return state_map.PRE(prop)
160 end,
161 RI = function(prop)
162 if ignorable[prop] then
163 return 'RI', false
164 end
165 if prop == 'Regional_Indicator' then
166 return 'PRE', false
167 end
168 return state_map.PRE(prop)
169 end,
170 ASTARTED = function(prop)
171 if ignorable[prop] then
172 return 'ASTARTED', false
173 end
174 if prop == 'ALetter' then
175 return 'ASTARTED', false
176 end
177 if prop == 'Hebrew_Letter' then
178 return 'HSTARTED', false
179 end
180 if prop == 'Numeric' then
181 return 'NSTARTED', false
182 end
183 if prop == 'ExtendNumLet' then
184 return 'EXTEND', false
185 end
186 if prop == 'MidLetter' or prop == 'MidNumLet' or prop == 'Single_Quote' then
187 return context_AHLetter_Mid
188 end
189 return state_map.PRE(prop)
190 end,
191 HSTARTED = function(prop)
192 if ignorable[prop] then
193 return 'HSTARTED', false
194 end
195 if prop == 'ALetter' then
196 return 'ASTARTED', false
197 end
198 if prop == 'Hebrew_Letter' then
199 return 'HSTARTED', false
200 end
201 if prop == 'Numeric' then
202 return 'NSTARTED', false
203 end
204 if prop == 'ExtendNumLet' then
205 return 'EXTEND', false
206 end
207 if prop == 'Single_Quote' then
208 return 'HSINGLE_QUOTE', false
209 end
210 if prop == 'MidLetter' or prop == 'MidNumLet' then
211 return context_AHLetter_Mid
212 end
213 if prop == 'Double_Quote' then
214 return context_HLetter_Double
215 end
216 return state_map.PRE(prop)
217 end,
218 HSINGLE_QUOTE = function(prop)
219 if ignorable[prop] then
220 return 'HSINGLE_QUOTE', false
221 end
222 if prop == 'ALetter' then
223 return 'ASTARTED', false
224 end
225 if prop == 'Hebrew_Letter' then
226 return 'HSTARTED', false
227 end
228 return state_map.PRE(prop)
229 end,
230 NSTARTED = function(prop)
231 if ignorable[prop] then
232 return 'NSTARTED', false
233 end
234 if prop == 'ALetter' then
235 return 'ASTARTED', false
236 end
237 if prop == 'Hebrew_Letter' then
238 return 'HSTARTED', false
239 end
240 if prop == 'Numeric' then
241 return 'NSTARTED', false
242 end
243 if prop == 'ExtendNumLet' then
244 return 'EXTEND', false
245 end
246 if prop == 'MidNum' or prop == 'MidNumLet' or prop == 'Single_Quote' then
247 return context_Numeric_Mid
248 end
249 return state_map.PRE(prop)
250 end,
251 }
252
253 local from_ZWJ, to_ZWJ = {}, {}
254 for k in next, state_map do
255 local zwj_state = 'ZWJ_' .. k
256 from_ZWJ[zwj_state], to_ZWJ[k] = k, zwj_state
257 end
258
259
260 -- The value of "state" is considered internal and should not be relied upon.
261 -- Just pass it to the function as is or pass nil. `nil` should only be passed when the passed codepoint starts a new cluster
262 local function read_codepoint(cp, state)
263 local mapped_state = from_ZWJ[state]
264 local new_word
265 local prop = property[cp]
266 state, new_word = state_map[mapped_state or state or 'START'](prop)
267 if mapped_state and extended_pictographic[cp] then
268 new_word = false
269 end
270 if prop == 'ZWJ' then
271 state = to_ZWJ[state]
272 end
273 return new_word, state
274 end
275
276 -- A Lua iterator for strings -- Only reporting the beginning of every word segment
277 local function word_boundaries_start(str)
278 local nextcode, str, i = utf8.codes(str)
279 local state = "START"
280 local saved_i, saved_code
281 return function()
282 local new_word, code
283 repeat
284 i, code = nextcode(str, i)
285 if saved_i then
286 new_word, state = state(code)
287 if new_word ~= nil then
288 i, code, saved_i, saved_code = saved_i, saved_code, nil, nil
289 end
290 else
291 if not i then return end
292 new_word, state = read_codepoint(code, state)
293 if new_word == nil then
294 saved_i, saved_code = i, code
295 end
296 end
297 until new_word
298 return i, code
299 end
300 end
301 -- A more useful iterator: returns the byterange of the segment in reverse order followed by a string with the word
302 local function word_boundaries(str)
303 local iter = word_boundaries_start(str)
304 return function(_, cur)
305 if cur == #str then return end
306 local new = iter()
307 if not new then return #str, cur + 1, str:sub(cur + 1) end
308 return new - 1, cur + 1, str:sub(cur + 1, new - 1)
309 end, nil, iter() - 1
310 end
311 return {
312 read_codepoint = read_codepoint,
313 word_boundaries_start = word_bounaries_start,
314 word_boundaries = word_boundaries,
315 }