gposFontList = new ArrayList<>();
18 | for (TestFont.TestFontNames name : TestFont.TestFontNames.values()) {
19 | Font[] fonts;
20 | try {
21 | fonts = TestFontUtils.loadFont(name.getFile());
22 | assertNotNull(fonts);
23 | } catch (IOException e) {
24 | System.out.format("caught exception (%s) when loading font %s\n", e.getMessage(), name);
25 | continue;
26 | }
27 | for (int i = 0; i < fonts.length; ++i) {
28 | Font font = fonts[i];
29 | if (font.hasTable(Tag.GPOS)) {
30 | System.out.format("Font %s(%d) has GPOS\n", name, i);
31 | gposFontList.add(font);
32 |
33 | Table gpos = font.getTable(Tag.GPOS);
34 | Header gposHeader = gpos.header();
35 | System.out.println(gposHeader);
36 | }
37 | }
38 | }
39 | assertTrue("have test gpos file", gposFontList.size() > 0);
40 |
41 | for (Font font : gposFontList) {}
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/java/test/com/google/typography/font/sfntly/MetricsTests.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.google.typography.font.sfntly;
18 |
19 | import com.google.typography.font.sfntly.table.core.HorizontalMetricsTable;
20 | import com.google.typography.font.sfntly.testutils.TestFont;
21 | import com.google.typography.font.sfntly.testutils.TestFontUtils;
22 | import java.io.File;
23 | import junit.framework.TestCase;
24 |
25 | /** @author Stuart Gill */
26 | public class MetricsTests extends TestCase {
27 |
28 | private static final File TEST_FONT_FILE = TestFont.TestFontNames.OPENSANS.getFile();
29 |
30 | public void testBasicHmtxValidity() throws Exception {
31 | Font[] fonts = TestFontUtils.loadFont(TEST_FONT_FILE);
32 | Font font = fonts[0];
33 | HorizontalMetricsTable hmtxTable = font.getTable(Tag.hmtx);
34 |
35 | for (int gid = 0; gid < 100; gid++) {
36 | int width = hmtxTable.advanceWidth(gid);
37 | assertFalse(width == -1);
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/java/test/com/google/typography/font/sfntly/data/SfStringUtilsTest.java:
--------------------------------------------------------------------------------
1 | package com.google.typography.font.sfntly.data;
2 |
3 | import java.util.Arrays;
4 | import java.util.HashSet;
5 | import junit.framework.TestCase;
6 |
7 | public class SfStringUtilsTest extends TestCase {
8 |
9 | public void testGetAllCodepoints() {
10 | assertEquals(
11 | new HashSet<>(Arrays.asList(72, 101, 108, 111)), SfStringUtils.getAllCodepoints("Hello"));
12 |
13 | assertEquals(
14 | new HashSet<>(Arrays.asList(0x1F645)),
15 | SfStringUtils.getAllCodepoints(new String(new int[] {0x1F645}, 0, 1)));
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/java/test/com/google/typography/font/sfntly/issue_tests/Issue27Tests.java:
--------------------------------------------------------------------------------
1 | package com.google.typography.font.sfntly.issue_tests;
2 |
3 | import com.google.typography.font.sfntly.Font;
4 | import com.google.typography.font.sfntly.FontFactory;
5 | import com.google.typography.font.sfntly.Tag;
6 | import com.google.typography.font.sfntly.table.core.NameTable;
7 | import com.google.typography.font.sfntly.testutils.TestFont;
8 | import java.io.File;
9 | import java.io.FileInputStream;
10 | import junit.framework.TestCase;
11 |
12 | /*
13 | * Test for Issue 27.
14 | *
15 | * Adding a zero length name that sorts at the end of the name table.
16 | */
17 | public class Issue27Tests extends TestCase {
18 |
19 | private static final File fontFile = TestFont.TestFontNames.OPENSANS.getFile();
20 |
21 | public void testIssue27() throws Exception {
22 | FontFactory fontFactory = FontFactory.getInstance();
23 | Font.Builder fontBuilder = fontFactory.loadFontsForBuilding(new FileInputStream(fontFile))[0];
24 | NameTable.Builder nameTableBuilder = (NameTable.Builder) fontBuilder.getTableBuilder(Tag.name);
25 | // add a name that will sort after all the other names in the table
26 | nameTableBuilder
27 | .nameBuilder(
28 | Font.PlatformId.Windows.value(),
29 | Font.WindowsEncodingId.UnicodeUCS4.value(),
30 | NameTable.WindowsLanguageId.Spanish_UnitedStates.value(),
31 | NameTable.NameId.WWSSubfamilyName.value())
32 | .setName("");
33 | Font font = fontBuilder.build();
34 | assertNotNull(font);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/java/test/com/google/typography/font/sfntly/math/FontMathTest.java:
--------------------------------------------------------------------------------
1 | package com.google.typography.font.sfntly.math;
2 |
3 | import junit.framework.TestCase;
4 |
5 | public class FontMathTest extends TestCase {
6 |
7 | public void testLog2() {
8 | assertEquals(-1, FontMath.log2(0));
9 | assertEquals(0, FontMath.log2(1));
10 | assertEquals(1, FontMath.log2(2));
11 | assertEquals(1, FontMath.log2(3));
12 | assertEquals(2, FontMath.log2(4));
13 | assertEquals(4, FontMath.log2(31));
14 | assertEquals(5, FontMath.log2(32));
15 | assertEquals(31, FontMath.log2(-1));
16 | }
17 |
18 | public void testPaddingRequired() {
19 | assertEquals(0, FontMath.paddingRequired(4, 4));
20 | assertEquals(3, FontMath.paddingRequired(5, 4));
21 | assertEquals(1, FontMath.paddingRequired(7, 4));
22 | assertEquals(0, FontMath.paddingRequired(8, 4));
23 | assertEquals(0, FontMath.paddingRequired(17, 1));
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/java/test/com/google/typography/font/sfntly/table/bitmap/IndexSubTableTest.java:
--------------------------------------------------------------------------------
1 | package com.google.typography.font.sfntly.table.bitmap;
2 |
3 | import com.google.typography.font.sfntly.data.ReadableFontData;
4 | import junit.framework.TestCase;
5 |
6 | public class IndexSubTableTest extends TestCase {
7 |
8 | public void testCreateBuilder() {
9 | try {
10 | IndexSubTable.Builder.createBuilder(27);
11 | fail();
12 | } catch (IllegalArgumentException e) {
13 | assertEquals("Invalid index subtable format 27", e.getMessage());
14 | }
15 | }
16 |
17 | public void testToString() {
18 | ReadableFontData data =
19 | ReadableFontData.createReadableFontData(
20 | new byte[] {
21 | 0,
22 | 1, // indexFormat
23 | 0,
24 | 17, // imageFormat
25 | 0,
26 | 0,
27 | 0,
28 | 8 // imageDataOffset
29 | });
30 |
31 | IndexSubTable table =
32 | new IndexSubTable(data, 'a', 'a') {
33 | @Override
34 | public int glyphStartOffset(int glyphId) {
35 | throw new UnsupportedOperationException();
36 | }
37 |
38 | @Override
39 | public int glyphLength(int glyphId) {
40 | throw new UnsupportedOperationException();
41 | }
42 |
43 | @Override
44 | public int numGlyphs() {
45 | throw new UnsupportedOperationException();
46 | }
47 | };
48 |
49 | String expected =
50 | String.format(
51 | "%s%n", "IndexSubTable: [0x61 : 0x61], format = 1, image format = 17, imageOff = 0x8");
52 | assertEquals(expected, table.toString());
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/java/test/com/google/typography/font/sfntly/table/core/CMapCharIteratorsTest.java:
--------------------------------------------------------------------------------
1 | package com.google.typography.font.sfntly.table.core;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Arrays;
5 | import java.util.Iterator;
6 | import java.util.List;
7 | import junit.framework.TestCase;
8 |
9 | public class CMapCharIteratorsTest extends TestCase {
10 |
11 | public void testCharRangeIterator() {
12 | CMap.CharacterRangeIterator it = new CMap.CharacterRangeIterator(5, 8);
13 |
14 | assertEquals(Arrays.asList(5, 6, 7), toList(it));
15 | }
16 |
17 | public void testCharRangesIterator() {
18 | int[][] ranges = {{0, 5}, {8, 8}, {10, 11}, {0, 3}};
19 |
20 | CMap.CharacterRangesIterator it =
21 | new CMap.CharacterRangesIterator(ranges.length) {
22 | @Override
23 | protected int getRangeStart(int rangeIndex) {
24 | return ranges[rangeIndex][0];
25 | }
26 |
27 | @Override
28 | protected int getRangeEnd(int rangeIndex) {
29 | return ranges[rangeIndex][1];
30 | }
31 | };
32 |
33 | assertEquals(Arrays.asList(0, 1, 2, 3, 4, 10, 0, 1, 2), toList(it));
34 | }
35 |
36 | private static List toList(Iterator it) {
37 | List list = new ArrayList<>();
38 | while (it.hasNext()) {
39 | list.add(it.next());
40 | }
41 | return list;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/java/test/com/google/typography/font/sfntly/table/opentype/LanguageTagTest.java:
--------------------------------------------------------------------------------
1 | package com.google.typography.font.sfntly.table.opentype;
2 |
3 | import static org.hamcrest.CoreMatchers.is;
4 | import static org.junit.Assert.assertThat;
5 |
6 | import org.junit.Test;
7 |
8 | public class LanguageTagTest {
9 |
10 | @Test
11 | public void testDataConsistency() {
12 | for (LanguageTag languageTag : LanguageTag.values()) {
13 | assertThat(languageTag.languageSystem(), is(languageTag.languageSystem().trim()));
14 |
15 | for (String iso3 : languageTag.iso3List()) {
16 | assertThat(iso3.replaceAll("^[a-z]{3}$", ""), is(""));
17 | }
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/java/test/com/google/typography/font/sfntly/testutils/SfAssert.java:
--------------------------------------------------------------------------------
1 | package com.google.typography.font.sfntly.testutils;
2 |
3 | import com.google.typography.font.sfntly.table.Table;
4 | import java.io.ByteArrayOutputStream;
5 | import java.io.IOException;
6 | import org.junit.Assert;
7 |
8 | public final class SfAssert {
9 |
10 | public static void assertTableHexDumpEquals(String hex, Table table) {
11 | try {
12 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
13 | table.serialize(baos);
14 | byte[] bytes = baos.toByteArray();
15 |
16 | Assert.assertEquals(hex, hexdump(bytes));
17 | } catch (IOException e) {
18 | throw new IllegalStateException(e);
19 | }
20 | }
21 |
22 | private static String hexdump(byte[] bytes) {
23 | StringBuilder sb = new StringBuilder();
24 | for (int i = 0; i < bytes.length; i++) {
25 | sb.append("0123456789abcdef".charAt((bytes[i] >> 4) & 0x0f));
26 | sb.append("0123456789abcdef".charAt(bytes[i] & 0x0f));
27 | if (i + 1 < bytes.length) {
28 | sb.append(i % 16 == 15 ? "\n" : " ");
29 | }
30 | }
31 | return sb.toString();
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/java/test/com/google/typography/font/sfntly/testutils/TestFont.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.google.typography.font.sfntly.testutils;
18 |
19 | import java.io.File;
20 |
21 | /**
22 | * Holds TestFontNames enums which can be loaded into Fonts. Later, this will have support for
23 | * checking the fingerprints to make sure that the fonts haven't been changed.
24 | *
25 | * @author Vinay Shah
26 | */
27 | public class TestFont {
28 |
29 | private static final String TESTDATA_PATH = "../data/testdata/";
30 |
31 | public enum TestFontNames {
32 | DROIDSANS("DroidSans-Regular.ttf"),
33 | OPENSANS("OpenSans-Regular.ttf"),
34 | ROBOTO("Roboto-Regular.ttf");
35 |
36 | private final String path;
37 |
38 | private TestFontNames(String path) {
39 | this.path = path;
40 | }
41 |
42 | public File getFile() {
43 | return new File(TESTDATA_PATH + path);
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/java/test/com/google/typography/font/sfntly/testutils/TestUtilsTest.java:
--------------------------------------------------------------------------------
1 | package com.google.typography.font.sfntly.testutils;
2 |
3 | import org.assertj.core.api.Assertions;
4 | import org.junit.Test;
5 |
6 | public class TestUtilsTest {
7 |
8 | @Test
9 | public void hexFrom() {
10 | Assertions.assertThat(TestUtils.fromHex("").size()).isEqualTo(0);
11 |
12 | Assertions.assertThat(TestUtils.fromHex("12 34 56 78").size()).isEqualTo(4);
13 |
14 | Assertions.assertThat(TestUtils.fromHex("12 34 56 78").readULong(0)).isEqualTo(0x12345678);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/java/test/com/google/typography/font/tools/conversion/eot/HuffmanEncoderTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.google.typography.font.tools.conversion.eot;
18 |
19 | import junit.framework.TestCase;
20 |
21 | /** @author Raph Levien */
22 | public class HuffmanEncoderTest extends TestCase {
23 |
24 | public void testBitsUsed() {
25 | assertEquals(1, HuffmanEncoder.bitsUsed(0));
26 | assertEquals(1, HuffmanEncoder.bitsUsed(1));
27 | assertEquals(8, HuffmanEncoder.bitsUsed(255));
28 | assertEquals(9, HuffmanEncoder.bitsUsed(256));
29 | assertEquals(32, HuffmanEncoder.bitsUsed(-1));
30 | }
31 |
32 | public void testInitConsistency() {
33 | BitIOWriter bits = new BitIOWriter();
34 | HuffmanEncoder h = new HuffmanEncoder(bits, 512);
35 | assertNull(h.checkTree());
36 | }
37 |
38 | public void testRetainConsistency() {
39 | BitIOWriter bits = new BitIOWriter();
40 | HuffmanEncoder h = new HuffmanEncoder(bits, 8);
41 | for (int i = 0; i < 1024; i++) {
42 | assertNull("Iteration " + i, h.checkTree());
43 | h.writeSymbol((i & 1) != 0 ? 0 : i & 7);
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------