Skip to content

Emojis in PDF - #3853

Merged
laurmaedje merged 50 commits into
mainfrom
emoji-in-pdf
Apr 17, 2024
Merged

laurmaedje merged 50 commits into
mainfrom
emoji-in-pdf

Conversation

@elegaanz

@elegaanz elegaanz commented Apr 2, 2024 •

Copy link
Copy Markdown
Member

So far, this only renders emojis stored in the SBIX table. The code may be dirty sometimes. I mainly open this PR so that we can start reviewing and discussing what was done so far before I go deeper.

Things that don't work in the current SBIX implementation :

  • the baseline seems wrong (but I have some ideas of what could fix it) According to this page the font bounding box lowest point should probably be used, but in practice it seems too close to 0 to do anything useful. I decided to use the descender size instead, which looks nice with Apple Color Emoji. It will probably need some testing with other fonts to make sure this is a viable solution.
  • the resolution is not that great. I feel like if you zoom on the PDF file, even a little, you start to see pixels. I don't know if I'm a not passing the correct resolution/font size to ttf-parser or if their resolution selection algorithm is bad.
  • it is not possible to select, copy or search emojis.
  • rendering doesn't work with Poppler based viewers
The file I am using for testing, if you want to test this branch
= Emoji reference

#align(center, emoji.heart)

I #emoji.heart emojis #emoji.warning

#text(font: "Apple Color Emoji", [123 #emoji.baby 456])

#show table.cell: align.with(center)

#let emojis = dictionary(emoji).pairs().map(pair => table.cell[
    #pair.at(1) \
    #pair.at(0)
])

#table(
    columns: 8,
    ..emojis
)

Will fix #144

Render emojis stored in the SBIX table
@Enivex

Enivex commented Apr 2, 2024

Copy link
Copy Markdown
Collaborator

the resolution is not that great

Should they be rasterized at all unless the output format is png?

Comment thread crates/typst-pdf/src/page.rs Resolved
@LaurenzV

LaurenzV commented Apr 2, 2024

Copy link
Copy Markdown
Collaborator

Should they be rasterized at all unless the output format is png?

sbix only contains bitmap images, so they already are rasterized.

@LaurenzV

LaurenzV commented Apr 2, 2024

Copy link
Copy Markdown
Collaborator

the resolution is not that great. I feel like if you zoom on the PDF file, even a little, you start to see pixels. I don't know if I'm a not passing the correct resolution/font size to ttf-parser or if their resolution selection algorithm is bad.

You can try passing u16::MAX and see whether this fixes it. If not, then it's probably just the font being very pixelated...

@elegaanz elegaanz left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small self-review, mostly questions I had while working on this

Comment thread crates/typst-pdf/src/font.rs Resolved
Comment thread crates/typst-pdf/src/font.rs Resolved
Comment thread crates/typst-pdf/src/font.rs Resolved
Comment thread crates/typst-pdf/src/lib.rs Resolved
Comment thread crates/typst-pdf/src/page.rs Resolved
@elegaanz

elegaanz commented Apr 2, 2024

Copy link
Copy Markdown
Member Author

the resolution is not that great. I feel like if you zoom on the PDF file, even a little, you start to see pixels. I don't know if I'm a not passing the correct resolution/font size to ttf-parser or if their resolution selection algorithm is bad.

You can try passing u16::MAX and see whether this fixes it. If not, then it's probably just the font being very pixelated...

I used to have a harcoded 256 (or some relatively high value like that), and it was looking nice. So it is not the font's fault.

@LaurenzV

LaurenzV commented Apr 2, 2024 •

Copy link
Copy Markdown
Collaborator

I just checked, and it seems like at least for this font, pixels_per_em stands for the actual resolution of the emoji. In our case, the calculated ppem is 11, so we get a bitmap that is 20x20 in size. So maybe let's just hardcode a reasonable value in there, like you tried before? I'm not sure if the definition for ppem varies from font to font, though...

EDIT: The documentation for glyph_raster_image seems to confirm that:

Returns a reference to a glyph’s raster image.
A font can define a glyph using a raster or a vector image instead of a simple outline. Which is primarily used for emojis. This method should be used to access raster images.
pixels_per_em allows selecting a preferred image size. The chosen size will be closer to an upper one. So when font has 64px and 96px images and pixels_per_em is set to 72, 96px image will be returned. To get the largest image simply use std::u16::MAX.

So looks like it really just stands for the bitmap resolution...

