Difference between revisions of "Use a TrueType font"
(Removed the useless definition of names_of_glyph (not used in the code).) |
m |
||
(3 intermediate revisions by 2 users not shown) | |||
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. | ||
− | + | For plain/LaTeX you are better using the package luaotfload, on the CTAN, and developped here: http://github.com/khaledhosny/luaotfload/, ConTeXt have this functionality built-in. | |
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 | + | local fonttype, f |
− | + | local fonttype = string.match(string.lower(name), "otf$") and "opentype" | |
− | + | or string.match(string.lower(name), "ttf$") and "truetype" | |
− | + | ||
− | |||
− | |||
− | |||
if fonttype then | if fonttype then | ||
− | if | + | filename = kpse.find_file(name, "opentype fonts") or kpse.find_file(name, "truetype fonts") |
− | ttffont = | + | if size < 0 then |
+ | size = (- 655.36) * size | ||
+ | end | ||
+ | ttffont = fontloader.to_table(fontloader.open(filename)) | ||
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.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 | ||
− | + | local names_of_char = { } | |
− | for char, glyph in pairs(ttffont.map.map) | + | for char, glyph in pairs(ttffont.map.map) do |
− | + | names_of_char[ttffont.glyphs[glyph].name] = ttffont.map.backmap[glyph] | |
− | names_of_char[ttffont.glyphs[glyph].name] | ||
− | |||
end | end | ||
− | + | for char, glyph in pairs(ttffont.map.map) do | |
− | + | local glyph_table = ttffont.glyphs[glyph] | |
− | 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 63: | Line 59: | ||
if glyph_table.kerns then | if glyph_table.kerns then | ||
local kerns = { } | local kerns = { } | ||
− | for _, kern in pairs(glyph_table.kerns) | + | 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 78: | Line 74: | ||
ordering = "Identity", | ordering = "Identity", | ||
supplement = 0, | supplement = 0, | ||
− | version = 1 | + | version = 1 } |
− | |||
end | end | ||
else | else | ||
Line 85: | Line 80: | ||
end | end | ||
return f | return f | ||
− | end | + | end) |
− | ) | ||
} | } | ||
</pre> | </pre> | ||
Line 92: | Line 86: | ||
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. |
Latest revision as of 23:23, 2 October 2011
A TrueType (or Openype) font can be used without going through a TFM or an OFM file, like in XeTeX.
For plain/LaTeX you are better using the package luaotfload, on the CTAN, and developped here: http://github.com/khaledhosny/luaotfload/, ConTeXt have this functionality built-in.
The code shows how we can use the table of a TrueType font to produce an internal TeX font table:
\pdfoutput1 \directlua{ callback.register("define_font", function(name, size) local fonttype, f local fonttype = string.match(string.lower(name), "otf$") and "opentype" or string.match(string.lower(name), "ttf$") and "truetype" if fonttype then filename = kpse.find_file(name, "opentype fonts") or kpse.find_file(name, "truetype fonts") if size < 0 then size = (- 655.36) * size end ttffont = fontloader.to_table(fontloader.open(filename)) if ttffont then f = { } f.name = ttffont.fontname f.fullname = ttffont.names[1].names.fullname f.parameters = { } f.designsize = size f.size = size f.direction = 0 f.parameters.slant = 0 f.parameters.space = size * 0.25 f.parameters.space_stretch = 0.3 * size f.parameters.space_shrink = 0.1 * size f.parameters.x_height = 0.4 * size f.parameters.quad = 1.0 * size f.parameters.extra_space = 0 f.characters = { } local mag = size / ttffont.units_per_em local names_of_char = { } for char, glyph in pairs(ttffont.map.map) do names_of_char[ttffont.glyphs[glyph].name] = ttffont.map.backmap[glyph] end for char, glyph in pairs(ttffont.map.map) do local glyph_table = ttffont.glyphs[glyph] f.characters[char] = { index = glyph, width = glyph_table.width * mag, name = glyph_table.name } if glyph_table.boundingbox[4] then f.characters[char].height = glyph_table.boundingbox[4] * mag end if glyph_table.boundingbox[2] then f.characters[char].depth = -glyph_table.boundingbox[2] * mag end if glyph_table.kerns then local kerns = { } for _, kern in pairs(glyph_table.kerns) do kerns[names_of_char[kern.char]] = kern.off * mag end f.characters[char].kerns = kerns end end f.filename = filename f.type = "real" f.format = fonttype f.embedding = "subset" f.cidinfo = { registry = "Adobe", ordering = "Identity", supplement = 0, version = 1 } end else f = font.read_tfm(name, size) end return f end) }
The index of the f.characters table is the Unicode value, among the properties of each entry of that table we have index 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.