scastie icon indicating copy to clipboard operation
scastie copied to clipboard

support for reftree

Open MasseGuillaume opened this issue 8 years ago • 10 comments

/cc @stanch

reftree is a really nice way to visualize data structures. it would be nice to use it in scastie.

https://github.com/scalacenter/scastie/commit/85de6200e854574ea0986bae75a23be4b8c00841 adds graphviz to the docker container running the sbt instances. I only need to redeploy this change.

The scrimage demo (https://github.com/scalacenter/scastie/blob/master/demo/scrimage.scala#L1) shows how we can render an image in the jvm and send it back to the browser.

MasseGuillaume avatar Apr 20 '17 17:04 MasseGuillaume

@stanch Done.

https://scastie.scala-lang.org/MasseGuillaume/fNDpQ9AgRbKA4KEvP3NaeQ/1

I added a image(path: String) to the api to display images from the server using base64 encoding.

For some reason, animations throw an Exception.

2017-04-25-181414_3000x1920_scrot

MasseGuillaume avatar Apr 25 '17 22:04 MasseGuillaume

Very cool! Let me see if I can run the docker container and dig around it to find the issue with animations. I guess it’s also time for me to stop relying on Consolas...

stanch avatar Apr 25 '17 23:04 stanch

take a look at https://github.com/scalacenter/scastie/blob/master/CONTRIBUTING.md#contributing

cd docker
<edit Dockerfile>
docker build -t scalacenter/scastie-docker-sbt:0.0.14 .

<bump docker version>
https://github.com/scalacenter/scastie/blob/master/build.sbt#L122

sbt
> sbtRunner/reStart  or sbtRunner/docker then docker run --network=host scalacenter/scastie-sbt-runner:0.16.0
> ~server/reStart

MasseGuillaume avatar Apr 25 '17 23:04 MasseGuillaume

So after about 2 hours of debugging I finally traced it to a problem with text rendering. Converting SVG to BufferedImage (which is what the animations do via Apache Batik), without text — fine; with text — crashes.

Searching for “java alpine linux text rendering” lead me into this thread: https://github.com/docker-library/openjdk/issues/73#issuecomment-207816929. With -Dsun.java2d.debugfonts=true, here is what I got:

...
Apr 26, 2017 11:40:02 PM sun.font.SunFontManager findFont2D
INFO: Load fonts looking for:Lucida Sans Regular
java.lang.Exception: Stack trace
	at java.lang.Thread.dumpStack(Thread.java:1333)
	at sun.font.SunFontManager.loadFonts(SunFontManager.java:3298)
	at sun.awt.X11FontManager.loadFonts(X11FontManager.java:460)
	at sun.font.SunFontManager.findFont2D(SunFontManager.java:2348)
	at sun.font.SunFontManager.getDefaultPhysicalFont(SunFontManager.java:1220)
	at sun.font.SunFontManager.initialiseDeferredFont(SunFontManager.java:1100)
	at sun.font.SunFontManager.initialiseDeferredFonts(SunFontManager.java:927)
	at sun.font.SunFontManager.loadFonts(SunFontManager.java:3302)
	at sun.awt.X11FontManager.loadFonts(X11FontManager.java:460)
	at sun.font.SunFontManager.loadFontFiles(SunFontManager.java:3429)
	at sun.font.SunFontManager.getInstalledFontFamilyNames(SunFontManager.java:3766)
	at sun.java2d.SunGraphicsEnvironment.getAvailableFontFamilyNames(SunGraphicsEnvironment.java:225)
	at sun.java2d.SunGraphicsEnvironment.getAvailableFontFamilyNames(SunGraphicsEnvironment.java:252)
	at sun.java2d.HeadlessGraphicsEnvironment.getAvailableFontFamilyNames(HeadlessGraphicsEnvironment.java:94)
	at org.apache.batik.bridge.DefaultFontFamilyResolver.<clinit>(DefaultFontFamilyResolver.java:87)
...

I am happy to say that running apk add --update ttf-dejavu like suggested in the linked comment completely solved the problem. Additionally for pretty rendering I would be grateful if you could add the Source Code Pro font, which is the new default font in reftree, replacing proprietary Consolas.

I will send a PR with these changes.

stanch avatar Apr 27 '17 00:04 stanch

Diagram: https://scastie.scala-lang.org/zzQ4rnCFRVKQUqQjM7n0AA

Animation: https://scastie.scala-lang.org/8oZtpMStRduxM2PfphZsfA

The animation shows as a static image. ImageIO.read does no read the gif: https://github.com/scalacenter/scastie/blob/master/runtime-scala/jvm/src/main/scala/com.olegych.scastie.api.runtime/Runtime.scala#L17 This could solve the issue: http://elliot.kroo.net/software/java/GifSequenceWriter/

also

You can add additional bogus fields to the data url to force replaying of the animated gif: url(data:image/gif;bogus:ABCDEF;base64,R0lGODlhA...); see http://stackoverflow.com/a/12228790/449071

MasseGuillaume avatar May 17 '17 11:05 MasseGuillaume

This could solve the issue: http://elliot.kroo.net/software/java/GifSequenceWriter/

Not sure I understand, doesn’t the problem have to do with the reader? Also scrimage already has a class like that, I am using it to generate those gifs ;)

stanch avatar May 17 '17 13:05 stanch

ImageIO does not read the animation part of the gif.

basically what we need is to read the gif data and convert it to base64 so it can be sent to the client.

MasseGuillaume avatar May 17 '17 13:05 MasseGuillaume

Actually I am not sure if ImageIO is really needed :) base64 encoding can be done on the file itself (and the MIME type can be derived from the file extension). Image width can be left unspecified or set to some percentage of the screen... (or it can be calculated with ImageIO just like it is now, that should work for animations as well).

stanch avatar May 17 '17 13:05 stanch

Since this is closed, should I open a new issue for correct rendering of non-PNG images?

Edit: I meant animated images, or anything else not fully supported by ImageIO.

stanch avatar Sep 28 '17 09:09 stanch

Oh indeed this was still pending. Reopening this issue.

MasseGuillaume avatar Sep 28 '17 10:09 MasseGuillaume