Debian Codegraph @

texmf-dist/tex/generic/tex4ht/fontspec-4ht.lua

raw symbols SCIP
1 -- fontspec-4ht.lua (2019-06-18-15:43), generated from tex4ht-4ht.tex
2 -- Copyright 2016-2019 TeX Users Group
3 --[[
4 %
5 % This work may be distributed and/or modified under the
6 % conditions of the LaTeX Project Public License, either
7 % version 1.3c of this license or (at your option) any
8 % later version. The latest version of this license is in
9 % http://www.latex-project.org/lppl.txt
10 % and version 1.3c or later is part of all distributions
11 % of LaTeX version 2005/12/01 or later.
12 %
13 % This work has the LPPL maintenance status "maintained".
14 %
15 % The Current Maintainer of this work
16 % is the TeX4ht Project <http://tug.org/tex4ht>.
17 %
18 % If you modify this program, changing the
19 % version identification would be appreciated.
20 \immediate\write-1{version 2019-06-18-15:43}
21
22 --]]
23 local M = {}
24
25 local glyph_id = node.id "glyph"
26 local whatsit_id = node.id "whatsit"
27 local special_subtype = node.subtype "special"
28 local dir_id = node.id "dir"
29 local glue_id = node.id "glue"
30
31 local escape = function(char)
32 -- prepare tex4ht special for entity with unicode value
33 return string.format("t4ht@+&{35}x%x{59}", char)
34 end
35
36 local make_node = function(data)
37 -- make special whathsit
38 local n = node.new(whatsit_id,special_subtype)
39 n.data = data
40 return n
41 end
42
43 local function first_node(head)
44 local head = head
45 while head.prev do
46 head = head.prev
47 end
48 return head
49 end
50
51 -- this should be table with patterns for allowed fonts
52 local allowed_names = {"^cmr", "^cmb","^cmt", "^cmb", "^cmcs", "^rm%-l", "^cmi", "^ec%-lm", "none"}
53
54 local testfont = function(name)
55 -- test font name for all allowed names, when it is found, return true
56 for _, x in ipairs(allowed_names) do
57 local r = name:match(x)
58 if r then
59 return true
60 end
61 end
62 return false
63 end
64
65 local fonttypes = {}
66 local get_font_type = function(id)
67 if fonttypes[id]~=nil then return fonttypes[id] end
68 local f = font.getfont(id) or {name = "none"} -- font object can be nil sometimes
69 local name = f.name
70 local type = testfont(string.lower(name))
71 if not type then
72 print("Unsupported font", name)
73 end
74 fonttypes[id] = type
75 return type
76 end
77
78 local xchar = string.byte("x")
79
80 local utfchar = unicode.utf8.char
81 function M.char_to_entity(head)
82 -- traverse characters
83 for n in node.traverse(head) do
84 if n.id == glyph_id then
85 -- we need to process only default text font, ie cmr, because user may request special mathematical fonts,
86 -- which should be processed via htf files as usual
87 local t = get_font_type(n.font)
88 if t == true then
89 local char = n.char
90 if char > 127 then
91 local new = escape(char)
92 local x = make_node(new)
93 -- insert tex4ht special before char, it will replace the char
94 node.insert_before(head, n, x)
95 -- in standard tex4ht accented characters are replaced with "x" char. they are later removed anyway
96 -- maybe we don't need to do that, but we can, so why not?
97 n.char = xchar
98 end
99 end
100 elseif n.id == dir_id then
101 -- when text direction is TRT, the spaces in the DVI file have negative width and they are not recognized by tex4ht
102 -- so we just change the direction to normal TLT
103 n.dir = "+TLT"
104 end
105 end
106 return first_node(head)
107 end
108
109 M.allowed = allowed_names
110
111 return M
112