Repository navigation
feat: @jimp/layout, a flexbox-style layout renderer for OG images - #1559
Merged
Merged
Conversation
`strokeAlign: "inside"` keeps a rectangle's outline within it, like a CSS border, with inner corners `strokeWidth` tighter. `"outside"` puts it all around the rectangle. Both are drawn as the exact ring between two rounded rectangles; `"center"` stays the default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Lays out a tree of boxes, text and images in rows and columns (a subset of CSS flexbox written in TypeScript) and draws it with Jimp, for Open Graph cards, banners and badges. Text is measured with measureText and measureTextHeight, and drawn with print. Boxes are drawn with drawRect, boxShadow and roundCorners. Includes Box/Text/Image components and a JSX runtime at @jimp/layout/jsx-runtime. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The guide builds an OG card with @jimp/layout step by step; its images are rendered by scripts/layout-guide-images.mjs from the guide's code. render() documents every node type and prop. drawRect gains strokeAlign in its page, the drawing concept and the playground. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- wrap: true starts a new line (or column) for children that don't fit. Each line flexes, justifies and stretches on its own; gap separates lines too, and lines are packed at the start. - aspectRatio gives a node's height from its width or the other way round, set or stretched, and overrides an image's own ratio. - Like CSS's automatic minimum size, flex items don't shrink below their content: text in a row stays as wide as its longest word, images keep their size and boxes fit what's in them. minWidth/minHeight: 0 opts out, and overflow: hidden boxes can always shrink. This replaces the shrink: 0 default for images. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
key is allowed on any element so code written for React type checks; it's
ignored. Empty strings are skipped like null and false, so
{subtitle && <Text>{subtitle}</Text>} adds no empty line. The README leads
with JSX.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The guide and the render() page show each example as JSX and as plain objects in synced tabs. The render() page gains a JSX section (setup, children rules, your own components) and images for the badge and wrapped-tags examples. Code is formatted to fit its column without scrolling. Every example was type checked, run, and compared pixel for pixel with the images beside it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With "jsx": "preserve" (Astro's default), @jsxImportSource alone falls back to the classic runtime and React.createElement. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator
Author
|
🚀 PR was released in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
@jimp/layout, a declarative layout renderer for Open Graph cards, social banners and badges, built entirely on Jimp. You describe a tree of boxes, text and images, it's laid out like a subset of CSS flexbox, andrenderdraws it. There's no browser, SVG or WASM step, so it runs anywhere Jimp does (Node, browsers, workers, Deno, Bun).Rendered by
packages/docs/scripts/layout-guide-images.mjsfrom the guide's code, 1200 × 630, in about 45 ms (fonts and avatar loaded beforehand).What's in it
@jimp/layout(new package, not in thejimpbundle)render(tree, { Jimp, width?, height?, background? })returns an image of the Jimp class you pass in.layout(tree, options)returns the computed boxes without drawing anything.box,textandimage. Each is a flat object, validated with zod (strict, so a typo likejustifyContentthrows, and the error includes the path to the node).direction,gap,padding,margin;justify(start/center/end/space-between/around/evenly) andalign/alignSelf(start/center/end/stretch);grow/shrink(with min/max freezing) andwrap: truefor multi-line rows or columns;%andautosizes,min/maxsizes andaspectRatio;position: "absolute"withtop/right/bottom/left.minWidth: 0opts out, andoverflow: "hidden"boxes can always shrink.background(a colour, or a linear/radial gradient sized to the box),borderWidth/borderColor,radius(uniform or per corner),shadow(one or several),opacityandoverflow: "hidden"(which clips to the rounded corners).font,fontSize,color(a colour or gradient),lineHeight,letterSpacingandtextAlign, all inherited from ancestor boxes, plusmaxLines, which truncates with an ellipsis (…, or...when the font lacks the glyph). Works withloadTTFand bitmap fonts.srcis a Jimp image or anythingJimp.readtakes.fitis fill/cover/contain, images keep their aspect ratio when only one dimension is set, and aradiusrounds them (for avatars).Box/Text/Imagecomponents plus a small JSX runtime:@jimp/layout/jsx-runtimeandjsx-dev-runtime, about 30 lines, no React. Your own components are plain functions.keyis accepted and ignored.null/false/empty strings are skipped.@jimp/plugin-draw:drawRectgainsstrokeAlign: "center" | "inside" | "outside"."inside"gives a CSS border: the outline stays inside the box, and the inner corners arestrokeWidthtighter. It's drawn as the exact ring between two rounded rects with evenodd, so there are no stroker artefacts at tight corners."center"remains the default, so existing output is unchanged. It has tests, docs, a playground control and a README entry.Docs (JSX first; every example also has an Objects tab, and the tabs stay in sync across pages)
guides/og-images). It covers JSX setup (tsconfig or a per-file pragma, and how to run it in Node) and builds the card above from small components (Card,Header,Body,Tags) in four steps, each with its rendered image. It then shows how to serve the card withResponse.render()reference page (under Drawing):layout()positions, an absolutely positioned background photo, and wrapped tags;drawRectpage, the drawing concept page and thejimpREADME now mentionstrokeAlignand@jimp/layout.packages/docs/scripts/layout-guide-images.mjs. Code is formatted to fit its column, so nothing scrolls sideways.Design decisions
strokeAlign. Everything else was already in existing plugins, so layout composes those instead of duplicating them:drawRect.boxShadow(plugin-border) on a transparent box-sized image, then composite it. Its defaults and its CSS-style blur (σ = blur/2) carry over.overflow: "hidden"and image radii, renders into a layer and appliesroundCorners.opacitySource.cover/contain/resize.drawRectrather than a new method.methods.drawRect(image, …), as plugin-shadow does) instead of called on the instance. Sorenderworks with acreateJimpbuild that has none of those plugins, andJimpis only needed to construct images (and toreadstring or buffer sources).measureText, height ismeasureTextHeightat the wrap width, and drawing isprintwith the samemaxWidth, so wrapping always matches.maxLinesbinary-searches the longest prefix plus ellipsis that still fits.width/height, the image is sized to the tree (useful for badges).direction: "column", as in React Native.align-content: flex-start) and sharegap; there's noalign-contentor separate row/column gaps.boxShadow.core,types,utils,plugin-draw,plugin-print,plugin-border,plugin-cover,plugin-contain,plugin-resizeandzod. There are no new third-party dependencies; the lockfile diff is workspace links only.Tests
layout.test.ts(69 tests, Node and browser): layout maths with a synthetic 10 px monospace bitmap font. It covers sizing, percentages, min/max, padding/margins, rows and columns, everyjustify/alignvalue, grow/shrink/freezing, automatic minimum sizes,aspectRatio,wrap, absolute positioning, text wrapping andmaxLines, inheritance, images, validation errors and the components.render.test.ts(15, Node and browser): pixel assertions for backgrounds, inside borders, gradient direction, opacity, clipping, shadow placement, paint order and images.render.node.test.ts(8): image snapshots via@jimp/test-utils/image-snapshotof the OG card, box styles, image fit, an avatar, text alignment/gradients/letter spacing, wrapped tags with anaspectRatioimage, and bitmap fonts. One more test checks that the JSX-built card is pixel-identical to the object-built one.jsx.test.tsx: JSX builds the same tree as objects (fragments, arrays,key,null/false, interpolated text), and custom components with children work.plugin-draw: 7 newstrokeAligntests.…subsets of Inter Regular/Bold (OFL), generated bypackages/layout/scripts/subset-fonts.sh, which is the same approach as plugin-print's script.Locally, root
pnpm build,pnpm lintandpnpm testall pass (87/87 turbo tasks). So dopnpm test:browserfor@jimp/layout,pnpm buildinpackages/docs(astro checkreports 0 errors), andpnpm install --frozen-lockfile.Left out / follow-ups
flex-basis,order,z-index,align-content, separate row/column gaps, and per-side border widths/colours.print.@jimp/layoutisn't in the generated TypeDoc type reference: starlight-typedoc is configured for thejimppackage only, and adding a second instance touches the shared astro config. Therender()page documents every prop by hand instead.@jimp/layout-yogaadd-on could swap it in later without changing the API.🤖 Generated with Claude Code