diff options
author | Karl Berry <karl@freefriends.org> | 2017-04-19 23:04:15 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2017-04-19 23:04:15 +0000 |
commit | f74161ca4e7eb3c5af9afcbe92522ae3b58987c1 (patch) | |
tree | 3c30a13df5ac7968195c85070a2b074578e669d2 /Master/texmf-dist/tex/context/base/mkiv/lang-dis.lua | |
parent | 1d5a55ac8f5506a7ae9cfca52f4b588182e199d4 (diff) |
context beta/cont-tmf.zip (Apr 16 12:55)
git-svn-id: svn://tug.org/texlive/trunk@43931 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/context/base/mkiv/lang-dis.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/mkiv/lang-dis.lua | 320 |
1 files changed, 208 insertions, 112 deletions
diff --git a/Master/texmf-dist/tex/context/base/mkiv/lang-dis.lua b/Master/texmf-dist/tex/context/base/mkiv/lang-dis.lua index 84d9b2d5ba8..e2c0d220ebe 100644 --- a/Master/texmf-dist/tex/context/base/mkiv/lang-dis.lua +++ b/Master/texmf-dist/tex/context/base/mkiv/lang-dis.lua @@ -12,7 +12,9 @@ local nodes = nodes local tasks = nodes.tasks local nuts = nodes.nuts -local nodepool = nuts.pool + +local enableaction = tasks.enableaction +local setaction = tasks.setaction local tonode = nuts.tonode local tonut = nuts.tonut @@ -27,22 +29,31 @@ local getattr = nuts.getattr local getsubtype = nuts.getsubtype local setsubtype = nuts.setsubtype local getchar = nuts.getchar +local setchar = nuts.setchar local getdisc = nuts.getdisc local setdisc = nuts.setdisc +local getlang = nuts.setlang +local getboth = nuts.getboth +local setlist = nuts.setlist +local setlink = nuts.setlink local isglyph = nuts.isglyph local copy_node = nuts.copy -local free_node = nuts.free local remove_node = nuts.remove local traverse_id = nuts.traverse_id -local traverse_nodes = nuts.traverse +local flush_list = nuts.flush_list +local flush_node = nuts.flush_node local nodecodes = nodes.nodecodes local disccodes = nodes.disccodes local disc_code = nodecodes.disc local glyph_code = nodecodes.glyph + local discretionary_code = disccodes.discretionary +local explicit_code = disccodes.explicit +local automatic_code = disccodes.automatic +local regular_code = disccodes.regular local a_visualize = attributes.private("visualizediscretionary") local setattribute = tex.setattribute @@ -51,135 +62,158 @@ local getlanguagedata = languages.getdata local check_regular = true -local expanders = { - [disccodes.discretionary] = function(d,template) - -- \discretionary - return template - end, - [disccodes.explicit] = function(d,template) - -- \- - local pre, post, replace = getdisc(d) - local done = false - if pre then - local char = isglyph(pre) - if char and char <= 0 then - done = true - pre = nil +local expanders -- this will go away + +-- the penalty has been determined by the mode (currently we force 1): +-- +-- 0 : exhyphenpenalty +-- 1 : hyphenpenalty +-- 2 : automatichyphenpenalty +-- +-- following a - : the pre and post chars are already appended and set +-- so we have pre=preex and post=postex .. however, the previous +-- hyphen is already injected ... downside: the font handler sees this +-- so this is another argument for doing a hyphenation pass in context + +if LUATEXVERSION < 1.005 then + + expanders = { + [discretionary_code] = function(d,template) + -- \discretionary + return template + end, + [explicit_code] = function(d,template) + -- \- + local pre, post, replace = getdisc(d) + local done = false + if pre then + local char = isglyph(pre) + if char and char <= 0 then + done = true + flush_list(pre) + pre = nil + end end - end - if post then - local char = isglyph(post) - if char and char <= 0 then - done = true - post = nil + if post then + local char = isglyph(post) + if char and char <= 0 then + done = true + flush_list(post) + post = nil + end end - end - if done then - setdisc(d,pre,post,replace,discretionary_code,tex.exhyphenpenalty) - end - return template - end, - [disccodes.automatic] = function(d,template) - -- following a - : the pre and post chars are already appended and set - -- so we have pre=preex and post=postex .. however, the previous - -- hyphen is already injected ... downside: the font handler sees this - -- so this is another argument for doing a hyphenation pass in context - local pre, post, replace = getdisc(d) - if pre then - -- we have a preex characters and want that one to replace the - -- character in front which is the trigger - if not template then - -- can there be font kerns already? - template = getprev(d) - if template and getid(template) ~= glyph_code then - template = getnext(d) + if done then + -- todo: take existing penalty + setdisc(d,pre,post,replace,explicit_code,tex.exhyphenpenalty) + else + setsubtype(d,explicit_code) + end + return template + end, + [automatic_code] = function(d,template) + local pre, post, replace = getdisc(d) + if pre then + -- we have a preex characters and want that one to replace the + -- character in front which is the trigger + if not template then + -- can there be font kerns already? + template = getprev(d) if template and getid(template) ~= glyph_code then - template = nil + template = getnext(d) + if template and getid(template) ~= glyph_code then + template = nil + end end end - end - if template then - local pseudohead = getprev(template) - if pseudohead then - while template ~= d do - pseudohead, template, removed = remove_node(pseudohead,template) - -- free old replace ? - replace = removed - -- break ? + if template then + local pseudohead = getprev(template) + if pseudohead then + while template ~= d do + pseudohead, template, removed = remove_node(pseudohead,template) + -- free old replace ? + replace = removed + -- break ? + end + else + -- can't happen end + setdisc(d,pre,post,replace,automatic_code,tex.hyphenpenalty) else - -- can't happen + -- print("lone regular discretionary ignored") end - setdisc(d,pre,post,replace,discretionary_code,tex.hyphenpenalty) else - -- print("lone regular discretionary ignored") + setdisc(d,pre,post,replace,automatic_code,tex.hyphenpenalty) end - else - setdisc(d,pre,post,replace,discretionary_code,tex.hyphenpenalty) - end - return template - end, - [disccodes.regular] = function(d,template) - if check_regular then - -- simple - if not template then - -- can there be font kerns already? - template = getprev(d) - if template and getid(template) ~= glyph_code then - template = getnext(d) + return template + end, + [regular_code] = function(d,template) + if check_regular then + -- simple + if not template then + -- can there be font kerns already? + template = getprev(d) if template and getid(template) ~= glyph_code then - template = nil + template = getnext(d) + if template and getid(template) ~= glyph_code then + template = nil + end end end - end - if template then - local language = template and getfield(template,"lang") - local data = getlanguagedata(language) - local prechar = data.prehyphenchar - local postchar = data.posthyphenchar - local pre, post, replace = getdisc(d) -- pre can be set - local done = false - if prechar and prechar > 0 then - done = true - pre = copy_node(template) - setchar(pre,prechar) - end - if postchar and postchar > 0 then - done = true - post = copy_node(template) - setchar(post,postchar) - end - if done then - setdisc(d,pre,post,replace,discretionary_code,tex.hyphenpenalty) + if template then + local language = template and getlang(template) + local data = getlanguagedata(language) + local prechar = data.prehyphenchar + local postchar = data.posthyphenchar + local pre, post, replace = getdisc(d) -- pre can be set + local done = false + if prechar and prechar > 0 then + done = true + pre = copy_node(template) + setchar(pre,prechar) + end + if postchar and postchar > 0 then + done = true + post = copy_node(template) + setchar(post,postchar) + end + if done then + setdisc(d,pre,post,replace,regular_code,tex.hyphenpenalty) + end + else + -- print("lone regular discretionary ignored") end - else - -- print("lone regular discretionary ignored") + return template end - return template - else - -- maybe also set penalty here - setsubtype(d,discretionary_code) + end, + [disccodes.first] = function() + -- forget about them + end, + [disccodes.second] = function() + -- forget about them + end, + } + + function languages.expand(d,template,subtype) + if not subtype then + subtype = getsubtype(d) end - end, - [disccodes.first] = function() - -- forget about them - end, - [disccodes.second] = function() - -- forget about them - end, -} + if subtype ~= discretionary_code then + return expanders[subtype](d,template) + end + end -languages.expanders = expanders +else -function languages.expand(d,template,subtype) - if not subtype then - subtype = getsubtype(d) - end - if subtype ~= discretionary_code then - return expanders[subtype](d,template) + function languages.expand() + -- nothing to be fixed end + end +languages.expanders = expanders + +-- -- -- -- -- + local setlistcolor = nodes.tracers.colors.setlist function languages.visualizediscretionaries(head) @@ -206,7 +240,7 @@ function languages.showdiscretionaries(v) setattribute(a_visualize,unsetvalue) else -- also nil if not enabled then - nodes.tasks.enableaction("processors","languages.visualizediscretionaries") + enableaction("processors","languages.visualizediscretionaries") enabled = true end setattribute(a_visualize,1) @@ -229,3 +263,65 @@ function languages.serializediscretionary(d) -- will move to tracer ) end +-- -- + +local wiped = 0 + +local function wipe(head,delayed) + local p, n = getboth(delayed) + local _, _, h, _, _, t = getdisc(delayed,true) + if p or n then + if h then + setlink(p,h) + setlink(t,n) + setfield(delayed,"replace") + else + setlink(p,n) + end + end + if head == delayed then + head = h + end + wiped = wiped + 1 + flush_node(delayed) + return head +end + +function languages.flatten(head) + local nuthead = tonut(head) + local delayed = nil + for d in traverse_id(disc_code,nuthead) do + if delayed then + nuthead = wipe(nuthead,delayed) + end + delayed = d + end + if delayed then + return tonode(wipe(nuthead,delayed)), true + else + return head, false + end +end + +function languages.nofflattened() + return wiped -- handy for testing +end + +-- experiment + +local flatten = languages.flatten +local getlist = nodes.getlist + +nodes.handlers.flattenline = flatten + +function nodes.handlers.flatten(head,where) + if head and (where == "box" or where == "adjusted_hbox") then + return flatten(head) + end + return true +end + +directives.register("hyphenator.flatten",function(v) + setaction("processors","nodes.handlers.flatten",v) + setaction("contributers","nodes.handlers.flattenline",v) +end) |