Debian Codegraph @

texmf-dist/tex/luatex/luaotfload/luaotfload-dvi.lua

raw
1 -------------------------------------------------------------------------------
2 -- FILE: luaotfload-dvi.lua
3 -- DESCRIPTION: part of luaotfload / DVI
4 -------------------------------------------------------------------------------
5
6
7 assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") {
8 name = "luaotfload-dvi",
9 version = "3.29", --TAGVERSION
10 date = "2024-12-03", --TAGDATE
11 description = "luaotfload submodule / DVI",
12 license = "GPL v2.0",
13 author = "Marcel Krüger",
14 copyright = "Luaotfload Development Team",
15 }
16
17 local getfont = font.getfont
18 local setfont = node.direct.setfont
19 local getdisc = node.direct.getdisc
20 local getlist = node.direct.getlist
21 local is_glyph = node.direct.is_glyph
22 local todirect = node.direct.todirect
23 local traverse_glyph = node.direct.traverse_glyph
24 local traverse_list = node.direct.traverse_list
25 local traverse_id = node.direct.traverse_id
26 local traverse = node.direct.traverse
27 local uses_font = node.direct.uses_font
28 local define_font = font.define
29 local hlist_t = node.id'hlist'
30 local vlist_t = node.id'vlist'
31 local disc_t = node.id'disc'
32 local glyph_t = node.id'glyph'
33
34 require'luaotfload-configuration'
35 local configuration = config.luaotfload
36
37 -- DVI output support
38 --
39 -- When writing DVI files, we can't assume that the DVI reader has access to our
40 -- font dictionaries, so we need an independent representation for our glyphs. The
41 -- approach we chose in coordination with the dvisvgm author is to create a DVI font
42 -- name which indicates the font filename and some essential backend parameters
43 -- (especially otential extend, shrink, embolden, etc. factors) and then use GIDs in
44 -- the page stream.
45 --
46 -- Normally we could easily implement this using virtual fonts, but because we are
47 -- dealing with DVI output, LuaTeX is not evaluating virtual fonts by itself.
48 -- So instead, we process the shaped output and replace all glyph nodes with glyph
49 -- nodes from special fonts which have the characteristics expected brom the DVI reader.
50
51 -- mapped_fonts maps fontids from the user to fontids used in the DVI file
52 local mapped_fonts = setmetatable({}, {__index = function(t, fid)
53 local font = getfont(fid)
54 local mapped = font and font.backend_font or false
55 t[fid] = mapped
56 return mapped
57 end})
58
59 local full_hprocess
60 local function full_vprocess(head)
61 for _, id, _, list in traverse_list(head) do
62 if id == hlist_t then
63 full_hprocess(list)
64 elseif id == vlist_t then
65 full_vprocess(list)
66 else
67 assert(false)
68 end
69 end
70 end
71
72 function full_hprocess(head)
73 local last_f, last_mapping, last_mapped_font
74 for n, id in traverse(head) do
75 if id == hlist_t then
76 full_hprocess(getlist(n))
77 elseif id == vlist_t then
78 full_vprocess(getlist(n))
79 elseif id == disc_t then
80 local _, _, rep = getdisc(n)
81 for n, c, f in traverse_glyph(rep) do
82 if f ~= last_f then
83 last_f, last_mapping = f, mapped_fonts[f]
84 last_mapped_font = last_mapping and last_mapping.font
85 end
86 if last_mapping then
87 local mapped = last_mapping[c]
88 if mapped then setfont(n, last_mapped_font, mapped) end
89 end
90 end
91 elseif id == glyph_t then
92 local c, f = is_glyph(n)
93 if f ~= last_f then
94 last_f, last_mapping = f, mapped_fonts[f]
95 last_mapped_font = last_mapping and last_mapping.font
96 end
97 if last_mapping then
98 local mapped = last_mapping[c]
99 if mapped then setfont(n, last_mapped_font, mapped) end
100 end
101 end
102 end
103 end
104
105 local function delayed_register_callback()
106 luatexbase.add_to_callback('pre_shipout_filter', function(list)
107 full_vprocess(todirect(list)) -- list should always be a single list node, so we can treat it as if it were a vlist with one node
108 return true
109 end, 'luaotfload.dvi')
110 delayed_register_callback = function() end
111 end
112
113 local function process(head, font)
114 local mapping = mapped_fonts[font]
115 if not mapping then return head end
116 local mapped_font = mapping.font
117 for n, c, f in traverse_glyph(head) do if f == font then
118 local mapped = mapping[c]
119 if mapped then setfont(n, mapped_font, mapped) end
120 end end
121 for n in traverse_id(disc_t, head) do if uses_font(n, font) then
122 local pre, post, rep = getdisc(n)
123 for n, c, f in traverse_glyph(pre) do if f == font then
124 local mapped = mapping[c]
125 if mapped then setfont(n, mapped_font, mapped) end
126 end end
127 for n, c, f in traverse_glyph(post) do if f == font then
128 local mapped = mapping[c]
129 if mapped then setfont(n, mapped_font, mapped) end
130 end end
131 for n, c, f in traverse_glyph(rep) do if f == font then
132 local mapped = mapping[c]
133 if mapped then setfont(n, mapped_font, mapped) end
134 end end
135 end end
136 end
137 local function manipulate(tfmdata, _, dvi_kind)
138 if dvi_kind == 'xdvipsk' then
139 -- xdvipsk wants to read tea leaves instead of using reasonable interfaces.
140 -- They will have to make sense of whatever output this produces.
141 return
142 elseif dvi_kind ~= 'dvisvgm' then
143 texio.write_nl(string.format('WARNING (luaotfload): Unsupported DVI backend %q, falling back to dvisvgm.', dvi_kind))
144 end
145 -- Legacy fonts can be written to the DVI file directly
146 if 2 ~= (tfmdata.encodingbytes or ((tfmdata.format == 'truetype' or tfmdata.format == 'opentype') and 2 or 1)) then
147 return
148 end
149 delayed_register_callback()
150 local newfont = {}
151 for k,v in pairs(tfmdata) do
152 newfont[k] = v
153 end
154 local newchars = {}
155 newfont.characters = newchars
156 local lookup = {}
157 for k,v in pairs(tfmdata.characters) do
158 local newchar = {
159 width = v.width, -- Only width should really be necessary
160 height = v.height,
161 depth = v.depth,
162 }
163 newchars[v.index or k] = newchar
164 lookup[k] = v.index or k
165 end
166 newfont.checksum = string.unpack('>I4', 'LuaF') -- Use a magic checksum such that the reader
167 -- can uniquely identify these fonts.
168 local name = '[' .. newfont.name .. ']:' -- TODO: Why .name? I would have expected .filename
169 if newfont.subfont and newfont.subfont ~= 1 then
170 name = name .. 'index=' .. tostring(math.tointeger(newfont.subfont-1)) .. ';'
171 end
172 if newfont.extend and newfont.extend ~= 1000 then
173 name = name .. 'extend=' .. tostring(math.tointeger(math.round(newfont.extend*65.536))) .. ';'
174 end
175 if newfont.slant and newfont.slant ~= 0 then
176 name = name .. 'slant=' .. tostring(math.tointeger(math.round(newfont.slant*65.536))) .. ';'
177 end
178 if newfont.mode == 2 and newfont.width and newfont.width ~= 0 then
179 name = name .. 'embolden=' .. tostring(math.tointeger(math.round(newfont.width*65.78176))) .. ';'
180 end
181 newfont.name = name:sub(1,-2) -- Stri th trailing : or ;
182 tfmdata.backend_font = lookup
183 lookup.font = define_font(newfont)
184 end
185
186 fonts.constructors.features.otf.register {
187 name = "dvifont",
188 default = configuration.run.default_dvi_driver,
189 manipulators = {
190 node = manipulate,
191 base = manipulate,
192 },
193 -- Processors are not needed since we are using pre_shipout_filter
194 -- processors = {
195 -- node = process,
196 -- base = process,
197 -- },
198 }