Skip to content

Instantly share code, notes, and snippets.

@phi-gamma
Last active December 21, 2015 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phi-gamma/6353993 to your computer and use it in GitHub Desktop.
Save phi-gamma/6353993 to your computer and use it in GitHub Desktop.
-----------------------------------------------------------------------
-- FILE: sidebearings.lua
-- USAGE: ./sidebearings.lua
-- DESCRIPTION: access sidebearings with Luatex
-- REQUIREMENTS: luaotfload version >= 2.2
-- AUTHOR: Philipp Gesang (Phg), <phg42.2a@gmail.com>
-- VERSION: 1.0
-- CREATED: 2013-08-27 15:27:20+0200
-----------------------------------------------------------------------
--
packagedata = packagedata or { }
packagedata.sidebearings = { }
local sidebearings = packagedata.sidebearings
local utfbyte = utf.byte
local texsprint = tex.sprint
local get_sidebearings = function (id, char)
local tfmdata = font.getfont (id)
if not (tfmdata and tfmdata.shared) then
return 0, 0
end
local descriptions = tfmdata.shared.rawdata.descriptions
local glyphdata = descriptions [char]
if not glyphdata then
--- font lacks the glyph
return 0, 0
end
local boundingbox = glyphdata.boundingbox
local lside = boundingbox [1] or 0
local wd = boundingbox [3] or glyphdata.width
local rside = glyphdata.width - wd
inspect (glyphdata)
return lside / 100, rside /100
end
local sidebearings = function (id, char, left)
char = utfbyte (char)
local lside, rside = get_sidebearings (id, char)
if left then
texsprint (tostring (lside), "pt")
else
texsprint (tostring (rside), "pt")
end
end
packagedata.sidebearings.left = function (char)
return sidebearings (font.current (), char, true)
end
packagedata.sidebearings.right = function (char)
return sidebearings (font.current (), char, false)
end
\ifdefined \directlua
\input luaotfload.sty
\directlua {require "sidebearings"}
\def \lsidebearing #1{%
\directlua {packagedata.sidebearings.left [[#1]]}%
}
\def \rsidebearing #1{%
\directlua {packagedata.sidebearings.right [[#1]]}%
}
\font \mainfont = "file:Iwona-Regular.otf"
\else
\def \lsidebearing #1{\the \XeTeXglyphbounds1 \the \XeTeXcharglyph`#1}
\def \rsidebearing #1{\the \XeTeXglyphbounds3 \the \XeTeXcharglyph`#1}
\font \mainfont = "[Iwona-Regular.otf]"
\fi
\mainfont
\def \test #1{[#1] left: \lsidebearing {#1}, right: \rsidebearing {#1}\par}
\test a
\test b
\test x
\test y
\test а
\test б
\test ю
\test я
\bye
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment