[latex3-commits] [git/LaTeX3-latex3-luaotfload] harfnode-dev: No more harf-base (3208ae3)

Marcel Fabian Krüger tex at 2krueger.de
Thu Oct 10 13:20:01 CEST 2019


Repository : https://github.com/latex3/luaotfload
On branch  : harfnode-dev
Link       : https://github.com/latex3/luaotfload/commit/3208ae30fd0ebd9273e1fc2ecbfd600d9c79bbcc

>---------------------------------------------------------------

commit 3208ae30fd0ebd9273e1fc2ecbfd600d9c79bbcc
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date:   Thu Oct 10 13:20:01 2019 +0200

    No more harf-base
    
    First this is trying to avoid messing up the luaharfbuzz table. It is a
    built-in library, we should not modify it.
    
    Also texlig/texrep where only used once anyway, so they can be made
    local. We really don't want people to mess with them.
    
    Also moves CH_GID_PREFIX to a font specific gid_offset which happens to
    be constant. The font table is available anyway wherever this is used,
    so might actually be faster and it allows to have different values in
    different fonts in the future. (E.g. someone might want to use valid Unicode
    codepoints in the PUA for fonts where these are available)
    Also cahnges the nameing from PREFIX to offset. No deep reason for that,
    it just feels more natural. The value changed from 0x110100 to 0x120000
    to make it easier to see the GID if the value is given in hex. Also it
    allows to add additional glyphs at additional planes.


>---------------------------------------------------------------

3208ae30fd0ebd9273e1fc2ecbfd600d9c79bbcc
 src/harf/harf-base.lua       | 40 ----------------------------------------
 src/harf/harf-load.lua       | 22 ++++++++++++++++++----
 src/harf/harf-luaotfload.lua |  8 ++------
 src/harf/harf-node.lua       | 20 +++++++++++++++-----
 4 files changed, 35 insertions(+), 55 deletions(-)

