diff options
author | Sven Gothel <[email protected]> | 2011-04-01 15:13:17 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2011-04-01 15:13:17 +0200 |
commit | d4c400b9e70550bc93ebc2c0df2cd916b9ea0b3b (patch) | |
tree | 38f1b53b1aeb5af4b12e9a3901ee45a2da690ee3 | |
parent | f47753b63c9530a6af36cb69135dee0421abfe6b (diff) |
Load fonts via File or URL .
5 files changed, 97 insertions, 29 deletions
diff --git a/src/jogl/classes/com/jogamp/graph/font/FontFactory.java b/src/jogl/classes/com/jogamp/graph/font/FontFactory.java index 1752a693c..9bf4b1f73 100644 --- a/src/jogl/classes/com/jogamp/graph/font/FontFactory.java +++ b/src/jogl/classes/com/jogamp/graph/font/FontFactory.java @@ -27,7 +27,12 @@ */ package com.jogamp.graph.font; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.net.URL; import java.security.AccessController; +import java.security.PrivilegedAction; import com.jogamp.common.util.ReflectionUtil; @@ -58,8 +63,6 @@ public class FontFactory { fontConstr = (FontConstructor) ReflectionUtil.createInstance(fontImplName, FontFactory.class.getClassLoader()); } - public static final FontConstructor getFontConstr() { return fontConstr; } - public static final FontSet getDefault() { return get(UBUNTU); } @@ -73,8 +76,34 @@ public class FontFactory { } } - public static final Font get(String path) { - return fontConstr.create(path); + public static final Font get(File file) throws IOException { + return fontConstr.create(file); } + public static final Font get(final URL url) throws IOException { + final IOException[] ioeA = new IOException[1]; + + Font f = (Font) AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + try { + return fontConstr.create(url); + } catch (IOException ioe) { + ioeA[0] = ioe; + } + return null; + } + }); + if(null != ioeA[0]) { + throw ioeA[0]; + } + return f; + } + + public static boolean isPrintableChar( char c ) { + Character.UnicodeBlock block = Character.UnicodeBlock.of( c ); + return (!Character.isISOControl(c)) && + c != 0 && + block != null && + block != Character.UnicodeBlock.SPECIALS; + } } diff --git a/src/jogl/classes/jogamp/graph/font/FontConstructor.java b/src/jogl/classes/jogamp/graph/font/FontConstructor.java index a382d292e..721b2072f 100644 --- a/src/jogl/classes/jogamp/graph/font/FontConstructor.java +++ b/src/jogl/classes/jogamp/graph/font/FontConstructor.java @@ -27,8 +27,13 @@ */ package jogamp.graph.font; +import java.io.File; +import java.io.IOException; +import java.net.URL; + import com.jogamp.graph.font.Font; public interface FontConstructor { - Font create(String name); + Font create(File file) throws IOException ; + Font create(URL url) throws IOException ; } diff --git a/src/jogl/classes/jogamp/graph/font/JavaFontLoader.java b/src/jogl/classes/jogamp/graph/font/JavaFontLoader.java index 33505e797..a9ab902a9 100644 --- a/src/jogl/classes/jogamp/graph/font/JavaFontLoader.java +++ b/src/jogl/classes/jogamp/graph/font/JavaFontLoader.java @@ -27,6 +27,11 @@ */ package jogamp.graph.font; +import java.io.File; +import java.io.IOException; + +import javax.media.opengl.GLException; + import com.jogamp.common.util.IntObjectHashMap; import com.jogamp.graph.font.Font; import com.jogamp.graph.font.FontSet; @@ -118,12 +123,17 @@ public class JavaFontLoader implements FontSet { } Font abspath(String fname, int family, int style) { - final Font f = FontFactory.getFontConstr().create(javaFontPath+fname); - if(null != f) { - fontMap.put( ( family << 8 ) | style, f ); + final String err = "Problem loading font "+fname+", file "+javaFontPath+fname ; + + try { + final Font f = FontFactory.get( new File(javaFontPath+fname) ); + if(null != f) { + fontMap.put( ( family << 8 ) | style, f ); + return f; + } + throw new GLException(err); + } catch (IOException ioe) { + throw new GLException(err, ioe); } - return f; - - } - + } } diff --git a/src/jogl/classes/jogamp/graph/font/UbuntuFontLoader.java b/src/jogl/classes/jogamp/graph/font/UbuntuFontLoader.java index e09ea85e5..3614add5c 100644 --- a/src/jogl/classes/jogamp/graph/font/UbuntuFontLoader.java +++ b/src/jogl/classes/jogamp/graph/font/UbuntuFontLoader.java @@ -27,11 +27,15 @@ */ package jogamp.graph.font; +import java.io.IOException; +import javax.media.opengl.GLException; + import com.jogamp.common.util.IntObjectHashMap; import com.jogamp.graph.font.Font; import com.jogamp.graph.font.FontSet; import com.jogamp.graph.font.FontFactory; import com.jogamp.opengl.util.Locator; +import java.net.URL; public class UbuntuFontLoader implements FontSet { @@ -55,6 +59,8 @@ public class UbuntuFontLoader implements FontSet { }; final static String relPath = "fonts/ubuntu/" ; + // debug final static String relPath = "/usr/local/projects/JOGL/jogl/src/jogl/classes/jogamp/graph/font/fonts/ubuntu/" ; + private UbuntuFontLoader() { } @@ -113,20 +119,22 @@ public class UbuntuFontLoader implements FontSet { return font; } - - Font abspath(String fname) { - return FontFactory.getFontConstr().create( - Locator.getResource(UbuntuFontLoader.class, relPath+fname).getPath() ); - } - + Font abspath(String fname, int family, int style) { - final Font f = FontFactory.getFontConstr().create( - Locator.getResource(UbuntuFontLoader.class, relPath+fname).getPath() ); - if(null != f) { - fontMap.put( ( family << 8 ) | style, f ); + final String err = "Problem loading font "+fname+", stream "+relPath+fname; + try { + URL url = Locator.getResource(UbuntuFontLoader.class, relPath+fname); + if(null == url) { + throw new GLException(err); + } + final Font f= FontFactory.get ( url ) ; + if(null != f) { + fontMap.put( ( family << 8 ) | style, f ); + return f; + } + throw new GLException(err); + } catch(IOException ioe) { + throw new GLException(err, ioe); } - return f; - } - - + } } diff --git a/src/jogl/classes/jogamp/graph/font/typecast/TypecastFontConstructor.java b/src/jogl/classes/jogamp/graph/font/typecast/TypecastFontConstructor.java index f3bde47d1..179e4ed2c 100644 --- a/src/jogl/classes/jogamp/graph/font/typecast/TypecastFontConstructor.java +++ b/src/jogl/classes/jogamp/graph/font/typecast/TypecastFontConstructor.java @@ -29,20 +29,24 @@ package jogamp.graph.font.typecast; import java.io.File; import java.io.IOException; +import java.net.URL; + +import javax.media.opengl.GLException; import jogamp.graph.font.FontConstructor; import jogamp.graph.font.typecast.ot.OTFontCollection; +import com.jogamp.common.util.IOUtil; import com.jogamp.graph.font.Font; public class TypecastFontConstructor implements FontConstructor { - public Font create(String path) { - OTFontCollection fontset; + public Font create(File ffile) throws IOException { + OTFontCollection fontset; try { - fontset = OTFontCollection.create(new File(path)); + fontset = OTFontCollection.create(ffile); return new TypecastFont(fontset); } catch (IOException e) { e.printStackTrace(); @@ -50,4 +54,16 @@ public class TypecastFontConstructor implements FontConstructor { return null; } + public Font create(URL furl) throws IOException { + final File tf = File.createTempFile( "joglfont", ".ttf"); + final int len = IOUtil.copyURLToFile(furl, tf); + if(len==0) { + tf.delete(); + throw new GLException("Font of stream "+furl+" was zero bytes"); + } + final Font f = create(tf); + tf.delete(); + return f; + } + }
\ No newline at end of file |