summaryrefslogtreecommitdiff
path: root/tests/tests/graphics/src/android/graphics/fonts/FontFileTestUtil.java
blob: fcaab4bc0b7f6aff73470f96837354b655d7eb22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * 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 android.graphics.fonts;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

public class FontFileTestUtil {
    private static final int SFNT_VERSION_1 = 0x00010000;
    private static final int SFNT_VERSION_OTTO = 0x4F54544F;
    private static final int TTC_TAG = 0x74746366;
    private static final int NAME_TAG = 0x6E616D65;
    private static final int META_TAG = 0x6D657461;
    private static final int EMJI_TAG = 0x456D6A69;

    public static String getPostScriptName(File file) throws IOException {
        try (FileInputStream fis = new FileInputStream(file)) {
            final FileChannel fc = fis.getChannel();
            long size = fc.size();
            ByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, size)
                    .order(ByteOrder.BIG_ENDIAN);

            int magicNumber = buffer.getInt(0);

            int fontOffset = 0;
            if (magicNumber == TTC_TAG) {
                fontOffset = buffer.getInt(12);  // 0th offset
                magicNumber = buffer.getInt(fontOffset);
                if (magicNumber != SFNT_VERSION_1 && magicNumber != SFNT_VERSION_OTTO) {
                    throw new IOException("Unknown magic number at 0th font: #" + magicNumber);
                }
            } else if (magicNumber != SFNT_VERSION_1 && magicNumber != SFNT_VERSION_OTTO) {
                throw new IOException("Unknown magic number: #" + magicNumber);
            }

            int numTables = buffer.getShort(fontOffset + 4);  // offset to number of table
            int nameTableOffset = 0;
            for (int i = 0; i < numTables; ++i) {
                int tableEntryOffset = fontOffset + 12 + i * 16;
                int tableTag = buffer.getInt(tableEntryOffset);
                if (tableTag == NAME_TAG) {
                    nameTableOffset = buffer.getInt(tableEntryOffset + 8);
                    break;
                }
            }

            if (nameTableOffset == 0) {
                throw new IOException("name table not found.");
            }

            int nameTableCount = buffer.getShort(nameTableOffset + 2);
            int storageOffset = buffer.getShort(nameTableOffset + 4);

            for (int i = 0; i < nameTableCount; ++i) {
                int platformID = buffer.getShort(nameTableOffset + 6 + i * 12);
                int encodingID = buffer.getShort(nameTableOffset + 6 + i * 12 + 2);
                int languageID = buffer.getShort(nameTableOffset + 6 + i * 12 + 4);
                int nameID = buffer.getShort(nameTableOffset + 6 + i * 12 + 6);
                int length = buffer.getShort(nameTableOffset + 6 + i * 12 + 8);
                int stringOffset = buffer.getShort(nameTableOffset + 6 + i * 12 + 10);

                if (nameID == 6 && platformID == 3 && encodingID == 1 && languageID == 1033) {
                    byte[] name = new byte[length];
                    ByteBuffer slice = buffer.slice();
                    slice.position(nameTableOffset + storageOffset + stringOffset);
                    slice.get(name);
                    // encoded in UTF-16BE for platform ID = 3
                    return new String(name, StandardCharsets.UTF_16BE);
                }
            }
        }
        return null;
    }
}