Вопрос

Is reportlab capable of displaying unusual glyphs such as 👌? If so, how?

I've installed the Symbola font, which I have verified contains a glyph for 👌, but when I display it using a PDFTextObject with font set to Symbola, it displays as a box.

Here's some code that should reproduce the problem. Note that on Ubuntu, to install a font on a per-user basis, it can be put in a folder called .fonts in the users home directory. Reportlab doesn't look in this folder, so I've provided its explicit location.

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
width, height = A4
from reportlab.lib.colors import red
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
# NOTE: provide the location of Symbola.ttf on your setup...
pdfmetrics.registerFont(TTFont("Symbola", "/home/my-user-name/.fonts/Symbola.ttf"))
c = canvas.Canvas("test-symbola-font.pdf")
to = c.beginText()
to.setTextOrigin(50,height-50)
to.setFont("Symbola", 30)
to.setFillColor(red)
to.textLine(u"Some symbols: 😱🙌👌")
c.drawText(to)
c.showPage()
c.save()

The 3 emoji glyphs (a shocked face, face with hands in the air and a hand doing the OK symbol) are in Symbola, but are rendered in the pdf as boxes around question marks. This is true with both the 'default' branch of reportlab from the mercurial repo on bitbucket (pip reports version 2.7) and the normal pip-installed version (pip reports 3.0 - strange!).

In both cases glyphs from Symbola are embedded, but I havn't found a way to determine WHICH glyphs are embedded! pdffonts reports the embedded Symbola font as "TrueType", not "CID TrueType", a 16 bit TrueType - perhaps that's the problem?? I had a look at the generated pdf with acrobat's preflight utility, and indeed the 3 glyphs are missing.

The Symbola font is working nicely on my system - glyphs like these emoji display in browsers, terminal etc..., and I've checked they're actually coming from Symbola.

Update: The missing glyphs reported by Acrobat have code points U+F080, U+F081 and U+F082 (ie: those code points are in the text in the pdf). These are incorrect (should be U+1F631, U+1F64C and U_1F44C).

Это было полезно?

Решение

This was a bug in reportlab, which has been fixed. If you've hit this bug, checkout the mercurial repository and pip install from there (into a virtual environment preferably!). The fix will find it's way into a soon-to-come release also.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top