CBDT/CBLC font support for color emojis
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
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.
Note that with the latest code, after merging #477, no error is raised anymore, but emojis are not visible in the generated PDF
Seems like fonttools has support for them: https://github.com/fonttools/fonttools/pull/397
We need to figure what changes need to be made to OutputProducer._add_fonts in order to correctly embed them...
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.
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.
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.
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:
- Add support for Type-3 fonts.
- Identify the type of color font.
- Render color glyphs into PDF objects for all four types of color fonts.