Skip to content

Instantly share code, notes, and snippets.

@Semptum
Created January 26, 2023 13:37
Show Gist options
  • Save Semptum/076cca71bc251ebe19ff96c5effca3cb to your computer and use it in GitHub Desktop.
Save Semptum/076cca71bc251ebe19ff96c5effca3cb to your computer and use it in GitHub Desktop.
Code by Graham Douglas to strip glue and replace with spaces
local HLIST = node.id("hlist")
local GLUE = node.id("glue")
local GLYPH = node.id("glyph")
local WHAT = node.id("whatsit")
function lineparse(line)
local box_order=line.glue_order
local box_ratio=line.glue_set
local box_sign=line.glue_sign --%0 = normal, 1 = stretching, 2 = shrinking
-- % This loop uses a function to look for all lines of the typeset paragraph
-- % This function scans all the items in a typeset line of the paragraph
for n in node.traverse(line.head) do
-- % We have seen some glue: calculate its size, draw a box
if n.id == GLUE then
-- % glue parameters
local gwidth=n.width --%horizontal or vertical displacement
local gstr=n.stretch --%extra (+ve) displacement or stretch amount
local gstrord=n.stretch_order --%factor applied to stretch amount
local gshr=n.shrink --%extra (-ve) displacement or shrink amount
local gshrord=n.shrink_order --%factor applied to shrink amount
local width=0
--% This section is how TeX calculates glue settings
-- see
if box_sign==0 then --%(normal)
width = gwidth
elseif box_sign==1 then --%(stretch)
if gstrord == box_order then
width = gwidth + (box_ratio * gstr)
else
width = gwidth
end
elseif box_sign == 2 then --%(shrink)
if gshrord == box_order then
width = gwidth - (box_ratio * gshr)
else
width = gwidth
end
end --%if box_sign
-- now calculate the glue's width in bp .
if width > 0 then --% don't bother with zero width glues
if n.subtype == 13 then
print(SPACEVECTORS[51].width - width)
local g = node.new("glyph")
g.font = 51
g.char= 32
g.width= SPACEVECTORS[51].width
g.lang = tex.language
-- need a kern to account for any difference in (set) glue and space width
-- to ensure vusual appearance is preserved: <glue>---> <space> + kern
local krn = node.new("kern")
krn.subtype=1
krn.kern=-1*(SPACEVECTORS[51].width - width)
--local pdfn=node.new(WHAT, "pdf_literal")
--local wp = (width/65536)*(72/72.27)
--pdfn.data=" 0.2 w 0 0 "..wp.." 5 re q 0.9 0.9 0.0 rg f Q "
--pdfn.mode=0
--% insert into the typeset paragraph
local np = n.prev
local nn = n.next
np.next=g
g.next=krn
krn.next=nn
end
end
end --% if n.id == GLUE
--% Insert the line bounding box
local lw = line.width
local ld = line.depth
local lh = line.height
local lhw = ((lw/65536)*(72/72.27))
local lhd = ((ld/65536)*(72/72.27))
local lhh = ((lh/65536)*(72/72.27))
local ltot=lhd+lhh
local pdfn=node.new(WHAT, "pdf_literal")
pdfn.data=" 0.1 w 0 -"..lhd.." "..lhw.. " "..ltot.." re q 0.9 g f Q"
pdfn.mode=0
--local ln=line.head.next
pdfn.next=line.head
line.head=pdfn
end -- for n in node.traverse
end -- end of function
parsepara = function (head)
for line in node.traverse_id(HLIST, head) do
lineparse(line)
end
return head
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment