Skip to content

Commit

Permalink
feat(shapers): Warn when asked to measure a character not shaped in a…
Browse files Browse the repository at this point in the history
… font
  • Loading branch information
alerque committed Dec 11, 2024
1 parent 2adf631 commit e0b5cd8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
5 changes: 4 additions & 1 deletion packages/math/base-elements.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions shapers/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions types/unit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,16 @@ 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
Either change this char to one suitable for your language, or load a font that
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,
}
Expand Down

0 comments on commit e0b5cd8

Please sign in to comment.