Skip to content

Markdown blocks have trouble rendering unicode emoji #749

Closed

Description

Summary

When using unicode emoji in a markdown block, the output appears to be getting trimmed. I tried to see if this was an issue with goldmark, but I don't think it is. I see that there is goldmark-emoji for :joy:-style emojis, but it didn't look like d2 was using this.

Issue appears to be for all layout engines.

Open to working on this if provided some guidance (would be my foray into the codebase).

Resources

D2 test file
Single emoji: |md
  🐵
|

Single emoji with text at beginning: |md
  This is a monkey emoji: 🐵
|

Single emoji with text at end: |md
  🐵 That was a monkey emoji.
|

Single emoji with text around: |md
  This is a monkey emoji: 🐵 It's pretty cool.
|

Multiple emojis: |md
  🐵🐵🐵🐵
|

Multiple emojis with text at beginning: |md
  These are monkey emojis: 🐵🐵🐵🐵
|

Multiple emojis with text at end: |md
  🐵🐵🐵🐵 Those were monkey emojis.
|

Multiple emojis with text around: |md
  These are monkey emojis: 🐵🐵🐵🐵 They are pretty cool.
|

— Playground Link

Exported SVG & PNG

Exported SVG
Exported PNG

Activity

  1. bo-ku-ra commented on Feb 3, 2023

    @bo-ku-ra
    Contributor

    is the following relevant?
    #517
    #513

  2. alixander commented on Feb 5, 2023

    @alixander
    Collaborator

    @jameschensmith if you're open to looking into this, I would look here to start: https://github.com/terrastruct/d2/blob/8fa00ca4c3ec9e5ff55f6d2c74be983a3132247f/lib/textmeasure/markdown.go#L88

    For Markdown to work, D2 has to manually measure everything and match it with CSS. For example, H1 tags have x padding, y line height, z margin. https://github.com/terrastruct/d2/blob/8fa00ca4c3ec9e5ff55f6d2c74be983a3132247f/lib/textmeasure/markdown.go#L33

    The way fonts work, we measure each glyph: https://github.com/terrastruct/d2/blob/8fa00ca4c3ec9e5ff55f6d2c74be983a3132247f/lib/textmeasure/textmeasure.go#L172

    If you can reproduce this with labels, then it's not a markdown problem. But that might not be ascertainable, since we pad labels, so it won't have that noticeable cutoff.

    I suspect the emoji unicode gets measured incorrectly. I'm not really sure what the correct measurement should be, whether it's constant or some unit size of the font-size.

    Good luck and please let me know if you get stuck, happy to dig into it more with you!

  3. alixander commented on Feb 5, 2023

    @alixander
    Collaborator

    oh interesting. maybe the font we use, Source Sans Pro, actually just has a subset of emojis supported.

    adobe-fonts/source-sans#85

    Maybe we need to package an emoji font like https://github.com/samuelngs/apple-emoji-linux, detect if the user used any emojis, and then inject that font into their SVG if so.

    https://github.com/terrastruct/d2/blob/0da4e150cd01c8ec4c3b6d25487d11bf5d4ea2a7/d2renderers/d2svg/d2svg.go#L1062

  4. alixander commented on Feb 5, 2023

    @alixander
    Collaborator

    hah, wow interesting, til about emojis

  5. alixander commented on Feb 6, 2023

    @alixander
    Collaborator

    ohh that makes sense. I don't think the shim is necessary though, better to overflow into the padding a bit imo.

    My fear is just that even if we measure this "right", if it's not part of the font we use, and users on different machines will just fallback to their OS's emoji font, which will over/underflow anyway. I don't think there's an option other than having the TTF support a large corpus of emojis

  6. alixander commented on Feb 6, 2023

    @alixander
    Collaborator

    Might have to implement this #638 first. Emoji packs might be huge to embed just for a 🙈

  7. alixander commented on Feb 7, 2023

    @alixander
    Collaborator

    @bo-ku-ra oh wow, good find. this looks incredibly relevant

  8. bo-ku-ra commented on Feb 7, 2023

    @bo-ku-ra
    Contributor

    japanese uses double-byte characters.
    so there seems to be a lot of information about it on japanese websites.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions