Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn when asked to measure a character not shaped in a font #2193

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/font.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ SILE.registerCommand("font", function (options, content)
-- We must *actually* load the font here, because by the time we're inside
-- SILE.shaper.shapeToken, it's too late to respond appropriately to things
-- that the post-load hook might want to do.
SILE.font.cache(SILE.font.loadDefaults({}), SILE.shaper.getFace)
SILE.font.cache(SILE.font.loadDefaults(options), SILE.shaper.getFace)

if SU.ast.hasContent(content) then
SILE.process(content)
Expand Down
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code (even before this PR) seems to assume that a character will be mapped to only a single glyph, but this is not true in general.

else
SU.error("Unable to measure character", char)
end
Expand Down
7 changes: 4 additions & 3 deletions types/unit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,17 @@ 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.dump{found, measurable, zenkaku, zenkakuchar}
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
Loading