-
Notifications
You must be signed in to change notification settings - Fork 3
/
luametalatex-microtype.sty
87 lines (87 loc) · 3.63 KB
/
luametalatex-microtype.sty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
% To be loaded *after* microtype
%
% Patching \pickup@font to emulate LuaTeX's font expansion interface using \adjustspacing...
\RequirePackage{microtype}% Just to ensure it isn't loaded in the wrong position. Normally this shouldn't be necessary
\def\luametalatex@@setexpansion#-#1#2#3#-\relax{%
\adjustspacingstretch #1
\adjustspacingshrink #2
\adjustspacingstep #3
}%
\newluafunction\luametalatex@@expandglyphsinfont
\protected\luadef\pdffontexpand\luametalatex@@expandglyphsinfont
\def\luametalatex@@everyjobandnow#1{\toksapp\everyjob{#1}#1}%
%
\MT@addto@setup{%
\g@addto@macro\MT@pickupfont{%
\expandafter\expandafter\expandafter\luametalatex@@setexpansion\csname pickup@font@@hook@luametalatex@microtype@\the\fontid\font@name\endcsname{-1}{-1}{-1}\relax
}%
\MT@MT@pickupfont
\pickup@font
}%
\begingroup
\catcode`\!=\catcode`\%
\catcode`\%=12
\@firstofone{!
\endgroup
\luametalatex@@everyjobandnow{\directlua{!
local current = font.current
local func = luatexbase.new_luafunction'expandglyphsinfont'
local dim_cmd = '__luametalatex_microtype_tmp_dim'
token.setdimension(dim_cmd, 0)
local dim_tok = token.create(dim_cmd)
token.setlua('pdffontexpand', func, 'protected')
lua.get_functions_table()[func] = function()
token.put_next(token.create'fontid')
local font = token.scaninteger()
local stretch = token.scaninteger()
local shrink = token.scaninteger()
local step = token.scaninteger()
if font == current() then
tex.adjustspacingstretch = stretch
tex.adjustspacingshrink = shrink
tex.adjustspacingstep = step
end
token.set_macro('pickup@font@@hook@luametalatex@microtype@' .. font, string.format("{}{%i}{%i}{%i}", stretch, shrink, step), "global")
end
local set_font_cmd = token.command_id'set_font'
local char_given_cmd = token.command_id'char_given'
func = luatexbase.new_luafunction'rpcode'
token.setlua('rpcode', func, 'value')
lua.get_functions_table()[func] = function(id, value)
token.putnext(luametalatex.primitive_tokens.fontid)
local fid = token.scaninteger()
local cid = token.scaninteger()
local quad = font.getfontdimen(fid, 6)/1000 ! 6 - quad
if value == 'value' then
token.put_next(luametalatex.primitive_tokens.rpcode, token.new(fid, set_font_cmd), token.new(cid, char_given_cmd))
local raw = token.scan_dimen()
return luametalatex.value.integer, (raw / quad + .5)//1
else
token.scan_keyword'='
local factor = token.scaninteger()
token.setdimension(dim_cmd, (factor * quad + .5)//1)
token.put_next(luametalatex.primitive_tokens.rpcode, token.new(fid, set_font_cmd), token.new(cid, char_given_cmd), dim_tok)
return
end
end
func = luatexbase.new_luafunction'lpcode'
token.setlua('lpcode', func, 'value')
lua.get_functions_table()[func] = function(id, value)
token.putnext(luametalatex.primitive_tokens.fontid)
local fid = token.scaninteger()
local cid = token.scaninteger()
local quad = font.getfontdimen(fid, 6)/1000 ! 6 - quad
if value == 'value' then
token.put_next(luametalatex.primitive_tokens.lpcode, token.new(fid, set_font_cmd), token.new(cid, char_given_cmd))
local raw = token.scan_dimen()
return luametalatex.value.integer, (raw / quad + .5)//1
else
token.scan_keyword'='
local factor = token.scaninteger()
token.setdimension(dim_cmd, (factor * quad + .5)//1)
token.put_next(luametalatex.primitive_tokens.lpcode, token.new(fid, set_font_cmd), token.new(cid, char_given_cmd), dim_tok)
return
end
end
}}!
}%