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.
  
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 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("define_font",
+
callback.register('define_font',
 
   function(name, size)
 
   function(name, size)
     local fonttype, f
+
     fonttype = nil
     local fonttype = string.match(string.lower(name), "otf$") and "opentype"
+
     filename = kpse.find_file(name,"opentype fonts")
                  or string.match(string.lower(name), "ttf$") and "truetype"
+
    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
       filename = kpse.find_file(name, "opentype fonts") or kpse.find_file(name, "truetype fonts")
+
       if (size < 0) then size = (- 655.36) * size end
      if size < 0 then
+
       ttffont = fontforge.to_table(fontforge.open(filename))
        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
         f.direction = 0
+
         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 = { }
+
        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
+
      for char, glyph in pairs(ttffont.map.map)
           local glyph_table = ttffont.glyphs[glyph]
+
        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
+
          f.characters[char].kerns = kerns
 
           end
 
           end
 
         end
 
         end
       
 
 
         f.filename = filename
 
         f.filename = filename
         f.type = "real"
+
         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...

Please note that all contributions to LuaTeXWiki are considered to be released under the GNU Free Documentation License 1.3 (see LuaTeXWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)