ESC
开源 4 分钟阅读

florilegium:为LuaLaTeX程序化生成的代尔夫特蓝花卉页边距,用Lua绘制

为LuaLaTeX程序化生成的代尔夫特蓝花卉页边距,使用Lua绘制。每个页面生成独特的花园,支持动态适应页面布局、季节变化和访客元素,并可导出到PostScript。

来源:GitHub

florilegium

起源

Reddit帖子 询问如何在LaTeX中制作这样的花卉。我不知道原作是如何制作的,这个项目也不试图复制它。这是一种可能的方法,也是我所做的方式:手工编写的图元带有随机参数,在Lua中绘制,通过程序化布局放置,其“填充未使用空间”部分借鉴了1998年装饰文献中的一个想法。它得到了类似的外观。

它是什么

为LaTeX程序化生成的代尔夫特蓝花卉页边距。无图像、无导入路径、无外部资产:每一片花瓣、叶子和茎都是在页面写入时用Lua计算的参数曲线,以水彩画风格绘制,并直接写入PDF。每一页都有自己的花园。

六个种子

六个种子的生长组合。相同代码,不同种子——花园填充文本留下的任意空间。

两个文档共享一个包,第三个展示了其他功能:

florilegium.texflorilegium-grow.tex
组合手工编写:茎、叶和花手工放置,每个参数采样生长:植物通过贪婪的空间填充循环生长以适应页面
适应无(针对A4、68毫米边距调整)页面布局,以及最后一页文本下方的空闲空间
预览手工编写生长

两个PDF都是自描述的:正文内容是边距绘制的说明。

生长文档的最后一页

生长文档的最后一页:文本提前结束,因此枝条拱入下方的空间。

细节

300 dpi裁剪图。斑驳的 washes 与白色条纹提亮,裂叶带有白色叶脉,勿忘我花序,茎后有一朵淡色幽灵花。

观看它生长

生长过程用它填充的空闲空间
种子6生长种子6生长,标记空闲圆盘

种子6,从空白边距到完成页面。茎先长出,叶子在茎尖通过节点时展开,每根茎以一朵花结束。然后贪婪循环接管:在右侧,每个虚线圆圈是它找到的最大空闲圆盘,就在枝条生长到它之前。大洞先慢慢出现,小洞最后快速出现。蝴蝶和蜜蜂飞入,鸟儿滑落到它的枝条上,蜗牛爬上茎直到最后一帧,这正是这个种子总是给出的页面。

生长过程在页面布局时记录一次:每根茎也以递增长度绘制,每个图案以几个较小的尺度重放相同的随机绘制。一帧然后写出时机已到的部件,按其原始绘制顺序。make grow-6 渲染这样一个种子(build/grow-6.gif 和 .mp4,约45秒);添加 EXPLAIN=1 以显示虚线圆盘。

边距阅读文本

三个页面的showcase.tex

showcase.tex,“一年的边距”:第一页是春天,最后一页是夏天、秋天。每个部分描述了它展示的功能。

生长的边距可以响应周围的文档:

  • 首字母。 \florilegiuminitial[5]{E} 将段落的第一个字母设置在一个五行高的瓷砖中。字母的轮廓从OpenType字体(默认为Latin Modern Bold)读取,并像花瓣一样绘制;藤蔓在其笔画上交替上下穿过,叶子在合适的地方生长,花在间隙中开放,通常在字母的字谷中。字母也以不可见方式排版,因此段落仍复制为“Every”。
  • 标题处的花。 使用 sections 选项,每个编号的节和小节在其标题旁边的边距中获得一朵花,以铜色绘制,就像标题曾经被突出显示为红色一样。
  • 文本要求处的花。 \florilegiumbloom 在其所在的行旁边放置一朵花;\florilegiumbloom[lily] 请求花园中的任何图案。标记在页面框中找到,当页面输出时,因此它在一次运行中即可工作。
  • 季节。 使用 aging 选项,花园从第一页的春天(主要是花蕾,小淡叶)老化到最后一页的秋天(种荚,落瓣,几片铜叶)。它需要两次运行,以获取页面计数;\florilegiumage{0.8} 手动设置年龄。

四个首字母

一个种子穿过季节

一个种子在春、早夏、晚夏和秋。季节改变生长什么,而不是在哪里。

访客

