Editing Use a TrueType font
From LuaTeXWiki
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
A TrueType (or Openype) font can be used without going through a TFM or an OFM file, like in XeTeX. | A TrueType (or Openype) font can be used without going through a TFM or an OFM file, like in XeTeX. | ||
− | + | The best way to do so is to use the package luaotfload, on the CTAN, and developped here: http://github.com/mpg/luaotfload/ | |
The code shows how we can use the table of a TrueType font to produce an internal TeX font table: | The code shows how we can use the table of a TrueType font to produce an internal TeX font table: | ||
Line 8: | Line 8: | ||
\pdfoutput1 | \pdfoutput1 | ||
\directlua{ | \directlua{ | ||
− | callback.register( | + | callback.register('define_font', |
function(name, size) | function(name, size) | ||
− | + | fonttype = nil | |
− | + | filename = kpse.find_file(name,"opentype fonts") | |
− | + | if (filename) | |
− | + | then fonttype = 'opentype' | |
+ | else filename = kpse.find_file(name, "truetype fonts") | ||
+ | end | ||
+ | if filename and not fonttype then fonttype = 'truetype' end | ||
if fonttype then | if fonttype then | ||
− | + | if (size < 0) then size = (- 655.36) * size end | |
− | + | ttffont = fontforge.to_table(fontforge.open(filename)) | |
− | |||
− | |||
− | ttffont = | ||
if ttffont then | if ttffont then | ||
f = { } | f = { } | ||
Line 27: | Line 27: | ||
f.designsize = size | f.designsize = size | ||
f.size = size | f.size = size | ||
− | + | direction = 0 | |
f.parameters.slant = 0 | f.parameters.slant = 0 | ||
f.parameters.space = size * 0.25 | f.parameters.space = size * 0.25 | ||
Line 38: | Line 38: | ||
local mag = size / ttffont.units_per_em | local mag = size / ttffont.units_per_em | ||
− | + | names_of_char = { } | |
− | for char, glyph in pairs(ttffont.map.map) do | + | for char, glyph in pairs(ttffont.map.map) |
− | names_of_char[ttffont.glyphs[glyph].name] = ttffont.map.backmap[glyph] | + | do |
+ | names_of_char[ttffont.glyphs[glyph].name] | ||
+ | = ttffont.map.backmap[glyph] | ||
end | end | ||
− | + | for char, glyph in pairs(ttffont.map.map) | |
− | + | do | |
+ | glyph_table = ttffont.glyphs[glyph] | ||
f.characters[char] = { | f.characters[char] = { | ||
index = glyph, | index = glyph, | ||
width = glyph_table.width * mag, | width = glyph_table.width * mag, | ||
− | name = glyph_table.name } | + | name = glyph_table.name, |
+ | } | ||
if glyph_table.boundingbox[4] then | if glyph_table.boundingbox[4] then | ||
f.characters[char].height = glyph_table.boundingbox[4] * mag | f.characters[char].height = glyph_table.boundingbox[4] * mag | ||
Line 59: | Line 63: | ||
if glyph_table.kerns then | if glyph_table.kerns then | ||
local kerns = { } | local kerns = { } | ||
− | for _, kern in pairs(glyph_table.kerns) do | + | for _, kern in pairs(glyph_table.kerns) |
+ | do | ||
kerns[names_of_char[kern.char]] = kern.off * mag | kerns[names_of_char[kern.char]] = kern.off * mag | ||
end | end | ||
− | + | f.characters[char].kerns = kerns | |
end | end | ||
end | end | ||
− | |||
f.filename = filename | f.filename = filename | ||
− | f.type = | + | f.type = 'real' |
f.format = fonttype | f.format = fonttype | ||
f.embedding = "subset" | f.embedding = "subset" | ||
Line 74: | Line 78: | ||
ordering = "Identity", | ordering = "Identity", | ||
supplement = 0, | supplement = 0, | ||
− | version = 1 } | + | version = 1 |
+ | } | ||
end | end | ||
else | else | ||
Line 80: | Line 85: | ||
end | end | ||
return f | return f | ||
− | end) | + | end |
+ | ) | ||
} | } | ||
</pre> | </pre> | ||
Line 86: | Line 92: | ||
The index of the <tt>f.characters</tt> table is the Unicode value, among the properties of each entry of that table we have <tt>index</tt> which is the glyph index. Using this property we get the mapping between Unicode characters and glyphs. Kerning is handled via TeX font LKP. | The index of the <tt>f.characters</tt> table is the Unicode value, among the properties of each entry of that table we have <tt>index</tt> which is the glyph index. Using this property we get the mapping between Unicode characters and glyphs. Kerning is handled via TeX font LKP. | ||
− | What is missing in the code above is better calculation of standard TeX font parameters and OpenType features. | + | What is missing in the code above is better calculation of standard TeX font parameters and OpenType features. � suivre... |