简单来说
一本讲夜班公交的折页小杂志,用只有两种油墨的印刷机印。彩色插图都变成同一种粉色的深浅色调。
成品一览
Night Buses是一本关于某座城市夜班公交的免费小志:四页,120 × 160 mm,一张纸对折,供一台有蓝色和荧光粉两个滚筒的孔版印刷机印刷。配置里的每种颜色都是两种墨之一、粉色的25%网调,或者奶油色纸张本身。插图按全彩绘制(六色线路图、灰色的月亮、黑底琥珀色的线路牌),从粉色滚筒印成粉色的深浅网调,颜色越深网调越密。线路图印成粉色,同一页靠下的一幅小插图保留原来的六种颜色。封面标题排两次,粉色副本比蓝色的向右偏0.8 mm、向上偏0.5 mm。CodePen示例还能生成彩色PDF和灰度打样。
这道食谱解答
- 怎样把所有示意图重新着色为同一种专色油墨?
简短回答
// renderToPdf reads the ink from the config and recolours every SVG it is handed; the canvas
// does the same to a picture registered with singleInk: true. Both get the drawing as drawn.
const diagramStyle = { singleInk: true, inkColor: col('spot') };
// The kit's loadSvg hands the flag to registerResourceImage, so the canvas tints the drawing
// as it paints, and keeps the markup for imageBytes: the PDF recolours those bytes.
const loadArt = (map) => Promise.all([
loadSvg('network.svg', map, { singleInk: true }),
loadSvg('moon.svg', moon(), { singleInk: true }),
loadSvg('blind.svg', blind(), { singleInk: true }),
]);
const pdfOptions = { fontProvider: fontsourceProvider, resourceBytes: imageBytes };
用料
做法
#1 · 两个滚筒,每种颜色都取自其一
// The two drums: the blue for the type and the furniture, the fluorescent pink for the art.
const DRUMS = { ink: '#1d4fb8', spot: '#f0509a' };
const PAPER = '#f3efe6'; // cream stock: where no ink falls
// A screen prints a share of an ink's dots and lets the paper show between them.
const screen = (hex, share) => `#${[1, 3, 5].map((i) => Math.round(share
* parseInt(hex.slice(i, i + 2), 16) + (1 - share) * parseInt(PAPER.slice(i, i + 2), 16))
.toString(16).padStart(2, '0')).join('')}`;
const palette = { ...DRUMS, 'spot-25': screen(DRUMS.spot, 0.25), paper: PAPER };
// Every colour is linked to its palette entry by id.
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
// The engine's defaults link to main-color: aimed at the spot, none of them adds a third ink.
const colorPalette = [...Object.entries(palette), ['main-color', DRUMS.spot]]
.map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
孔版印刷机的每种墨由自己的滚筒印出,所以配置里任何地方出现第三种颜色,都需要第三个滚筒。screen()把一种墨按比例与纸色混合,就像网屏只印一部分网点、网点之间留出纸色。粉色的25%网调是#f2c7d3,用作行内标签和题注的底色。main-color指向粉色,所以配置里没有改动的默认值都从这个滚筒印出,而不是引擎自带的蓝色#295AA3。
#2 · 每种输出各重新着色一次
代码就是上面的简短回答。开启diagramStyle.singleInk后,renderToPdf把SVG的每种颜色变成inkColor的一个网调,浓度为1减去该颜色的相对亮度(图示样式),于是深蓝的N12印成84%,黄色的N50印成24%。用singleInk: true注册的图片,Canvas也按同样的方式着色,所以工具包的loadSvg(fileId, svg, { singleInk: true })带着这个开关注册每幅原图,并留下同一份SVG,由imageBytes交给resourceBytes。在Canvas上和按300 dpi栅格化的PDF里取样,N12线路都是(242, 109, 171),正是公式在浓度0.836时给出的网调。每种输出只给插图着色一次:若把它过两遍公式,这条线会减淡到44%。
#3 · 标题印两次,故意套印不准
const OFF = { x: 0.8, y: -0.5 }; // mm: where the pink drum lands against the blue one
// Array order is paint order: the pink copy goes down first and the blue covers all but a
// sliver. Canvas and PDF paint opaque colour, so the overlap stays blue, not riso purple.
const twice = (id, { placement: { offset: { x, y }, ...rest }, ...element }) => [
{ ...element, id: `${id}-spot`, color: col('spot'),
placement: { ...rest, offset: { x: mm(x.value + OFF.x), y: mm(y.value + OFF.y) } } },
{ ...element, id, color: col('ink'), placement: { ...rest, offset: { x, y } } }];
const rule = (id, direction, x, y, size) => ({ kind: 'rule', id, direction, color: col('ink'),
thickness: pt(id === 'pole' ? 3 : 0.8), placement: at('page', 'top-left', x, y, size) });
// span: 'page' lets the design run past the text block's foot: kept in the column, the
// pole would be cut off there.
const cover = { id: 'cover', span: 'page', header: { elements: [] }, footer: { elements: [] },
advancedDesign: { enabled: true, slot: { elements: [
{ kind: 'image', id: 'moon', resourceId: 'moon',
placement: at('page', 'top-left', 28, 14, { width: mm(92), height: mm(92) }) },
rule('wire-1', 'horizontal', 0, 30, { width: mm(TRIM.width) }),
rule('wire-2', 'horizontal', 0, 34.5, { width: mm(TRIM.width) }),
rule('pole', 'vertical', 98, 62, { height: mm(TRIM.height - 62) }), // off the foot
{ kind: 'box', id: 'flag', style: { backgroundColor: col('ink'), borderRadius: mm(1) },
placement: at('page', 'top-left', 89, 62, { width: mm(18), height: mm(21) }) },
// One word a line: the box is narrower than two of them.
{ kind: 'text', id: 'stops', content: '{attr.stops}', ...mono, fontSize: pt(9),
lineHeight: 1.25, align: 'center', color: col('paper'), overflow: 'wrap',
placement: at('#flag', 'top-left', 3, 2.5, { width: mm(12) }) },
{ kind: 'text', id: 'issue', content: '{attr.issue}', ...mono,
placement: at('page', 'top-left', MARGIN.inner, 8) },
{ kind: 'text', id: 'line', content: '{attr.line}', fontFamily: 'Epilogue', fontWeight: 700,
fontSize: pt(9), color: col('ink'), align: 'left', overflow: 'wrap',
placement: at('page', 'top-left', MARGIN.inner, 13, { width: mm(46) }) },
...twice('title', { kind: 'text', content: '{titleText}', fontFamily: 'Anton', fontSize: pt(86),
lineHeight: 0.9,
textTransform: 'uppercase', align: 'left',
overflow: 'wrap', placement: at('page', 'top-left', MARGIN.inner, 94, { width: mm(76) }) }),
] } } };
孔版印刷机一个滚筒接一个滚筒地印,纸张两次落下的位置绝不会完全相同。twice()返回一个文字元素的两份副本,粉色的在前,向右0.8 mm、向上0.5 mm。数组顺序就是绘制顺序,所以蓝色副本盖住粉色,只在每个字母的上缘和右缘露出一条细边。Canvas和PDF绘制的是不透明颜色,所以重叠处保持蓝色,而真正的孔版会印出紫色。这个版面设计是通栏的,因为站牌杆伸出版心底部,而限定在栏内的设计会在栏底被裁掉。
#4 · 小字用深色墨
const opener = { enabled: true, slot: { elements: [
{ kind: 'text', id: 'kicker', content: '{attr.kicker}', ...mono,
placement: at('container', 'top-left', 0, 0.5) },
...twice('title', { kind: 'text', content: '{titleText}', fontFamily: 'Anton',
fontSize: pt(34), lineHeight: 0.95, textTransform: 'uppercase', align: 'left',
overflow: 'wrap', placement: at('#kicker', 'below', 0, 2.5, { width: mm(MEASURE) }) }),
] } };
const route = { id: 'route', background: col('spot-25'), borderColor: col('spot'),
borderWidth: pt(0.75), borderRadius: pt(1.2), fontFamily: 'Space Mono', bold: true,
fontSize: em(0.86), color: col('ink') };
const captionStyle = { fontFamily: 'Epilogue', fontSize: pt(8), color: col('ink'),
backgroundEnabled: true, background: col('spot-25'), padding: mm(1.6) };
const quote = { id: 'quote', backgroundEnabled: false, marginTop: pt(LEAD), marginBottom: pt(4),
stripe: { enabled: true, side: 'top', width: pt(2.5), color: col('spot') },
padding: { top: mm(2.6), right: pt(0), bottom: pt(0), left: pt(0) },
titleStyle: { ...label, gap: mm(1.2) }, // the speaker, above the words
body: { fontFamily: 'Anton', fontSize: pt(14), lineHeight: pt(17), color: col('ink'),
textAlign: 'left', firstLineIndent: pt(0) } };
在奶油色纸上,蓝色的对比度是6.4:1,荧光粉只有2.9:1,印8或9 pt的字太淡。所以引擎排的每个字都从蓝色滚筒印出。行内标签是25%网调底上的蓝色Space Mono(4.85:1),外加粉色描边;题注是同一网调底上的蓝色Epilogue。粉色印引文上方的色条、标签描边、项目符号、页码方块和标题的错位副本。插图里的文字属于插图本身,随插图印成粉色。说话人的名字是框的标题,用7.5 pt粗体Space Mono大写字母排在引文上方。
#5 · 把小插图放在短文旁边
// The inset keeps the map's own colours. Single ink touches SVG only, so the map goes in
// as a PNG snapshot, which neither the canvas nor the PDF recolours.
async function registerSnapshot(fileId, svg, width, height) {
const canvas = new OffscreenCanvas(width, height);
canvas.getContext('2d').drawImage(await decodeSvgImage(svg), 0, 0, width, height);
const png = URL.createObjectURL(await canvas.convertToBlob());
await loadImage(fileId, png); // the kit registers the PNG and keeps its bytes for the PDF
URL.revokeObjectURL(png);
}
const INSET = 30; // mm: the snapshot's width; its height follows the map's 100 × 69.5
const drawn = { id: 'drawn', marginTop: pt(LEAD), advancedDesign: { enabled: true,
slot: { elements: [
{ kind: 'image', id: 'inset', resourceId: 'as-drawn',
placement: at('container', 'top-left', 0, 0.5, { width: mm(INSET), height: 'auto' }) },
{ kind: 'text', id: 'label', content: '{titleText}', ...mono,
placement: at('#inset', 'right-of', 5, 0) },
{ kind: 'text', id: 'note', content: '{attr.note}', fontFamily: 'Epilogue',
fontSize: pt(8.5), lineHeight: 1.4, color: col('ink'), align: 'left', overflow: 'wrap',
placement: at('#label', 'below', 0, 1.5, { width: mm(MEASURE - INSET - 5) }) },
] } } };
在单栏里,浮动体占满所在区域的整个宽度,所以浮动一幅小图会让区域的其余部分空着。于是这幅小插图改用标题的版面设计:### As drawn {style="drawn" note="…"}印出图片,并在图片右边把标题排成标签,下面是短文。图片计入版面设计预留的高度,所以即使短文变短,下一个块也从图片下方开始。registerSnapshot()把同一张network()线路图画成PNG,而单色墨只对SVG重新着色,所以这幅小插图在屏幕上和PDF里都保留六种颜色。
完整食谱
// ═══ Postext Cookbook · Nº 051 · Two-ink riso zine: art in one spot colour ═════════
// https://postext.dev/en/cookbook/riso-zine-single-ink
// Code: MIT · Text: original (CC BY 4.0) · Pictures: drawn in code (CC BY 4.0)
// Fonts: Epilogue, Anton, Space Mono (SIL OFL 1.1) · Needs postext ≥ 1.25.0
// A four-page zine for a risograph with a blue drum and a fluorescent pink one. The drawings
// are made in full colour and printed from the pink drum as tints of pink.
import {
buildDocumentWithFonts, renderPageToCanvas, registerResourceImage, decodeSvgImage,
} from 'https://esm.sh/postext';
import { renderToPdf, decompressWoff2 } from 'https://esm.sh/postext-pdf';
const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'riso-zine-single-ink';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region inks: two drums and the paper; every colour in the config links to one of them
// The two drums: the blue for the type and the furniture, the fluorescent pink for the art.
const DRUMS = { ink: '#1d4fb8', spot: '#f0509a' };
const PAPER = '#f3efe6'; // cream stock: where no ink falls
// A screen prints a share of an ink's dots and lets the paper show between them.
const screen = (hex, share) => `#${[1, 3, 5].map((i) => Math.round(share
* parseInt(hex.slice(i, i + 2), 16) + (1 - share) * parseInt(PAPER.slice(i, i + 2), 16))
.toString(16).padStart(2, '0')).join('')}`;
const palette = { ...DRUMS, 'spot-25': screen(DRUMS.spot, 0.25), paper: PAPER };
// Every colour is linked to its palette entry by id.
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
// The engine's defaults link to main-color: aimed at the spot, none of them adds a third ink.
const colorPalette = [...Object.entries(palette), ['main-color', DRUMS.spot]]
.map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
// #endregion
// #region answer: drawings in any colours, printed from the pink drum
// renderToPdf reads the ink from the config and recolours every SVG it is handed; the canvas
// does the same to a picture registered with singleInk: true. Both get the drawing as drawn.
const diagramStyle = { singleInk: true, inkColor: col('spot') };
// The kit's loadSvg hands the flag to registerResourceImage, so the canvas tints the drawing
// as it paints, and keeps the markup for imageBytes: the PDF recolours those bytes.
const loadArt = (map) => Promise.all([
loadSvg('network.svg', map, { singleInk: true }),
loadSvg('moon.svg', moon(), { singleInk: true }),
loadSvg('blind.svg', blind(), { singleInk: true }),
]);
const pdfOptions = { fontProvider: fontsourceProvider, resourceBytes: imageBytes };
// #endregion
const TRIM = { width: 120, height: 160 }; // mm: a 240 × 160 sheet folded once
const MARGIN = { top: 13, bottom: 16, inner: 12, outer: 10 }; // mm
const MEASURE = TRIM.width - MARGIN.inner - MARGIN.outer; // 98 mm, about 65 characters
组合好的script.js可以直接运行:把它粘贴到任何页面的模块脚本中,或在CodePen上打开这道食谱。 GitHub上的食谱文件夹 ↗ (在新标签页中打开)
变化
#换上橙色滚筒
月亮、线路图、线路牌、行内标签、色条和两个标题的错位副本都变成橙色,文字仍是蓝色;题注、短文和版权页仍写着粉色滚筒,直到你改写它们。
-const DRUMS = { ink: '#1d4fb8', spot: '#f0509a' };
+const DRUMS = { ink: '#1d4fb8', spot: '#ff6c2f' };#印一本双色教材
白纸上用黑色文字加蓝色专色,同样的插图印成蓝色的深浅网调,就像双色课本里的示意图;题注、短文和版权页仍写着粉色滚筒,直到你改写它们。
-const DRUMS = { ink: '#1d4fb8', spot: '#f0509a' };
-const PAPER = '#f3efe6'; // 奶油色纸:不着墨的地方
+const DRUMS = { ink: '#1f1f1f', spot: '#0078bf' };
+const PAPER = '#ffffff'; // 白纸:不着墨的地方常见问题
易错点
'top'浮动体默认排在引用页的下一页,除非允许它置于引用页顶
默认情况下浮动体不会排在自己的引用之前,所以在第N页引用的整页宽'top'浮动体会出现在第N+1页的顶部。在资源或其类型的defaultPlacement上设置placement.citingPage: true(或者对全书设置layout.floatsAtCitingPage,自postext 1.25起),它就改为置于第N页顶部,引用处之上的文字移到它下方,前提是它不超过layout.maxTopFraction且引用行仍留在该页。它不会置于章首页或强制分页之后的页面顶部:那时仍排在下一页,而'auto'或'bottom'可以占用引用页的底部。 图的放置 →
- 在PDF里,重新着色改写的是SVG标记:十六进制颜色、
rgb()、rgba()、hsl()和hsla(),以及关键字white和black。用red命名颜色或让填充保持默认黑色的形状在那里会保留原色,那就需要第三个滚筒。每个填充和描边都要给十六进制值。 colorSpace: 'grayscale'把postext绘制的内容变成灰色,但位图按原样嵌入,所以PNG小插图在灰度打样里仍是彩色。只有CodePen示例的第二个按钮生成打样;上面的页面是Canvas,本页提供的PDF是彩色版。- 各栏齐底可能在顶部浮动体下方、上一页段落的续文之前留出一行空白;
headings.balancing.stretchAfterFloats: false让文字紧贴浮动体。这里的文字不会触发它。改动文字后,检查线路图下方的第一行,因为多出一行就可能把小插图推到第五页。 - CodePen示例里两个按钮都写着“Build the PDF”。第一个生成彩色PDF,第二个生成灰度打样;文件生成后,按钮换成一个“Open the PDF ↗”链接和一个带文件名的下载链接。
致谢
- 文本
- 原创文字, CC BY 4.0
- 字体
- Epilogue (SIL OFL 1.1) · Anton (SIL OFL 1.1) · Space Mono (SIL OFL 1.1)


