summaryrefslogtreecommitdiff
path: root/tex/context/modules/mkiv/s-languages-hyphenation.lua
diff options
context:
space:
mode:
authorContext Git Mirror Bot <phg42.2a@gmail.com>2016-05-07 15:20:36 +0200
committerContext Git Mirror Bot <phg42.2a@gmail.com>2016-05-07 15:20:36 +0200
commit00a715fb30c2765bffae1bc07a9249359aeea38a (patch)
tree70f9bff572256afbb15bf15a0ffaae3708f965c4 /tex/context/modules/mkiv/s-languages-hyphenation.lua
parentb162038956eefaaa786603e8faceff35016bdc82 (diff)
downloadcontext-00a715fb30c2765bffae1bc07a9249359aeea38a.tar.gz
2016-05-07 14:41:00
Diffstat (limited to 'tex/context/modules/mkiv/s-languages-hyphenation.lua')
-rw-r--r--tex/context/modules/mkiv/s-languages-hyphenation.lua154
1 files changed, 117 insertions, 37 deletions
diff --git a/tex/context/modules/mkiv/s-languages-hyphenation.lua b/tex/context/modules/mkiv/s-languages-hyphenation.lua
index c5a4f91f1..b8de773a0 100644
--- a/tex/context/modules/mkiv/s-languages-hyphenation.lua
+++ b/tex/context/modules/mkiv/s-languages-hyphenation.lua
@@ -11,101 +11,182 @@ moduledata.languages.hyphenation = moduledata.languages.hyphenation or { }
local a_colormodel = attributes.private('colormodel')
+local tex = tex
+local context = context
+
local nodecodes = nodes.nodecodes
-local nodepool = nodes.pool
+local nuts = nodes.nuts
+local nodepool = nuts.pool
+
local disc_code = nodecodes.disc
local glyph_code = nodecodes.glyph
+
local emwidths = fonts.hashes.emwidths
local exheights = fonts.hashes.exheights
+
local newkern = nodepool.kern
local newrule = nodepool.rule
local newglue = nodepool.glue
-local insert_node_after = node.insert_after
-local traverse_by_id = node.traverse_id
-local hyphenate = languages.hyphenators.handler -- lang.hyphenate
-local find_tail = node.tail
-local remove_node = nodes.remove
+local insert_node_after = nuts.insert_after
+local traverse_by_id = nuts.traverse_id
+
+local tonut = nodes.tonut
+local tonode = nodes.tonode
+local getid = nuts.getid
+local getnext = nuts.getnext
+local getdisc = nuts.getdisc
+local getattr = nuts.getattr
+local getfont = nuts.getfont
+local getfield = nuts.getfield
+local setlink = nuts.setlink
+local setdisc = nuts.setdisc
+local setfield = nuts.setfield
+local free_node = nuts.free
local tracers = nodes.tracers
local colortracers = tracers and tracers.colors
local setnodecolor = colortracers.set
+-- maybe this will become code code
+
+local states = table.setmetatableindex(function(t,k)
+ return {
+ lefthyphenmin = tex.lefthyphenmin,
+ righthyphenmin = tex.righthyphenmin,
+ hyphenationmin = tex.hyphenationmin,
+ prehyphenchar = tex.prehyphenchar,
+ posthyphenchar = tex.posthyphenchar,
+ }
+end)
+
+interfaces.implement {
+ name = "storelanguagestate",
+ actions = function()
+ states[tex.language] = {
+ lefthyphenmin = tex.lefthyphenmin,
+ righthyphenmin = tex.righthyphenmin,
+ hyphenationmin = tex.hyphenationmin,
+ prehyphenchar = tex.prehyphenchar,
+ posthyphenchar = tex.posthyphenchar,
+ }
+ end
+}
+
+function moduledata.languages.getstate(l)
+ return states[l] -- code
+end
+
+-- end
+
local function identify(head,marked)
- local current, prev = head, nil
+ local current = tonut(head)
+ local prev = nil
while current do
- local id = current.id
- local next = current.next
+ local id = getid(current)
+ local next = getnext(current)
if id == disc_code then
- if prev and next then -- and next.id == glyph_code then -- catch other usage of disc
+ if prev and next then -- asume glyphs
marked[#marked+1] = prev
+ local pre, post, replace, pre_tail, post_tail, replace_tail = getdisc(current,true)
+ if replace then
+ setlink(prev,replace)
+ setlink(replace_tail,next)
+ setdisc(pre,post,nil)
+ prev = tail
+ else
+ setlink(prev,next)
+ end
+ free_node(current)
end
elseif id == glyph_code then
prev = current
+ else
+ prev = nil
end
current = next
end
end
-local function strip(head,marked)
+local function mark(head,marked,w,h,d,how)
+ head = tonut(head)
for i=1,#marked do
- local prev = marked[i]
- remove_node(head,prev.next,true)
+ local current = marked[i]
+ local font = getfont(current)
+ local em = emwidths[font]
+ local ex = exheights[font]
+ local width = w*em
+ local rule = newrule(width,h*ex,d*ex)
+ head, current = insert_node_after(head,current,newkern(-width/2))
+ head, current = insert_node_after(head,current,rule)
+ head, current = insert_node_after(head,current,newkern(-width/2))
+ head, current = insert_node_after(head,current,newglue(0))
+ setnodecolor(rule,how) -- ,getattr(current,a_colormodel))
end
end
-local function mark(head,marked,w,h,d,how)
- for i=1,#marked do
- local prev = marked[i]
- local font = prev.font
- local em = emwidths[font]
- local ex = exheights[font]
- local width = w*em
- local rule = newrule(width,h*ex,d*ex)
- head, prev = insert_node_after(head,prev,newkern(-width/2))
- head, prev = insert_node_after(head,prev,rule)
- head, prev = insert_node_after(head,prev,newkern(-width/2))
- head, prev = insert_node_after(head,prev,newglue(0))
- setnodecolor(rule,how,prev[a_colormodel])
+local function getlanguage(head,l,left,right)
+ local t = { }
+ for n in traverse_by_id(glyph_code,tonut(head)) do
+ t[n] = {
+ getfield(n,"lang"),
+ getfield(n,"left"),
+ getfield(n,"right"),
+ }
end
end
-local langs, tags, noflanguages = { }, { }, 0
-
-local colorbytag = false
+local langs = { }
+local tags = { }
+local noflanguages = 0
+local colorbytag = false
function moduledata.languages.hyphenation.showhyphens(head)
if noflanguages > 0 then
local marked = { }
+ local cached = { }
+ -- somehow assigning -1 fails
+ for n in traverse_by_id(glyph_code,tonut(head)) do
+ cached[n] = {
+ getfield(n,"lang"),
+ getfield(n,"left"),
+ getfield(n,"right")
+ }
+ end
for i=1,noflanguages do
local m = { }
local l = langs[i]
+ local s = states[l]
marked[i] = m
- for n in traverse_by_id(glyph_code,head) do
- n.lang = l
+ local lmin = s.lefthyphenmin
+ local rmin = s.righthyphenmin
+ for n in next, cached do
+ setfield(n,"lang",l)
+ setfield(n,"left",lmin)
+ setfield(n,"right",rmin)
end
languages.hyphenators.methods.original(head)
identify(head,m)
- strip(head,m)
end
for i=noflanguages,1,-1 do
local l = noflanguages - i + 1
mark(head,marked[i],1/16,l/2,l/4,"hyphenation:"..(colorbytag and tags[i] or i))
end
+ for n, d in next, cached do
+ setfield(n,"lang",d[1])
+ setfield(n,"left",d[2])
+ setfield(n,"right",d[3])
+ end
return head, true
else
return head, false
end
end
-local savedlanguage
-
function moduledata.languages.hyphenation.startcomparepatterns(list)
if list and list ~= "" then
tags = utilities.parsers.settings_to_array(list)
end
- savedlanguage = tex.language
- tex.language = 0
noflanguages = #tags
for i=1,noflanguages do
langs[i] = tags[i] and languages.getnumber(tags[i])
@@ -115,7 +196,6 @@ end
function moduledata.languages.hyphenation.stopcomparepatterns()
noflanguages = 0
- tex.language = savedlanguage or tex.language
nodes.tasks.disableaction("processors","moduledata.languages.hyphenation.showhyphens")
end