From e0b5cd88fd326d19d47cd4cf17aae2ea74db1cfa Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Mon, 9 Dec 2024 16:55:55 +0300 Subject: [PATCH] feat(shapers): Warn when asked to measure a character not shaped in a font --- packages/math/base-elements.lua | 5 ++++- shapers/base.lua | 5 +++-- types/unit.lua | 6 +++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/math/base-elements.lua b/packages/math/base-elements.lua index 0aa9bb17c..0bc84fdc9 100644 --- a/packages/math/base-elements.lua +++ b/packages/math/base-elements.lua @@ -1625,7 +1625,10 @@ function elements.sqrt:shape () -- Note: In TeX, the radical sign extends a lot below the baseline, -- and MathML Core also has a lot of layout text about it. -- Not only it doesn't look good, but it's not very clear vs. OpenType. - local radicalGlyph = SILE.shaper:measureChar("√") + local radicalGlyph, found = SILE.shaper:measureChar("√") + if not found then + SU.error("Math font does not contain a square root glyph") + end local ratio = (self.radicand.height:tonumber() + self.radicand.depth:tonumber()) / (radicalGlyph.height + radicalGlyph.depth) local vertAdHocOffset = (ratio > 1 and math.log(ratio) or 0) * self.radicalVerticalGap diff --git a/shapers/base.lua b/shapers/base.lua index 9a0a08063..2cad46c42 100644 --- a/shapers/base.lua +++ b/shapers/base.lua @@ -67,8 +67,9 @@ function shaper:measureChar (char) local options = SILE.font.loadDefaults({}) options.tracking = SILE.settings:get("shaper.tracking") local items = self:shapeToken(char, options) - if #items > 0 then - return { height = items[1].height, width = items[1].width, depth = items[1].depth } + if items and items[1] then + local item = items[1] + return item, item.gid ~= 0 else SU.error("Unable to measure character", char) end diff --git a/types/unit.lua b/types/unit.lua index 169de78a9..93d66c988 100644 --- a/types/unit.lua +++ b/types/unit.lua @@ -223,8 +223,8 @@ unittypes["zw"] = { relative = true, definition = function (v) local zenkakuchar = SILE.settings:get("document.zenkakuchar") - local measurable, zenkaku = pcall(SILE.shaper.measureChar, SILE.shaper, zenkakuchar) - if not measurable then + local measurable, zenkaku, found = pcall(SILE.shaper.measureChar, SILE.shaper, zenkakuchar) + if not found or not measurable then SU.warn(([[ Zenkaku width (全角幅) unit zw is falling back to 1em == 1zw as we cannot measure %s @@ -232,7 +232,7 @@ unittypes["zw"] = { has it. ]]):format(zenkakuchar)) end - local width = measurable and zenkaku.width or SILE.settings:get("font.size") + local width = found and measurable and zenkaku.width or SILE.settings:get("font.size") return v * width end, }