How to Calculate a String's Width in Ruby

How do I calculate a String's width in Ruby?

You should use the RMagick gem to render a "Draw" object using the font you want (you can load .ttf files and such)

The code would look something like this:

   the_text = "TheTextYouWantTheWidthOf"
label = Draw.new
label.font = "Vera" #you can also specify a file name... check the rmagick docs to be sure
label.text_antialias(true)
label.font_style=Magick::NormalStyle
label.font_weight=Magick::BoldWeight
label.gravity=Magick::CenterGravity
label.text(0,0,the_text)
metrics = label.get_type_metrics(the_text)
width = metrics.width
height = metrics.height

You can see it in action in my button maker here: http://risingcode.com/button/everybodywangchungtonite

Determing string actual length not just chars count

The response is here

How do I calculate a String's width in Ruby?

the_text = "TheTextYouWantTheWidthOf"
label = Draw.new
label.font = "Vera" #you can also specify a file name... check the rmagick docs to be sure
label.text_antialias(true)
label.font_style=Magick::NormalStyle
label.font_weight=Magick::BoldWeight
label.gravity=Magick::CenterGravity
label.text(0,0,the_text)
metrics = label.get_type_metrics(the_text)
width = metrics.width
height = metrics.height

How do I calculate the average length of a string in an array?

I would write like:

arr.join.size / arr.size.to_f

Also, you can use sum since Ruby 2.4:

arr.sum(&:size) / arr.size.to_f
#=> 2.0

Rubygame::TTF - The difference between a string's width and the width of all it's characters

The result is close, but... Does anyone know why there's a difference of 8 between the sum of the character widths and the string's width?

Yes: because a good TrueType font, like any good font, will be kerned by the layout engine. http://en.wikipedia.org/wiki/Kerning

When doing layout with variable width fonts - which you want - always use the full string width. Per-character numbers are meaningless. (...and, really, the "right" way to do this is to use something that does layout for you. Pango is a project that does that; go count the lines of code, and consider if you really want to write that yourself.)

String count without using .length or .size methods?

Other option, mapping String#chars with index then picking the last:

str = "123456"
str.chars.map.with_index { |_, i| i + 1 }.last
#=> 6

It generates an Array, but we are not looking for efficiency here.

Or even using String#index with offset:

str = "aaaa"
str.index(str[-1], -1) + 1
#=> 4

It looks for the index of the latest char starting from the end.

How to match a string in array, regardless of the string size in Ruby

Converting strings into array and making an intersection operation should be an option. I know, this is not the best solution, but might save your day.

[17] pry(main)> x = "alto saxophone 1"
=> "alto saxophone 1"
[18] pry(main)> y = "i am not an anglophone"
=> "i am not an anglophone"
[19] pry(main)> z = "alto 1"
=> "alto 1"
[20] pry(main)> x.split & z.split == z.split # & is array intersection
=> true
[21] pry(main)> x.split & y.split == y.split
=> false


Related Topics



Leave a reply



Submit