summaryrefslogtreecommitdiff
path: root/android/support/text/emoji/MetadataListReader.java
blob: 1008c171a8d77385586cd75f46f32e67bb250f4a (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * Copyright (C) 2017 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.support.text.emoji;

import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;

import android.content.res.AssetManager;
import android.support.annotation.AnyThread;
import android.support.annotation.IntRange;
import android.support.annotation.RequiresApi;
import android.support.annotation.RestrictTo;
import android.support.text.emoji.flatbuffer.MetadataList;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * Reads the emoji metadata from a given InputStream or ByteBuffer.
 *
 * @hide
 */
@RestrictTo(LIBRARY_GROUP)
@AnyThread
@RequiresApi(19)
class MetadataListReader {

    /**
     * Meta tag for emoji metadata. This string is used by the font update script to insert the
     * emoji meta into the font. This meta table contains the list of all emojis which are stored in
     * binary format using FlatBuffers. This flat list is later converted by the system into a trie.
     * {@code int} representation for "Emji"
     *
     * @see MetadataRepo
     */
    private static final int EMJI_TAG = 'E' << 24 | 'm' << 16 | 'j' << 8 | 'i';

    /**
     * Deprecated meta tag name. Do not use, kept for compatibility reasons, will be removed soon.
     */
    private static final int EMJI_TAG_DEPRECATED = 'e' << 24 | 'm' << 16 | 'j' << 8 | 'i';

    /**
     * The name of the meta table in the font. int representation for "meta"
     */
    private static final int META_TABLE_NAME = 'm' << 24 | 'e' << 16 | 't' << 8 | 'a';

    /**
     * Construct MetadataList from an input stream. Does not close the given InputStream, therefore
     * it is caller's responsibility to properly close the stream.
     *
     * @param inputStream InputStream to read emoji metadata from
     */
    static MetadataList read(InputStream inputStream) throws IOException {
        final OpenTypeReader openTypeReader = new InputStreamOpenTypeReader(inputStream);
        final OffsetInfo offsetInfo = findOffsetInfo(openTypeReader);
        // skip to where metadata is
        openTypeReader.skip((int) (offsetInfo.getStartOffset() - openTypeReader.getPosition()));
        // allocate a ByteBuffer and read into it since FlatBuffers can read only from a ByteBuffer
        final ByteBuffer buffer = ByteBuffer.allocate((int) offsetInfo.getLength());
        final int numRead = inputStream.read(buffer.array());
        if (numRead != offsetInfo.getLength()) {
            throw new IOException("Needed " + offsetInfo.getLength() + " bytes, got " + numRead);
        }

        return MetadataList.getRootAsMetadataList(buffer);
    }

    /**
     * Construct MetadataList from a byte buffer.
     *
     * @param byteBuffer ByteBuffer to read emoji metadata from
     */
    static MetadataList read(final ByteBuffer byteBuffer) throws IOException {
        final ByteBuffer newBuffer = byteBuffer.duplicate();
        final OpenTypeReader reader = new ByteBufferReader(newBuffer);
        final OffsetInfo offsetInfo = findOffsetInfo(reader);
        // skip to where metadata is
        newBuffer.position((int) offsetInfo.getStartOffset());
        return MetadataList.getRootAsMetadataList(newBuffer);
    }

    /**
     * Construct MetadataList from an asset.
     *
     * @param assetManager AssetManager instance
     * @param assetPath asset manager path of the file that the Typeface and metadata will be
     *                  created from
     */
    static MetadataList read(AssetManager assetManager, String assetPath)
            throws IOException {
        try (InputStream inputStream = assetManager.open(assetPath)) {
            return read(inputStream);
        }
    }

    /**
     * Finds the start offset and length of the emoji metadata in the font.
     *
     * @return OffsetInfo which contains start offset and length of the emoji metadata in the font
     *
     * @throws IOException
     */
    private static OffsetInfo findOffsetInfo(OpenTypeReader reader) throws IOException {
        // skip sfnt version
        reader.skip(OpenTypeReader.UINT32_BYTE_COUNT);
        // start of Table Count
        final int tableCount = reader.readUnsignedShort();
        if (tableCount > 100) {
            //something is wrong quit
            throw new IOException("Cannot read metadata.");
        }
        //skip to begining of tables data
        reader.skip(OpenTypeReader.UINT16_BYTE_COUNT * 3);

        long metaOffset = -1;
        for (int i = 0; i < tableCount; i++) {
            final int tag = reader.readTag();
            // skip checksum
            reader.skip(OpenTypeReader.UINT32_BYTE_COUNT);
            final long offset = reader.readUnsignedInt();
            // skip mLength
            reader.skip(OpenTypeReader.UINT32_BYTE_COUNT);
            if (META_TABLE_NAME == tag) {
                metaOffset = offset;
                break;
            }
        }

        if (metaOffset != -1) {
            // skip to the begining of meta tables.
            reader.skip((int) (metaOffset - reader.getPosition()));
            // skip minorVersion, majorVersion, flags, reserved,
            reader.skip(
                    OpenTypeReader.UINT16_BYTE_COUNT * 2 + OpenTypeReader.UINT32_BYTE_COUNT * 2);
            final long mapsCount = reader.readUnsignedInt();
            for (int i = 0; i < mapsCount; i++) {
                final int tag = reader.readTag();
                final long dataOffset = reader.readUnsignedInt();
                final long dataLength = reader.readUnsignedInt();
                if (EMJI_TAG == tag || EMJI_TAG_DEPRECATED == tag) {
                    return new OffsetInfo(dataOffset + metaOffset, dataLength);
                }
            }
        }

        throw new IOException("Cannot read metadata.");
    }

    /**
     * Start offset and length of the emoji metadata in the font.
     */
    private static class OffsetInfo {
        private final long mStartOffset;
        private final long mLength;

        OffsetInfo(long startOffset, long length) {
            mStartOffset = startOffset;
            mLength = length;
        }

        long getStartOffset() {
            return mStartOffset;
        }

        long getLength() {
            return mLength;
        }
    }

    private static int toUnsignedShort(final short value) {
        return value & 0xFFFF;
    }

    private static long toUnsignedInt(final int value) {
        return value & 0xFFFFFFFFL;
    }

    private interface OpenTypeReader {
        int UINT16_BYTE_COUNT = 2;
        int UINT32_BYTE_COUNT = 4;

        /**
         * Reads an {@code OpenType uint16}.
         *
         * @throws IOException
         */
        int readUnsignedShort() throws IOException;

        /**
         * Reads an {@code OpenType uint32}.
         *
         * @throws IOException
         */
        long readUnsignedInt() throws IOException;

        /**
         * Reads an {@code OpenType Tag}.
         *
         * @throws IOException
         */
        int readTag() throws IOException;

        /**
         * Skip the given amount of numOfBytes
         *
         * @throws IOException
         */
        void skip(int numOfBytes) throws IOException;

        /**
         * @return the position of the reader
         */
        long getPosition();
    }

    /**
     * Reads {@code OpenType} data from an {@link InputStream}.
     */
    private static class InputStreamOpenTypeReader implements OpenTypeReader {

        private final byte[] mByteArray;
        private final ByteBuffer mByteBuffer;
        private final InputStream mInputStream;
        private long mPosition = 0;

        /**
         * Constructs the reader with the given InputStream. Does not close the InputStream, it is
         * caller's responsibility to close it.
         *
         * @param inputStream InputStream to read from
         */
        InputStreamOpenTypeReader(final InputStream inputStream) {
            mInputStream = inputStream;
            mByteArray = new byte[UINT32_BYTE_COUNT];
            mByteBuffer = ByteBuffer.wrap(mByteArray);
            mByteBuffer.order(ByteOrder.BIG_ENDIAN);
        }

        @Override
        public int readUnsignedShort() throws IOException {
            mByteBuffer.position(0);
            read(UINT16_BYTE_COUNT);
            return toUnsignedShort(mByteBuffer.getShort());
        }

        @Override
        public long readUnsignedInt() throws IOException {
            mByteBuffer.position(0);
            read(UINT32_BYTE_COUNT);
            return toUnsignedInt(mByteBuffer.getInt());
        }

        @Override
        public int readTag() throws IOException {
            mByteBuffer.position(0);
            read(UINT32_BYTE_COUNT);
            return mByteBuffer.getInt();
        }

        @Override
        public void skip(int numOfBytes) throws IOException {
            while (numOfBytes > 0) {
                int skipped = (int) mInputStream.skip(numOfBytes);
                if (skipped < 1) {
                    throw new IOException("Skip didn't move at least 1 byte forward");
                }
                numOfBytes -= skipped;
                mPosition += skipped;
            }
        }

        @Override
        public long getPosition() {
            return mPosition;
        }

        private void read(@IntRange(from = 0, to = UINT32_BYTE_COUNT) final int numOfBytes)
                throws IOException {
            if (mInputStream.read(mByteArray, 0, numOfBytes) != numOfBytes) {
                throw new IOException("read failed");
            }
            mPosition += numOfBytes;
        }
    }

    /**
     * Reads OpenType data from a ByteBuffer.
     */
    private static class ByteBufferReader implements OpenTypeReader {

        private final ByteBuffer mByteBuffer;

        /**
         * Constructs the reader with the given ByteBuffer.
         *
         * @param byteBuffer ByteBuffer to read from
         */
        ByteBufferReader(final ByteBuffer byteBuffer) {
            mByteBuffer = byteBuffer;
            mByteBuffer.order(ByteOrder.BIG_ENDIAN);
        }

        @Override
        public int readUnsignedShort() throws IOException {
            return toUnsignedShort(mByteBuffer.getShort());
        }

        @Override
        public long readUnsignedInt() throws IOException {
            return toUnsignedInt(mByteBuffer.getInt());
        }

        @Override
        public int readTag() throws IOException {
            return mByteBuffer.getInt();
        }

        @Override
        public void skip(final int numOfBytes) throws IOException {
            mByteBuffer.position(mByteBuffer.position() + numOfBytes);
        }

        @Override
        public long getPosition() {
            return mByteBuffer.position();
        }
    }
}