-- fontspec-4ht.lua (2026-12-03-12:45), generated from tex4ht-4ht.tex -- Copyright 2016-2026 TeX Users Group --[[ % % This work may be distributed and/or modified under the % conditions of the LaTeX Project Public License, either % version 1.3c of this license or (at your option) any % later version. The latest version of this license is in % http://www.latex-project.org/lppl.txt % and version 1.3c or later is part of all distributions % of LaTeX version 2005/12/01 or later. % % This work has the LPPL maintenance status "maintained". % % The Current Maintainer of this work % is the TeX4ht Project . % % If you modify this program, changing the % version identification would be appreciated. \immediate\write-1{version 2026-12-03-12:45} --]] local M = {} local glyph_id = node.id "glyph" local whatsit_id = node.id "whatsit" local special_subtype = node.subtype "special" local dir_id = node.id "dir" local glue_id = node.id "glue" local hlist_id = node.id "hlist" local vlist_id = node.id "vlist" local findhtf = require "tex4ht-findhtf" -- local normalize = require'lua-uni-normalize' local escape = function(char) -- prepare tex4ht special for entity with unicode value return string.format("t4ht@+&{35}x%x{59}", char) end local make_node = function(data) -- make special whathsit local n = node.new(whatsit_id,special_subtype) n.data = data return n end local function first_node(head) local head = head while head.prev do head = head.prev end return head end -- this should be table with patterns for allowed fonts local allowed_names = {"^cmr", "^cmb","^cmt", "^cmb", "^cmcs", "^rm%-l", "^cmi", "^ec%-lm", "none"} local testfont = function(name) -- test font name for all allowed names, when it is found, return true for _, x in ipairs(allowed_names) do local r = name:match(x) if r then return true end end return false end local fonttypes = {} local get_font_type = function(id) if fonttypes[id]~=nil then return fonttypes[id] end local f = font.getfont(id) or {name = "none"} -- font object can be nil sometimes local name = f.name local type = testfont(string.lower(name)) if not type then print("Unsupported font", name) end fonttypes[id] = type return type end local xchar = string.byte("x") local utfchar = unicode.utf8.char function M.char_to_entity(head) -- traverse characters for n in node.traverse(head) do if n.id == glyph_id then -- we need to process only default text font, ie cmr, because user may request special mathematical fonts, -- which should be processed via htf files as usual local t = get_font_type(n.font) if t == true then local char = n.char if char > 127 then local new = escape(char) local x = make_node(new) -- insert tex4ht special before char, it will replace the char node.insert_before(head, n, x) -- in standard tex4ht accented characters are replaced with "x" char. they are later removed anyway -- maybe we don't need to do that, but we can, so why not? n.char = xchar end end elseif n.id == dir_id then -- when text direction is TRT, the spaces in the DVI file have negative width and they are not recognized by tex4ht -- so we just change the direction to normal TLT n.dir = "+TLT" end end return first_node(head) end -- default mapping, based on rm-lm.htf local mapping = { [915] = 0, [916] = 1, [920] = 2, [923] = 3, [926] = 4, [928] = 5, [931] = 6, [933] = 7, [934] = 8, [936] = 9, [937] = 10, [64256] = 11, [64257] = 12, [64258] = 13, [64259] = 14, [64260] = 15, [305] = 16, [567] = 17, [96] = 18, [180] = 19, [711] = 20, [728] = 21, [175] = 22, [730] = 23, [184] = 24, [223] = 25, [230] = 26, [339] = 27, [248] = 28, [198] = 29, [338] = 30, [216] = 31, [8221] = 34, [38] = 38, [8217] = 39, [40] = 40, [41] = 41, [161] = 60, [191] = 62, [91] = 91, [8220] = 92, [93] = 93, [729] = 95, [8216] = 96, [8211] = 123, [8212] = 124, [733] = 125, [732] = 126, [168] = 127, [258] = 128, [260] = 129, [262] = 130, [268] = 131, [270] = 132, [282] = 133, [280] = 134, [286] = 135, [313] = 136, [317] = 137, [321] = 138, [323] = 139, [327] = 140, [330] = 141, [336] = 142, [340] = 143, [344] = 144, [346] = 145, [352] = 146, [536] = 147, [356] = 148, [538] = 149, [368] = 150, [366] = 151, [376] = 152, [377] = 153, [382] = 154, [379] = 155, [306] = 156, [304] = 157, [273] = 158, [10816] = 159, [259] = 160, [261] = 161, [263] = 162, [269] = 163, [271] = 164, [283] = 165, [281] = 166, [302] = 167, [314] = 168, [318] = 169, [322] = 170, [324] = 171, [328] = 172, [331] = 173, [337] = 174, [341] = 175, [345] = 176, [347] = 177, [353] = 178, [537] = 179, [357] = 180, [539] = 181, [369] = 182, [367] = 183, [255] = 184, [378] = 185, [382] = 186, [380] = 187, [307] = 188, [161] = 189, [34] = 190, [163] = 191, [192] = 192, [193] = 193, [194] = 194, [195] = 195, [196] = 196, [197] = 197, [171] = 198, [199] = 199, [200] = 200, [201] = 201, [202] = 202, [203] = 203, [204] = 204, [205] = 205, [206] = 206, [207] = 207, [208] = 208, [209] = 209, [210] = 210, [211] = 211, [212] = 212, [213] = 213, [214] = 214, [187] = 215, [8240] = 216, [217] = 217, [218] = 218, [219] = 219, [220] = 220, [221] = 221, [222] = 222, [224] = 224, [225] = 225, [226] = 226, [227] = 227, [228] = 228, [229] = 229, [231] = 231, [232] = 232, [233] = 233, [234] = 234, [235] = 235, [236] = 236, [237] = 237, [238] = 238, [239] = 239, [240] = 240, [241] = 241, [242] = 242, [243] = 243, [244] = 244, [245] = 245, [246] = 246, [8738] = 247, [8960] = 248, [249] = 249, [250] = 250, [251] = 251, [252] = 252, [253] = 253, [254] = 254, [8222] = 255, } local fontsizes = {} local used_fonts = {} local new_fonts = {} local function get_font_size(font_id) -- we need to get font size for the font, because we will create new font with the same size, but different encoding if not fontsizes[font_id] then -- local f = used_fonts[font_id] or font.getfont(font_id) local f = font.getcopy(font_id) used_fonts[font_id] = f if f then fontsizes[font_id] = f.size end end return fontsizes[font_id] end local function get_new_font(font_name, size) -- define and cache a new TFM font at a given size with a given name, -- we will use it for mapping characters that are not in the original font, -- but are needed for pictures, so we need to use different encoding, -- because original font is usually Latin and we need to support Cyrillic and other non-Latin characters local key = font_name .. "-" .. size if not used_fonts[key] then local f = font.read_tfm(font_name, size) if f then local id = font.define(f) used_fonts[key] = id else print("Warning: font", font_name, "not found for char, it will not be processed") end end return used_fonts[key] end local function get_htf_file(font_name) local orignal_font_name = font_name -- htf files usually are usually names just by a part of the font name, to support different tfm files that follow the same mapping -- we must remove characters from the font name until we find a corresponding htf file repeat local htf_file = findhtf[font_name] -- print("Searching for htf file for font", font_name, "found:", htf_file) if htf_file then return htf_file end font_name = font_name:sub(1, -2) -- remove last character until font_name == "" return nil, "Cannot find htf file for " .. orignal_font_name .. ", please make sure that the font is supported by tex4ht and that the corresponding htf file exists" end local function get_htf_mapping(htf_file) local map = {} local f = io.open(htf_file, "r") if not f then print("Warning: cannot open htf file", htf_file) return false end local i = 0 -- print("parsing htf file", htf_file) for line in f:lines() do if i == 0 and line:match("^%.") then -- this htf file is mapping to another htf file, return that instead local name = line:match("^%.([%S]+)") local htf_file = get_htf_file(name) if htf_file then -- print("htf file", htf_file, "is mapping to another htf file", name) return get_htf_mapping(htf_file) else return false end else local char = line:match("'([^']*)'") if char then local chars = {} if char:match('^&#x') then for hex_code in char:gmatch('&#x([0-9A-Fa-f]+);') do local codepoint = tonumber(hex_code, 16) table.insert(chars, codepoint) -- print(string.format("[%i] = %i, ", codepoint, i)) end if #chars == 1 then map[chars[1]] = i elseif #chars > 1 then -- combination of characters, we ignore these -- I've tried to fix them using uninormalize, but that didn't work end end i = i + 1 end end end return map end local font_names = {} local htf_mappings = {} -- because we are using 8bit fonts instead of OpenType, we may need to use different fonts -- to support all characters. So this mapping should contain different tfm fonts of the same family -- that cover all characters that may be used in the picture mode. For example, we may use Latin font for Latin characters, -- Cyrillic font for Cyrillic characters, and Greek font for Greek characters. This mapping should contain the names of the tfm files that are used for each character set. local tfm_mappings = { ["rm-lmr10"] = {"larm1000", "grmn1000"}, ["rm-lmbx10"] = {"lbbx1000", "grxn1000"}, ["rm-lmri10"] = {"lati1000", "grmi1000"}, ["ec-lmtt10"] = {"latt1000", "gttn1000"}, ["rm-lmsl10"] = {"lasl1000", "grmo1000"}, ["rm-lmcsc10"] = {"lacc1000", "grmc1000"}, ["rm-lmss10"] = {"lass1000", "grsn1000"}, ["rm-lmssbx10"]= {"lasx1000", "grsx1000"}, ["rm-lmssbo10"]= {"laso1000", "grso1000"}, ["cmr10"] = {"larm1000", "grmn1000"}, ["cmbx10"] = {"lbbx1000", "grxn1000"}, ["cmti10"] = {"lati1000", "grmi1000"}, ["cmtt10"] = {"latt1000", "gttn1000"}, } function M.add_tfm_mapping(base_font, mapped_fonts) -- this function is supposed to be called from \Configure{FontspecMapping} -- the second argument is supposed to be comma separated string of font names local t = {} for font in mapped_fonts:gmatch("([^,^%s]+)") do table.insert(t, font) end tfm_mappings[base_font] = t end local function load_htf_mapping(tfm_mapping, htf_mapping) -- load different fonts so we can use more characters than supported by the original font for _, font in ipairs(tfm_mapping) do local htf_file = get_htf_file(font) if htf_file then local map = get_htf_mapping(htf_file) for unicode, char in pairs(map) do htf_mapping[unicode] = { char = char, font = font } end end end end local function map_char(char, node) local m local htf_mapping = htf_mappings[node.font] local msg -- we test for nil explicitly, because we distinguish between "not found" and "not tested yet" if htf_mapping == nil then -- set htf_mapping to false, so we don't try to find it again, if we cannot find the htf file htf_mapping = false local font_name = font_names[node.font] or font.getfont(node.font).name if font_name then local htf_file = get_htf_file(font_name) if htf_file then htf_mapping = get_htf_mapping(htf_file) local tfm_mapping = tfm_mappings[font_name] or {} load_htf_mapping(tfm_mapping, htf_mapping) else msg = "cannot find htf file for font " .. font_name .. " default mapping will be used in the picture" end else msg = "cannot get font name for font id " .. node.font .. " default mapping will be used in the picture" end htf_mappings[node.font] = htf_mapping end if htf_mapping then m = htf_mapping[char] or mapping[char] else msg = msg or "couldn't load mapping from the htf file" print("[tex4ht] warning: " .. msg) -- use default mapping if we cannot find the htf file m = mapping[char] end -- print("Char", char, "font", font_name, "mapping", m) if type(m) == "number" then -- simple mapping where we will reuse the original font, suitable for Latin characters node.char = m elseif type(m) == "table" and m.font then -- in the case of more complex mapping, where we need to use different font, -- we will create new font with the same size, but different encoding, suitable for cyrrillic and other non-Latin characters local current_size = get_font_size(node.font) if not current_size then print("Warning: font size not found for font id", node.font, "for char", char, "it will not be processed", utf8.char(char)) return node end local new_font_id = get_new_font(m.font, current_size) if new_font_id then node.char = m.char node.font = new_font_id end else print("Warning: char code", char, "not found in mapping, it will not be processed", utf8.char(char)) end return node end local is_picture = false function M.fix_pictures(head) -- print("Fix pictures, is picture:", is_picture) -- local is_is_picture = is_picture or false local nextchar = nil for n in node.traverse(head) do if n.id == whatsit_id and n.subtype == special_subtype then if n.data and n.data:match("t4ht%+%+") then is_picture = true -- print("Picture mode on") elseif n.data == "t4ht+" then -- print("Picture mode off") is_picture = false elseif n.data:match("t4ht@%+%&") then -- this is special with char code for picture, we need to save it for next glyph, -- because we will replace it with a mapping from this Unicode codepoint back to the font glyph id nextchar = n.data:match("t4ht@%+%&{35}x(.-){59}") -- print("Next char:", tonumber(nextchar, 16)) end elseif n.id == glyph_id and is_picture then -- when we are in picture mode, we need to process all characters, because they can be used as part of picture local char = n.char -- print("Picture char:", char) if nextchar then local char = tonumber(nextchar, 16) -- if char > 255 then -- print("Warning: char code", char, "is too big for picture mode, it will not be processed", utf8.char(char)) -- end map_char(char, n) -- n.char = mapping[char] or char end nextchar = nil elseif n.id == hlist_id or n.id == vlist_id then M.fix_pictures(n.list) end end return head end M.allowed = allowed_names M.mapping = mapping M.font_mappings = tfm_mappings return M