diff options
Diffstat (limited to 'Master/texmf-dist/tex/context/base/typo-spa.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/typo-spa.lua | 138 |
1 files changed, 76 insertions, 62 deletions
diff --git a/Master/texmf-dist/tex/context/base/typo-spa.lua b/Master/texmf-dist/tex/context/base/typo-spa.lua index 48c7263c70e..e81a59bda95 100644 --- a/Master/texmf-dist/tex/context/base/typo-spa.lua +++ b/Master/texmf-dist/tex/context/base/typo-spa.lua @@ -13,47 +13,52 @@ local utf = unicode.utf8 local next, type = next, type local utfchar = utf.char -local trace_hspacing = false trackers.register("nodes.hspacing", function(v) trace_hspacing = v end) +local trace_spacing = false trackers.register("typesetters.spacing", function(v) trace_spacing = v end) + +local report_spacing = logs.reporter("typesetting","spacing") + +local nodes, fonts, node = nodes, fonts, node local has_attribute = node.has_attribute local unset_attribute = node.unset_attribute local insert_node_before = node.insert_before local insert_node_after = node.insert_after local remove_node = nodes.remove -local make_penalty_node = nodes.penalty -local make_glue_node = nodes.glue -local glyph = node.id("glyph") -local fontdata = fonts.identifiers -local quaddata = fonts.quads +local fonthashes = fonts.hashes +local fontdata = fonthashes.identifiers +local quaddata = fonthashes.quads -spacings = spacings or { } -spacings.mapping = spacings.mapping or { } -spacings.attribute = attributes.private("spacing") +local texattribute = tex.attribute -storage.register("spacings/mapping", spacings.mapping, "spacings.mapping") +local nodecodes = nodes.nodecodes +local glyph_code = nodecodes.glyph -function spacings.setspacing(id,char,left,right,alternative) - local mapping = spacings.mapping[id] - if not mapping then - mapping = { } - spacings.mapping[id] = mapping - end - local map = mapping[char] - if not map then - map = { } - mapping[char] = map - end - map.left, map.right, map.alternative = left, right, alternative -end +local nodepool = nodes.pool +local tasks = nodes.tasks + +local new_penalty = nodepool.penalty +local new_glue = nodepool.glue -function spacings.process(namespace,attribute,head) +typesetters = typesetters or { } +local typesetters = typesetters + +typesetters.spacings = typesetters.spacings or { } +local spacings = typesetters.spacings + +spacings.mapping = spacings.mapping or { } +local a_spacings = attributes.private("spacing") +spacings.attribute = a_spacings + +storage.register("typesetters/spacings/mapping", spacings.mapping, "typesetters.spacings.mapping") + +local function process(namespace,attribute,head) local done, mapping = false, spacings.mapping local start = head -- head is always begin of par (whatsit), so we have at least two prev nodes -- penalty followed by glue while start do - if start.id == glyph then + if start.id == glyph_code then local attr = has_attribute(start,attribute) if attr and attr > 0 then local map = mapping[attr] @@ -61,7 +66,9 @@ function spacings.process(namespace,attribute,head) map = map[start.char] unset_attribute(start,attribute) -- needed? if map then - local left, right, alternative = map.left, map.right, map.alternative + local left = map.left + local right = map.right + local alternative = map.alternative local quad = quaddata[start.font] local prev = start.prev if left and left ~= 0 and prev then @@ -72,19 +79,16 @@ function spacings.process(namespace,attribute,head) local prevprev = prev.prev local somepenalty = nodes.somepenalty(prevprev,10000) if somepenalty then - if trace_hspacing then - logs.report("spacing","removing penalty and space before %s", utfchar(start.char)) + if trace_spacing then + report_spacing("removing penalty and space before %s (left)", utfchar(start.char)) end head, _ = remove_node(head,prev,true) head, _ = remove_node(head,prevprev,true) else - local somespace = nodes.somespace(prev,true) - if somespace then - if trace_hspacing then - logs.report("spacing","removing space before %s", utfchar(start.char)) - end - head, _ = remove_node(head,prev,true) + if trace_spacing then + report_spacing("removing space before %s (left)", utfchar(start.char)) end + head, _ = remove_node(head,prev,true) end end ok = true @@ -92,11 +96,11 @@ function spacings.process(namespace,attribute,head) ok = not (nodes.somespace(prev,true) and nodes.somepenalty(prev.prev,true)) or nodes.somespace(prev,true) end if ok then - if trace_hspacing then - logs.report("spacing","inserting penalty and space before %s", utfchar(start.char)) + if trace_spacing then + report_spacing("inserting penalty and space before %s (left)", utfchar(start.char)) end - insert_node_before(head,start,make_penalty_node(10000)) - insert_node_before(head,start,make_glue_node(tex.scale(quad,left))) + insert_node_before(head,start,new_penalty(10000)) + insert_node_before(head,start,new_glue(tex.scale(quad,left))) done = true end end @@ -109,8 +113,8 @@ function spacings.process(namespace,attribute,head) local nextnext = next.next local somespace = nodes.somespace(nextnext,true) if somespace then - if trace_hspacing then - logs.report("spacing","removing penalty and space after %s", utfchar(start.char)) + if trace_spacing then + report_spacing("removing penalty and space after %s (right)", utfchar(start.char)) end head, _ = remove_node(head,next,true) head, _ = remove_node(head,nextnext,true) @@ -118,8 +122,8 @@ function spacings.process(namespace,attribute,head) else local somespace = nodes.somespace(next,true) if somespace then - if trace_hspacing then - logs.report("spacing","removing space after %s", utfchar(start.char)) + if trace_spacing then + report_spacing("removing space after %s (right)", utfchar(start.char)) end head, _ = remove_node(head,next,true) end @@ -129,11 +133,11 @@ function spacings.process(namespace,attribute,head) ok = not (nodes.somepenalty(next,10000) and nodes.somespace(next.next,true)) or nodes.somespace(next,true) end if ok then - if trace_hspacing then - logs.report("spacing","inserting penalty and space after %s", utfchar(start.char)) + if trace_spacing then + report_spacing("inserting penalty and space after %s (right)", utfchar(start.char)) end - insert_node_after(head,start,make_glue_node(tex.scale(quad,right))) - insert_node_after(head,start,make_penalty_node(10000)) + insert_node_after(head,start,new_glue(tex.scale(quad,right))) + insert_node_after(head,start,new_penalty(10000)) done = true end end @@ -146,22 +150,32 @@ function spacings.process(namespace,attribute,head) return head, done end -lists.handle_spacing = nodes.install_attribute_handler { - name = "spacing", - namespace = spacings, - processor = spacings.process, -} +local enabled = false + +function spacings.setup(id,char,left,right,alternative) + local mapping = spacings.mapping[id] + if not mapping then + mapping = { } + spacings.mapping[id] = mapping + end + local map = mapping[char] + if not map then + map = { } + mapping[char] = map + end + map.left, map.right, map.alternative = left, right, alternative +end -function spacings.enable() - tasks.enableaction("processors","lists.handle_spacing") +function spacings.set(id) + if not enabled then + tasks.enableaction("processors","typesetters.spacings.handler") + enabled = true + end + texattribute[a_spacings] = id end ---~ local data = { ---~ name = "spacing", ---~ namespace = spacings, ---~ processor = spacings.process, ---~ } ---~ nodes.process_attribute = process_attribute ---~ function lists.handle_spacing(head) ---~ return process_attribute(head,data) ---~ end +spacings.handler = nodes.installattributehandler { + name = "spacing", + namespace = spacings, + processor = process, +} |