Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for emojis #145

Closed
Gabbalo opened this issue May 10, 2021 · 7 comments
Closed

Support for emojis #145

Gabbalo opened this issue May 10, 2021 · 7 comments

Comments

@Gabbalo
Copy link

Gabbalo commented May 10, 2021

Is there any chance to implement the support for emojis to the generator? There is a free font at google called Noto which might be used (not sure).
As emojis are used widely all over the web and some pages even accept them as password, it would be great, if this wonderful asset whould have the support for it, too, wouldn't it?

@Lucas-C
Copy link
Member

Lucas-C commented May 10, 2021

Good question! Thanks for asking.
Some elements to answer:

  • the Zapf Dingbats is a builtin font in fpdf2,
    so that is one way to display some signs & symbols using a font right now:
pdf.set_font("ZapfDingbats")
  • currently, fpdf2 does not seem able to parse NotoColorEmoji.ttf:
from fpdf import FPDF
pdf = FPDF()
pdf.add_font("NotoColorEmoji", fname="NotoColorEmoji.ttf", uni=True)
pdf.set_font("NotoColorEmoji")
pdf.add_page()
pdf.text(50, 50, "😃")
pdf.output("emoji.pdf")

Raises:

Traceback (most recent call last):
  File "./emoji.py", line 18, in <module>
    pdf.output("emoji.pdf")
  File "/opt/fpdf2/fpdf/fpdf.py", line 1850, in output
    self.close()
  File "/opt/fpdf2/fpdf/fpdf.py", line 499, in close
    self._enddoc()  # close document
  File "/opt/fpdf2/fpdf/fpdf.py", line 2540, in _enddoc
    self._putresources()  # trace_size is performed inside
  File "/opt/fpdf2/fpdf/fpdf.py", line 2435, in _putresources
    self._putfonts()
  File "/opt/fpdf2/fpdf/fpdf.py", line 2116, in _putfonts
    ttfontstream = ttf.makeSubset(font["ttffile"], subset)
  File "/opt/fpdf2/fpdf/ttfonts.py", line 498, in makeSubset
    self.getLOCA(indexToLocFormat, numGlyphs)
  File "/opt/fpdf2/fpdf/ttfonts.py", line 895, in getLOCA
    start = self.seek_table("loca")
  File "/opt/fpdf2/fpdf/ttfonts.py", line 128, in seek_table
    tpos = self.get_table_pos(tag)
  File "/opt/fpdf2/fpdf/ttfonts.py", line 115, in get_table_pos
    offset = self.tables[tag]["offset"]
KeyError: 'loca'

Seems like TTFontFile.makeSubset("NotoColorEmoji.ttf", subset) looks for a loca table and does not find one.

I think that is because the Noto font is a CBDT/CBLC font, and we do not support them yet.

Of course, anyone is welcome to try to dig deeper on the subject,
and PRs are very welcome to improve fpdf2 support of emoji fonts!

As mentioned before by @alexp1917, the other maintainer of fpdf2, we could consider using an external lib for font parsing & manipulation, in order to improve fpdf2 support of fonts in terms of features & robustness.

@Lucas-C
Copy link
Member

Lucas-C commented Jul 14, 2021

Implemented by @moe-25 as part of #183

@Lucas-C Lucas-C closed this as completed Jul 14, 2021
@Lucas-C
Copy link
Member

Lucas-C commented Sep 1, 2021

This has just been released in fpdf2.4.3

@markusnordstrom
Copy link

markusnordstrom commented Sep 10, 2021

Hi,

Using the same example code from the second post of this thread, I still get the same error and no emojis on version 2.4.3. The only change I see in this is the line number 895 changing to 909:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/fpdf/ttfonts.py", line 909, in getLOCA
    start = self.seek_table("loca")
  File "/usr/local/lib/python3.7/site-packages/fpdf/ttfonts.py", line 128, in seek_table
    tpos = self.get_table_pos(tag)
  File "/usr/local/lib/python3.7/site-packages/fpdf/ttfonts.py", line 115, in get_table_pos
    offset = self.tables[tag]["offset"]
KeyError: 'loca'

@Lucas-C
Copy link
Member

Lucas-C commented Sep 10, 2021

CBDT/CBLC fonts are still not supported by fpdf2.

You can however use the DejaVuSans font:

from fpdf import FPDF
pdf = FPDF()
pdf.add_font("DejaVuSans", fname="test/fonts/DejaVuSans.ttf", uni=True)
pdf.set_font("DejaVuSans")
pdf.add_page()
pdf.text(50, 50, "😃")
pdf.output("emoji.pdf")

@markusnordstrom
Copy link

Thanks for the reply! As DejaVuSans seems to have only the most basic emojis, I will have to look for another solution. Are there any plans to include CBDT/CBLC font support, or any issue I could follow meanwhile? Looking at the header, I don't think this issue is actually solved yet, but I'll leave it to you to consider whether it should be reopened.

Example code still creating just squares:

from fpdf import FPDF
pdf = FPDF()
pdf.add_font("DejaVuSans", fname="test/fonts/DejaVuSans.ttf", uni=True)
pdf.set_font("DejaVuSans")
pdf.add_page()
pdf.text(50, 50, "🐟🎧🌟")
pdf.output("emoji.pdf")

@Lucas-C
Copy link
Member

Lucas-C commented Sep 15, 2021

I think I consider that fpdf2 now has some "basic" support for emojis (with some fonts),
which is why I closed this.

I'm totally OK if you want to open an issue about CBDT/CBLC font support,
if you have a need for them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants