Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"
like image 374
Absalon Castañon Avatar asked Sep 14 '18 21:09

Absalon Castañon


2 Answers

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.

like image 109
Absalon Castañon Avatar answered Nov 08 '22 08:11

Absalon Castañon


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.

like image 45
hidakatsuya Avatar answered Nov 08 '22 06:11

hidakatsuya