LuaTeX:如何改变字母的宽度(边界框)?

LuaTeX:如何改变字母的宽度(边界框)?

我尝试采用 Marcel Krüger 的解决方案

在 lualatex 中使用 unicode-math 并使用字母和数字的文本字体时修复数学重音符号的位置

改变字母的宽度(边界框):

\documentclass[12pt]{article}
\usepackage{unicode-math}

\begingroup
\long\def\x#1{\directlua{\unexpanded{#1}}}
\catcode`\#=12 \catcode`\%=12
\expandafter\endgroup\x{
--[[Declare a helper \DeclareWidth to invoke the package later]]
local id = luatexbase.new_luafunction'DeclareWidth'
local WidthMappings = {}
token.set_lua('DeclareWidth', id)
lua.get_functions_table()[id] = function()
--[[This is executed when the command is called. We have to parse the input. Take a peek at the usage of \DeclareWidth below before trying to read the code, then it should be relativly easy to follow]]
local t = {}
repeat
local cp = assert(token.scan_int(), 'No codepoint found')
token.scan_keyword'='
t[cp] = assert(token.scan_int(), 'No offset found')
until not token.scan_keyword';'
assert(token.scan_token().cmdname == 'relax', 'Final delimiter missing')
--[[Save the parsed mapping in a global table and then send the index back to TeX]]
WidthMappings[#WidthMappings+1] = t
tex.sprint(string.format("width_id=%i", #WidthMappings))
end

--[[Now implement the feature. Nothing particularly interesting here, it's 
the same as almost any use of otf.register: Take the feature value, do some lookups, apply to characters]]
fonts.constructors.features.otf.register {
    name = 'width_id',
    description = 'Change selected width values',
    initializers = {
        base = function(tfmdata, value, features)
        local mapping = assert(WidthMappings[value], "I'm going to strike")
        local characters = tfmdata.characters
        for cp, c_width in next, mapping do
        assert(characters[cp], 'Why are you doing this to me?').width = c_width
        end
        end,
    },
}
}


\setmathfont{Latin Modern Math}[math-style=literal, RawFeature={\DeclareWidth`\X=1200\relax}]

\begin{document}

$XX$

\end{document}

但什么也没发生。X 的宽度没有改变。这里出了什么问题?

答案1

通过查看以下主题(Philipp Gesang 的回答),我找到了解决方案:

在 LuaTeX 中访问侧边距

重要的几行是:

local descriptions = tfmdata.shared.rawdata.descriptions
  local glyphdata    = descriptions [char]

因此我尝试通过以下方式访问宽度tfmdata.shared.rawdata.descriptions并将最后一部分更改为:

fonts.constructors.features.otf.register {
    name = 'width_id',
    description = 'Change selected width values',
    initializers = {
        base = function(tfmdata, value, features)
        local mapping = assert(WidthMappings[value], "I'm going to strike")
        local characters = tfmdata.characters
        local descriptions = tfmdata.shared.rawdata.descriptions
        for cp, c_width in next, mapping do
        local glyphdata = descriptions[cp]
        glyphdata.width = c_width
        end
        end,
    },
}
}

这有效。我不知道为什么,但它有效。我不明白这段代码的真正含义,也许有人可以解释一下。

相关内容