racket / drracket

DrRacket, IDE for Racket
http://www.racket-lang.org/
Other
447 stars 94 forks source link

Emoji rendering in drracket #443

Open AlienKevin opened 3 years ago

AlienKevin commented 3 years ago

I was thrilled to see decent unicode support in drracket but was disappointed when emojis can't render at all. Is there a configuration I missed or this is not supported currently?

jestarray commented 2 years ago

Yes please, DrRacket displays emojis emojis with no color and such: ❌ ✅ image

rfindler commented 2 years ago

I think things have improved since the last release. Give the snapshots a try.

jestarray commented 2 years ago

ahh, my linux install did not have an emoji font (arch) ! it's working great in 8.2 and 8.1 on Linux: for those on arch linux sudo pacman -S noto-fonts-emoji image

However on my Windows 10 it doesn't have the colors (racket 8.3 snapshot):

windows_racket_8_3_emoji

unsure how to go about fix it for windows... For those willing to test, here is a row of emojis to copy into racket https://getemoji.com/

I can imagine some fun exercises for my students revolving around emoji animation, using (text "🥚" 100 "red") , but the color parameter is unnecessary for single character emojis, along with the size being capped but it's workable. There shouldn't be too much perf overhead when rendering text in place of bitmaps and other 2htdp shape primitives right? Another problem is (flip-horizontal (text "🦈" 100 "red")) results in this error: flip: cannot flip shapes that contain text. I will post this as an issue in 2htdp/image

emoji_test

mflatt commented 2 years ago

On Windows, the API that Cairo uses to draw text does not support color mode (and Racket uses Pango+Cairo for drawing text). The same is true on Mac OS, but there's separate drawing code for emojis that uses Mac APIs directly (https://github.com/racket/draw/blob/master/draw-lib/racket/draw/private/emoji.rkt), because that was a lot easier than trying to patch Cairo. So, someone who is interested in this could look at either patching Cairo or adding Windows-specific code to "emoji.rkt".

jestarray commented 2 years ago

According to this 1.15.8 released in 2017 has some support: https://anzwix.com/a/Cairo/1.15.8%20Release

+* Support colored emoji glyphs, stored as PNG images in OpenType fonts.

Time to update cairo deps to latest? Hopefully there aren't too many breaking changes https://github.com/freedesktop/cairo/blob/master/NEWS

plane commented 2 years ago

Racket is using Cairo 1.14.12 right now. 1.16.0 has been the stable release for three years now, and the release notes claim that existing APIs were not changed from 1.14, so hopefully it'll be a relatively painless upgrade.

mflatt commented 2 years ago

Merely upgrading to 1.16 does not appear to draw emojis in color. It's possible that it's a question of using Cairo the right way, or maybe also upgrading the Pango library.

In case it helps anyone explore, here's a build of Cairo 1.16 that can be dropped in place of "libcairo-2.dll" in Racket's "lib" directory: https://www.cs.utah.edu/~mflatt/tmp/libcairo-2.dll

jestarray commented 2 years ago

https://github.com/freedesktop/cairo/blob/master/test/ft-color-font.c https://gitlab.gnome.org/GNOME/pango/-/issues/302 https://github.com/Kozea/cairocffi/issues/148 https://gitlab.freedesktop.org/cairo/cairo/-/issues/404 (this seems like a working example on how you would use cairo to draw emoji on windows)

p.s. To achieve colored Emoji on native Windows, there is much bigger work involved, as GDI and Uniscribe does not support colored Emoji, and thus requires porting to Direct2D and DirectWrite, in PangoWin32 and most probably Cairo.

Yeah, I tried replacing the dll you linked but it doesn't seem to work either. I found some relevant issues but don't have enough time to look into it

jestarray commented 2 years ago

@mflatt https://gitlab.freedesktop.org/cairo/cairo/-/issues/518

#include <cairo.h>
#define _USE_MATH_DEFINES
#include <math.h>

int main() {

cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 200, 100);
cairo_t* cr = cairo_create(surface);
cairo_surface_destroy(surface);

// Draw some text
cairo_select_font_face(cr, "Segoe UI Emoji", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr, 20);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_move_to(cr, 0, 50);
cairo_show_text(cr, "gg😀 😃 😄");

cairo_surface_write_to_png(cairo_get_target(cr), "out.png");
cairo_destroy(cr);
return 0;
}

out

Alright so on Windows it is not implemented yet, on Linux though it seems to render emoji just fine. If you can test on mac and it works, then we might not the emoji patches anymore.

Interestingly my distro(arch linux) has been shipping Cairo 1.17.4 for the past year and no major hiccups yet. If you're going to upgrade to 1.17.2+, pixman would need to be bumped to 0.36.0 and might as well update Pango and the rest.