Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Prawn - Determine the width of a mono spaced font

Tags:

ruby

fonts

prawn

I'm trying to overlay polygons on top of a grid of mono-spaced characters. In order to calculate the coordinates for the polygons I need to know how wide a single character is. How do you determine the width of a single mono-spaced (Courier 12) character using the Ruby Prawn gem?

like image 491
Josh Rickard Avatar asked Dec 21 '22 00:12

Josh Rickard


1 Answers

According to this there is a width_of method buried in Prawn:

pdf.width_of(text, :size => 10)

Here is a bit more documentation I was able to dig up. In terms of your question I think you would do:

pdf.width_of('a', :size => 12) #provided a was the single character in question
like image 64
ScottJShea Avatar answered Jan 08 '23 02:01

ScottJShea