I get same width by bounds.
def get_width(font_px)
test_document = Layout::Document.new
text = Layout::FormattedText.new 'AB', Geom::Point2d.new, Layout::FormattedText::ANCHOR_TYPE_CENTER_CENTER
style = text.style
style.font_size = font_px
style.font_family = '黑体'
text.style = style
text.apply_style style
test_document.add_entity text, test_document.layers.first, test_document.pages.first
test_document.pages.first.entities.to_a[0].bounds.width
end
puts "10px => #{get_width(10)}"
puts "15px => #{get_width(15)}"
10px => 0.3652777671813965
15px => 0.3652777671813965
=> nil
Firstly, I think in this case that text.style = style
is not needed as the code is using the #apply_style
method.
Try …
(1) text.grow_mode = Layout::FormattedText::GROW_MODE_UNBOUNDED
UPDATE: This does not matter as by default grow mode is UNBOUNDED.
If this doesn’t work, then (2) reorder statements to add the text entity to the document before making style changes.
UPDATE: Testing shows an entity’s style is not initialized until after it has been added to a document.
1 Like
Please be specific about what is “ok”.
UPDATE: Testing shows an entity’s style is not initialized until after it has been added to a document.
So the correct suggestion above was (2).
ADD (9 days later): I logged an API Issue on this:
1 Like
‘it’s OK’ means that the method I quoted solved my problem.