Prawn with some emojis for ttf-font not rendering text correctly
Asked Answered
S

3

8

I have a ruby script to generate a pdf document with some text. The text contains emojis in it.

The problem with the first line of text is that it prints the three emojis separated by something that looks like a cross when they should be a single emoji(family of three members).

The problem with the second line is that it just prints a square instead of the intended emoji(shush face). I've tried with some other fonts but it still won't work. These are the fonts:

DejaVuSans

ipam

NotoSans-Medium

I can't find the problem

Is there anything missing?

Am I doing something wrong?

The gems are installed and the fonts are in the right place

require "prawn"
require "prawn/emoji"
require "prawn/measurement_extensions"

$pdf = Prawn::Document.new(:page_size => [200.send(:mm),200], :margin => 0)
$pdf.font "./resources/Montserrat-Medium.ttf"

st = "\u{1F468}\u200D\u{1F469}\u200D\u{1F466}".encode("UTF-8")
st2="\u{1F92B}".encode("UTF-8")

$pdf.draw_text st,:at => [10, 100]

$pdf.draw_text st2,:at => [10, 80]

$pdf.render_file "test.pdf"
Smacker answered 14/9, 2018 at 21:11 Comment(4)
Can you check your shush-face unicode value? I see it as U+1F62F. For example, this works in ruby 2.1.8: st2="\u{1F62F}".encode("UTF-8")Lecky
I checked again - you are correct in using "\u{1F92B}" for the shushing face. Perhaps the font doesn't support that glyph?Lecky
You did not specify anything about Montserrat-Medium. You wrote you tested with DejaVuSans, ipam, NotoSans-Medium. Does your font exist?Regular
I tried all of them and none workedMelodize
S
1

Turns out Prawn doesn't know how to parse the joined emojis (those formed by the a set of simple emojis joined by \u200D). Prawn/emoji is supposed to do that but there is a bug on the regex used to identify the emojis that causes the joined emojis to be drawn separately.

Also the index and the image gallery used is a little bit outdated.

The solution is to substitute @emoji_index.to_regexp in the class Drawer , in the prawn/emoji source code for a regex that can recognize the joined emojis and update the emoji gallery, after that run the task to update the index and you are good to go.

The fonts have nothing to do with it.

Smacker answered 24/10, 2018 at 0:1 Comment(0)
P
1

I'm creator of prawn-emoji.

Certainly prawn-emoji v2.1 or older can't draw joined-emojis like 👨‍👨‍👦 and 1️⃣. https://github.com/hidakatsuya/prawn-emoji/issues/24

So today, i released prawn-emoji v3.0. This release includes support for joined emoji like 👨‍👨‍👦(ZWJ Sequence) and 1️⃣(Combining Sequence), and switch to Twemoji.

Please see below for further details.
https://github.com/hidakatsuya/prawn-emoji/blob/master/CHANGELOG.md

Please try to use prawn-emoji v3.0 if you'd like.

Hope this help.

Paraldehyde answered 8/6, 2019 at 16:8 Comment(0)
R
0

It does work. You can look up the character codes for deja vu sans.

You can also search for which fonts support which Unicode characters. If you are seeing an empty box with Montserrat-Medium, that means that unicode character is not supported, for example the character, \u200D

Here is a helpful link to search which fonts support that character - http://www.fileformat.info/info/unicode/char/200d/fontsupport.htm

Here is another link for code \u{1F92B}, which is your shush emoji- http://www.fileformat.info/info/unicode/char/1F92B/fontsupport.htm
Both DejaVuSans and Montserrat-Medium dont support it.

require 'prawn'
require 'prawn/emoji'

Prawn::Document.generate 'foo.pdf' do
  font "./resources/Montserrat-Medium.ttf"
  text "For Montserrat-Medium"  
  text "\u{1F468}\u200D\u{1F469}\u200D\u{1F466}".encode("UTF-8")
  text "\u{1F92B}"
  text " "

  font './resources/DejaVuSans.ttf'
  text " For DejaVuSans"
  text "\u{1F468}\u200D\u{1F469}\u200D\u{1F466}".encode("UTF-8")
  text "\u{1F92B}"
end

enter image description here

Regular answered 14/10, 2018 at 14:45 Comment(2)
What I am expecting is to see "\u{1F468}\u200D\u{1F469}\u200D\u{1F466}" as the family icon, as I am using prawn/emoji, which supposedly "provides feature for drawing emoji"Melodize
I dont understand what you mean by "family icon". The gem clearly says, "In order to draw emoji, you must use a TTF - True Type Font. I recommend you use a Japanese font."Regular

© 2022 - 2024 — McMap. All rights reserved.