From e4d077496bd2765d35edd28d1714a67761557712 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Sat, 18 Jul 2020 09:04:40 +0300 Subject: [PATCH] refactor(core): Use bitwise functions shim instead of assuming bit32 --- .luacheckrc | 1 - configure.ac | 4 +++- core/harfbuzz-shaper.lua | 8 +++++--- core/sile.lua | 1 - core/utilities.lua | 9 +++++---- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index cbb3dc6aee..64b700bad2 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -25,7 +25,6 @@ globals = { "SU", "std", "pl", - "bit", "SYSTEM_SILE_PATH", "SHARED_LIB_EXT", "ProFi" diff --git a/configure.ac b/configure.ac index cb8bbf5f5a..31a11f1493 100644 --- a/configure.ac +++ b/configure.ac @@ -110,7 +110,9 @@ AX_LUA_HEADERS AX_LUA_LIBS AS_IF([test "x$with_system_luarocks" = "xyes"], [ - AX_LUA_MODULE([bit32], [bit32]) + AS_IF([test "$LUA_SHORT_VERSION" -lt 52], + AX_LUA_MODULE([bit32], [bit32]) + ) AX_LUA_MODULE([lpeg], [lpeg]) AX_LUA_MODULE([cassowary], [cassowary]) AS_IF([test "$LUA_SHORT_VERSION" -lt 53], diff --git a/core/harfbuzz-shaper.lua b/core/harfbuzz-shaper.lua index 586ba65d52..aaf6659088 100644 --- a/core/harfbuzz-shaper.lua +++ b/core/harfbuzz-shaper.lua @@ -1,6 +1,8 @@ -if not SILE.shapers then SILE.shapers = { } end local hb = require("justenoughharfbuzz") local icu = require("justenoughicu") +local bitshim = require("bitshim") + +if not SILE.shapers then SILE.shapers = { } end SILE.settings.declare({ name = "harfbuzz.subshapers", @@ -62,9 +64,9 @@ SILE.shapers.harfbuzz = pl.class({ SU.debug("fonts", "Resolved font family '"..opts.family.."' -> "..(face and face.filename)) if not face or not face.filename then SU.error("Couldn't find face '"..opts.family.."'") end if SILE.makeDeps then SILE.makeDeps:add(face.filename) end - if bit32.rshift(face.index, 16) ~= 0 then + if bitshim.rshift(face.index, 16) ~= 0 then SU.warn("GX feature in '"..opts.family.."' is not supported, fallback to regular font face.") - face.index = bit32.band(face.index, 0xff) + face.index = bitshim.band(face.index, 0xff) end local fh, err = io.open(face.filename, "rb") if err then SU.error("Can't open font file '"..face.filename.."': "..err) end diff --git a/core/sile.lua b/core/sile.lua index 5b3ac97876..ca9b34753d 100644 --- a/core/sile.lua +++ b/core/sile.lua @@ -1,7 +1,6 @@ -- Initialize Lua environment and global utilities local lua_version = _VERSION:sub(-3) if lua_version < "5.3" then require("compat53") end -- Backport of lots of Lua 5.3 features to Lua 5.[12] -bit32 = bit32 or require("bit32") -- Backport of Lua 5.2+ bitwise functions to Lua 5.1 pl = require("pl.import_into")() -- Penlight on-demand module loader if (os.getenv("SILE_COVERAGE")) then require("luacov") end diff --git a/core/utilities.lua b/core/utilities.lua index 3ed51046b5..9038b8e47c 100644 --- a/core/utilities.lua +++ b/core/utilities.lua @@ -1,3 +1,4 @@ +local bitshim = require("bitshim") local utilities = {} local epsilon = 1E-12 @@ -314,9 +315,9 @@ utilities.codepoint = function (uchar) seq = c < 0x80 and 1 or c < 0xE0 and 2 or c < 0xF0 and 3 or c < 0xF8 and 4 or --c < 0xFC and 5 or c < 0xFE and 6 or error("invalid UTF-8 character sequence") - val = bit32.band(c, 2^(8-seq) - 1) + val = bitshim.band(c, 2^(8-seq) - 1) else - val = bit32.bor(bit32.lshift(val, 6), bit32.band(c, 0x3F)) + val = bitshim.bor(bitshim.lshift(val, 6), bitshim.band(c, 0x3F)) end seq = seq - 1 end @@ -403,10 +404,10 @@ utilities.splitUtf8 = function (str) -- Return an array of UTF8 strings each rep seq = c < 0x80 and 1 or c < 0xE0 and 2 or c < 0xF0 and 3 or c < 0xF8 and 4 or --c < 0xFC and 5 or c < 0xFE and 6 or error("invalid UTF-8 character sequence") - val = bit32.band(c, 2^(8-seq) - 1) + val = bitshim.band(c, 2^(8-seq) - 1) this = this .. str[i] else - val = bit32.bor(bit32.lshift(val, 6), bit32.band(c, 0x3F)) + val = bitshim.bor(bitshim.lshift(val, 6), bitshim.band(c, 0x3F)) this = this .. str[i] end seq = seq - 1