Skip to content

Instantly share code, notes, and snippets.

@dohyunkim
Last active June 21, 2024 10:58
Show Gist options
  • Save dohyunkim/ace5ea747e24a4dbf81972a222c41f63 to your computer and use it in GitHub Desktop.
Save dohyunkim/ace5ea747e24a4dbf81972a222c41f63 to your computer and use it in GitHub Desktop.
parshape following a mp path
\documentclass{article}
\usepackage{luamplib}
\begin{document}
\directlua{
function gettablines ()
local gap, width = 12, string.format("\csstring\%f",tex.sp'\the\linewidth'/65781.76)
local instance = luamplib.instances['\mpfiginstancename']
local lines = instance:get_path"lines"
local offsets = instance:get_path"offsets"
ta = { table.getn(lines)*2 + 1 }
local minob = 0
for i, v in ipairs(lines) do
local oa, ob = offsets[i][1], offsets[i][2]
minob = ob < minob and ob or minob
oa = oa > 0 and oa or 0
ob = ob < 0 and ob or 0
local va = string.format("\csstring\%f",v[1] + oa)
local vb = string.format("\csstring\%f",v[2] - va + ob)
table.insert(ta, va.."bp")
table.insert(ta, vb.."bp")
end
minob = math.abs(minob)
for i, v in ipairs(lines) do
local ob = offsets[i][2]
ob = ob > 0 and ob or 0
ob = ob > minob and minob or ob % trial and error
local va = string.format("\csstring\%f",v[2] + gap + ob)
local vb = string.format("\csstring\%f",width - va)
table.insert(ta, va.."bp")
table.insert(ta, vb.."bp")
end
table.insert(ta, "0pt "..width.."bp")
ta.remain = instance:get_number'remain'
end
}
\mpfig*
path p, q, topline, botline, lines, offsets; numeric n, remain;
p = fullcircle xscaled 180 yscaled 220 rotated 45;
p := p shifted - llcorner p;
q = reverse p;
topline = (ulcorner p -- urcorner p) xscaled 1.2;
botline = (llcorner p -- lrcorner p) xscaled 1.2;
n = 0;
forever:
topline := topline shifted (0, -\the\baselineskip);
exitif point 0 of topline <= point 0 of botline;
n := n+1;
pair a, b; numeric offa, offb, ta, tb, dxa, dxb, dya, dyb;
(ta, whatever) = p intersectiontimes topline;
(tb, whatever) = q intersectiontimes topline;
a = point ta of p;
b = point tb of q;
% calculation of offsets
(dxa, dya) = direction ta of p;
(dxb, dyb) = direction tb of q;
dxa / dya = offa / \the\ht\strutbox;
dxb / dyb = offb / \the\ht\strutbox;
% done
if a < b:
z[n] = (xpart a, xpart b);
z.o[n] = (offa, offb);
else:
z[n] = (xpart b, xpart a);
z.o[n] = (offb, offa);
fi
remain := ypart a - ypart point 0 of botline;
exitif ypart(point 0 of topline - point 0 of botline) < \the\ht\strutbox;
endfor
lines = for i=1 upto n: z[i]-- endfor cycle;
offsets = for i=1 upto n: z.o[i]-- endfor cycle;
\endmpfig
\directlua{ gettablines() }
\setbox0\vbox{\tolerance9999 \emergencystretch 1em
\parshape=\directlua{tex.sprint(table.concat(ta,' '))}\noindent
All human beings are born free and equal in dignity and rights. They are endowed with
reason and conscience and should act towards one another in a spirit of brotherhood.
Everyone is entitled to all the rights and freedoms set forth in this Declaration,
without distinction of any kind, such as race, colour, sex, language, religion,
political or other opinion, national or social origin, property, birth or other status.
Furthermore, no distinction shall be made on the basis of the political, jurisdictional
or international status of the country or territory to which a person belongs, whether
it be independent, trust, non-self-governing or under any other limitation of sovereignty.
Everyone has the right to life, liberty and security of person.
No one shall be held in slavery or servitude; slavery and the slave trade shall be
prohibited in all their forms.
No one shall be subjected to torture or to cruel, inhuman or degrading treatment or punishment.
Everyone has the right to recognition everywhere as a person before the law.
All are equal before the law and are entitled without any discrimination to equal protection
of the law. All are entitled to equal protection against any discrimination in violation of
this Declaration and against any incitement to such discrimination.
Everyone has the right to an effective remedy by the competent national tribunals for acts
violating the fundamental rights granted him by the constitution or by law.
\par
\vskip -\prevdepth
\vskip \directlua{tex.sprint(ta.remain)}bp
}
\splittopskip=0pt
\setbox2=\vsplit0 to \directlua{tex.sprint((ta[1]-1)/2)}\baselineskip
\vbox to 0pt{\box2\vss}\nointerlineskip\box0
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment