danbernier / WordCram

open-source word clouds for Processing
http://wordcram.org
Apache License 2.0
199 stars 53 forks source link

WordCram Fails in "Android" mode #9

Open jwiese opened 12 years ago

jwiese commented 12 years ago

When running the Hello World in "java" mode, no problems at all, but the following exception is thrown for "android" mode, probably because java.awt.font.FontRenderContext isn't included in Android.

FATAL EXCEPTION: Animation Thread java.lang.NoClassDefFoundError: java.awt.font.FontRenderContext at wordcram.WordShaper.(WordShaper.java:29) at wordcram.WordCram.getWordCramEngine(WordCram.java:740) at wordcram.WordCram.drawAll(WordCram.java:781) at processing.test.helloworld.helloworld.setup(helloworld.java:40) at processing.core.PApplet.handleDraw(Unknown Source) at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source) at processing.core.PApplet.run(Unknown Source) at java.lang.Thread.run(Thread.java:856)

Not knowing much about the structure of WordCram, how much of the code would be reusable for a standalone (non-processing-based) Android word cloud library?

danbernier commented 12 years ago

Interesting, I haven't tried running it on Android yet. Time to roll up my sleeves!

It's been a while since I tried to remove Processing from WordCram, but as I remember it, the places I couldn't (easily) remove Processing were pretty essential. Re: Android, I assumed WordCram would run on it, since I'd read all the good news about Processing on Android, but there are some old AWT bits in there.

jwiese commented 12 years ago

Thanks for the quick reply! If I can be helpful in getting this to run on Android, please let me know how I can help.

danbernier commented 11 years ago

I just started looking into this. It might be tougher than I thought.

WordCram turns each word into a java.awt.Shape, so it can create a bounding-box tree for collision detection. (It used to not use java.awt.*, but that was godawful slow - if I rememeber right, it was rendering the font to a bitmap & looking at the pixels.)

To turn a String-and-a-Font into a Shape, it uses the FontRenderContext - look at WordShaper.java for the details. FontRenderContext, and apparently java.awt.* in general, aren't supported on Android. So I'm kind of back to the drawing board for supporting Android. It looks like I'm not the only one: http://stackoverflow.com/questions/11516543/how-to-draw-the-glyph-on-canvas-from-ttf-file

Having no Android expertise myself, if you know how to do this, that'd be a tremendous help! :smile:

jwiese commented 11 years ago

Yep, this is part of why I asked about removing processing from wordcram.

In Android, I think the way to do it is using android.graphics.Rect, so the general solution is probably to have a Shape-like class in WordCram source that we can initialize from android.graphics.Rect or awt.Shape depending on the platform.

Another issue is the font options in Android. From http://wiki.processing.org/w/Android :

Using createFont() (instead of loadFont()) can help improve text quality, particularly with 2D rendering. Use PFont.list() to get a list of available fonts on the system, and select one of those, or include a .ttf or .otf file in the data directory of your sketch.

method for retrieving the bounding box: http://developer.android.com/reference/android/graphics/Paint.html#getTextBounds(java.lang.String, int, int, android.graphics.Rect).

Paint p = new Paint();
p.setTypeface(Typeface.DEFAULT); //not fully reliable what fonts are installed on a phone, but can include .ttf/.otf files
p.setSize(fontSize);
Rect r = new Rect();
p.getTextBounds(word, 0,0, r);
return new WCShape(r); //WCShape represents the WordCram Shape object that I mentioned above

Is this helpful?