0

I tried to adapt this solution by Marcel Krüger

Fixing the position of math accents when using unicode-math in lualatex with a text font for letters and digits

for changing the width (bounding box) of a letter:

\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}

But nothing happens. The width of the X does not change. What is wrong here?

2
  • You are changing the width of Latin Uppercase Letter X, but in the mathematical block the X gets remapped to U+1D44A (Mathematical Italic Capital X). Try replacing `\X with "1D44A. Apr 17, 2022 at 14:25
  • @MarcelKrüger: This does not change anything. Apr 17, 2022 at 14:49

1 Answer 1

0

I found a solution by looking into following topic (answer by Philipp Gesang):

Accessing side-bearings in LuaTeX

The important lines are:

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

So I tried to to access the width by tfmdata.shared.rawdata.descriptions and changed the last part into:

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,
    },
}
}

And this works. I don't know why, but it works. I do not understand anything of this code, maybe someone can explain it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .