Debian Codegraph @

texk/xdvipsk/xdvipsk_tounicode.lua

raw symbols SCIP
1 --[[
2 xxd -i -n xdvipsk_tounicode xdvipsk_tounicode.lua >xdvipsk_tounicode.h
3 --]]
4
5 ---------------
6 glyphtounicode_add = {}
7
8 function uni32_to_utf16(val)
9 --[[
10 converts an unicode value to an UTF-16 array table (one or two elements)
11 --]]
12 local utf16_arr = nil
13 if val <= 0x10FFFF then
14 if val >= 0x10000 then
15 val = val - 0x10000
16 utf16_arr = {0xD800 | ((val >> 10) & 0x3FF), 0xDC00 | (val & 0x3FF)}
17 else
18 utf16_arr = {val}
19 end
20 end
21 return utf16_arr
22 end
23
24 function utf16_to_uni32(utf16_arr)
25 --[[
26 converts an UTF-16 array table to a 32 bit unicode values array table
27 --]]
28 local uni32_arr = {}
29 local uni32_val = 0
30 for _, val in ipairs(utf16_arr) do
31 if (val & 0xFC00) == 0xD800 then
32 uni32_val = (val & 0x3FF) << 10
33 elseif (val & 0xFC00) == 0xDC00 then
34 uni32_arr[#uni32_arr + 1] = (uni32_val | (val & 0x3FF)) + 0x10000
35 else
36 uni32_arr[#uni32_arr + 1] = val
37 end
38 end
39 return uni32_arr
40 end
41
42 function load_touni(gltab, overwrite, map_name)
43 for key, val in pairs(gltab) do
44 local key_to_add = key
45 if map_name and (map_name ~= '') and (not string.find(key_to_add, ':', 1, true)) and (not string.find(key_to_add, '/', 1, true)) then
46 key_to_add = 'pfb:' .. map_name .. '/' .. key_to_add
47 end
48 if (not glyphtounicode_add[key_to_add]) or overwrite then
49 local res_arr = {}
50 for _, ucode in ipairs(val) do
51 if type(ucode) == 'string' then
52 res_arr[#res_arr + 1] = ucode
53 else
54 local utf16_arr = uni32_to_utf16(ucode)
55 for _, uval in ipairs(utf16_arr) do
56 res_arr[#res_arr + 1] = uval
57 end
58 end
59 end
60 glyphtounicode_add[key_to_add] = res_arr
61 end
62 end
63 end
64
65 function is_private(uni_rec)
66 local ret_val = true
67 if uni_rec then
68 local uni_type = type(uni_rec)
69 if uni_type == 'number' then
70 ret_val = -- Private Use Areas
71 (((uni_rec >= 0xE000) and (uni_rec < 0xF900)) or
72 ((uni_rec >= 0xF0000) and (uni_rec < 0xFFFFE)) or
73 ((uni_rec >= 0x100000) and (uni_rec < 0x10FFFE)))
74 elseif uni_type == 'table' then
75 local uni_copy = {}
76 for _, val in ipairs(uni_rec) do
77 if type(val) == 'number' then
78 uni_copy[#uni_copy + 1] = val
79 end
80 end
81 local uni32_arr = utf16_to_uni32(uni_copy)
82 for _, val in ipairs(uni32_arr) do
83 ret_val = is_private(val)
84 if ret_val then
85 break
86 end
87 end
88 end
89 end
90 return ret_val
91 end
92
93 function get_glname_touni_rec_single(glname, font_pref, glyphtounicode_table)
94 --[[
95 returns:
96 nil if not found
97 second return value -- false in case of 'uniXXXX' like glyph name -- they will be parsed by GS or Acrobat Distiller, no need to add them to the /GlyphNames2Unicode
98 --]]
99 local uni_rec = glyphtounicode_table[glname]
100 local needs_g2u = true
101 if (not uni_rec) and (glyphtounicode_table == glyphtounicode_add) then
102 uni_rec = glyphtounicode_main[glname]
103 end
104 if (not uni_rec) or is_private(uni_rec) then
105 local gl_pos = string.find(glname, '/', 1, true)
106 if (gl_pos) then -- 'pfb:msbm10/A'
107 font_pref = string.sub(glname, 1, gl_pos)
108 glname = string.sub(glname, gl_pos + 1)
109 uni_rec, needs_g2u = get_glname_touni_rec_single(glname, font_pref, glyphtounicode_table)
110 if (not uni_rec) and (glyphtounicode_table == glyphtounicode_add) then
111 uni_rec, needs_g2u = get_glname_touni_rec_single(glname, font_pref, glyphtounicode_main)
112 end
113 end
114 end
115 if (not uni_rec) or is_private(uni_rec) then
116 local gl_pos = string.find(glname, '.', 1, true)
117 if (gl_pos) then -- 'one.superior'
118 glname = string.sub(glname, 1, gl_pos - 1)
119 local glname_alias = font_pref .. glname
120 uni_rec, needs_g2u = get_glname_touni_rec_single(glname_alias, font_pref, glyphtounicode_table)
121 if (not uni_rec) and (glyphtounicode_table == glyphtounicode_add) then
122 uni_rec, needs_g2u = get_glname_touni_rec_single(glname_alias, font_pref, glyphtounicode_main)
123 end
124 end
125 end
126 if (not uni_rec) or is_private(uni_rec) then -- 'f_f_i'
127 local gl_pos = string.find(glname, '_', 1, true)
128 if gl_pos then
129 local uni_rec_comb = {}
130 while true do
131 local uni_rec_alias
132 if gl_pos then
133 local glname_part = string.sub(glname, 1, gl_pos - 1)
134 glname = string.sub(glname, gl_pos + 1)
135 local glname_alias = font_pref .. glname_part
136 uni_rec_alias, needs_g2u = get_glname_touni_rec_single(glname_alias, font_pref, glyphtounicode_table)
137 if (not uni_rec_alias) and (glyphtounicode_table == glyphtounicode_add) then
138 uni_rec_alias, needs_g2u = get_glname_touni_rec_single(glname_alias, font_pref, glyphtounicode_main)
139 end
140 else
141 local glname_alias = font_pref .. glname
142 uni_rec_alias, needs_g2u = get_glname_touni_rec_single(glname_alias, font_pref, glyphtounicode_table)
143 if (not uni_rec_alias) and (glyphtounicode_table == glyphtounicode_add) then
144 uni_rec_alias, needs_g2u = get_glname_touni_rec_single(glname_alias, font_pref, glyphtounicode_main)
145 end
146 end
147 if uni_rec_alias then
148 local uni_type = type(uni_rec_alias)
149 if uni_type == 'number' then
150 uni_rec_comb[#uni_rec_comb + 1] = uni_rec_alias
151 elseif uni_type == 'table' then
152 for _, val in ipairs(uni_rec_alias) do
153 uni_rec_comb[#uni_rec_comb + 1] = val
154 end
155 end
156 else
157 uni_rec_comb = {}
158 break
159 end
160 if (not gl_pos) then
161 break
162 end
163 gl_pos = string.find(glname, '_', 1, true)
164 end
165 if #uni_rec_comb > 0 then
166 uni_rec = uni_rec_comb
167 end
168 end
169 end
170 if (not uni_rec) or is_private(uni_rec) then
171 if ((glname == 'fi') or (glname == 'fl') or (glname == 'ff') or (glname == 'ffi') or (glname == 'ffl')) then
172 local uni_rec_comb = {}
173 for ii = 1, #glname do
174 local glname_alias = font_pref .. string.sub(glname, ii, ii)
175 local uni_rec_alias, needs_g2u = get_glname_touni_rec_single(glname_alias, font_pref, glyphtounicode_table)
176 if (not uni_rec_alias) and (glyphtounicode_table == glyphtounicode_add) then
177 uni_rec_alias, needs_g2u = get_glname_touni_rec_single(glname_alias, font_pref, glyphtounicode_main)
178 end
179 if uni_rec_alias then
180 local uni_type = type(uni_rec_alias)
181 if uni_type == 'number' then
182 uni_rec_comb[#uni_rec_comb + 1] = uni_rec_alias
183 elseif uni_type == 'table' then
184 for _, val in ipairs(uni_rec_alias) do
185 uni_rec_comb[#uni_rec_comb + 1] = val
186 end
187 end
188 else
189 uni_rec_comb = {}
190 break
191 end
192 end
193 if #uni_rec_comb > 0 then
194 uni_rec = uni_rec_comb
195 end
196 end
197 end
198 if not uni_rec then
199 if (string.find(glname, 'uni', 1, true) == 1) then
200 needs_g2u = false
201 uni_rec = {}
202 local hex_str = string.sub(glname, 4)
203 while hex_str ~= '' do
204 local val = tonumber(string.sub(hex_str, 1, 4), 16)
205 if val ~= nil then
206 uni_rec[#uni_rec + 1] = val
207 else
208 needs_g2u = true
209 uni_rec = nil
210 break
211 end
212 hex_str = string.sub(hex_str, 5)
213 end
214 end
215 end
216 if not uni_rec then
217 if (string.sub(glname, 1, 1) == 'u') then
218 needs_g2u = false
219 local hex_str = string.sub(glname, 2)
220 local val = tonumber(hex_str, 16)
221 if val ~= nil then
222 uni_rec = uni32_to_utf16(val)
223 else
224 needs_g2u = true
225 end
226 end
227 end
228 return uni_rec, needs_g2u
229 end
230
231 function get_glname_tounicode(glname, exact)
232 --[[
233 returns:
234 numeric part of the unicode record
235 empty table if not found
236 second return value -- false in case of 'uniXXXX' like glyph name
237 --]]
238 local uni_rec
239 local ret_arr = {}
240 local needs_g2u = true
241 if exact then
242 uni_rec = glyphtounicode_add[glname]
243 if (not uni_rec) then
244 uni_rec = glyphtounicode_main[glname]
245 end
246 else
247 uni_rec, needs_g2u = get_glname_touni_rec_single(glname, '', glyphtounicode_add)
248 end
249 if uni_rec then
250 local uni_type = type(uni_rec)
251 if uni_type == 'number' then
252 ret_arr = {uni_rec}
253 elseif uni_type == 'table' then
254 for _, val in ipairs(uni_rec) do
255 if type(val) == 'number' then
256 ret_arr[#ret_arr + 1] = val
257 end
258 end
259 end
260 end
261 return ret_arr, needs_g2u
262 end
263
264 function get_glname_subst_single(glname, exact, glyphtounicode_table)
265 --[[
266 returns:
267 alias name of the glyph and numeric part of the unicode record
268 empty string and empty table if not found
269 --]]
270 local uni_rec
271 local ret_str = ''
272 local ret_arr = {}
273 uni_rec = glyphtounicode_table[glname]
274 if uni_rec then
275 local uni_type = type(uni_rec)
276 if uni_type == 'string' then
277 ret_str = uni_rec
278 elseif type(uni_rec) == 'table' then
279 for _, val in ipairs(uni_rec) do
280 if type(val) == 'number' then
281 ret_arr[#ret_arr + 1] = val
282 elseif type(val) == 'string' then
283 ret_str = val
284 end
285 end
286 end
287 end
288 if ((#ret_arr == 0) and (not exact)) then
289 local gl_pos = string.find(glname, '/', 1, true)
290 if (gl_pos) then -- 'pfb:msbm10/A'
291 glname = string.sub(glname, gl_pos + 1)
292 ret_str, ret_arr = get_glname_subst_single(glname, true, glyphtounicode_table)
293 end
294 end
295 return ret_str, ret_arr
296 end
297
298 function get_glname_subst(glname, exact)
299 --[[
300 returns:
301 alias name of the glyph
302 empty string if not found
303 generates uXXXXX name for overriden glyphs with no name alias and different unicode values
304 second return value -- false in case of 'uniXXXX' like glyph name
305 --]]
306 local ret_str_main = ''
307 local ret_arr_main = {}
308 local ret_str_add = ''
309 local ret_arr_add = {}
310 local needs_g2u = true
311 ret_str_add, ret_arr_add = get_glname_subst_single(glname, exact, glyphtounicode_add)
312 ret_str_main, ret_arr_main = get_glname_subst_single(glname, false, glyphtounicode_main)
313 if ret_str_add == '' then
314 ret_str_add = ret_str_main
315 end
316 if (ret_str_add == '') and (#ret_arr_main > 0) and (#ret_arr_add > 0) then
317 local equal = true
318 if #ret_arr_add ~= #ret_arr_main then
319 equal = false
320 else
321 for ii = 1, #ret_arr_add do
322 if ret_arr_add[ii] ~= ret_arr_main[ii] then
323 equal = false
324 break
325 end
326 end
327 end
328 if not equal then
329 ret_arr_add_32 = utf16_to_uni32(ret_arr_add)
330 if #ret_arr_add_32 == 1 then
331 ret_str_add = string.format('u%04X', ret_arr_add_32[1])
332 else
333 ret_str_add = 'uni'
334 for ii = 1, #ret_arr_add do
335 ret_str_add = ret_str_add .. string.format('%04X', ret_arr_add[ii])
336 end
337 end
338 needs_g2u = false
339 end
340 end
341 return ret_str_add, needs_g2u
342 end
343
344 ---------------
345 tfm_2_pfb = {}
346
347 function add_tfm_2_pfb(tfm_name, pfb_name)
348 tfm_2_pfb[tfm_name] = pfb_name
349 end
350
351 function encode_tfm_2_pfb_single(glyphtounicode_table)
352 local new_entries = {}
353 for key, val in pairs(glyphtounicode_table) do
354 local gl_pos = string.find(key, '/', 1, true)
355 if gl_pos and (string.find(key, 'tfm:', 1, true) == 1) then
356 local glname = string.sub(key, gl_pos + 1)
357 local pref = string.sub(key, 5, gl_pos - 1)
358 local pfb_name = tfm_2_pfb[pref]
359 if pfb_name then
360 new_entries['pfb:' .. pfb_name .. '/' .. glname] = val
361 end
362 end
363 end
364 for key, val in pairs(new_entries) do
365 glyphtounicode_table[key] = val
366 end
367 end
368
369 function encode_tfm_2_pfb()
370 encode_tfm_2_pfb_single(glyphtounicode_main)
371 encode_tfm_2_pfb_single(glyphtounicode_add)
372 end
373
374 ---------------
375 function deploy_font_glyph_map(pfb_name)
376 local map_name = font_aliases[pfb_name]
377 if map_name ~= nil then
378 load_touni(lua_tables[map_name], false, pfb_name)
379 end
380 end
381
382 -- pfb_name -- lowercased pfb font name without an extension
383 -- enc_file_name -- lowercased full encoding file name
384 -- returns an alias id, if the encoding should be aliased, zero otherwise
385 enc_alias_id = 0
386 -- map of maps, index -- encoding names
387 -- values -- maps of encodings or their alias names, assigned to particular unicode map names
388 -- index -- unicode map names of pfb files using the particular encoding
389 enc_maps = {
390 -- ['lm-mathsy.enc'] = {
391 -- ['lmsy'] = 'lm-mathsy.enc', -- lmsy10, lmsy9, lmsy8, etc.
392 -- ['lmbsy'] = 'lm-mathsy.enc123', -- lmbsy10, lmbsy9, etc., lm-mathsy.enc123 -- alias of the lm-mathsy.enc, 123 -- alias id to be returned from get_enc_alias()
393 -- },
394 }
395 function get_enc_alias(pfb_name, enc_file_name)
396 local alias_id = 0
397 local map_name = font_aliases[pfb_name]
398
399 if map_name ~= nil then
400 local enc_map = enc_maps[enc_file_name]
401 local alias_name
402 if enc_map ~= nil then
403 alias_name = enc_map[map_name]
404 if alias_name ~= nil then
405 local enc_len = string.len(enc_file_name)
406 local alias_len = string.len(alias_name)
407 local alias_id_str = ''
408 if alias_len > enc_len then
409 alias_id_str = string.sub(alias_name, enc_len + 1, alias_len)
410 alias_id = tonumber(alias_id_str)
411 end
412 else
413 enc_alias_id = enc_alias_id + 1
414 alias_id = enc_alias_id
415 enc_map[map_name] = enc_file_name .. tostring(alias_id)
416 end
417 else
418 enc_map = {[map_name] = enc_file_name}
419 enc_maps[enc_file_name] = enc_map
420 end
421 end
422
423 return alias_id
424 end
425
426 ---------------
427 -- pfb_name -- lowercased pfb font name without an extension
428 -- just checks attempts of loading font unicode map table,
429 -- true result doesn't mean it actually has been found and loaded
430 -- for avoiding multiple loads
431 g2u_attempt = {}
432 function is_g2u_loaded(pfb_name)
433 local ret_val = (g2u_attempt[pfb_name] ~= nil)
434 g2u_attempt[pfb_name] = true
435 return ret_val
436 end
437 --file end null char