Skip to content

Commit

Permalink
fix(core): Move the zenkaku width (zw) unit into core
Browse files Browse the repository at this point in the history
Where it belongs.

This touches on sile-typesetter#1245.
  • Loading branch information
ctrlcctrlv committed Jun 18, 2022
1 parent b211d9d commit 2c3d1c9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
11 changes: 9 additions & 2 deletions core/base-shaper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,21 @@ SILE.shapers.base = pl.class({
return shapespace(width and width.length or items[1].width)
end,

measureChar = function (self, char)
measureChar = function (self, char, fallible)
local options = SILE.font.loadDefaults({})
options.tracking = SILE.settings:get("shaper.tracking")
local items = self:shapeToken(char, options)
local error_func
if fallible == nil or fallible == false then -- default
error_func = SU.error
else
error_func = SU.warn
end
if #items > 0 then
return { height = items[1].height, width = items[1].width }
else
SU.error("Unable to measure character", char)
error_func("Unable to measure character", char)
return nil
end
end,

Expand Down
1 change: 1 addition & 0 deletions core/measurement.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

local function _tonumber (amount)
return SU.cast("number", amount)
end
Expand Down
7 changes: 7 additions & 0 deletions core/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ function settings:_init()
help = "Skip to be added to left side of line"
})

self:declare({
parameter = "document.zenkakuchar",
default = "",
type = "string",
help = "The character measured to determine the length of a zenkaku width (全角幅)"
})

SILE.registerCommand("set", function(options, content)
local parameter = SU.required(options, "parameter", "\\set command")
local makedefault = SU.boolean(options.makedefault, false)
Expand Down
20 changes: 20 additions & 0 deletions core/units.lua
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,24 @@ units["en"] = {
definition = "0.5em"
}

-- jlreq measures distances in units of 1em, but also assumes that an em is the
-- width of a full-width character. In SILE terms it isn't: measuring an "m" in
-- a 10pt Japanese font gets you 5 points. So we measure a full-width character
-- and use that as a unit. We call it zw following ptex (zenkaku width)
units["zw"] = {
relative = true,
definition = function (v)
local zenkaku = SILE.settings:get("document.zenkakuchar")
local measure_zenkaku = SILE.shaper:measureChar(zenkaku, true)
if measure_zenkaku ~= nil then
measure_zenkaku = measure_zenkaku.width
else
measure_zenkaku = 1
SU.warn("Zenkaku width (全角幅) unit zw is falling back to 1em == 1zw as we cannot measure " .. zenkaku ..
". Either change this char to one suitable for your language, or load a font that has it.")
end
return v * measure_zenkaku
end
}

return units
11 changes: 0 additions & 11 deletions languages/ja.lua
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,5 @@ return {
SILE.call("medskip")
end)

-- jlreq measures distances in units of 1em, but also assumes that an em is the
-- width of a full-width character. In SILE terms it isn't: measuring an "m" in
-- a 10pt Japanese font gets you 5 points. So we measure a full-width character
-- and use that as a unit. We call it zw following ptex (zenkaku width)
SILE.units["zw"] = {
relative = true,
definition = function (v)
return v * SILE.shaper:measureChar("").width
end
}

end
}
1 change: 0 additions & 1 deletion packages/ruby/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ The \autodoc:package{ruby} package provides the
\autodoc:command[check=false]{\ruby[reading=<ruby text>]{<base text>}} command
which sets a piece of ruby above or beside the base text. For example:
% Unit zw throws errors if there is not way to shape あ
\font:add-fallback[family=Noto Sans CJK JP]
\set[parameter=ruby.height,value=12pt]
Expand Down

0 comments on commit 2c3d1c9

Please sign in to comment.