deeplook / svglib

Read SVG files and convert them to other formats.
GNU Lesser General Public License v3.0
322 stars 79 forks source link

Rendering emoji? #286

Open deeplook opened 3 years ago

deeplook commented 3 years ago

This is mainly a question for @replabrobin, but I put it here because there is no issue management for reportlab on github. ;)

So I would like to render emoji in SVG and came up with this initial attempt giving me the much expected black squares. Is there any way reportlab could make this work as expected?

# pip install emoji reportlab svglib

from io import StringIO
from textwrap import dedent

from emoji import emojize
from reportlab.graphics.renderPM import drawToPIL
from svglib.svglib import svg2rlg

drawing = svg2rlg(StringIO(dedent(emojize('''\
    <?xml version="1.0"?>
    <svg width="500" height="100">
      <text style="fill:#000000; stroke:none; font-size:28; font-family:monospace;">
        <tspan x="0" y="28">Übertitle</tspan>
        <tspan x="0" y="56">👍 (should be "thumbs up")</tspan>
        <tspan x="0" y="84">:thumbs_down: (should be "thumbs down")</tspan>
      </text>
    </svg>
'''))))
drawToPIL(drawing)
Screenshot 2021-05-26 at 08 41 31
claudep commented 3 years ago

Interesting... I tried myself with setting a specific emoji font, NotoColorEmoji.ttf, however reportlab unfortunately doesn't support the font (reportlab.pdfbase.ttfonts.TTFError: missing location table).

411A commented 1 year ago

Hi, is there any workaround to deal with Unicode characters? I'm using Cascadia Code font, but I don't know how can I pass a font... or will it work or not. I really need this but apparently, it's very hard to achieve!

deeplook commented 1 year ago

FWIW, I just found this article from 2015, so I'm not sure this is still relevant... https://kaviraj.me/generating-pdf-containing-emoji-python/