diff --git a/src/harf/harf-base.lua b/src/harf/harf-base.lua
deleted file mode 100644
index e0d2299..0000000
--- a/src/harf/harf-base.lua
+++ /dev/null
@@ -1,40 +0,0 @@
-local hb = require("luaharfbuzz")
-
--- The engine’s TFM structure indexes glyphs by character codes, which means
--- all our glyphs need character codes. We fake them by adding the maximum
--- possible Unicode code point to the glyph id. This way we have a simple
--- mapping for all glyphs without interfering with any valid Unicode code
--- point.
---
--- The engine uses the first 256 code points outside valid Unicode code space
--- for escaping raw bytes, so we skip them in our prefix.
-hb.CH_GID_PREFIX = 0x110000 + 256
-
--- Legacy TeX Input Method Disguised as Font Ligatures hack.
---
--- Single replacements, keyed by character to replace. Handled separately
--- because TeX ligaturing mechanism does not support one-to-one replacements.
-local trep = {
-  [0x0022] = 0x201D, -- ["]
-  [0x0027] = 0x2019, -- [']
-  [0x0060] = 0x2018, -- [`]
-}
-
--- Ligatures. The value is a character "ligature" table as described in the
--- manual.
-local tlig ={
-  [0x2013] = { [0x002D] = { char = 0x2014 } }, -- [---]
-  [0x002D] = { [0x002D] = { char = 0x2013 } }, -- [--]
-  [0x0060] = { [0x0060] = { char = 0x201C } }, -- [``]
-  [0x0027] = { [0x0027] = { char = 0x201D } }, -- ['']
-  [0x0021] = { [0x0060] = { char = 0x00A1 } }, -- [!`]
-  [0x003F] = { [0x0060] = { char = 0x00BF } }, -- [?`]
-  [0x002C] = { [0x002C] = { char = 0x201E } }, -- [,,]
-  [0x003C] = { [0x003C] = { char = 0x00AB } }, -- [<<]
-  [0x003E] = { [0x003E] = { char = 0x00BB } }, -- [>>]
-}
-
-hb.texrep = trep
-hb.texlig = tlig
-
-return hb
diff --git a/src/harf/harf-load.lua b/src/harf/harf-load.lua
index 85bf9ab..0c8911e 100644
--- a/src/harf/harf-load.lua
+++ b/src/harf/harf-load.lua
@@ -1,4 +1,4 @@
-local hb = require("harf-base")
+local hb = luaharfbuzz or require'luaharfbuzz'
 
 local hbfonts = hb.fonts
 local hbfonts = hbfonts or {}
@@ -134,6 +134,7 @@ local function loadfont(spec)
     end
 
     data = {
+      gid_offset = 0x120000,
       face = hbface,
       font = hbfont,
       upem = upem,
@@ -162,7 +163,19 @@ local function sanitize(psname)
   return psname:gsub('[][\0-\32\127-\255(){}<>/%%]', '-')
 end
 
-local tlig = hb.texlig
+-- Ligatures. The value is a character "ligature" table as described in the
+-- manual.
+local tlig ={
+  [0x2013] = { [0x002D] = { char = 0x2014 } }, -- [---]
+  [0x002D] = { [0x002D] = { char = 0x2013 } }, -- [--]
+  [0x0060] = { [0x0060] = { char = 0x201C } }, -- [``]
+  [0x0027] = { [0x0027] = { char = 0x201D } }, -- ['']
+  [0x0021] = { [0x0060] = { char = 0x00A1 } }, -- [!`]
+  [0x003F] = { [0x0060] = { char = 0x00BF } }, -- [?`]
+  [0x002C] = { [0x002C] = { char = 0x201E } }, -- [,,]
+  [0x003C] = { [0x003C] = { char = 0x00AB } }, -- [<<]
+  [0x003E] = { [0x003E] = { char = 0x00BB } }, -- [>>]
+}
 
 local function scalefont(data, spec)
   local size = spec.size
@@ -174,6 +187,7 @@ local function scalefont(data, spec)
   local hbfont = data.font
   local upem = data.upem
   local space = data.space
+  local gid_offset = data.gid_offset
 
   if size < 0 then
     size = -655.36 * size
@@ -188,7 +202,7 @@ local function scalefont(data, spec)
   local glyphs = data.glyphs
   local characters = {}
   for gid, glyph in next, glyphs do
-    characters[hb.CH_GID_PREFIX + gid] = {
+    characters[gid_offset + gid] = {
       index  = gid,
       width  = glyph.width  * scale,
       height = glyph.height * scale,
@@ -199,7 +213,7 @@ local function scalefont(data, spec)
 
   local unicodes = data.unicodes
   for uni, gid in next, unicodes do
-    characters[uni] = characters[hb.CH_GID_PREFIX + gid]
+    characters[uni] = characters[gid_offset + gid]
   end
 
   -- Select font palette, we support `palette=index` option, and load the first
diff --git a/src/harf/harf-luaotfload.lua b/src/harf/harf-luaotfload.lua
index 2e45cff..96c98d2 100644
--- a/src/harf/harf-luaotfload.lua
+++ b/src/harf/harf-luaotfload.lua
@@ -1,8 +1,4 @@
-if not pcall(require, "luaharfbuzz") then
-  luatexbase.module_error("harf", "'luaharfbuzz' module is required.")
-end
-
-local harf = require("harf-base")
+local harf = luaharfbuzz or require'luaharfbuzz'
 
 local define_font = require("harf-load")
 local harf_node   = require("harf-node")
@@ -155,7 +151,7 @@ aux.slot_of_name = function(fontid, glyphname, unsafe)
 
     local gid = hbfont:get_glyph_from_name(glyphname)
     if gid ~= nil then
-      return nominals[gid] or gid + harf.CH_GID_PREFIX
+      return nominals[gid] or gid + hbshared.gid_offset
     end
     return nil
   end
diff --git a/src/harf/harf-node.lua b/src/harf/harf-node.lua
index b6f2ddd..576b446 100644
--- a/src/harf/harf-node.lua
+++ b/src/harf/harf-node.lua
@@ -1,4 +1,4 @@
-local hb = require("harf-base")
+local hb = luaharfbuzz or require'luaharfbuzz'
 
 local assert            = assert
 local next              = next
@@ -146,7 +146,15 @@ end
 
 local process
 
-local trep = hb.texrep
+-- Legacy TeX Input Method Disguised as Font Ligatures hack.
+--
+-- Single replacements, keyed by character to replace. Handled separately
+-- because TeX ligaturing mechanism does not support one-to-one replacements.
+local trep = {
+  [0x0022] = 0x201D, -- ["]
+  [0x0027] = 0x2019, -- [']
+  [0x0060] = 0x2018, -- [`]
+}
 
 local function itemize(head, fontid, direction)
   local fontdata = font.getfont(fontid)
@@ -585,6 +593,7 @@ local function tonodes(head, node, run, glyphs, color)
   local nominals = hbshared.nominals
   local hbfont = hbshared.font
   local fontglyphs = hbshared.glyphs
+  local gid_offset = hbshared.gid_offset
   local rtl = dir:is_backward()
   local lastprops
 
@@ -610,7 +619,7 @@ local function tonodes(head, node, run, glyphs, color)
       nodeindex = glyph.cluster + 1
     end
     local gid = glyph.codepoint
-    local char = nominals[gid] or hb.CH_GID_PREFIX + gid
+    local char = nominals[gid] or gid_offset + gid
     local id = getid(node)
     local nchars, nglyphs = glyph.nchars, glyph.nglyphs
 
@@ -918,12 +927,13 @@ local function set_tounicode()
       local hbshared = hbdata.shared
       local glyphs = hbshared.glyphs
       local nominals = hbshared.nominals
+      local gid_offset = hbshared.gid_offset
       for gid = 0, #glyphs do
         local glyph = glyphs[gid]
         if glyph.used then
           local tounicode = glyph.tounicode or "FFFD"
-          local character = characters[gid + hb.CH_GID_PREFIX]
-          newcharacters[gid + hb.CH_GID_PREFIX] = character
+          local character = characters[gid + gid_offset]
+          newcharacters[gid + gid_offset] = character
           local unicode = nominals[gid]
           if unicode then
             newcharacters[unicode] = character





More information about the latex3-commits mailing list