How to use an OpenType font.
I am trying to use an open type font. In the manual I can see a piece of Lua code for loading such a font. I "suppose" that I have to enclose it within \direclua0{}, so I wrote \directlua0{function load_font (filename) local metrics = nil local font = fontforge.open(filename) if font then metrics = fontforge.to_table(font) fontforge.close(font) end return metrics end } \directlua0{twelverm = load_font('C:/Windows/Fonts/LMRoman12-Regular.otf')} but it still does not solve the problem. I have no idea of how to make the font known for TeX. This is command is expandable. As an example, the following input: $\pi = \directlua0{tex.print(math.pi)}$ will result in p = 3.1415926535898 But this leaves me none the wiser about fonts. The problem is: which is the equivalent of \font\tenrm=cmr10 for OpenType (or TrueType) fonts? This problem is present to some extent all troughout the manual. It seems as if you were taking always something for granted about the interaction between Lua and TeX that is not explained anywhere. You may find it obvious, but keep in mind that manuals are intended for people who still don't know. For somebody like me who comes from TeX, the impression got when reading the manual (or at least the impression I had) is as having all the pieces from a conglomerate rock but not having the cement. And the lack of examples is exasperating. This I understand given the development state of the program, but men, since one of the main features of LuaTeX is accesing and modifing the TeX parameters and state with Lua code, some exemples of this, even if kept to a minimum, would be very helpful. I wouldn't have any problem in taking the time to write them myself... if knew it, but the LuaTeX manual is still very far from serving to the learning of LuaTeX in the same way as the TeXBook serves for the learnig TeX or "the red book" serves for the learning of the postscript language. Continuing with my problem with using an otf font, I found some pages below in the manual an example intended for a diferent purpose but that looked usefull for solving my problem even if not the way it should be done. So below the avobe mentioned lua code I boldly wrote \directlua0 { callback.register('define_font', function (name,size) if name == 'LMRoman12-Regular' then f = twelverm else f = font.read_tfm(name,size) end return f end ) } \font\twelverm= LMRoman12-Regular it does not work, but it exhibits a bug in LuaTeX: I get a warning at \dump time: luaTeX warning: lua-loaded font [1] (<NULL>) has no character table! but right afterwards the program dies and is closed by Windows. --Javier A.
Javier Múgica wrote:
I am trying to use an open type font. In the manual I can see a piece of Lua code for loading such a font. I "suppose" that I have to enclose it within \direclua0{}, so I wrote
The table format that is returned by fontforge cannot be used immediately as a return table from define_font, it needs to be converted first, that is why your test didn't work out. There is (or at least used to be) a wiki page about TrueType/OpenType loading on the http://luatex.bluwiki.com , and that should help you get started. Best wishes, Taco
Le 1 oct. 08 à 11:47, Taco Hoekwater a écrit :
There is (or at least used to be) a wiki page about TrueType/OpenType loading on the http://luatex.bluwiki.com , and that should help you get started.
Indeed, the information is still on the Wiki and you are invited to supply more entries... Yannis
Javier Múgica wrote:
[...] lack of examples [...]
There is (or at least used to be) a wiki page about TrueType/OpenType loading on the http://luatex.bluwiki.com , and that should help you get started.
Best wishes, Taco
So the examples I were missing can be found there. You could provide this link in the page http://www.luatex.org/documentation.html it will help a lot. Thank you
I think this should work: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \directlua0{ callback.register('define_font', 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 (size < 0) then size = (- 655.36) * size end ttffont = fontforge.to_table(fontforge.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 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 = { } mag = size / ttffont.units_per_em names_of_char = { } for char, glyph in pairs(ttffont.map.map) do names_of_char[ttffont.glyphs[glyph].name] = ttffont.map.backmap[glyph] end names_of_glyph = { } for char, glyph in pairs(ttffont.map.map) do names_of_glyph[ttffont.glyphs[glyph].name] = glyph end for char, glyph in pairs(ttffont.map.map) do glyph_table = ttffont.glyphs[glyph] f.characters[char] = { index = glyph, width = glyph_table.width * mag, name = glyph_table.name, } 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 ) } \font\test=nazli.ttf at 12pt \test Hi. This is just a test. \end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5 Hope that helps ________________________________
Date: Wed, 1 Oct 2008 11:38:41 +0200 From: javier@digi21.eu To: dev-luatex@ntg.nl Subject: [Dev-luatex] How to use an OpenType font.
I am trying to use an open type font. In the manual I can see a piece of Lua code for loading such a font. I "suppose" that I have to enclose it within \direclua0{}, so I wrote
\directlua0{function load_font (filename) local metrics = nil local font = fontforge.open(filename) if font then metrics = fontforge.to_table(font) fontforge.close(font) end return metrics end } \directlua0{twelverm = load_font('C:/Windows/Fonts/LMRoman12-Regular.otf')}
but it still does not solve the problem. I have no idea of how to make the font known for TeX.
This is command is expandable. As an example, the following input: $\pi = \directlua0{tex.print(math.pi)}$
will result in p = 3.1415926535898
But this leaves me none the wiser about fonts. The problem is: which is the equivalent of
\font\tenrm=cmr10
for OpenType (or TrueType) fonts?
This problem is present to some extent all troughout the manual. It seems as if you were taking always something for granted about the interaction between Lua and TeX that is not explained anywhere. You may find it obvious, but keep in mind that manuals are intended for people who still don't know. For somebody like me who comes from TeX, the impression got when reading the manual (or at least the impression I had) is as having all the pieces from a conglomerate rock but not having the cement. And the lack of examples is exasperating. This I understand given the development state of the program, but men, since one of the main features of LuaTeX is accesing and modifing the TeX parameters and state with Lua code, some exemples of this, even if kept to a minimum, would be very helpful. I wouldn't have any problem in taking the time to write them myself... if knew it, but the LuaTeX manual is still very far from serving to the learning of LuaTeX in the same way as the TeXBook serves for the learnig TeX or "the red book" serves for the learning of the postscript language.
Continuing with my problem with using an otf font, I found some pages below in the manual an example intended for a diferent purpose but that looked usefull for solving my problem even if not the way it should be done. So below the avobe mentioned lua code I boldly wrote
\directlua0 { callback.register('define_font', function (name,size) if name == 'LMRoman12-Regular' then f = twelverm else f = font.read_tfm(name,size) end return f end ) } \font\twelverm= LMRoman12-Regular it does not work, but it exhibits a bug in LuaTeX: I get a warning at \dump time:
luaTeX warning: lua-loaded font [1] () has no character table!
but right afterwards the program dies and is closed by Windows.
--Javier A.
_________________________________________________________________
Hello, a few corrections and annotations (the code, BTW, is taken from the already mentioned wiki http://luatex.bluwiki.com/go/Use_a_TrueType_font).
mag = size / ttffont.units_per_em
This should be "local mag = size / ttffont.units_per_em".
f.characters[char] = { index = glyph, width = glyph_table.width * mag, name = glyph_table.name, }
This does not set the height and depth of the character correctly, which I found results in spacing errors before and after large display maths. In my code, I have added: 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 ) }
Also note that the loaded font will not have any ligatures. Jonathan
Guten Tag Javier Múgica, am Mittwoch, 1. Oktober 2008 um 11:38 schrieben Sie:
I am trying to use an open type font. In the manual I can see a piece of Lua code for loading such a font. I "suppose" that I have to enclose it within \direclua0{}, so I wrote
but it still does not solve the problem. I have no idea of how to make the font known for TeX.
The example below works for me together with a "lualatex" format. But is rather crude and certainly will need extension.
This problem is present to some extent all troughout the manual. It seems as if you were taking always something for granted about the interaction between Lua and TeX that is not explained anywhere. You may find it obvious, but keep in mind that manuals are intended for people who still don't know. For somebody like me who comes from TeX, the impression got when reading the manual (or at least the impression I had) is as having all the pieces from a conglomerate rock but not having the cement. And the lack of examples is exasperating.
luatex is still very beta. So you shouldn't expect its documentation to be complete.
This I understand given the development state of the program, but men, since one of the main features of LuaTeX is accesing and modifing the TeX parameters and state with Lua code, some exemples of this, even if kept to a minimum, would be very helpful.
Heiko Oberdiek has made some packages with use lua. They are quite informativ. There is a lua-inputenc on CTAN. And the wiki-page where I got the font code has also some example. The context sources contains a lot of lua code. (But I don't find them very useful -- to complicated for me.) \pdfoutput=1 \documentclass{article} % From http://luatex.bluwiki.com/go/Use_a_TrueType_font \directlua0{ callback.register('define_font', 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 (size < 0) then size = (- 655.36) * size end ttffont = fontforge.to_table(fontforge.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 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 = { } mag = size / ttffont.units_per_em names_of_char = { } for char, glyph in pairs(ttffont.map.map) do names_of_char[ttffont.glyphs[glyph].name] = ttffont.map.backmap[glyph] end names_of_glyph = { } for char, glyph in pairs(ttffont.map.map) do names_of_glyph[ttffont.glyphs[glyph].name] = glyph end for char, glyph in pairs(ttffont.map.map) do glyph_table = ttffont.glyphs[glyph] f.characters[char] = { index = glyph, width = glyph_table.width * mag, name = glyph_table.name, } 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 ) } \begin{document} \font\lm=lmroman10-regular at 10pt \lm This is Latin Modern in Opentype format This is Latin Modern in Opentype format This is Latin Modern in Opentype format This is Latin Modern in Opentype format \end{document} -- Mit freundlichen Grüßen Ulrike Fischer mailto:list@nililand.de
participants (6)
-
Javier Múgica
-
Jonathan Sauer
-
Taco Hoekwater
-
Ulrike Fischer
-
Yannis Haralambous
-
وفا خلیقی، Vafa Khalighi