带鸟和蜗牛的细节

大多数页面都有访客:开阔空间中的一两只蝴蝶和蜜蜂,一根主茎上爬行的蜗牛,以及大约五分之一页上停在自己枝条上的鸟。它们来自各自的随机流,因此没有它们的页面正是它本来应有的页面;novisitors 选项将它们排除。

任意墨水

六个墨水中的一个种子

整个色调来自一种颜色,ink:代尔夫特蓝、深褐、绿、深红、紫、青。从最暗笔画到最淡 wash 的渐变在OKLCH中从该色调计算,因此浅端不会变灰。

在PostScript中

postscript/florilegium.ps的两页,种子7

postscript/florilegium.ps:纯PostScript中的生长边距,一个独立文件。Ghostscript或打印机在解释页面时计算每一片花瓣,两页文本解释如何。

绘制核心、每个图元和生长循环从ink.lua、flora.lua和grow.lua逐过程移植。它不需要TeX和任何其他文件:

sh gs -dALLOWPSTRANSPARENCY -sDEVICE=pdfwrite -dSeed=7 -o garden.pdf postscript/florilegium.ps make postscript # 同样 -> build/postscript-7.pdf;SEED=12 表示另一个花园

-dSeed=N 选择花园;没有它,时钟选择一个,因此每次打印都是新的。-dAge=0 到 1 设置季节,-dNoVisitors 省略蝴蝶、蜜蜂、蜗牛和鸟。两页大约需要2.5秒。许多Lua直接映射:PostScript自己的变换矩阵放置每个图元(平移、旋转、缩放),路径和贝塞尔曲线是原生的,运行时构建的过程替代了Lua的闭包(花瓣的半宽度是传递给轮廓构建器的过程)。有四个东西不映射:

  • 图层。 花园以一种顺序生长但从后向前绘制,因此每个填充和笔画用pathforall捕获到每层的列表中,列表在最后回放。
  • 透明度。 PostScript没有。Ghostscript用-dALLOWPSTRANSPARENCY提供它。没有该标志,或在打印机上,每个半透明填充与其可能落在的颜色混合并绘制为不透明。结果更平坦,但它是相同的花园。
  • 随机性。 生成器是32位整数算术中的Park-Miller,PostScript实数是单精度,因此这里的种子7与LaTeX中的种子7是不同的花园。
  • 文本。 文件用Times排版自己的两页。它知道自己的标题在哪里、文本在哪里结束,因此标题处的铜色花和最后一行下的花园像在LaTeX中一样工作。它没有首字母、动画或手工编写组合。
florilegium.ps的两次打印,两个不同的花园

文件两次发送到Epson喷墨打印机,使用lp postscript/florilegium.ps,没有给出种子。计算机的打印系统为每个作业再次运行程序,因此每次打印从时钟获取种子并生长出不同的花园。打印机得到不透明版本,因为它没有透明度。

构建

LuaLaTeX,一次运行,每页约一秒(两次运行用于showcase.tex,其季节需要页面计数)。

sh make # 三个PDF make seed-7 # 种子7的一页 -> build/seed-7.pdf make gallery # gallery/seeds.png + seeds.gif, inks.png, seasons.png (python3, Pillow, pdftoppm) made grow-6 # 种子6的生长动画 -> build/grow-6.gif + .mp4 (ffmpeg) made grow-6 EXPLAIN=1 # 同样标记空闲圆盘 -> build/grow-6-explain.*

未安装TeX?构建一次图像,然后在其中运行所有内容:

sh docker build -t florilegium-tex tools make RUN=tools/in-docker

或将florilegium.sty、六个.lua文件和一个.tex文件粘贴到Overleaf项目中,并将编译器设置为LuaLaTeX。

在你自己的文档中使用:

latex \usepackage[grown]{florilegium} % 或普通 \usepackage{florilegium} 用于手工编写版本 % 生长版本的选项:sections(标题处的花),aging(两次运行), % novisitors(没有蝴蝶、蜜蜂、蜗牛或鸟) \definecolor{ink}{HTML}{5B2A1F} % 可选:整个色调的色相(这是深褐) \definecolor{accent}{HTML}{B4662F} % 可选:细线的铜色 … \florilegiuminitial[4]{T}his段落以装饰的T开头。 … 在这一行旁边的花\florilegiumbloom … \florilegiumage{0.8} % 仅生长版本:从这里起为秋天 \florilegiumtextend % 仅生长版本:在文本的最末

