jessedoyle / prawn-icon

Easy icons for Prawn.
Other
28 stars 15 forks source link

Prawn::Errors::CannotFit in table when using custom font #21

Closed tandibar closed 8 years ago

tandibar commented 8 years ago

I am using a special font like this:

font_families.update(
  "Monda" => {
    normal: File.join(Rails.root, "app/assets/fonts/monda/Monda-Regular.ttf"),
    bold: File.join(Rails.root, "app/assets/fonts/monda/Monda-Bold.ttf")
  }
)
font "Monda"

if i run your simple example after that

data = [
  [table_icon('fa-birthday-cake'), 'Cake'],
  ['is', 'Great!']
]
table(data)

it crashes with Prawn::Errors::CannotFit exception. Any idea?

jessedoyle commented 8 years ago

@tandibar - Thanks for filing an issue!

Any chance you can provide me a link to the .ttf files that you're using?

I've noticed this issue before, and I believe the issue lies in the code for Prawn::Table (see my comment in #7). With the proper font files, I should be able to replicate the issue and further investigate.

jessedoyle commented 8 years ago

I found the necessary font files on Google fonts. Here's some initial observations:

For the time being, you can simply force the table cell to have a certain width when dealing with custom fonts:

  font 'Monda'

  data = [
    [table_icon('fa-birthday-cake', width: 24), 'Cake'],
    ['is', 'Great!']
  ]

  table(data)

The real question is:

Why is Prawn::Table not determining an appropriate minimum width for the cell that includes the icon? Perhaps the font change is not being handled properly - I'll need to dig deeper.

jessedoyle commented 8 years ago

I can confirm that this is caused by a defect in the Prawn::Table codebase.

Please see Prawn::Table #42. Prawn::Table appears to not consider the cell's specified font when calculating the minimum width requirements for a table cell.

Furthermore, this code reveals that Prawn::Table naively uses the M character as the widest individual character in a font. Clearly this behaviour is incompatible with icon fonts.

@tandibar - I'm going to keep this issue open for a few days as reminder to myself, but there is not very much that can be done for this issue from Prawn::Icon's perspective (other than specifying the cell's width).

tandibar commented 8 years ago

Hi, thx for taking care so your quickly. I just tried with manually setting width and it works. I need to set at least 19 as width. But there is much white space in the cells. bildschirmfoto 2015-11-11 um 19 37 05

I will see what Prawn::Table does to take care of this. For me it's totally okay to close this issue.

jessedoyle commented 8 years ago

Perfect thanks!

jessedoyle commented 8 years ago

Also note that the valign and align settings may be useful for moving the icons around in the cell:

  data = [
    [table_icon('fa-birthday-cake', width: 24, valign: :center, align: :center), 'Cake'],
    ['is', 'Great!']
  ]

  table(data)