Nodes/Duanyll Nodepack/Get Text BBox with Anchor
ComfyUI Node

Get Text BBox with Anchor

Ask Where the Text Would Go Before You Draw It

By Duanyll·Created about a year ago·Updated 4 months ago· 2
Get Text BBox with Anchor
  • anchor_box
  • BBOX
width
height
text
text_height
font_name
position
margin

Positioning text on an image is normally guesswork: pick a font size, drop it somewhere, render, squint, adjust. GetTextBBoxWithAnchor removes the guessing - it computes the exact bounding box your text would occupy before anything is drawn, relative to a nine-way position and an optional anchor box. Then you draw (or decide not to) with the coordinates you already know.

It's the measurement node for the pack's text-overlay workflow. Pair it with the pack's DrawTextInBBox to place captions and watermarks precisely, or use it purely to plan layout - knowing the box lets you check collisions, margins, and fit without rendering a single pixel.

How it works

The node measures the text with the selected font at your requested text_height, then works out where it sits. With no anchor, placement is relative to the whole canvas; give it an anchor_box (any BBOX) and it positions relative to that box - outside it for captions, centered on it, above it, wherever the nine-way position enum says. A margin controls the gap.

The clever part is what happens when space runs out: the algorithm tries the requested size, and if the text would overflow the canvas or collide with the anchor, it shrinks the font step by step until it fits - down to a sensible floor - then returns the box that actually works. Chinese text auto-switches to the bundled Chinese font so CJK doesn't measure as tofu.

Inputs and outputs

  • width / height - canvas size in pixels.
  • text - the string to measure.
  • text_height - desired cap height (the algorithm may shrink it).
  • font_name - default, bold, comic, cursive_italic, or chinese.
  • position - top-left through bottom-right (9 options).
  • margin - padding between text box and anchor/canvas.
  • anchor_box (optional) - reference BBOX for relative placement.

Output is a BBOX you can hand to DrawTextInBBox (or any bbox consumer).

Installing it

It ships in Duanyll Nodepack:

cd ComfyUI/custom_nodes
git clone https://github.com/Duanyll/duanyll_nodepack

or via ComfyUI Manager. Fonts are bundled in the pack's assets - nothing to download.

Gotchas

The returned box can be smaller than you asked for - that's the auto-shrink, and it's a feature, but it means "text_height 60" may come back as a shorter box when the canvas is tight. Second, it measures with the bundled fonts, which won't perfectly match an arbitrary system font; if your text looks off, it's the font, not the math. And it returns a box, not rendered text - forget to hook the box into a draw node and you'll get a lot of empty rectangles. Most people find that acceptable trade for never guessing again.

Categoryduanyll/bbox

Inputs (8)

NameTypeDefaultDescription
widthINT
heightINT
textSTRING
text_heightINT
font_nameCOMBO5 options: default, bold, comic, cursive_italic, chinese
positionCOMBO9 options: top-left, top, top-right, left, center, right, +3
marginINT
anchor_boxoptBBOX

Outputs (1)

NameTypeDescription
BBOXBBOX