diff options
author | Norbert Preining <norbert@preining.info> | 2021-03-03 03:01:26 +0000 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2021-03-03 03:01:26 +0000 |
commit | 6a1383661dd7817761345a9d4ddfd9db6ba2dd1e (patch) | |
tree | dec33644ee8df10255c235853ff6ef809b223a90 /macros/luatex/generic/luatexko/luatexko.lua | |
parent | 4763ec8ceefaf2b038d591b8c15d88d081aec6c9 (diff) |
CTAN sync 202103030301
Diffstat (limited to 'macros/luatex/generic/luatexko/luatexko.lua')
-rw-r--r-- | macros/luatex/generic/luatexko/luatexko.lua | 390 |
1 files changed, 225 insertions, 165 deletions
diff --git a/macros/luatex/generic/luatexko/luatexko.lua b/macros/luatex/generic/luatexko/luatexko.lua index 8b7f15b966..22fcaf54cf 100644 --- a/macros/luatex/generic/luatexko/luatexko.lua +++ b/macros/luatex/generic/luatexko/luatexko.lua @@ -1,6 +1,6 @@ -- luatexko.lua -- --- Copyright (c) 2013-2020 Dohyun Kim <nomos at ktug org> +-- Copyright (c) 2013-2021 Dohyun Kim <nomos at ktug org> -- Soojin Nam <jsunam at gmail com> -- -- This work may be distributed and/or modified under the @@ -13,8 +13,8 @@ luatexbase.provides_module { name = 'luatexko', - date = '2020/07/27', - version = '2.9', + date = '2021/03/01', + version = '3.0', description = 'typesetting Korean with LuaTeX', author = 'Dohyun Kim, Soojin Nam', license = 'LPPL v1.3+', @@ -41,6 +41,7 @@ local noderemove = node.remove local nodeslide = node.slide local nodewrite = node.write local rangedimensions = node.rangedimensions +local set_attribute = node.set_attribute local setglue = node.setglue local setproperty = node.setproperty local unset_attribute = node.unset_attribute @@ -69,12 +70,10 @@ local tableunpack = table.unpack local add_to_callback = luatexbase.add_to_callback local attributes = luatexbase.attributes local call_callback = luatexbase.call_callback -local callback_descriptions = luatexbase.callback_descriptions local create_callback = luatexbase.create_callback local module_warning = luatexbase.module_warning local new_user_whatsit = luatexbase.new_user_whatsit local registernumber = luatexbase.registernumber -local remove_from_callback = luatexbase.remove_from_callback local function warning (...) return module_warning("luatexko", stringformat(...)) @@ -141,21 +140,23 @@ local function option_in_font (fontdata, optionname) if type(fontdata) == "number" then fontdata = get_font_data(fontdata) end - if fontdata.specification then - return fontdata.specification.features.normal[optionname] + if fontdata.shared then + return fontdata.shared.features[optionname] end end local function font_opt_dim (fd, optname) local dim = option_in_font(fd, optname) if dim then - local params + local params, m, u if type(fd) == "number" then params = getparameters(fd) else params = fd.parameters end - local m, u = dim:match"^(.+)(e[mx])%s*$" + if type(dim) == "string" then + m, u = dim:match"^(.+)(e[mx])%s*$" + end if m and u and params then if u == "em" then dim = m * params.quad @@ -473,20 +474,27 @@ end local function hangul_space_skip (curr, newfont) if curr.lang ~= nohyphen and curr.font ~= newfont then - local n = getnext(curr) - if n and n.id == glueid and n.subtype == spaceskip then - local params = getparameters(curr.font) - local oldwd, oldst, oldsh, oldsto, oldsho = getglue(n) - if params - and oldwd == params.space - and oldst == params.space_stretch - and oldsh == params.space_shrink - and oldsto == 0 - and oldsho == 0 then -- not user's spaceskip - - local newwd = font_options.hangulspaceskip[newfont] - if newwd then - setglue(n, newwd[1], newwd[2], newwd[3]) + -- fontloader's "node" mode sets space_stretch to zero + -- when the font is a monospaced font (fontspec's \setmonofont + -- command does the same thing), which we will bypass here + -- for alignment of CJK and Latin glyphs in verbatim environment. + -- See http://www.ktug.org/xe/index.php?document_srl=249772 + if not font_options.monospaced[curr.font] then + local n = getnext(curr) + if n and n.id == glueid and n.subtype == spaceskip then + local params = getparameters(curr.font) + local oldwd, oldst, oldsh, oldsto, oldsho = getglue(n) + if params + and oldwd == params.space + and oldst == params.space_stretch + and oldsh == params.space_shrink + and oldsto == 0 + and oldsho == 0 then -- not user's spaceskip + + local newwd = font_options.hangulspaceskip[newfont] + if newwd then + setglue(n, newwd[1], newwd[2], newwd[3]) + end end end end @@ -1290,6 +1298,7 @@ function luatexko.get_strike_out_down (box) else down = -0.5*ex end + local raise = font_options.charraise[f] or 0 return down - raise end @@ -1729,12 +1738,21 @@ local function fontdata_warning(activename, ...) end end +local dfltfntsize = get_font_param(fontcurrent(), "quad") or 655360 + local function process_vertical_font (fontdata) + local fullname = fontdata.fullname + + if fontdata.type == "virtual" then + fontdata_warning("vitrual."..fullname, + "Virtual font `%s' cannot be\nused for vertical writing.", fullname) + return + end + local subfont = fontdata.specification and fontdata.specification.sub local tsb_tab = get_tsb_table(fontdata.filename, subfont) if not tsb_tab then - local fullname = fontdata.fullname fontdata_warning("vertical."..fullname, "Vertical metrics table (vmtx) not found in the font\n`%s'", fullname) return @@ -1745,12 +1763,13 @@ local function process_vertical_font (fontdata) local parameters = fontdata.parameters or {} local scale = parameters.factor or 655.36 local quad = parameters.quad or 655360 + local xheight = parameters.x_height or quad/2 local ascender = parameters.ascender or quad*0.8 - local goffset = font_opt_dim(fontdata, "charraise") or - (parameters.x_height or quad/2)/2 + local goffset = xheight/2 * (dfltfntsize / quad) -- TODO? + -- declare shift amount of horizontal box inside vertical env. - fontdata.horizboxmoverightamount = goffset + fontdata.vertcharraise = goffset for i,v in pairs(fontdata.characters) do local voff = goffset - (v.width or 0)/2 @@ -1780,6 +1799,8 @@ local function process_vertical_font (fontdata) parameters.space_stretch = spacechar.width/2 parameters.space_shrink = spacechar.width/2 end + parameters.ascender = quad/2 + goffset + parameters.descender = quad/2 - goffset local res = fontdata.resources or {} local fea = shared.features or {} @@ -1822,8 +1843,9 @@ function luatexko.gethorizboxmoveright () texattribute.luatexkohanjafontattr, texattribute.luatexkofallbackfontattr } do if v and v > 0 then - local amount = get_font_data(v).horizboxmoverightamount + local amount = get_font_data(v).vertcharraise if amount then + amount = amount + (font_options.charraise[v] or 0) set_macro("luatexkohorizboxmoveright", texsp(amount).."sp") break end @@ -1833,19 +1855,25 @@ end -- charraise +local raiseattr = luatexbase.new_attribute"luatexkoraiseattr" + local function process_charraise (head) local curr = head while curr do - if curr.id == glyphid then - local f = curr.font - local raise = font_options.charraise[f] - if raise and not option_in_font(f, "vertical") then - local props = my_node_props(curr) - if not props.charraised then + local id = curr.id + if id == glyphid then + if not has_attribute(curr,raiseattr) then + local f = curr.font + local raise = font_options.charraise[f] + if raise then curr.yoffset = (curr.yoffset or 0) + raise - props.charraised = true end + set_attribute(curr, raiseattr, 1) end + elseif id == discid then + process_charraise(curr.pre) + process_charraise(curr.post) + process_charraise(curr.replace) end curr = getnext(curr) end @@ -1899,19 +1927,15 @@ local function process_fake_slant_corr (head) -- for font fallback return head end -local function process_fake_slant_font (fontdata) - local fsl = fontdata.slant +local function process_fake_slant_font (fontdata, fsl) if fsl and fsl > 0 then - fsl = fsl/1000 - local hb = is_harf(fontdata) + fontdata.slant = fsl*1000 local params = fontdata.parameters or {} - if not hb then -- hb done by luaotfload - params.slant = (params.slant or 0) + fsl*65536 -- slant per point - end + params.slant = (params.slant or 0) + fsl*65536 -- slant per point + local hb = is_harf(fontdata) local scale = hb and hb.scale or params.factor or 655.36 - local shared = fontdata.shared or {} local descrs = shared.rawdata and shared.rawdata.descriptions or {} @@ -1945,9 +1969,7 @@ end -- wrap up local pass_fun = function(...) return ... end -create_callback("luatexko_hpack_first", "data", pass_fun) create_callback("luatexko_prelinebreak_first", "data", pass_fun) -create_callback("luatexko_hpack_second", "data", pass_fun) create_callback("luatexko_prelinebreak_second", "data", pass_fun) create_callback("luatexko_do_atbegshi", "data", pass_fun) @@ -1957,141 +1979,181 @@ function luatexko.process_atbegshi (box) end end -add_to_callback("hpack_filter", function(h) +add_to_callback("pre_shaping_filter", function(h, gc) + local par = gc == "" + or gc == "vbox" + or gc == "vtop" + or gc == "insert" + or gc == "vcenter" h = process_fonts(h) - h = call_callback("luatexko_hpack_first", h) - h = call_callback("luatexko_hpack_second", h) - return process_linebreak(h) -end, "luatexko.hpack_filter.pre_rendering", 1) + h = call_callback("luatexko_prelinebreak_first", h, par) + h = call_callback("luatexko_prelinebreak_second", h, par) + return process_linebreak(h, par) +end, "luatexko.pre_shaping_filter") -add_to_callback("pre_linebreak_filter", function(h) - h = process_fonts(h) - h = call_callback("luatexko_prelinebreak_first", h, true) - h = call_callback("luatexko_prelinebreak_second", h, true) - return process_linebreak(h, true) -end, "luatexko.pre_linebreak_filter.pre_rendering", 1) - -local font_opt_procs = { - removeclassicspaces = { - luatexko_hpack_first = process_remove_spaces, - luatexko_prelinebreak_first = process_remove_spaces, - }, - interhangul = { - luatexko_hpack_second = process_interhangul, - luatexko_prelinebreak_second = process_interhangul, +local otfregister = fonts.constructors.features.otf.register + +local function activate_process (cbnam, cbfun, name) + if not active_processes[name] then + add_to_callback(cbnam, cbfun, "luatexko."..cbnam.."."..name) + active_processes[name] = true + end +end + +otfregister { + name = "removeclassicspaces", + description = "remove spaces in classic typesetting", + default = false, + manipulators = { + node = function() + activate_process("luatexko_prelinebreak_first", process_remove_spaces, "removeclassicspaces") + end, + plug = function() + activate_process("luatexko_prelinebreak_first", process_remove_spaces, "removeclassicspaces") + end, }, - interlatincjk = { - luatexko_hpack_second = process_interlatincjk, - luatexko_prelinebreak_second = process_interlatincjk, +} + +otfregister { + name = "interhangul", + description = "insert more glue between Hangul chars", + default = false, + manipulators = { + node = function() + activate_process("luatexko_prelinebreak_second", process_interhangul, "interhangul") + end, + plug = function() + activate_process("luatexko_prelinebreak_second", process_interhangul, "interhangul") + end, }, - charraise = { - hpack_filter = process_charraise, - pre_linebreak_filter = process_charraise, +} + +otfregister { + name = "interlatincjk", + description = "insert glue between CJK and Latin", + default = false, + manipulators = { + node = function() + activate_process("luatexko_prelinebreak_second", process_interlatincjk, "interlatincjk") + end, + plug = function() + activate_process("luatexko_prelinebreak_second", process_interlatincjk, "interlatincjk") + end, }, - compresspunctuations = { - hpack_filter = process_glyph_width, - pre_linebreak_filter = process_glyph_width, +} + +otfregister { + name = "charraise", + description = "raise chars", + default = false, + manipulators = { + node = function(fontdata) + activate_process("post_shaping_filter", process_charraise, "charraise") + end, + plug = function(fontdata) + activate_process("post_shaping_filter", process_charraise, "charraise") + end, }, - slant = { - hpack_filter = process_fake_slant_corr, - pre_linebreak_filter = process_fake_slant_corr, +} + +otfregister { + name = "compresspunctuations", + description = "compress width of CJK punctuations", + default = false, + manipulators = { + node = function() + activate_process("post_shaping_filter", process_glyph_width, "compresspunctuations") + end, + plug = function() + activate_process("post_shaping_filter", process_glyph_width, "compresspunctuations") + end, }, } --- luaotfload expansion feature, committed at 2020.06.08 -luatexko.plug_expansion_loaded = false -for _,v in next, fonts.handlers.otf.features.manipulators.plug do - if v.name == "expansion" then - luatexko.plug_expansion_loaded = true - break - end -end +otfregister { + name = "slant", + description = "fake slant fallback fonts", + default = false, + manipulators = { + node = function(fontdata, _, value) + process_fake_slant_font(fontdata, value) + activate_process("post_shaping_filter", process_fake_slant_corr, "slant") + end, + plug = function(fontdata, _, value) + process_fake_slant_font(fontdata, value) + activate_process("post_shaping_filter", process_fake_slant_corr, "slant") + end, + }, +} -local font_opt_procs_single = { - expansion = function(fontdata) - if not luatexko.plug_expansion_loaded and is_harf(fontdata) then - local val = option_in_font(fontdata, "expansion") - local setup = fonts.expansions.setups[val] or {} - fontdata.stretch = fontdata.stretch or (setup.stretch or 2)*10 - fontdata.shrink = fontdata.shrink or (setup.shrink or 2)*10 - fontdata.step = fontdata.step or (setup.step or .5)*10 - end - if tex.adjustspacing == 0 then - texset("global", "adjustspacing", 2) - end - end, - - protrusion = function(fontdata) - if is_harf(fontdata) then -- for now, no support in harf mode - local val = option_in_font(fontdata, "protrusion") - local setup = fonts.protrusions.setups[val] or {} - local quad = fontdata.parameters.quad - for i, v in pairs(setup) do - local chr = fontdata.characters[i] - if chr then - local wdq = chr.width/quad - chr.left_protruding = chr.left_protruding or wdq*v[1]*1000 - chr.right_protruding = chr.right_protruding or wdq*v[2]*1000 - end - end - end - if tex.protrudechars == 0 then - texset("global", "protrudechars", 2) - end - if option_in_font(fontdata, "compresspunctuations") then +otfregister { + name = "vertical", + description = "vertical typesetting", + default = false, + manipulators = { + node = process_vertical_font, + plug = function(fontdata) local fullname = fontdata.fullname - fontdata_warning("protrude."..fullname, - "Both `compresspunctuations' and `protrusion' are\nenabled for `%s'.\n".. - "Beware bad justifications.", fullname) - end - end, - - slant = process_fake_slant_font, - - vertical = function(fontdata) - local fullname = fontdata.fullname - if is_harf(fontdata) then fontdata_warning("vertical."..fullname, "Currently, vertical writing is not supported\nby harf mode.".. "`Renderer=Node' option is needed for\n`%s'", fullname) - elseif fontdata.type == "virtual" then - fontdata_warning("vitrual."..fullname, - "Virtual font `%s' cannot be\nused for vertical writing.", fullname) - else - process_vertical_font(fontdata) - end - end, + end, + }, } -local function process_patch_font (fontdata) - if type(fontdata) ~= "table" or - fontdata.format ~= "opentype" and fontdata.format ~= "truetype" then - return - end -- as we have called patch_font_unsafe for hb fonts - - for name, procs in pairs( font_opt_procs ) do - if not active_processes[name] and option_in_font(fontdata, name) then - if name == "charraise" and option_in_font(fontdata, "vertical") then - else - for cbnam, cbfun in pairs( procs ) do - add_to_callback(cbnam, cbfun, "luatexko."..cbnam.."."..name) - end - active_processes[name] = true +otfregister { + name = "expansion", + description = "glyph expansion", + default = false, + manipulators = { + node = function() + if tex.adjustspacing == 0 then + texset("global", "adjustspacing", 2) end - end - end + end, + plug = function(fontdata, _, value) + local setup = fonts.expansions.setups[value] or {} + fontdata.stretch = fontdata.stretch or (setup.stretch or 2)*10 + fontdata.shrink = fontdata.shrink or (setup.shrink or 2)*10 + fontdata.step = fontdata.step or (setup.step or .5)*10 + if tex.adjustspacing == 0 then + texset("global", "adjustspacing", 2) + end + end, + }, +} - for name, func in pairs( font_opt_procs_single ) do - if option_in_font(fontdata, name) then - func(fontdata) +local function process_protrusion (fontdata, _, value) + local setup = fonts.protrusions.setups[value] or {} + local quad = fontdata.parameters.quad + for i, v in pairs(setup) do + local chr = fontdata.characters[i] + if chr then + local wdq = chr.width/quad + chr.left_protruding = chr.left_protruding or wdq*v[1]*1000 + chr.right_protruding = chr.right_protruding or wdq*v[2]*1000 end end + if tex.protrudechars == 0 then + texset("global", "protrudechars", 2) + end + if option_in_font(fontdata, "compresspunctuations") then + local fullname = fontdata.fullname + fontdata_warning("protrude."..fullname, + "Both `compresspunctuations' and `protrusion' are\nenabled for `%s'.\n".. + "Beware bad justifications.", fullname) + end end -add_to_callback("luaotfload.patch_font", process_patch_font, -"luatexko.patch_font") -add_to_callback("luaotfload.patch_font_unsafe", process_patch_font, -"luatexko.patch_font") +otfregister { + name = "protrusion", + description = "glyph protrusion", + default = false, + manipulators = { + node = process_protrusion, + plug = process_protrusion, + }, +} local auxiliary_procs = { dotemph = { @@ -2105,11 +2167,9 @@ local auxiliary_procs = { luatexko_do_atbegshi = process_ruby_post_linebreak, }, autojosa = { - luatexko_hpack_first = process_josa, luatexko_prelinebreak_first = process_josa, }, reorderTM = { - luatexko_hpack_first = process_reorder_tonemarks, luatexko_prelinebreak_first = process_reorder_tonemarks, }, } @@ -2151,16 +2211,16 @@ end, function luatexko.deactivateall (str) luatexko.deactivated = {} - for _, name in ipairs{ "hpack_filter", + for _, name in ipairs{ "pre_shaping_filter", + "post_shaping_filter", "pre_linebreak_filter", "hyphenate", "luatexko_do_atbegshi", -- might not work properly - "luaotfload.patch_font_unsafe", -- added for harf - "luaotfload.patch_font" } do + } do local t = {} - for i, v in ipairs( callback_descriptions(name) ) do + for i, v in ipairs( luatexbase.callback_descriptions(name) ) do if v:find(str or "^luatexko%.") then - local ff, dd = remove_from_callback(name, v) + local ff, dd = luatexbase.remove_from_callback(name, v) tableinsert(t, { ff, dd, i }) end end |