diff options
author | Sven Gothel <[email protected]> | 2023-02-12 00:53:13 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2023-02-12 00:53:13 +0100 |
commit | d9be96fcbcf6ca71ef72aa45ca02fcdd25c3b0ff (patch) | |
tree | 600d4be5e7e6fb2dcc64265a1ab8c2cf2d69728b /src/jogl/classes/jogamp/graph/font | |
parent | aea3a62c0139d1dfc208ede21d3f402d9a66df12 (diff) |
Typecast: *Font*: Use constructor instead for static read(). TTFont: Add ctor for File and InputStream
Diffstat (limited to 'src/jogl/classes/jogamp/graph/font')
3 files changed, 88 insertions, 22 deletions
diff --git a/src/jogl/classes/jogamp/graph/font/typecast/ot/OTFont.java b/src/jogl/classes/jogamp/graph/font/typecast/ot/OTFont.java index 9c7c0da0f..9ddd080aa 100644 --- a/src/jogl/classes/jogamp/graph/font/typecast/ot/OTFont.java +++ b/src/jogl/classes/jogamp/graph/font/typecast/ot/OTFont.java @@ -18,18 +18,14 @@ package jogamp.graph.font.typecast.ot; -import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import jogamp.graph.font.typecast.ot.table.CmapTable; import jogamp.graph.font.typecast.ot.table.GsubTable; -import jogamp.graph.font.typecast.ot.table.HdmxTable; import jogamp.graph.font.typecast.ot.table.HeadTable; import jogamp.graph.font.typecast.ot.table.HheaTable; import jogamp.graph.font.typecast.ot.table.HmtxTable; -import jogamp.graph.font.typecast.ot.table.KernTable; -import jogamp.graph.font.typecast.ot.table.LocaTable; import jogamp.graph.font.typecast.ot.table.MaxpTable; import jogamp.graph.font.typecast.ot.table.NameRecord; import jogamp.graph.font.typecast.ot.table.NameTable; @@ -57,7 +53,14 @@ public abstract class OTFont { private final VheaTable _vhea; private final GsubTable _gsub; - OTFont(final DataInputStream dis, TableDirectory tableDirectory, final int tablesOrigin) throws IOException { + /** + * + * @param dis input stream marked at start with read-ahead set to known stream length + * @param tableDirectory + * @param tablesOrigin + * @throws IOException + */ + OTFont(final DataInputStream dis, final TableDirectory tableDirectory, final int tablesOrigin) throws IOException { // Load some prerequisite tables // (These are tables that are referenced by other tables, so we need to load // them first) @@ -93,7 +96,7 @@ public abstract class OTFont { _name = new NameTable(dis, length); seekTable(tableDirectory, dis, tablesOrigin, Table.OS_2); _os2 = new Os2Table(dis); - + _gsub = null; // FIXME: delete? } @@ -183,7 +186,7 @@ public abstract class OTFont { } return sb; } - + @Override public String toString() { return _head.toString(); 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 269542019..9c1e4a99a 100644 --- a/src/jogl/classes/jogamp/graph/font/typecast/ot/OTFontCollection.java +++ b/src/jogl/classes/jogamp/graph/font/typecast/ot/OTFontCollection.java @@ -166,7 +166,7 @@ public class OTFontCollection { final ResourceReference resourceReference = resourceType.getReference(i); final int offset = resourceHeader.getDataOffset() + resourceReference.getDataOffset() + 4; - _fonts[i] = TTFont.read(dis, offset, offset); + _fonts[i] = new TTFont(dis, offset, offset); } } else if (TTCHeader.isTTC(dis)) { @@ -176,13 +176,13 @@ public class OTFontCollection { _ttcHeader = new TTCHeader(dis); _fonts = new TTFont[_ttcHeader.getDirectoryCount()]; for (int i = 0; i < _ttcHeader.getDirectoryCount(); i++) { - _fonts[i] = TTFont.read(dis, _ttcHeader.getTableDirectory(i), 0); + _fonts[i] = new TTFont(dis, _ttcHeader.getTableDirectory(i), 0); } } else { // This is a standalone font file _fonts = new TTFont[1]; - _fonts[0] = TTFont.read(dis, 0, 0); + _fonts[0] = new TTFont(dis, 0, 0); // TODO T2Fonts } diff --git a/src/jogl/classes/jogamp/graph/font/typecast/ot/TTFont.java b/src/jogl/classes/jogamp/graph/font/typecast/ot/TTFont.java index 3e0de88ef..e7f3f5e35 100644 --- a/src/jogl/classes/jogamp/graph/font/typecast/ot/TTFont.java +++ b/src/jogl/classes/jogamp/graph/font/typecast/ot/TTFont.java @@ -18,11 +18,22 @@ package jogamp.graph.font.typecast.ot; -import jogamp.graph.font.typecast.ot.table.*; - -import java.io.ByteArrayInputStream; +import java.io.BufferedInputStream; import java.io.DataInputStream; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; + +import jogamp.graph.font.typecast.ot.table.GaspTable; +import jogamp.graph.font.typecast.ot.table.GlyfDescript; +import jogamp.graph.font.typecast.ot.table.GlyfTable; +import jogamp.graph.font.typecast.ot.table.HdmxTable; +import jogamp.graph.font.typecast.ot.table.KernTable; +import jogamp.graph.font.typecast.ot.table.LocaTable; +import jogamp.graph.font.typecast.ot.table.Table; +import jogamp.graph.font.typecast.ot.table.TableDirectory; +import jogamp.graph.font.typecast.ot.table.VdmxTable; public class TTFont extends OTFont { @@ -32,23 +43,75 @@ public class TTFont extends OTFont { private HdmxTable _hdmx; private VdmxTable _vdmx; + private static TableDirectory readTableDir(final DataInputStream dis, final int directoryOffset) throws IOException { + // Load the table directory + dis.reset(); // throws if not marked or mark not supported + dis.skip(directoryOffset); + return new TableDirectory(dis); + } + + private static DataInputStream openStream(final File file) throws IOException { + if (!file.exists()) { + throw new IOException("File <"+file.getName()+"> doesn't exist."); + } + final int streamLen = (int) file.length(); + final BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file), streamLen); + if( !bis.markSupported() ) { + throw new IllegalArgumentException("stream of type "+bis.getClass().getName()+" doesn't support mark"); + } + bis.mark(streamLen); + return new DataInputStream(bis); + } + + private static DataInputStream openStream(final InputStream is, final int streamLen) throws IOException { + final BufferedInputStream bis = new BufferedInputStream(is, streamLen); + if( !bis.markSupported() ) { + throw new IllegalArgumentException("stream of type "+is.getClass().getName()+" doesn't support mark"); + } + bis.mark(streamLen); + return new DataInputStream(bis); + } + /** - * Constructor method - * @param dis + * Constructor + * @param file standalone font file + * @param tablesOrigin + * @throws IOException + */ + public TTFont(final File file) throws IOException { + this(openStream(file), 0, 0); + } + + /** + * Constructor + * @param is standalone font input stream + * @param streamLen length of input stream to rewind across whole font data set + * @throws IOException + */ + public TTFont(final InputStream is, final int streamLen) throws IOException { + this(openStream(is, streamLen), 0, 0); + } + + /** + * Constructor + * @param dis input stream marked at start with read-ahead set to known stream length * @param directoryOffset * @param tablesOrigin * @return * @throws IOException */ - public static TTFont read(final DataInputStream dis, final int directoryOffset, final int tablesOrigin) throws IOException { - // Load the table directory - dis.reset(); - dis.skip(directoryOffset); - final TableDirectory tableDirectory = new TableDirectory(dis); - return new TTFont(dis, tableDirectory, tablesOrigin); + public TTFont(final DataInputStream dis, final int directoryOffset, final int tablesOrigin) throws IOException { + this(dis, readTableDir(dis, directoryOffset), tablesOrigin); } - private TTFont(final DataInputStream dis, final TableDirectory tableDirectory, final int tablesOrigin) throws IOException { + /** + * + * @param dis input stream marked at start with read-ahead set to known stream length + * @param tableDirectory + * @param tablesOrigin + * @throws IOException + */ + TTFont(final DataInputStream dis, final TableDirectory tableDirectory, final int tablesOrigin) throws IOException { super(dis, tableDirectory, tablesOrigin); // 'loca' is required by 'glyf' |