Checking if formatted text in table cell has overflow

I have a script that takes my model in SketchUp and imports it into a LayOut document, creating tables and fills them with text.

Is there a way of checking if the cells formatted text has overflow?

What I would like to do is scale the text down by 1pt iteratively until the cell no longer has overflow.

Perhaps determine the column and row of the cell, get the column.width and row.height respectively.
Then compare against the formatted_text object’s bounds.


Layout::FprmattedText also has a grow mode.
I think it defaults to Layout::FormattedText::GROW_MODE_UNBOUNDED

I’ve tried multiple variations of this, with unexpected results. It seams that the bounds.width does not update after the formatted_text is initialized. After I apply the style with the correct font size and family the bounds size are still the initial size (corresponding to the default style of the formatted_text object).

#cell = cell to check
#cell_w = cell to check width
#text = cell contents as string

anchor_point = Geom::Point2d.new(0, 0)
anchor_type = Layout::FormattedText::ANCHOR_TYPE_CENTER_LEFT
test_text = Layout::FormattedText.new(text, anchor_point, anchor_type)
test_text.grow_mode = Layout::FormattedText::GROW_MODE_UNBOUNDED
test_text.apply_style(cell.style)
doc.add_entity(test_text, layers[2], pages.first)
text_w = test_text.bounds.width

if text_w > cell_w
	#Code to be executed
	#For now I just print the cell width and text width to the console
end

When I measure the actual formatted_text.bounds.width it does not equal text_w.

Yes I know. I’ve spoken about this multiple times and asked the LayOut API folks to put this into the documentation.

API consumers (you, I and others) must for most objects, first attach the object to a Layout::Document before making any property or style changes. This is because the #add_entity method sets an entity object to all the class’ defaults rather than use what the unattached entity’s style and properties have been set to.

So, reorder your code snippet to do the #add_entity immediately after creating the text object, … then afterward makes changes to the text style or properties.

Note also that within the API documentation there is a warning that the bounds of text objects can vary depending upon what font face is used. I’m not sure what to make of this warning. Ie, is the bounds method lacking? Does it just use Ariel font to do the computation? etc.

Note these open API Tracker issues: