We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProcessing DevelopmentLibraries,  Tool Development › Distributing data (images, fonts) with library
Page Index Toggle Pages: 1
Distributing data (images, fonts) with library (Read 2324 times)
Distributing data (images, fonts) with library
Sep 16th, 2009, 6:46am
 
Hi

I downloaded the Processing library template for Eclipse, really nice work.

I have a question though: is there some way to distribute assets (.vlw fonts, images, etc) with the library so that they are found by the Processing sketch? (Or rather, from the library).

If not, wouldn't it be a good idea to have some fonts distributed with Processing (For example, the GNU FreeFonts (gnu.org/software/freefont/) so that there always is at least some fonts you know you can use? I know you can use createFont, but you never really know what fonts people might have on some weird Linux distro.

/ Fredrik
Re: Distributing data (images, fonts) with library
Reply #1 - Sep 16th, 2009, 8:12am
 
Not sure if there is a simple way. You can make resources and load them yourself, but I don't know if there is a way to pass them back to Processing.
controlP5 just defines its own bitmap fonts (in Gif files), but unfortunately they don't even cover Latin1 characters, only base Ascii.
An alternative is to just ask the users of the library to handle themselves font loading the way they want, and pass the PFont info to the library. It is the most flexible way, particularly with regard to character ranges and such.
Re: Distributing data (images, fonts) with library
Reply #2 - Sep 21st, 2009, 10:07am
 
Yes, you can include resources in your JAR file.  

To access a resource inside a JAR file (lets say your library has a class named MyClass):

Code:

ClassLoader cl = MyClass.class.getClassLoader();
URL res = cl.getResource("somefilename");


Once you have the URL object, you can read it, do what you need to it, etc..

If you're using the library tempate and building with ANT, you can modify the ANT XML file to include files in the JAR for you.. Poke around below the "no changes required" line..

You'll probably want to add something like this:

Code:
<!-- move some files to the bin/ directory, so they are included in the JAR file -->
<mkdir dir="${bin}/myfiles" />
<copy todir="${bin}/myfiles">
   <fileset dir="resources/myfiles"/>
</copy>


Hope this helps.
d.
Re: Distributing data (images, fonts) with library
Reply #3 - Sep 22nd, 2009, 5:01am
 
The problem wasn't to load resources, but to tell Processing to use a .vlw file loaded from resources.
The problem with libraries is that Processing will search resources in the sketch's data folder, not in the library's one.
Re: Distributing data (images, fonts) with library
Reply #4 - Sep 22nd, 2009, 7:30am
 
Sorry, I guess I misunderstood the question a bit.

As far as I know, its not possible to bundle resources and then let the user of your library open them "normally" using loadFont(), loadImage(), etc, since PApplet only knows to look for files in a few places (see createInputRaw() in the source code).

The solution depends on what you are trying to do with your library. Do you need the font so you can use it from inside the library or do you simply want to make it available?

In both cases, you can create a PFont object "by hand" if you have access to the vlw file. (basically provide the functionality of loadFont) You can do that from the URL object that the classloader gives you (exception handling not included!):

Code:
ClassLoader cl = MyClass.class.getClassLoader();
URL res = cl.getResource("myfont.vlw");
InputStream stream = res.openStream();
PFont font = new PFont(stream);


From there you can use the font, or if you need to make it available to users, just add a factory method to your class containing the code above:

Code:

class MyLib {
public PFont createMyLibFont();
}


If you have more than one font, either make many methods or pass the font name as an argument.

Bottom line is: you can package assets with your lib, but if you want to pass them onto the user, you'll have to provide your own methods to open the files and create the appropriate Processing objects.

d.
Re: Distributing data (images, fonts) with library
Reply #5 - Sep 30th, 2009, 6:01am
 
Cool! Problem solved.
/bridell
Page Index Toggle Pages: 1