prawnpdf / prawn-table

Provides support for tables in Prawn
Other
206 stars 101 forks source link

No `fallback_fonts` support? #80

Open luiges90 opened 7 years ago

luiges90 commented 7 years ago

It seems that fallback_fonts are unsupported in tables. That means if I want to put text of multiple languages in one table while having suitable font for each languages I am out of luck 😞

pdf = Prawn::Document.new
pdf.font_families["zh"] = {
  :normal => { :file => "#{data_path}/bsmi00lp.ttf", :font => "zh" }
}
pdf.fallback_fonts ['Helvetica', 'zh']

table = [
  ['', '總覽SUMMARY', ''],
  ['...', '東西something', '...']
]

pdf.table(table, width: pdf.bounds.width, cell_style: {fallback_fonts: ['Helvetica', 'zh']})

Gives me this error

Your document includes text that''s not compatible with the Windows-1252 character set.
If you need full UTF-8 support, use TTF fonts instead of PDF''s built-in fonts.
hC4stillo commented 7 years ago

Having the same problem here, can't get fallback fonts inside table cells

oshanz commented 6 years ago

https://github.com/prawnpdf/prawn/issues/1054 seems that issue also is also about the same problem

fbukevin commented 6 years ago

With collecting some information, here is a workaround works for me:

  1. Check #{Prawn::DATADIR}/fonts/ if the TrueType font file you want exists. If not, download it and put it into the folder. I have no idea why gkai00mp.ttf (font for simplified Hant letters) was not installed when I use bundle to install prawn. But you can find it here.

  2. In the prawn pdf file

    prawn_document do |pdf| 
    pdf.font "#{Prawn::DATADIR}/fonts/gkai00mp.ttf"
    pdf.text "你好,世界!"
    end

Should works fine.

urkle commented 2 years ago

@pointlessone So, I ran across this issue while researching if Prawn + Prawn-Table is a viable option for building out reports.

It seems the issue is centered around the Table::Cell::Text class calling width_of on the font text (and other related methods) but the "text" does not get checked for fallback fonts before calling that method.

In the core Prawn this happens because all text rendering ends up going through a Formatted::Box before doing any work, and that Box class has a series of functions to process the text ( #process_fallback_fonts ).

So the question is how can we fix this issue cleanly in Prawn-Table? 1) I can "hack away" and copy portions of box into Cell::Text to have it implement the fallback_font functionality? 2) I can rework Cell::Text to utilize the Box class instead (hopefully).

pointlessone commented 2 years ago

@urkle I'm a bit rusty on the prawn-table internals. Option 2 sound like a cleaner approach. I take it's easier to copy code over but if we change how text boxes work in Prawn we will end up with a diverging implementation again.

urkle commented 2 years ago

@pointlessone I have a working set of PRs. It required making some slight changes to Prawn core (adding a new method to the Fomatted::Box to request it calculate the width)

prawnpdf/prawn#1259 - Core changes to prawn needed prawnpdf/prawn-table#145 - Changes to prawn-table needed