From 628509b39ea7c16210315d191860511d6be4aa69 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 24 Jul 2014 22:15:40 +0300 Subject: Added possibility to load font using InputStream parameter --- .../classes/com/jogamp/graph/font/FontFactory.java | 5 +++++ .../classes/jogamp/graph/font/FontConstructor.java | 2 ++ .../font/typecast/TypecastFontConstructor.java | 24 ++++++++++++++++++++++ .../graph/font/typecast/ot/OTFontCollection.java | 17 +++++++++++---- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/jogl/classes/com/jogamp/graph/font/FontFactory.java b/src/jogl/classes/com/jogamp/graph/font/FontFactory.java index db775e8dc..cfd06cf6c 100644 --- a/src/jogl/classes/com/jogamp/graph/font/FontFactory.java +++ b/src/jogl/classes/com/jogamp/graph/font/FontFactory.java @@ -29,6 +29,7 @@ package com.jogamp.graph.font; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.net.URI; import java.net.URLConnection; @@ -91,6 +92,10 @@ public class FontFactory { return fontConstr.create(file); } + public static final Font get(final InputStream stream) throws IOException { + return fontConstr.create(stream); + } + public static final Font get(final URLConnection conn) throws IOException { return fontConstr.create(conn); } diff --git a/src/jogl/classes/jogamp/graph/font/FontConstructor.java b/src/jogl/classes/jogamp/graph/font/FontConstructor.java index b452ae548..76b5dbb8b 100644 --- a/src/jogl/classes/jogamp/graph/font/FontConstructor.java +++ b/src/jogl/classes/jogamp/graph/font/FontConstructor.java @@ -29,11 +29,13 @@ package jogamp.graph.font; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.net.URLConnection; import com.jogamp.graph.font.Font; public interface FontConstructor { Font create(File file) throws IOException ; + Font create(InputStream stream) throws IOException ; Font create(URLConnection url) throws IOException ; } diff --git a/src/jogl/classes/jogamp/graph/font/typecast/TypecastFontConstructor.java b/src/jogl/classes/jogamp/graph/font/typecast/TypecastFontConstructor.java index 6154484d5..0b42a65d4 100644 --- a/src/jogl/classes/jogamp/graph/font/typecast/TypecastFontConstructor.java +++ b/src/jogl/classes/jogamp/graph/font/typecast/TypecastFontConstructor.java @@ -29,6 +29,7 @@ package jogamp.graph.font.typecast; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.net.URLConnection; import java.security.AccessController; import java.security.PrivilegedAction; @@ -66,6 +67,29 @@ public class TypecastFontConstructor implements FontConstructor { throw new InternalError("Unexpected Object: "+o); } + @Override + public Font create(final InputStream istream) throws IOException { + final Object o = AccessController.doPrivileged(new PrivilegedAction() { + @Override + public Object run() { + OTFontCollection fontset; + try { + fontset = OTFontCollection.create(istream); + return new TypecastFont(fontset); + } catch (final IOException e) { + return e; + } + } + }); + if(o instanceof Font) { + return (Font)o; + } + if(o instanceof IOException) { + throw (IOException)o; + } + throw new InternalError("Unexpected Object: "+o); + } + @Override public Font create(final URLConnection fconn) throws IOException { return AccessController.doPrivileged(new PrivilegedAction() { diff --git a/src/jogl/classes/jogamp/graph/font/typecast/ot/OTFontCollection.java b/src/jogl/classes/jogamp/graph/font/typecast/ot/OTFontCollection.java index e5ad2900a..87c68eae8 100644 --- a/src/jogl/classes/jogamp/graph/font/typecast/ot/OTFontCollection.java +++ b/src/jogl/classes/jogamp/graph/font/typecast/ot/OTFontCollection.java @@ -25,6 +25,7 @@ import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; @@ -58,9 +59,9 @@ public class OTFontCollection { /** * @param file The OpenType font file */ - public static OTFontCollection create(final File file) throws IOException { + public static OTFontCollection create(final InputStream istream) throws IOException { final OTFontCollection fc = new OTFontCollection(); - fc.read(file); + fc.read(istream); return fc; } @@ -119,11 +120,19 @@ public class OTFontCollection { } _resourceFork = true; } + + read(new FileInputStream(file)); + } + + /** + * @param inputstream The OpenType font stream + */ + protected void read(InputStream file) throws IOException { final DataInputStream dis = new DataInputStream( new BufferedInputStream( - new FileInputStream(file), (int) file.length())); - dis.mark((int) file.length()); + file)); + //dis.mark((int) file.length()); if (_resourceFork || _pathName.endsWith(".dfont")) { -- cgit v1.2.3