diff options
author | David Schweinsberg <[email protected]> | 2019-09-15 13:18:56 -0700 |
---|---|---|
committer | David Schweinsberg <[email protected]> | 2019-09-15 13:18:56 -0700 |
commit | 88367285132439ed13fb24c6631faa63a2746665 (patch) | |
tree | c1eb046866f780a3f852ec5cacb26430a729b7c4 | |
parent | 974c0c58947ef06873fe30bb9e6a58ebe08b9dc5 (diff) |
Load font with byte array
11 files changed, 212 insertions, 361 deletions
diff --git a/src/main/java/net/java/dev/typecast/ot/OTFont.java b/src/main/java/net/java/dev/typecast/ot/OTFont.java index c23aa8f..d753e84 100644 --- a/src/main/java/net/java/dev/typecast/ot/OTFont.java +++ b/src/main/java/net/java/dev/typecast/ot/OTFont.java @@ -1,72 +1,28 @@ /* - - ============================================================================ - The Apache Software License, Version 1.1 - ============================================================================ - - Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. - - Redistribution and use in source and binary forms, with or without modifica- - tion, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - 3. The end-user documentation included with the redistribution, if any, must - include the following acknowledgment: "This product includes software - developed by the Apache Software Foundation (http://www.apache.org/)." - Alternately, this acknowledgment may appear in the software itself, if - and wherever such third-party acknowledgments normally appear. - - 4. The names "Batik" and "Apache Software Foundation" must not be - used to endorse or promote products derived from this software without - prior written permission. For written permission, please contact - - 5. Products derived from this software may not be called "Apache", nor may - "Apache" appear in their name, without prior written permission of the - Apache Software Foundation. - - THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, - INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- - DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This software consists of voluntary contributions made by many individuals - on behalf of the Apache Software Foundation. For more information on the - Apache Software Foundation, please see <http://www.apache.org/>. - -*/ + * Typecast + * + * Copyright © 2004-2019 David Schweinsberg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package net.java.dev.typecast.ot; +import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; -import net.java.dev.typecast.ot.table.CmapTable; -import net.java.dev.typecast.ot.table.DirectoryEntry; -import net.java.dev.typecast.ot.table.HeadTable; -import net.java.dev.typecast.ot.table.HheaTable; -import net.java.dev.typecast.ot.table.HmtxTable; -import net.java.dev.typecast.ot.table.MaxpTable; -import net.java.dev.typecast.ot.table.NameTable; -import net.java.dev.typecast.ot.table.Os2Table; -import net.java.dev.typecast.ot.table.PostTable; -import net.java.dev.typecast.ot.table.Table; -import net.java.dev.typecast.ot.table.TableDirectory; -import net.java.dev.typecast.ot.table.VheaTable; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import net.java.dev.typecast.ot.table.*; /** * The TrueType font. @@ -75,7 +31,6 @@ import org.slf4j.LoggerFactory; public class OTFont { private Os2Table _os2; -// private CffTable _cff; private CmapTable _cmap; private HeadTable _head; private HheaTable _hhea; @@ -84,11 +39,10 @@ public class OTFont { private NameTable _name; private PostTable _post; private VheaTable _vhea; - - static final Logger logger = LoggerFactory.getLogger(OTFont.class); + private GsubTable _gsub; /** - * @param dis OpenType/TrueType font file data. + * @param fontData OpenType/TrueType font file data. * @param directoryOffset The Table Directory offset within the file. For a * regular TTF/OTF file this will be zero, but for a TTC (Font Collection) * the offset is retrieved from the TTC header. For a Mac font resource, @@ -99,12 +53,15 @@ public class OTFont { * individual font resource data. * @throws java.io.IOException */ - public OTFont(DataInputStream dis, int tablesOrigin) throws IOException { + OTFont(byte[] fontData, int tablesOrigin) throws IOException { // Load the table directory - dis.reset(); // dis.skip(directoryOffset); - TableDirectory tableDirectory = new TableDirectory(dis); + TableDirectory tableDirectory = new TableDirectory(fontData); + + DataInputStream dis = new DataInputStream(new ByteArrayInputStream(fontData)); + dis.mark(fontData.length); + dis.reset(); // Load some prerequisite tables // (These are tables that are referenced by other tables, so we need to load @@ -139,29 +96,12 @@ public class OTFont { _name = new NameTable(dis, length); seekTable(tableDirectory, dis, tablesOrigin, Table.OS_2); _os2 = new Os2Table(dis); - - // If this is a TrueType outline, then we'll have at least the - // 'glyf' table (along with the 'loca' table) -// _glyf = (GlyfTable) getTable(Table.glyf); } -// public Table getTable(int tableType) { -// for (Table _table : _tables) { -// if ((_table != null) && (_table.getType() == tableType)) { -// return _table; -// } -// } -// return null; -// } - public Os2Table getOS2Table() { return _os2; } -// public CffTable getCffTable() { -// return _cff; -// } - public CmapTable getCmapTable() { return _cmap; } @@ -178,10 +118,6 @@ public class OTFont { return _hmtx; } -// public LocaTable getLocaTable() { -// return _loca; -// } - public MaxpTable getMaxpTable() { return _maxp; } @@ -198,6 +134,10 @@ public class OTFont { return _vhea; } + public GsubTable getGsubTable() { + return _gsub; + } + public int getAscent() { return _hhea.getAscender(); } @@ -227,13 +167,13 @@ public class OTFont { // } // } - protected int seekTable( + int seekTable( TableDirectory tableDirectory, DataInputStream dis, int tablesOrigin, int tag) throws IOException { dis.reset(); - DirectoryEntry entry = tableDirectory.getEntryByTag(tag); + TableDirectory.Entry entry = tableDirectory.getEntryByTag(tag); if (entry == null) { return 0; } @@ -241,12 +181,6 @@ public class OTFont { return entry.getLength(); } -// protected void read( -// DataInputStream dis, -// int directoryOffset, -// int tablesOrigin) throws IOException { -// } - public String toString() { return _head.toString(); } diff --git a/src/main/java/net/java/dev/typecast/ot/OTFontCollection.java b/src/main/java/net/java/dev/typecast/ot/OTFontCollection.java index e18daac..6f7d7de 100644 --- a/src/main/java/net/java/dev/typecast/ot/OTFontCollection.java +++ b/src/main/java/net/java/dev/typecast/ot/OTFontCollection.java @@ -1,7 +1,7 @@ /* - * Typecast - The Font Development Environment + * Typecast * - * Copyright (c) 2004 David Schweinsberg + * Copyright © 2004-2019 David Schweinsberg * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,14 +23,12 @@ import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.util.ArrayList; +import java.nio.file.Files; import net.java.dev.typecast.ot.mac.ResourceHeader; import net.java.dev.typecast.ot.mac.ResourceMap; import net.java.dev.typecast.ot.mac.ResourceReference; import net.java.dev.typecast.ot.mac.ResourceType; -import net.java.dev.typecast.ot.table.DirectoryEntry; import net.java.dev.typecast.ot.table.TTCHeader; -import net.java.dev.typecast.ot.table.Table; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,34 +38,10 @@ import org.slf4j.LoggerFactory; */ public class OTFontCollection { - private String _pathName; - private String _fileName; private TTCHeader _ttcHeader; private OTFont[] _fonts; - private boolean _resourceFork = false; - static final Logger logger = LoggerFactory.getLogger(OTFontCollection.class); - - /** Creates new FontCollection */ - protected OTFontCollection() { - } - - /** - * @param file The OpenType font file - */ - public static OTFontCollection create(File file) throws IOException { - OTFontCollection fc = new OTFontCollection(); - fc.read(file); - return fc; - } - - public String getPathName() { - return _pathName; - } - - public String getFileName() { - return _fileName; - } + private static final Logger logger = LoggerFactory.getLogger(OTFontCollection.class); public OTFont getFont(int i) { return _fonts[i]; @@ -84,22 +58,20 @@ public class OTFontCollection { /** * @param file The OpenType font file */ - protected void read(File file) throws IOException { - _pathName = file.getPath(); - _fileName = file.getName(); - + public OTFontCollection(File file) throws IOException { if (!file.exists()) { throw new IOException(); } // Do we need to modify the path name to deal with font resources // in a Mac resource fork? + boolean resourceFork = false; if (file.length() == 0) { file = new File(file, "..namedfork/rsrc"); if (!file.exists()) { throw new IOException(); } - _resourceFork = true; + resourceFork = true; } DataInputStream dis = new DataInputStream( @@ -107,7 +79,7 @@ public class OTFontCollection { new FileInputStream(file), (int) file.length())); dis.mark((int) file.length()); - if (_resourceFork || _pathName.endsWith(".dfont")) { + if (resourceFork || file.getPath().endsWith(".dfont")) { // This is a Macintosh font suitcase resource ResourceHeader resourceHeader = new ResourceHeader(dis); @@ -137,7 +109,8 @@ public class OTFontCollection { ResourceReference resourceReference = resourceType.getReference(i); int offset = resourceHeader.getDataOffset() + resourceReference.getDataOffset() + 4; - _fonts[i] = new TTFont(dis, offset /*, offset*/); + byte[] fontData = Files.readAllBytes(file.toPath()); + _fonts[i] = new TTFont(fontData, offset /*, offset*/); } } else if (TTCHeader.isTTC(dis)) { @@ -147,13 +120,15 @@ public class OTFontCollection { _ttcHeader = new TTCHeader(dis); _fonts = new OTFont[_ttcHeader.getDirectoryCount()]; for (int i = 0; i < _ttcHeader.getDirectoryCount(); i++) { - _fonts[i] = new TTFont(dis, _ttcHeader.getTableDirectory(i)); + byte[] fontData = Files.readAllBytes(file.toPath()); + _fonts[i] = new TTFont(fontData, _ttcHeader.getTableDirectory(i)); } } else { // This is a standalone font file _fonts = new OTFont[1]; - _fonts[0] = new TTFont(dis, 0); + byte[] fontData = Files.readAllBytes(file.toPath()); + _fonts[0] = new TTFont(fontData, 0); // TODO T2Fonts } diff --git a/src/main/java/net/java/dev/typecast/ot/TTFont.java b/src/main/java/net/java/dev/typecast/ot/TTFont.java index 072994f..1829c0b 100644 --- a/src/main/java/net/java/dev/typecast/ot/TTFont.java +++ b/src/main/java/net/java/dev/typecast/ot/TTFont.java @@ -1,7 +1,26 @@ +/* + * Typecast + * + * Copyright © 2004-2019 David Schweinsberg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package net.java.dev.typecast.ot; import net.java.dev.typecast.ot.table.*; +import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; @@ -16,16 +35,19 @@ public class TTFont extends OTFont { /** * Constructor * - * @param dis + * @param fontData * @param tablesOrigin */ - public TTFont(DataInputStream dis, int tablesOrigin) throws IOException { - super(dis, tablesOrigin); + public TTFont(byte[] fontData, int tablesOrigin) throws IOException { + super(fontData, tablesOrigin); // Load the table directory - dis.reset(); // dis.skip(directoryOffset); - TableDirectory tableDirectory = new TableDirectory(dis); + TableDirectory tableDirectory = new TableDirectory(fontData); + + DataInputStream dis = new DataInputStream(new ByteArrayInputStream(fontData)); + dis.mark(fontData.length); + dis.reset(); // 'loca' is required by 'glyf' int length = seekTable(tableDirectory, dis, tablesOrigin, Table.loca); @@ -57,4 +79,24 @@ public class TTFont extends OTFont { } } + public GlyfTable getGlyfTable() { + return _glyf; + } + + public GaspTable getGaspTable() { + return _gasp; + } + + public KernTable getKernTable() { + return _kern; + } + + public HdmxTable getHdmxTable() { + return _hdmx; + } + + public VdmxTable getVdmxTable() { + return _vdmx; + } + } diff --git a/src/main/java/net/java/dev/typecast/ot/table/DirectoryEntry.java b/src/main/java/net/java/dev/typecast/ot/table/DirectoryEntry.java deleted file mode 100644 index 2ab838b..0000000 --- a/src/main/java/net/java/dev/typecast/ot/table/DirectoryEntry.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - - ============================================================================ - The Apache Software License, Version 1.1 - ============================================================================ - - Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. - - Redistribution and use in source and binary forms, with or without modifica- - tion, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - 3. The end-user documentation included with the redistribution, if any, must - include the following acknowledgment: "This product includes software - developed by the Apache Software Foundation (http://www.apache.org/)." - Alternately, this acknowledgment may appear in the software itself, if - and wherever such third-party acknowledgments normally appear. - - 4. The names "Batik" and "Apache Software Foundation" must not be - used to endorse or promote products derived from this software without - prior written permission. For written permission, please contact - - 5. Products derived from this software may not be called "Apache", nor may - "Apache" appear in their name, without prior written permission of the - Apache Software Foundation. - - THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, - INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- - DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This software consists of voluntary contributions made by many individuals - on behalf of the Apache Software Foundation. For more information on the - Apache Software Foundation, please see <http://www.apache.org/>. - -*/ - -package net.java.dev.typecast.ot.table; - -import java.io.DataInput; -import java.io.IOException; - -/** - * @author <a href="mailto:[email protected]">David Schweinsberg</a> - */ -public class DirectoryEntry implements Cloneable { - - private int _tag; - private int _checksum; - private int _offset; - private int _length; - - protected DirectoryEntry(DataInput di) throws IOException { - _tag = di.readInt(); - _checksum = di.readInt(); - _offset = di.readInt(); - _length = di.readInt(); - } - - public Object clone() { - try { - return super.clone(); - } catch (CloneNotSupportedException e) { - return null; - } - } - - public int getChecksum() { - return _checksum; - } - - public int getLength() { - return _length; - } - - public int getOffset() { - return _offset; - } - - public int getTag() { - return _tag; - } - - public String getTagAsString() { - return new StringBuffer() - .append((char)((_tag>>24)&0xff)) - .append((char)((_tag>>16)&0xff)) - .append((char)((_tag>>8)&0xff)) - .append((char)((_tag)&0xff)) - .toString(); - } - - public String toString() { - return new StringBuffer() - .append("'").append(getTagAsString()) - .append("' - chksm = 0x").append(Integer.toHexString(_checksum)) - .append(", off = 0x").append(Integer.toHexString(_offset)) - .append(", len = ").append(_length) - .toString(); - } -} diff --git a/src/main/java/net/java/dev/typecast/ot/table/LtshTable.java b/src/main/java/net/java/dev/typecast/ot/table/LtshTable.java index 48fd263..abf75fe 100644 --- a/src/main/java/net/java/dev/typecast/ot/table/LtshTable.java +++ b/src/main/java/net/java/dev/typecast/ot/table/LtshTable.java @@ -17,14 +17,12 @@ import java.io.IOException; */ public class LtshTable implements Table { - private DirectoryEntry de; private int version; private int numGlyphs; private int[] yPels; /** Creates new LtshTable */ - protected LtshTable(DirectoryEntry de, DataInput di) throws IOException { - this.de = (DirectoryEntry) de.clone(); + protected LtshTable(DataInput di) throws IOException { version = di.readUnsignedShort(); numGlyphs = di.readUnsignedShort(); yPels = new int[numGlyphs]; @@ -33,14 +31,6 @@ public class LtshTable implements Table { } } - /** - * Get the table type, as a table directory value. - * @return The table type - */ - public int getType() { - return LTSH; - } - public String toString() { StringBuffer sb = new StringBuffer(); sb.append("'LTSH' Table - Linear Threshold Table\n-------------------------------------") @@ -53,15 +43,5 @@ public class LtshTable implements Table { } return sb.toString(); } - - /** - * Get a directory entry for this table. This uniquely identifies the - * table in collections where there may be more than one instance of a - * particular table. - * @return A directory entry - */ - public DirectoryEntry getDirectoryEntry() { - return de; - } - + } diff --git a/src/main/java/net/java/dev/typecast/ot/table/PcltTable.java b/src/main/java/net/java/dev/typecast/ot/table/PcltTable.java index dbfd69f..40e077c 100644 --- a/src/main/java/net/java/dev/typecast/ot/table/PcltTable.java +++ b/src/main/java/net/java/dev/typecast/ot/table/PcltTable.java @@ -17,7 +17,6 @@ import java.io.IOException; */ public class PcltTable implements Table { - private DirectoryEntry de; private int version; private long fontNumber; private int pitch; @@ -35,8 +34,7 @@ public class PcltTable implements Table { private byte reserved; /** Creates new PcltTable */ - protected PcltTable(DirectoryEntry de, DataInput di) throws IOException { - this.de = (DirectoryEntry) de.clone(); + protected PcltTable(DataInput di) throws IOException { version = di.readInt(); fontNumber = di.readInt(); pitch = di.readUnsignedShort(); @@ -60,14 +58,6 @@ public class PcltTable implements Table { reserved = di.readByte(); } - /** - * Get the table type, as a table directory value. - * @return The table type - */ - public int getType() { - return PCLT; - } - public String toString() { return new StringBuffer() .append("'PCLT' Table - Printer Command Language Table\n---------------------------------------------") @@ -90,15 +80,5 @@ public class PcltTable implements Table { .append("\n reserved: ").append(reserved) .toString(); } - - /** - * Get a directory entry for this table. This uniquely identifies the - * table in collections where there may be more than one instance of a - * particular table. - * @return A directory entry - */ - public DirectoryEntry getDirectoryEntry() { - return de; - } - + } diff --git a/src/main/java/net/java/dev/typecast/ot/table/PrepTable.java b/src/main/java/net/java/dev/typecast/ot/table/PrepTable.java index d069616..2a47aed 100644 --- a/src/main/java/net/java/dev/typecast/ot/table/PrepTable.java +++ b/src/main/java/net/java/dev/typecast/ot/table/PrepTable.java @@ -17,29 +17,12 @@ import net.java.dev.typecast.ot.Disassembler; */ public class PrepTable extends Program implements Table { - private DirectoryEntry de; - - public PrepTable(DirectoryEntry de, DataInput di) throws IOException { - this.de = (DirectoryEntry) de.clone(); - readInstructions(di, de.getLength()); - } - - public int getType() { - return prep; + public PrepTable(DataInput di, int length) throws IOException { + readInstructions(di, length); } public String toString() { return Disassembler.disassemble(getInstructions(), 0); } - - /** - * Get a directory entry for this table. This uniquely identifies the - * table in collections where there may be more than one instance of a - * particular table. - * @return A directory entry - */ - public DirectoryEntry getDirectoryEntry() { - return de; - } - + } diff --git a/src/main/java/net/java/dev/typecast/ot/table/TableDirectory.java b/src/main/java/net/java/dev/typecast/ot/table/TableDirectory.java index d3ee90c..b361162 100644 --- a/src/main/java/net/java/dev/typecast/ot/table/TableDirectory.java +++ b/src/main/java/net/java/dev/typecast/ot/table/TableDirectory.java @@ -50,7 +50,9 @@ package net.java.dev.typecast.ot.table; +import java.io.ByteArrayInputStream; import java.io.DataInput; +import java.io.DataInputStream; import java.io.IOException; import net.java.dev.typecast.ot.Fixed; @@ -59,30 +61,76 @@ import net.java.dev.typecast.ot.Fixed; */ public class TableDirectory { - private int _version = 0; - private short _numTables = 0; - private short _searchRange = 0; - private short _entrySelector = 0; - private short _rangeShift = 0; - private DirectoryEntry[] _entries; - - public TableDirectory(DataInput di) throws IOException { - _version = di.readInt(); - _numTables = di.readShort(); - _searchRange = di.readShort(); - _entrySelector = di.readShort(); - _rangeShift = di.readShort(); - _entries = new DirectoryEntry[_numTables]; + public static class Entry { + + private int _tag; + private int _checksum; + private int _offset; + private int _length; + + Entry(DataInput di) throws IOException { + _tag = di.readInt(); + _checksum = di.readInt(); + _offset = di.readInt(); + _length = di.readInt(); + } + + public int getChecksum() { + return _checksum; + } + + public int getLength() { + return _length; + } + + public int getOffset() { + return _offset; + } + + int getTag() { + return _tag; + } + + String getTagAsString() { + return String.valueOf((char) ((_tag >> 24) & 0xff)) + + (char) ((_tag >> 16) & 0xff) + + (char) ((_tag >> 8) & 0xff) + + (char) ((_tag) & 0xff); + } + + public String toString() { + return "'" + getTagAsString() + + "' - chksm = 0x" + Integer.toHexString(_checksum) + + ", off = 0x" + Integer.toHexString(_offset) + + ", len = " + _length; + } + } + + private int _version; + private short _numTables; + private short _searchRange; + private short _entrySelector; + private short _rangeShift; + private Entry[] _entries; + + public TableDirectory(byte[] fontData) throws IOException { + DataInputStream dis = new DataInputStream(new ByteArrayInputStream(fontData)); + _version = dis.readInt(); + _numTables = dis.readShort(); + _searchRange = dis.readShort(); + _entrySelector = dis.readShort(); + _rangeShift = dis.readShort(); + _entries = new Entry[_numTables]; for (int i = 0; i < _numTables; i++) { - _entries[i] = new DirectoryEntry(di); + _entries[i] = new Entry(dis); } } - public DirectoryEntry getEntry(int index) { + public Entry getEntry(int index) { return _entries[index]; } - public DirectoryEntry getEntryByTag(int tag) { + public Entry getEntryByTag(int tag) { for (int i = 0; i < _numTables; i++) { if (_entries[i].getTag() == tag) { return _entries[i]; @@ -112,7 +160,7 @@ public class TableDirectory { } public String toString() { - StringBuffer sb = new StringBuffer() + StringBuilder sb = new StringBuilder() .append("Offset Table\n------ -----") .append("\n sfnt version: ").append(Fixed.floatValue(_version)) .append("\n numTables = ").append(_numTables) diff --git a/src/main/java/net/java/dev/typecast/ot/table/VmtxTable.java b/src/main/java/net/java/dev/typecast/ot/table/VmtxTable.java index 7fd2f8a..0309fe0 100644 --- a/src/main/java/net/java/dev/typecast/ot/table/VmtxTable.java +++ b/src/main/java/net/java/dev/typecast/ot/table/VmtxTable.java @@ -27,16 +27,13 @@ import java.io.IOException; */ public class VmtxTable implements Table { - private DirectoryEntry _de; private int[] _vMetrics = null; private short[] _topSideBearing = null; protected VmtxTable( - DirectoryEntry de, DataInput di, VheaTable vhea, MaxpTable maxp) throws IOException { - _de = (DirectoryEntry) de.clone(); _vMetrics = new int[vhea.getNumberOfLongVerMetrics()]; for (int i = 0; i < vhea.getNumberOfLongVerMetrics(); ++i) { _vMetrics[i] = @@ -74,15 +71,11 @@ public class VmtxTable implements Table { } } - public int getType() { - return vmtx; - } - public String toString() { StringBuffer sb = new StringBuffer(); sb.append("'vmtx' Table - Vertical Metrics\n-------------------------------\n"); - sb.append("Size = ").append(_de.getLength()).append(" bytes, ") - .append(_vMetrics.length).append(" entries\n"); +// sb.append("Size = ").append(_de.getLength()).append(" bytes, ") + sb.append(_vMetrics.length).append(" entries\n"); for (int i = 0; i < _vMetrics.length; i++) { sb.append(" ").append(i) .append(". advHeight: ").append(getAdvanceHeight(i)) @@ -97,13 +90,4 @@ public class VmtxTable implements Table { return sb.toString(); } - /** - * Get a directory entry for this table. This uniquely identifies the - * table in collections where there may be more than one instance of a - * particular table. - * @return A directory entry - */ - public DirectoryEntry getDirectoryEntry() { - return _de; - } } diff --git a/src/test/java/net/java/dev/typecast/ot/OTFontCollectionTest.java b/src/test/java/net/java/dev/typecast/ot/OTFontCollectionTest.java index da302a5..3427d47 100644 --- a/src/test/java/net/java/dev/typecast/ot/OTFontCollectionTest.java +++ b/src/test/java/net/java/dev/typecast/ot/OTFontCollectionTest.java @@ -1,3 +1,21 @@ +/* + * Typecast + * + * Copyright © 2004-2019 David Schweinsberg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package net.java.dev.typecast.ot; import java.io.File; @@ -29,7 +47,9 @@ public class OTFontCollectionTest extends TestCase { public void testLoadSingleFont() throws URISyntaxException, IOException { URL url = ClassLoader.getSystemResource("Lato-Regular.ttf"); File file = new File(url.toURI()); - OTFontCollection fontCollection = OTFontCollection.create(file); + OTFontCollection fontCollection = new OTFontCollection(file); assertEquals(1, fontCollection.getFontCount()); + OTFont font = fontCollection.getFont(0); + assertNotNull(font); } } diff --git a/src/test/java/net/java/dev/typecast/ot/TTFontTest.java b/src/test/java/net/java/dev/typecast/ot/TTFontTest.java index 1621f3e..03076b9 100644 --- a/src/test/java/net/java/dev/typecast/ot/TTFontTest.java +++ b/src/test/java/net/java/dev/typecast/ot/TTFontTest.java @@ -1,8 +1,28 @@ +/* + * Typecast + * + * Copyright © 2004-2019 David Schweinsberg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package net.java.dev.typecast.ot; import java.io.*; import java.net.URISyntaxException; import java.net.URL; +import java.nio.file.Files; + import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -25,12 +45,11 @@ public class TTFontTest extends TestCase { return new TestSuite(TTFontTest.class); } - public void testLoadSingleFont() throws URISyntaxException, IOException { + public void testLoadFont() throws URISyntaxException, IOException { URL url = ClassLoader.getSystemResource("Lato-Regular.ttf"); File file = new File(url.toURI()); - DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(file), (int)file.length())); - dis.mark((int)file.length()); - TTFont font = new TTFont(dis, 0); + byte[] fontData = Files.readAllBytes(file.toPath()); + TTFont font = new TTFont(fontData, 0); assertEquals(HeadTable.class, font.getHeadTable().getClass()); } } |