Comment thread crates/typst-pdf/src/font.rs Resolved
Comment thread crates/typst-pdf/src/lib.rs Resolved
Comment thread crates/typst-pdf/src/page.rs Resolved
Comment thread crates/typst-pdf/src/page.rs Resolved
Comment thread crates/typst-pdf/src/page.rs Resolved
Comment thread crates/typst-pdf/src/page.rs Resolved
Comment thread crates/typst-pdf/src/font.rs Resolved
Comment thread crates/typst-pdf/src/font.rs Resolved
Comment thread crates/typst-pdf/src/lib.rs Resolved
elegaanz added 21 commits April 3, 2024 12:01
And other small improvements
A bug in Poppler based PDF readers prevents them
from displaying color fonts without that change.
Taking twice the font size makes them
render nicely up to 200% zoom level,
and doesn't make the PDF file much bigger.
Also change the algorithm used for that in PDF,
to be consistent between the two formats.
This was buggy for fonts other than Apple Color Emoji

This reverts commit f780ed2.
This only worked with Apple Color Emoji.
For now we prefer to have this font a bit broken,
than all other ones.

This reverts commit b55f864.
And two other small TODOs
Comment thread crates/typst-pdf/src/lib.rs Resolved
Comment thread crates/typst-pdf/src/lib.rs Resolved
Comment thread crates/typst/src/text/font/color.rs Resolved
Comment thread crates/typst/src/text/font/color.rs Resolved
Comment thread crates/typst-pdf/src/font.rs Resolved
Comment thread crates/typst-pdf/src/lib.rs Resolved
Comment thread crates/typst-pdf/src/page.rs Resolved
Comment thread crates/typst-pdf/src/page.rs Resolved
Comment thread crates/typst/src/text/font/color.rs Resolved
Comment thread crates/typst/src/text/font/color.rs Resolved
Comment thread crates/typst/src/text/font/color.rs Resolved
Comment thread crates/typst/src/text/font/color.rs Resolved
Comment thread crates/typst/src/text/font/color.rs Resolved
Comment thread crates/typst/src/text/font/color.rs Resolved
Comment thread crates/typst/src/text/font/color.rs Resolved
@elegaanz
elegaanz requested a review from laurmaedje April 16, 2024 14:48
@Andrew15-5

Copy link
Copy Markdown
Contributor

Oh, wow! It's already kinda working! This is awesome!

#emoji.heart.green

💚

#emoji.face.smile

image

The only input I can give is that besides not all emojis showing up, the selection looks like a letter selection (thin):

image

But it's selectable and copyable, so that by itself is already amazing!

@elegaanz

Copy link
Copy Markdown
Member Author

Yes the selection really depends on the reader you are using, Firefox and Chrome render it correctly for instance. There seem to be no good solution here unfortunately.

That was breaking the last glyph of Type3 fonts.
Comment thread crates/typst-pdf/Cargo.toml Resolved
Comment thread crates/typst-pdf/src/font.rs Resolved
Comment thread crates/typst-pdf/src/lib.rs Resolved
Comment thread crates/typst-pdf/src/lib.rs Resolved
Comment thread crates/typst-pdf/src/lib.rs Resolved
@laurmaedje
laurmaedje added this pull request to the merge queue Apr 17, 2024
Merged via the queue into main with commit 21c78ab Apr 17, 2024
@laurmaedje
laurmaedje deleted the emoji-in-pdf branch April 17, 2024 15:16
@Andrew15-5

Copy link
Copy Markdown
Contributor

Yes the selection really depends on the reader you are using

It appears that it also depends on the compiler you are using. Here is a small example with Typst:

image

And here is an example from LuaLaTeX:

image image

Both are viewed in Okular. Interestingly, in Firefox it also only selects half of the colored char and the next space is the second half of the same character. In other words, the visual whitespace between chars is not selected at all, it selects as a part of emoji. Very weird. Another weirdness, is that the LuaLaTeX's output of green hearts isn't selectable/copyable at all! I assume because browser's built-in PDF reader isn't supposed to be fully compatible with the spec, it just has to be able to show the output, and all the quirks aren't considered.

I just tried copying the LuaLaTeX's version to Typst:

image

image

It also selects the whitespace as if it was half of the heart. So it technically selects 💚 but visually it selects as just 💚.

So in the end both compilers create different selectable or sometimes unselectable emojis, so I don't know if or how Typst's implementation can make the visual selectability better/prettier.

saruman9 added a commit to saruman9/android_fuzzing_pres that referenced this pull request Aug 3, 2024
Problem with a emoji in PDF was resolved: typst/typst#3853
Dikluwe pushed a commit to Dikluwe/typst-crystalline that referenced this pull request Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Emoji do not render in PDFs

5 participants