Slick Forums

Discuss the Slick 2D Library
It is currently Fri Dec 06, 2019 12:20 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Thu Jan 20, 2011 1:18 pm 
Offline
Regular
User avatar

Joined: Tue Apr 07, 2009 12:58 pm
Posts: 232
Location: Uruguay
It would be nice have constructors for HieroSettings and UnicodeFont using directly InputStreams, like the other resources like AngelCodeFont, etc.

I will attach some patches, hope you could take a look and apply them if no problems found.

Thanks!

_________________
Image


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2011 1:29 pm 
Offline
Regular
User avatar

Joined: Tue Apr 07, 2009 12:58 pm
Posts: 232
Location: Uruguay
HieroSettings.java patch:

Code:
Index: src/org/newdawn/slick/font/HieroSettings.java
===================================================================
--- src/org/newdawn/slick/font/HieroSettings.java   (revision 1529)
+++ src/org/newdawn/slick/font/HieroSettings.java   (working copy)
@@ -5,6 +5,7 @@
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.PrintStream;
 import java.util.ArrayList;
@@ -61,8 +62,18 @@
     * @throws SlickException if the file could not be read.
     */
    public HieroSettings(String hieroFileRef) throws SlickException {
+      this(ResourceLoader.getResourceAsStream(hieroFileRef), hieroFileRef);
+   }
+
+   /**
+    * Create a new set of configuration from a file
+    * @param hieroFileInputStream The stream with the Hiero settings file.
+    * @param hieroFileRef The file system or classpath location of the Hiero settings file.
+    * @throws SlickException if the file could not be read.
+    */
+   public HieroSettings(InputStream hieroFileInputStream, String hieroFileRef) throws SlickException {
       try {
-         BufferedReader reader = new BufferedReader(new InputStreamReader(ResourceLoader.getResourceAsStream(hieroFileRef)));
+         BufferedReader reader = new BufferedReader(new InputStreamReader(hieroFileInputStream));
          while (true) {
             String line = reader.readLine();
             if (line == null) break;


I kept 'hieroFileRef' parameter because it was used for the exception and because I want to maintain it similar to other slick resources constructors like image for example which receives an input stream and a string (I know it is being used for a cache in that case)

Hope you could take a look.

Thanks.

_________________
Image


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2011 1:52 pm 
Offline
Regular
User avatar

Joined: Tue Apr 07, 2009 12:58 pm
Posts: 232
Location: Uruguay
And here is the patch for UnicodeFont:

Code:
Index: src/org/newdawn/slick/UnicodeFont.java
===================================================================
--- src/org/newdawn/slick/UnicodeFont.java   (revision 1529)
+++ src/org/newdawn/slick/UnicodeFont.java   (working copy)
@@ -1,4 +1,3 @@
-
 package org.newdawn.slick;

 import java.awt.Font;
@@ -8,6 +7,7 @@
 import java.awt.font.GlyphVector;
 import java.awt.font.TextAttribute;
 import java.io.IOException;
+import java.io.InputStream;
 import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -51,14 +51,15 @@
    /**
     * Utility to create a Java font for a TTF file reference
     *
+    * @param ttfInputStream The stream with the TrueTypeFont file.
     * @param ttfFileRef The file system or classpath location of the TrueTypeFont file.
     * @return The font created
     * @throws SlickException Indicates a failure to locate or load the font into Java's font
     * system.
     */
-   private static Font createFont (String ttfFileRef) throws SlickException {
+   private static Font createFont(InputStream ttfInputStream, String ttfFileRef) throws SlickException {
       try {
-         return Font.createFont(Font.TRUETYPE_FONT, ResourceLoader.getResourceAsStream(ttfFileRef));
+         return Font.createFont(Font.TRUETYPE_FONT, ttfInputStream);
       } catch (FontFormatException ex) {
          throw new SlickException("Invalid font: " + ttfFileRef, ex);
       } catch (IOException ex) {
@@ -144,7 +145,7 @@
    public UnicodeFont (String ttfFileRef, String hieroFileRef) throws SlickException {
       this(ttfFileRef, new HieroSettings(hieroFileRef));
    }
-
+   
    /**
     * Create a new unicode font based on a TTF file and a set of heiro configuration
     *
@@ -152,9 +153,20 @@
     * @param settings The settings configured via the Hiero tool
     * @throws SlickException if the UnicodeFont could not be initialized.
     */
-   public UnicodeFont (String ttfFileRef, HieroSettings settings) throws SlickException {
+   public UnicodeFont(String ttfFileRef, HieroSettings settings) throws SlickException {
+      this(ResourceLoader.getResourceAsStream(ttfFileRef), ttfFileRef, settings);
+   }
+   
+   /**
+    * Create a new unicode font based on a TTF file and a set of heiro configuration
+    * @param ttfInputStream The stream of the ttf file.
+    * @param ttfFileRef The file system or classpath location of the TrueTypeFont file.
+    * @param settings The settings configured via the Hiero tool
+    * @throws SlickException if the UnicodeFont could not be initialized.
+    */
+   public UnicodeFont(InputStream ttfInputStream, String ttfFileRef, HieroSettings settings) throws SlickException {
       this.ttfFileRef = ttfFileRef;
-      Font font = createFont(ttfFileRef);
+      Font font = createFont(ttfInputStream, ttfFileRef);
       initializeFont(font, settings.getFontSize(), settings.isBold(), settings.isItalic());
       loadSettings(settings);
    }
@@ -168,9 +180,9 @@
     * @param italic True if the font should be rendered in bold typeface
     * @throws SlickException if the UnicodeFont could not be initialized.
     */
-   public UnicodeFont (String ttfFileRef, int size, boolean bold, boolean italic) throws SlickException {
+   public UnicodeFont(String ttfFileRef, int size, boolean bold, boolean italic) throws SlickException {
       this.ttfFileRef = ttfFileRef;
-      initializeFont(createFont(ttfFileRef), size, bold, italic);
+      initializeFont(createFont(ResourceLoader.getResourceAsStream(ttfFileRef), ttfFileRef), size, bold, italic);
    }

    /**


I can see the constructor with hieroFileRef is a bit unnecessary since you have a HieroSettings constructor with a fileRef, so UnicodeFont could be cleaner without having to have HieroSettings constructor knowledge, same thing for java.awt.Font construction, but this is only a suggestion because that would change the API and if someone is using that way now it will break his code.

Well, hope you could take a look at this patch also, however it depends on first patch, and again I kept the string parameter in some cases only because exceptions needed them.

Thanks again.

_________________
Image


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2011 2:24 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
You can achieve this by passing in Font objects.

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2011 3:17 pm 
Offline
Regular
User avatar

Joined: Tue Apr 07, 2009 12:58 pm
Posts: 232
Location: Uruguay
You are right about the UnicodeFont, I didn't see that.

What about the HieroSettings? I only see a constructor using a String, is there a way to build it from a stream?

Thanks.

_________________
Image


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2011 5:44 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
Yeah, you're absolutely right. Fixed in SVN.

Kev


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group