使用固定种子而不是页面号:lualatex "\def\florilegiumseed{7}\input{florilegium-grow.tex}"。

任何图元都可以在TikZ图片中、在当前作用域的偏移、旋转和缩放下单独绘制:

latex \begin{tikzpicture} \begin{scope}[shift={(2,2)},rotate=2

0]\florilegiumdraw{flower}{1.3}\end{scope} \begin{scope}[shift={(5,0)}]\florilegiumdraw{leaf}{3.0, {depth=0.6}}\end{scope} \begin{scope}[shift={(8,1)}]\florilegiumdraw{ghost}{“lily”, {s=1.4}}\end{scope} \begin{scope}[shift={(11,1)}]\florilegiumdraw{butterfly}{1.5}\end{scope} \end{tikzpicture}


## How it works

**Drawing** (`ink.lua`). All geometry is computed in Lua and written as PDF path operators
into one literal node per page; TeX does no arithmetic. Primitives are drawn in a local
frame under an affine transform, which is read from TikZ when a primitive is called inside
a scope. Colors come from a tone ramp derived from the one document color `ink`: navy to
mist at the hue of `ink`, interpolated in OKLCH so the light end stays blue instead of
turning gray. Tapered strokes are two offset Béziers, not sampled ribbons.

**Primitives** (`flora.lua`). Painted in the order a watercolorist would: a flat wash,
darker blotches multiplied over it, pigment pooling at the edges, streaks lifted out in
white, a few fine veins, and almost no outlines. Broad petals and flowers, star lilies seen
at a tilt, blossoms and florets, buds and bellflowers; lobed and entire leaves; forget-me-not
cymes that curl like a scorpion's tail, branching sprigs, umbels, tendrils, seed heads;
butterflies, bees, snails and a bird; copper sparkles, rosettes and the ivy-leaf fleuron
whose stem becomes the head rule. Any of them can be painted as a pale ghost.

**Authored composition** (`authored.lua`). A plant grows from a root in the bottom-left
corner: three stems through hand-picked control points, a fourth along the bottom of the
page as the footer, leaves at chosen fractions of the stems, and every other motif on a
branch that leaves its stem tangentially and arrives along the motif's axis — so every
flower can be traced back to the root.

**Grown composition** (`grow.lua`), in the spirit of Wong, Zongker & Salesin,
[*Computer-Generated Floral Ornament*](https://dl.acm.org/doi/10.1145/280814.280948)
(SIGGRAPH 1998). The stems sway up the column as smooth noise of their height; leaves are
hung at their nodes where there is room. Then, repeatedly: take the largest free disc within
reach of a stem, choose a motif that fits it, and grow a branch to it from the nearest stem
point; the branch becomes a stem in turn. Big holes are taken first, so flowers and lilies
land early, sprays and leaves fill the middle, and the loop ends by tucking leaflets, buds
and florets onto the stems. The end of the text is measured with `\pagetotal` in the same
pass, so a short last page grows branches into the space below it with one compilation.

**Reading the page** (`florilegium.lua`). A heading or `\florilegiumbloom` leaves an
invisible marker node in the text. When the page is shipped out, its box is walked node by
node, adding up heights, depths and the glue as it was set, to find each marker's height on
the page; the garden then gives those lines their flowers before any leaf is hung. An age
reweights the motifs: buds and sprays of buds in spring, seed heads in autumn, when some
leaves are painted with a ramp built from the accent instead of the ink.

**Initials** (`initial.lua`). The letter's outline comes from luaotfload's OpenType reader as
Bézier segments. Flattened into polygons, it gives an even-odd inside test, and a grid of
distances to the letter and the frame finds the largest gaps for the flowers. The vine is a
Catmull-Rom curve across the tile; where it crosses the letter it goes over and under in
turn, and the strands that go over are drawn again, clipped to the letter, with a thin halo
of paper.

**Procedural ≠ random.** The seed is derived from the page number, so page *n* always
gets garden *n*. Reproducibility is what makes the document stable enough to typeset.

See [CLAUDE.md](CLAUDE.md) for a handoff-style map of the code, the tunables, and ideas not
yet done.

## License

MIT.