CmapCoverage.cpp revision 998293f985dc6c23f90b160f3bc647807c76d3fe
1/*
2 * Copyright (C) 2013 The Android Open Source Project
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// Determine coverage of font given its raw "cmap" OpenType table
18
19#define LOG_TAG "Minikin"
20#include <cutils/log.h>
21
22#include <vector>
23using std::vector;
24
25#include <minikin/SparseBitSet.h>
26#include <minikin/CmapCoverage.h>
27
28namespace android {
29
30// These could perhaps be optimized to use __builtin_bswap16 and friends.
31static uint32_t readU16(const uint8_t* data, size_t offset) {
32    return ((uint32_t)data[offset]) << 8 | ((uint32_t)data[offset + 1]);
33}
34
35static uint32_t readU32(const uint8_t* data, size_t offset) {
36    return ((uint32_t)data[offset]) << 24 | ((uint32_t)data[offset + 1]) << 16 |
37        ((uint32_t)data[offset + 2]) << 8 | ((uint32_t)data[offset + 3]);
38}
39
40static void addRange(vector<uint32_t> &coverage, uint32_t start, uint32_t end) {
41#ifdef VERBOSE_DEBUG
42    ALOGD("adding range %d-%d\n", start, end);
43#endif
44    if (coverage.empty() || coverage.back() < start) {
45        coverage.push_back(start);
46        coverage.push_back(end);
47    } else {
48        coverage.back() = end;
49    }
50}
51
52// Get the coverage information out of a Format 12 subtable, storing it in the coverage vector
53static bool getCoverageFormat4(vector<uint32_t>& coverage, const uint8_t* data, size_t size) {
54    const size_t kSegCountOffset = 6;
55    const size_t kEndCountOffset = 14;
56    const size_t kHeaderSize = 16;
57    const size_t kSegmentSize = 8;  // total size of array elements for one segment
58    if (kEndCountOffset > size) {
59        return false;
60    }
61    size_t segCount = readU16(data, kSegCountOffset) >> 1;
62    if (kHeaderSize + segCount * kSegmentSize > size) {
63        return false;
64    }
65    for (size_t i = 0; i < segCount; i++) {
66        int end = readU16(data, kEndCountOffset + 2 * i);
67        int start = readU16(data, kHeaderSize + 2 * (segCount + i));
68        int rangeOffset = readU16(data, kHeaderSize + 2 * (3 * segCount + i));
69        if (rangeOffset == 0) {
70            int delta = readU16(data, kHeaderSize + 2 * (2 * segCount + i));
71            if (((end + delta) & 0xffff) > end - start) {
72                addRange(coverage, start, end + 1);
73            } else {
74                for (int j = start; j < end + 1; j++) {
75                    if (((j + delta) & 0xffff) != 0) {
76                        addRange(coverage, j, j + 1);
77                    }
78                }
79            }
80        } else {
81            for (int j = start; j < end + 1; j++) {
82                uint32_t actualRangeOffset = kHeaderSize + 6 * segCount + rangeOffset +
83                    (i + j - start) * 2;
84                if (actualRangeOffset + 2 > size) {
85                    // invalid rangeOffset is considered a "warning" by OpenType Sanitizer
86                    continue;
87                }
88                int glyphId = readU16(data, actualRangeOffset);
89                if (glyphId != 0) {
90                    addRange(coverage, j, j + 1);
91                }
92            }
93        }
94    }
95    return true;
96}
97
98// Get the coverage information out of a Format 12 subtable, storing it in the coverage vector
99static bool getCoverageFormat12(vector<uint32_t>& coverage, const uint8_t* data, size_t size) {
100    const size_t kNGroupsOffset = 12;
101    const size_t kFirstGroupOffset = 16;
102    const size_t kGroupSize = 12;
103    const size_t kStartCharCodeOffset = 0;
104    const size_t kEndCharCodeOffset = 4;
105    const size_t kMaxNGroups = 0xfffffff0 / kGroupSize;  // protection against overflow
106    // For all values < kMaxNGroups, kFirstGroupOffset + nGroups * kGroupSize fits in 32 bits.
107    if (kFirstGroupOffset > size) {
108        return false;
109    }
110    uint32_t nGroups = readU32(data, kNGroupsOffset);
111    if (nGroups >= kMaxNGroups || kFirstGroupOffset + nGroups * kGroupSize > size) {
112        return false;
113    }
114    for (uint32_t i = 0; i < nGroups; i++) {
115        uint32_t groupOffset = kFirstGroupOffset + i * kGroupSize;
116        uint32_t start = readU32(data, groupOffset + kStartCharCodeOffset);
117        uint32_t end = readU32(data, groupOffset + kEndCharCodeOffset);
118        addRange(coverage, start, end + 1);  // file is inclusive, vector is exclusive
119    }
120    return true;
121}
122
123bool CmapCoverage::getCoverage(SparseBitSet& coverage, const uint8_t* cmap_data, size_t cmap_size) {
124    vector<uint32_t> coverageVec;
125    const size_t kHeaderSize = 4;
126    const size_t kNumTablesOffset = 2;
127    const size_t kTableSize = 8;
128    const size_t kPlatformIdOffset = 0;
129    const size_t kEncodingIdOffset = 2;
130    const size_t kOffsetOffset = 4;
131    const int kMicrosoftPlatformId = 3;
132    const int kUnicodeBmpEncodingId = 1;
133    const int kUnicodeUcs4EncodingId = 10;
134    if (kHeaderSize > cmap_size) {
135        return false;
136    }
137    int numTables = readU16(cmap_data, kNumTablesOffset);
138    if (kHeaderSize + numTables * kTableSize > cmap_size) {
139        return false;
140    }
141    int bestTable = -1;
142    for (int i = 0; i < numTables; i++) {
143        uint16_t platformId = readU16(cmap_data, kHeaderSize + i * kTableSize + kPlatformIdOffset);
144        uint16_t encodingId = readU16(cmap_data, kHeaderSize + i * kTableSize + kEncodingIdOffset);
145        if (platformId == kMicrosoftPlatformId && encodingId == kUnicodeUcs4EncodingId) {
146            bestTable = i;
147            break;
148        } else if (platformId == kMicrosoftPlatformId && encodingId == kUnicodeBmpEncodingId) {
149            bestTable = i;
150        }
151    }
152#ifdef VERBOSE_DEBUG
153    ALOGD("best table = %d\n", bestTable);
154#endif
155    if (bestTable < 0) {
156        return false;
157    }
158    uint32_t offset = readU32(cmap_data, kHeaderSize + bestTable * kTableSize + kOffsetOffset);
159    if (offset + 2 > cmap_size) {
160        return false;
161    }
162    uint16_t format = readU16(cmap_data, offset);
163    bool success = false;
164    const uint8_t* tableData = cmap_data + offset;
165    const size_t tableSize = cmap_size - offset;
166    if (format == 4) {
167        success = getCoverageFormat4(coverageVec, tableData, tableSize);
168    } else if (format == 12) {
169        success = getCoverageFormat12(coverageVec, tableData, tableSize);
170    }
171    if (success) {
172        coverage.initFromRanges(&coverageVec.front(), coverageVec.size() >> 1);
173    }
174#ifdef VERBOSE_DEBUG
175    for (size_t i = 0; i < coverageVec.size(); i += 2) {
176        ALOGD("%x:%x\n", coverageVec[i], coverageVec[i + 1]);
177    }
178    ALOGD("success = %d", success);
179#endif
180    return success;
181}
182
183}  // namespace android
184