This repository has been archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ticket:28 fixed byte array sourced font bounds checking
- Loading branch information
stuartg
committed
Mar 30, 2013
1 parent
6f7f59f
commit dc9b022
Showing
7 changed files
with
131 additions
and
3 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
java/test/com/google/typography/font/sfntly/issue_tests/Issue28Tests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.google.typography.font.sfntly.issue_tests; | ||
|
||
import com.google.typography.font.sfntly.Font; | ||
import com.google.typography.font.sfntly.FontFactory; | ||
import com.google.typography.font.sfntly.Tag; | ||
import com.google.typography.font.sfntly.table.truetype.Glyph; | ||
import com.google.typography.font.sfntly.table.truetype.GlyphTable; | ||
import com.google.typography.font.sfntly.table.truetype.LocaTable; | ||
import com.google.typography.font.sfntly.testutils.TestFont; | ||
|
||
import junit.framework.TestCase; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Test class showing inconsistent behaviour between loading from a byte array | ||
* vs loading from a file input stream. | ||
*/ | ||
public class Issue28Tests extends TestCase { | ||
|
||
private static byte[] readToByteArray(File file) throws IOException { | ||
byte[] data = new byte[(int) file.length()]; | ||
FileInputStream fis = new FileInputStream(file); | ||
|
||
try { | ||
fis.read(data); | ||
} finally { | ||
fis.close(); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
private static Glyph getLastGlyph(Font font) { | ||
LocaTable locaTable = font.getTable(Tag.loca); | ||
int glyphId = locaTable.numGlyphs() - 1; | ||
GlyphTable glyfTable = font.getTable(Tag.glyf); | ||
int offset = locaTable.glyphOffset(glyphId); | ||
int length = locaTable.glyphLength(glyphId); | ||
return glyfTable.glyph(offset, length); | ||
} | ||
|
||
/** | ||
* Ensure that the stream and byte array sourced fonts both throw an exception when you | ||
* read off the end of a sliced ReadableFontData | ||
*/ | ||
public void testStreamVsBytes() throws Exception { | ||
FontFactory factory = FontFactory.getInstance(); | ||
|
||
byte[] data = readToByteArray(TestFont.TestFontNames.ROBOTO.getFile()); | ||
Font byteFont = factory.loadFonts(data)[0]; | ||
|
||
InputStream is = new FileInputStream(TestFont.TestFontNames.ROBOTO.getFile()); | ||
Font streamFont; | ||
try { | ||
streamFont = factory.loadFonts(is)[0]; | ||
} finally { | ||
is.close(); | ||
} | ||
|
||
|
||
// first test for byte array sourced font | ||
{ | ||
boolean thrown = false; | ||
Glyph byteGlyph = getLastGlyph(byteFont); | ||
try { | ||
int byteXMin = byteGlyph.xMin(); | ||
} catch (IndexOutOfBoundsException e) { | ||
// expected exception | ||
thrown = true; | ||
} | ||
assertTrue("IndexOutOfBoundsException was expected but was not thrown.", thrown); | ||
} | ||
|
||
// next test for stream sourced font | ||
{ | ||
boolean thrown = false; | ||
Glyph streamGlyph = getLastGlyph(streamFont); | ||
try { | ||
int streamXMin = streamGlyph.xMin(); | ||
} catch (IndexOutOfBoundsException e) { | ||
// expected exception | ||
thrown = true; | ||
} | ||
assertTrue("IndexOutOfBoundsException was expected but was not thrown.", thrown); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters