summaryrefslogtreecommitdiff
path: root/libs/minikin/FontFileParser.cpp
blob: a7d52102d440a88504e2e7226455323ca525631b (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
/*
 * 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.
 */

#include "minikin/FontFileParser.h"

#define LOG_TAG "Minikin"

#include <hb-ot.h>
#include <hb.h>

#include <cstdint>
#include <optional>
#include <string>
#include <vector>

#include "MinikinInternal.h"
#include "minikin/Constants.h"

namespace minikin {

namespace {

class SafeFontBufferReader {
public:
    SafeFontBufferReader(const void* buffer, size_t size)
            : mBuffer(reinterpret_cast<const uint8_t*>(buffer)),
              mSize(size),
              mPos(0),
              mError(false) {}

    template <typename T>
    T readBE() {
        if (mError) return T();

        if ((mSize - mPos) < sizeof(T)) {
            mError = true;
            return T();
        }
        const T* data = reinterpret_cast<const T*>(mBuffer + mPos);
        mPos += sizeof(T);
        return *data;
    }

    uint16_t readU16() {
        if (mError) return 0;

        if ((mSize - mPos) < 2) {
            mError = true;
            return 0;
        }
        uint16_t out = ((uint32_t)mBuffer[mPos]) << 8 | ((uint32_t)mBuffer[mPos + 1]);
        mPos += 2;
        return out;
    };

    uint32_t readU32() {
        if (mError) return 0;

        if ((mSize - mPos) < 4) {
            mError = true;
            return 0;
        }

        uint32_t out = ((uint32_t)mBuffer[mPos]) << 24 | ((uint32_t)mBuffer[mPos + 1]) << 16 |
                       ((uint32_t)mBuffer[mPos + 2]) << 8 | ((uint32_t)mBuffer[mPos + 3]);
        mPos += 4;
        return out;
    };

    void seek(size_t pos) {
        if (mError) return;

        if (pos > mSize) {
            mError = true;
        } else {
            mPos = pos;
        }
    }

    size_t remaining() const {
        if (mError) return 0;
        return mSize - mPos;
    }

    bool error() const { return mError; }

private:
    const uint8_t* mBuffer;
    size_t mSize;
    size_t mPos;
    bool mError;
};

bool isPostScriptNameAllowedChar(char c) {
    // OpenType spec says only ASCII codes 33 to 126, ecept for the '[', ']', '(', ')', '{', '}',
    // '<', '>', '/', '%'.
    if (!(33 <= c && c <= 126)) {
        return false;
    }
    if (c == '[' || c == ']' || c == '(' || c == ')' || c == '{' || c == '}' || c == '<' ||
        c == '>' || c == '/' || c == '%') {
        return false;
    }

    return true;
}

}  // namespace

// static
bool FontFileParser::analyzeFontRevision(const uint8_t* head_data, size_t head_size,
                                         uint32_t* out) {
    SafeFontBufferReader reader(head_data, head_size);

    if (reader.remaining() < 8) {
        return false;  // At least head table has 8 bytes, for version and fontRevision
    }

    uint32_t majorVersion = reader.readU16();
    if (reader.error()) return false;
    uint32_t minorVersion = reader.readU16();
    if (reader.error()) return false;

    // Invalid head table header.
    if (majorVersion != 1 && minorVersion != 0) return false;

    *out = reader.readU32();
    if (reader.error()) return false;
    return true;
}

// static
bool FontFileParser::checkPSName(const std::string& psName) {
    if (psName.size() > 63) return false;

    for (auto c : psName) {
        if (!isPostScriptNameAllowedChar(c)) {
            return false;
        }
    }
    return true;
}

FontFileParser::FontFileParser(const void* buffer, size_t size, uint32_t index)
        : mFace(makeHbFace(buffer, size, index)) {}

FontFileParser::FontFileParser(const HbFaceUniquePtr& face)
        : mFace(hb_face_reference(face.get())) {}

FontFileParser::FontFileParser(const HbFontUniquePtr& font)
        : mFace(hb_face_reference(hb_font_get_face(font.get()))) {}

FontFileParser::~FontFileParser() {}

// static
HbFaceUniquePtr FontFileParser::makeHbFace(const void* buffer, size_t size, uint32_t index) {
    HbBlobUniquePtr blob(hb_blob_create(reinterpret_cast<const char*>(buffer), size,
                                        HB_MEMORY_MODE_READONLY, nullptr, nullptr));
    return HbFaceUniquePtr(hb_face_create(blob.get(), index));
}

std::optional<uint32_t> FontFileParser::getFontRevision() const {
    if (!mFace) return std::optional<uint32_t>();

    HbBlob headTable(mFace, MakeTag('h', 'e', 'a', 'd'));
    if (!headTable) return std::optional<uint32_t>();

    uint32_t out = 0;
    if (!analyzeFontRevision(headTable.get(), headTable.size(), &out)) {
        return std::optional<uint32_t>();
    }

    return out;
}

std::optional<std::string> FontFileParser::getPostScriptName() const {
    if (!mFace) return std::optional<std::string>();

    unsigned int size = 64;  // PostScript name is up to 63 characters.
    char buf[64] = {};

    uint32_t result = hb_ot_name_get_utf8(mFace.get(), HB_OT_NAME_ID_POSTSCRIPT_NAME,
                                          HB_LANGUAGE_INVALID, &size, buf);

    if (result == 0) {  // not found.
        return std::optional<std::string>();
    }

    std::string out(buf, size);

    if (!checkPSName(out)) {  // Contains invalid characters.
        return std::optional<std::string>();
    }

    return out;
}

std::optional<bool> FontFileParser::isPostScriptType1Font() const {
    if (!mFace) return std::optional<bool>();

    HbBlob cffTable(mFace, MakeTag('C', 'F', 'F', ' '));
    HbBlob cff2Table(mFace, MakeTag('C', 'F', 'F', '2'));
    return cffTable || cff2Table;
}

}  // namespace minikin