Figma Support Forum

Suggestion: TextNode.getAllFontNames

An API method for TextNode.getAllFontNames would be much appreciated, so you wont have to loop over every char in a TextNode just to find out the fonts used. This seems hugely inefficient, and the solution feels hacky and verbose for such a simple task.

Maybe I’m missing something obvious?

This is my current solution to this problem:
`getFontNames = node => {
const getAllFontNames = node => {
const seen = new Set(), fonts = ;
for (const i of Array.from(node.characters).keys()) {
const
font = node.getRangeFontName(i, i + 1),
name = font.family + font.style; // concatenate property values into string

			if (!seen.has(name)) { 		// if unseen
				seen.add(name); 		// add the string to a set
				fonts.push(font); 		// and output the object
			}
		}
		return fonts;
	}
	return node.hasMissingFont ? ( figma.notify('Has missing font, replacing with Roboto Regular'), [defaultFont] )
		: node.fontName === figma.mixed ? getAllFontNames(node)
			: [node.fontName];
}`

Agree, the current method whilst it works, is rather cumbersome, and copying data from one text node to another, is troublesome if you have mixed styles.

Perhaps in addition to the .getPropertyForRange prototypes we have, we could interface via something like TextNode.ranges which returns an array of differently styled text ranges within the node.
Although I think this would require a fundamental change to how text nodes are handled currently.

That’d be great, or maybe a property TextNode.fontNames could be an array containing all fontNames per default.