diff options
Diffstat (limited to 'macros/luatex/generic/luavlna/luavlna.lua')
-rw-r--r-- | macros/luatex/generic/luavlna/luavlna.lua | 38 |
1 files changed, 34 insertions, 4 deletions
diff --git a/macros/luatex/generic/luavlna/luavlna.lua b/macros/luatex/generic/luavlna/luavlna.lua index dea4c052eb..d1eaa6f004 100644 --- a/macros/luatex/generic/luavlna/luavlna.lua +++ b/macros/luatex/generic/luavlna/luavlna.lua @@ -14,6 +14,7 @@ local glue_id = node.id "glue" local glyph_id = node.id "glyph" local hlist_id = node.id "hlist" local vlist_id = node.id "vlist" +local penalty_id = node.id "penalty" local math_id = node.id "math" local period_char = string.byte(".") @@ -224,6 +225,7 @@ local function prevent_single_letter (head) local word = "" local no_predegrees = M.no_predegrees local no_sufdegrees = M.no_sufdegrees + local no_initials = M.no_initials local in_math = false while head do local id = head.id @@ -284,7 +286,7 @@ local function prevent_single_letter (head) space = false -- handle initials -- uppercase letter followed by period (code 46) - elseif init and head.id == glyph_id and head.char == period_char and nextn.id == glue_id and utf_len(word) == 1 then + elseif no_initials~=true and init and head.id == glyph_id and head.char == period_char and nextn.id == glue_id and utf_len(word) == 1 then head = insert_penalty(head) elseif head.id == glyph_id then local char = utf_char(head.char) @@ -305,20 +307,48 @@ local function prevent_single_letter (head) return true end +local hyphenate_langs = {} +local hyphenchar = string.byte("-") +-- don't break if the next or previous char is hyphen, in order to support ligaturing of dashes +local is_hyphen_ligature = function(n) + local next_node = n.next + -- split_hyphens code + if next_node and next_node.id == glyph_id and next_node.char == hyphenchar then + return true + end + local prev_node = n.prev + if prev_node and prev_node.id == glyph_id and prev_node.char == hyphenchar then + return true + end + return false +end + +-- detect if the current character is a hyphen +local function is_breakable_hyphen(n) + return n.id == glyph_id and hyphenate_langs[n.lang] and n.char == hyphenchar +end + +-- don't process hyphens if the next node is penalty +-- necessary to support \nobreakdash +local function is_next_penalty(n) + local next_node = n.next + if next_node and next_node.id == penalty_id then + return true + end +end + -- Enable hyphenation of words that contain hyphens -- and repeating of the explicit hyphen on a new line when -- the hyphen is hyphenated -- It should be used in the `hyphenate` callback -- based on ShreewatsaR's code from: -- https://tex.stackexchange.com/a/417883/2891 -local hyphenate_langs = {} local break_hyphens = function(head, tail) - local hyphenchar = string.byte("-") local glyph_id = node.id("glyph") local n = head while n do local skip = node.has_attribute(n, M.preventsingleid) - if skip ~= 1 and n.id == glyph_id and hyphenate_langs[n.lang] and n.char == hyphenchar then + if skip ~= 1 and is_breakable_hyphen(n) and not is_next_penalty(n) and not is_hyphen_ligature(n) then -- Insert an infinite penalty before, and a zero-width glue node after, the hyphen. -- Like writing "\nobreak-\hspace{0pt}" or equivalently "\penalty10000-\hskip0pt" local p = node.new(node.id('penalty')) |