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

CBDT/CBLC font support for color emojis #224

Open
markusnordstrom opened this issue Sep 22, 2021 · 7 comments
Open

CBDT/CBLC font support for color emojis #224

markusnordstrom opened this issue Sep 22, 2021 · 7 comments

Comments

@markusnordstrom
Copy link

markusnordstrom commented Sep 22, 2021

Continued from #145

This code:

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")

...generates an error:

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'

Solving this can count as part of hacktoberfest

@Lucas-C
Copy link
Member

Lucas-C commented May 14, 2022

Note that solving this would allow to use the Noto Emoji font:

Noto Emoji is an open source font that has you covered for all your emoji needs, including support for the latest Unicode emoji specification (14.0). It has multiple weights and features 3,663 emoji.

Currently #418 seems like the best way to go.

@Lucas-C
Copy link
Member

Lucas-C commented Sep 7, 2022

Note that with the latest code, after merging #477, no error is raised anymore, but emojis are not visible in the generated PDF

@Lucas-C
Copy link
Member

Lucas-C commented Oct 18, 2022

Seems like fonttools has support for them: fonttools/fonttools#397

We need to figure what changes need to be made to OutputProducer._add_fonts in order to correctly embed them...

@philip-C96
Copy link

Note that with the latest code, after merging #477, no error is raised anymore, but emojis are not visible in the generated PDF

Hello, may i ask if the problem is still there, right? Since I m working on generating pdf file that contains Chinese character and emojis.

I used the latest version of fpdf2 with NotoColorEmoji.ttf as the backup font, the emoji will become a space (in my case), but frankly, the empty space can be copied and pasted, after pasting it to somewhere(like any kind of texting area (terminal/search bar/ or even mac's spotlight) the emoji can be shown.

In another scenario, I use TwitterColorEmoji-SVGinOT-MacOS.ttf, which is recommended in the documentation (ref: https://py-pdf.github.io/fpdf2/EmojisSymbolsDingbats.html#emojis) it works perfectly unless the v15 unicode emoji involve. (but that is another problem), but it can only produce black-and-white emoji and i want a colorful one.

@andersonhc
Copy link
Collaborator

but it can only produce black-and-white emoji and i want a colorful one.

Color fonts are not part of the PDF standard so readers won't render them. Some other PDF tools use workarounds like type3 fonts to produce them but we don't have any mechanism for that in fpdf2 at this moment.

@philip-C96
Copy link

Alright, i ll be settled with the black-and-white emoji. But what about the visibility problem with NotoColorEmoji.ttf, i didn't dig very deep about how does the library read the font, sorry about that.

@andersonhc
Copy link
Collaborator

andersonhc commented May 13, 2024

Expanding a little more on this topic. Despite their widespread use, color fonts are not supported by the PDF standard. This means that even if a PDF producer embeds all necessary color tables in a file, PDF clients will not render them correctly.

There are viable workarounds adopted by some PDF producers. A detailed 30-minute presentation available at the link below excellently outlines both the problem and potential solutions:
OpenType Color Fonts in PDF - https://pdfa.org/presentation/opentype-color-fonts-in-pdf/

Among the suggested solutions, the most effective is to convert color glyphs into Type-3 font glyphs. Type-3 fonts are unique in that each glyph is treated as a separate graphic object within the PDF.

There are four main types of color fonts, all included in the ISO 14496-22 standard, which is publicly accessible and can be downloaded here:
ISO 14496-22 OpenFontFormat - https://github.com/MPEGGroup/OpenFontFormat

The types are:

  • Adobe/Mozilla OpenType SVG
  • Microsoft OpenType COLR
  • Apple OpenType SBIX
  • Google OpenType CBDT

To successfully implement this feature in fpdf2, we would need to:

  1. Add support for Type-3 fonts.
  2. Identify the type of color font.
  3. Render color glyphs into PDF objects for all four types of color fonts.

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

4 participants