Skip to content

Instantly share code, notes, and snippets.

@dohyunkim
Last active November 15, 2018 03:37
Show Gist options
  • Select an option

  • Save dohyunkim/cde58679facb606a01a9 to your computer and use it in GitHub Desktop.

Select an option

Save dohyunkim/cde58679facb606a01a9 to your computer and use it in GitHub Desktop.
draw box around glyphs
luatexbase.provides_module(
{
name = 'drawcharbox',
date = '2011/05/20',
version = 0.2,
description = 'draw char box for each char',
author = 'Dohyun Kim',
license = 'Public Domain',
})
local rule = node.id("rule")
local kern = node.id("kern")
local hlist = node.id("hlist")
local vlist = node.id("vlist")
local glyph = node.id("glyph")
local nodenew = node.new
local traverse = node.traverse
local has_attribute = node.has_attribute
local drawcharboxattr = luatexbase.attributes.drawcharboxattr
local insert_after = node.insert_after
local add_to_callback = luatexbase.add_to_callback
local function newrule (w,h,d)
local r = nodenew(rule)
r.width = w or 0
r.height = h or 0
r.depth = d or 0
return r
end
local function newkern (w)
local k = nodenew(kern, 1)
k.kern = w or 0
return k
end
local function push_color (data)
local p = nodenew("whatsit", "pdf_colorstack")
p.stack = 0
p.command = 1
p.data = data or "0 G"
return p
end
local function pop_color ()
local p = nodenew("whatsit", "pdf_colorstack")
p.stack = 0
p.command = 2
return p
end
local lw = 0.1*65536
local hlw = lw/2
local tlw = lw*2/3
local function insertcharbox (head)
for curr in traverse(head) do
if curr.id == hlist or curr.id == vlist then
insertcharbox(curr.head)
elseif curr.id == glyph and has_attribute(curr, drawcharboxattr) then
local wd, ht, dp = curr.width, curr.height, curr.depth
insert_after(head, curr, pop_color())
insert_after(head, curr, newrule(wd, tlw, tlw)) -- baseline
insert_after(head, curr, push_color("0 0 1 RG"))
insert_after(head, curr, pop_color())
insert_after(head, curr, newkern(-wd+hlw))
insert_after(head, curr, newrule(wd-lw, hlw+ht, hlw-ht)) -- top
insert_after(head, curr, newkern(-wd))
insert_after(head, curr, newrule(lw, ht+hlw, dp+hlw)) -- right
insert_after(head, curr, newrule(wd-lw, hlw-dp, hlw+dp)) -- bottom
insert_after(head, curr, newrule(lw, ht+hlw, dp+hlw)) -- left
insert_after(head, curr, newkern(-wd-hlw))
insert_after(head, curr, push_color("1 0 0 RG"))
end
end
return true
end
add_to_callback("pre_output_filter", insertcharbox, "char_box_pre_output")
% written by Dohyun Kim
% Public Domain
\ProvidesPackage{drawcharbox}[2011/05/19 v0.1 Draw Char Box]
\newattribute\drawcharboxattr
\directlua{require "drawcharbox"}
\def\drawcharbox{\drawcharboxattr\@ne}
\endinput
% todo:
% * color choice by user
% * line width choice by user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment