painbrush
1.0.21.1.0
src/layer/make-text.ts−
src/layer/make-text.tsDeleted−133
Index: package/src/layer/make-text.ts
===================================================================
--- package/src/layer/make-text.ts
+++ package/src/layer/make-text.ts
@@ -1,133 +0,0 @@
-import {
- punchLayerOver,
- inflateLayer,
- overlayLayerOver,
-} from "./transform.ts";
-import { overlayLayersOver } from "./transform.ts";
-
-import {
- solidFillBrush,
- alphaBrush,
- type Brush,
-} from "../color/brush.ts";
-import { makeRectangleLayer } from "./make-rectangle.ts";
-import type { Font } from "../typography.ts";
-import type { Layer } from "../layer.ts";
-
-type TextLayerProps = {
- /**
- * Total pixels before truncating the text
- */
- maxLengthPx?: number;
- /**
- * Background behind each individual character
- */
- letterPlateBrush?: Brush;
- /**
- * Background for the whole bounding box of the text
- * */
- bgPlateBrush?: Brush;
- /**
- * Character to use to identify possible linebreaks.
- * Normally you just want a space ig??
- * */
- breakLinesOn?: string;
-};
-
-/**
- * Writes the text
- */
-export const makeTextLayer = (
- text: string,
- font: Font,
- brush: Brush = solidFillBrush([255, 255, 255]),
- {
- letterPlateBrush = alphaBrush(),
- bgPlateBrush = alphaBrush(),
- maxLengthPx = Infinity,
- breakLinesOn = " ",
- }: TextLayerProps = {},
-): Layer => {
- const { getCharacter } = font;
-
- const lineHeight = getCharacter("X").height;
-
- let charLayers: Parameters<typeof overlayLayersOver> = [];
-
- const words = text
- .split(breakLinesOn)
- .map((word, idx, arr) =>
- arr.length === idx + 1 ? word : word + breakLinesOn,
- )
- .map((word) =>
- word
- .split("\n")
- .map((word, idx, arr) =>
- arr.length === idx + 1 ? word : [word, "\n"],
- ),
- )
- .flat(2);
-
- let lineOffset = 0;
- let maxWidth = 0;
- let lines = 1;
- for (let word of words) {
- const newline = () => {
- maxWidth = Math.max(lineOffset, maxWidth);
- lineOffset = 0;
- lines++;
- };
- let wordLayers = [];
- let wordOffset = 0;
-
- for (let character of word) {
- if (character === "\n") {
- newline();
- continue;
- }
- const char = inflateLayer(
- getCharacter(character),
- brush,
- letterPlateBrush,
- );
-
- wordLayers.push([
- char,
- wordOffset,
- ]);
- wordOffset += char.width;
- }
-
- const prevLineOffset = lineOffset;
- const verticalOffset = lineHeight * (lines - 1);
- lineOffset = lineOffset + wordOffset;
- if (lineOffset > maxLengthPx) {
- newline();
- }
- charLayers.push(
- ...(wordLayers.map((layer) => [
- layer[0],
- {
- skipBlending: true,
- offset: {
- x: prevLineOffset + (layer[1] as number),
- y: verticalOffset,
- },
- },
- ]) as Parameters<typeof overlayLayersOver>),
- );
- }
-
- let textLayer = makeRectangleLayer(
- {
- x: (maxWidth = Math.max(lineOffset, maxWidth)),
- y: lineHeight * lines,
- },
- bgPlateBrush,
- );
-
- for (let layer of charLayers) {
- punchLayerOver(textLayer, layer[0], layer[1]);
- }
- return textLayer;
-};