summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkxl/spac-ali.lmt
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/context/base/mkxl/spac-ali.lmt')
-rw-r--r--Master/texmf-dist/tex/context/base/mkxl/spac-ali.lmt134
1 files changed, 134 insertions, 0 deletions
diff --git a/Master/texmf-dist/tex/context/base/mkxl/spac-ali.lmt b/Master/texmf-dist/tex/context/base/mkxl/spac-ali.lmt
new file mode 100644
index 00000000000..5c434fcdf2a
--- /dev/null
+++ b/Master/texmf-dist/tex/context/base/mkxl/spac-ali.lmt
@@ -0,0 +1,134 @@
+if not modules then modules = { } end modules ['spac-ali'] = {
+ version = 1.001,
+ optimize = true,
+ comment = "companion to spac-ali.mkiv",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "see context related readme files"
+}
+
+local div = math.div
+local format = string.format
+
+local tasks = nodes.tasks
+local enableaction = tasks.enableaction
+
+local nuts = nodes.nuts
+
+local setlist = nuts.setlist
+local setlink = nuts.setlink
+local getdirection = nuts.getdirection
+local takeattr = nuts.takeattr
+local getwidth = nuts.getwidth
+local findtail = nuts.tail
+
+local righttoleft_code = nodes.dirvalues.righttoleft
+local linelist_code = nodes.listcodes.line
+
+local hpack_nodes = nuts.hpack
+local nextlist = nuts.traversers.list
+
+local new_stretch = nuts.pool.stretch
+
+local a_realign = attributes.private("realign")
+
+local texsetattribute = tex.setattribute
+local texgetcount = tex.getcount
+
+local isleftpage = layouts.status.isleftpage
+
+typesetters = typesetters or { }
+local alignments = { }
+typesetters.alignments = alignments
+
+local report_realign = logs.reporter("typesetters","margindata")
+local trace_realign = trackers.register("typesetters.margindata", function(v) trace_margindata = v end)
+
+local nofrealigned = 0
+
+-- leftskip rightskip parfillskip
+-- raggedleft 0 + 0 -
+-- raggedright 0 0 fil
+-- raggedcenter 0 + 0 + -
+
+local function handler(head,leftpage,realpageno) -- traverse_list
+ for current, id, subtype, list in nextlist, head do
+ if subtype == linelist_code then
+ local a = takeattr(current,a_realign)
+ if a and a > 0 then
+ local pageno = div(a,10)
+ if pageno ~= realpageno then
+ local align = a % 10
+ local action = 0
+ if align == 1 then -- flushright
+ action = leftpage and 1 or 2
+ elseif align == 2 then -- flushleft
+ action = leftpage and 2 or 1
+ end
+ -- WS: watch this
+ local direction = getdirection(current)
+ -- or should this happen at the tex end:
+ if direction == righttoleft_code then
+ if action == 1 then
+ action = 2
+ elseif action == 2 then
+ action = 1
+ end
+ end
+ -- Currently we just prepend and append which kind of spoils the normalization unless
+ -- we extend our specs and also introduce left/right anchored stuff.
+ if action == 1 then
+ local head = list
+ setlink(findtail(list),new_stretch(3)) -- append
+ setlist(current,hpack_nodes(head,getwidth(current),"exactly",direction))
+ if trace_realign then
+ report_realign("flushing left, align %a, page %a, realpage %a",align,pageno,realpageno)
+ end
+ elseif action == 2 then
+ local head = setlink(new_stretch(3),list) -- prepend
+ setlist(current,hpack_nodes(head,getwidth(current),"exactly",direction))
+ if trace_realign then
+ report_realign("flushing right, align %a, page %a, realpage %a",align,pageno,realpageno)
+ end
+ elseif trace_realign then
+ report_realign("invalid flushing, align %a, page %a, realpage %a",align,pageno,realpageno)
+ end
+ nofrealigned = nofrealigned + 1
+ end
+ end
+ end
+ handler(list,leftpage,realpageno)
+ end
+ return head
+end
+
+function alignments.handler(head)
+ return handler(head,isleftpage(),texgetcount("realpageno"))
+end
+
+local enabled = false
+
+function alignments.set(n)
+ if not enabled then
+ enableaction("shipouts","typesetters.alignments.handler")
+ enabled = true
+ if trace_realign then
+ report_realign("enabled")
+ end
+ end
+ texsetattribute(a_realign,texgetcount("realpageno") * 10 + n)
+end
+
+interfaces.implement {
+ name = "setrealign",
+ actions = alignments.set,
+ arguments = "integer",
+}
+
+statistics.register("realigning", function()
+ if nofrealigned > 0 then
+ return format("%s processed",nofrealigned)
+ else
+ return nil
+ end
+end)