Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/inc/font/TTFReader.hxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 */
9
10
#pragma once
11
12
#include <font/TTFStructure.hxx>
13
#include <vcl/font/FontDataContainer.hxx>
14
#include <rtl/ustrbuf.hxx>
15
#include <sal/log.hxx>
16
17
namespace font
18
{
19
/** Handles reading of the name table */
20
class NameTableHandler
21
{
22
private:
23
    FontDataContainer const& mrFontDataContainer;
24
25
    const TableDirectoryEntry* mpTableDirectoryEntry;
26
    const char* mpNameTablePointer;
27
    const NameTable* mpNameTable;
28
    sal_uInt16 mnNumberOfRecords;
29
30
    const char* getTablePointer(const TableDirectoryEntry* pEntry, size_t nEntrySize)
31
469
    {
32
469
        size_t nSize = mrFontDataContainer.size();
33
469
        if (pEntry->offset > nSize)
34
29
        {
35
29
            SAL_WARN("vcl.fonts", "Table offset beyond end of available data");
36
29
            return nullptr;
37
29
        }
38
440
        if (nEntrySize > nSize - pEntry->offset)
39
4
        {
40
4
            SAL_WARN("vcl.fonts", "Insufficient available data for table entry");
41
4
            return nullptr;
42
4
        }
43
436
        return mrFontDataContainer.getPointer() + pEntry->offset;
44
440
    }
45
46
public:
47
    NameTableHandler(FontDataContainer const& rFontDataContainer,
48
                     const TableDirectoryEntry* pTableDirectoryEntry)
49
469
        : mrFontDataContainer(rFontDataContainer)
50
469
        , mpTableDirectoryEntry(pTableDirectoryEntry)
51
469
        , mpNameTablePointer(getTablePointer(mpTableDirectoryEntry, sizeof(NameTable)))
52
469
        , mpNameTable(reinterpret_cast<const NameTable*>(mpNameTablePointer))
53
469
        , mnNumberOfRecords(0)
54
469
    {
55
469
        if (mpNameTable)
56
436
        {
57
436
            mnNumberOfRecords = mpNameTable->nCount;
58
59
436
            const char* pEnd = mrFontDataContainer.getPointer() + mrFontDataContainer.size();
60
436
            const char* pStart = mpNameTablePointer + sizeof(NameTable);
61
436
            size_t nAvailableData = pEnd - pStart;
62
436
            size_t nMaxRecordsPossible = nAvailableData / sizeof(NameRecord);
63
436
            if (mnNumberOfRecords > nMaxRecordsPossible)
64
373
            {
65
373
                SAL_WARN("vcl.fonts", "Font claimed to have "
66
373
                                          << mnNumberOfRecords
67
373
                                          << " name records, but only space for "
68
373
                                          << nMaxRecordsPossible);
69
373
                mnNumberOfRecords = nMaxRecordsPossible;
70
373
            }
71
436
        }
72
469
    }
73
74
496
    sal_uInt32 getTableOffset() { return mpTableDirectoryEntry->offset; }
75
76
496
    const NameTable* getNameTable() { return mpNameTable; }
77
78
    /** Number of tables */
79
1.87k
    sal_uInt16 getNumberOfRecords() { return mnNumberOfRecords; }
80
81
    /** Get a name table record for index */
82
    const NameRecord* getNameRecord(sal_uInt32 index)
83
2.72M
    {
84
2.72M
        const char* pPointer = mpNameTablePointer + sizeof(NameTable);
85
2.72M
        pPointer += sizeof(NameRecord) * index;
86
2.72M
        return reinterpret_cast<const NameRecord*>(pPointer);
87
2.72M
    }
88
89
    /** Get offset to english unicode string
90
     *
91
     * See: https://learn.microsoft.com/en-us/typography/opentype/spec/name#name-ids
92
     */
93
    bool findEnglishUnicodeNameOffset(font::NameID eNameID, sal_uInt64& rOffset,
94
                                      sal_uInt16& rLength)
95
1.87k
    {
96
1.87k
        rOffset = 0;
97
1.87k
        rLength = 0;
98
99
2.72M
        for (sal_uInt16 n = 0, nCount = getNumberOfRecords(); n < nCount; n++)
100
2.72M
        {
101
2.72M
            const font::NameRecord* pNameRecord = getNameRecord(n);
102
103
2.72M
            if (pNameRecord->nPlatformID == 3 // Windows
104
18.8k
                && pNameRecord->nEncodingID == 1 // Unicode BMP
105
15.8k
                && pNameRecord->nLanguageID == 0x0409 // en-us
106
7.77k
                && pNameRecord->nNameID == sal_uInt16(eNameID))
107
496
            {
108
496
                rLength = pNameRecord->nLength;
109
496
                rOffset = getTableOffset() + getNameTable()->nStorageOffset
110
496
                          + pNameRecord->nStringOffset;
111
496
                return true;
112
496
            }
113
2.72M
        }
114
1.38k
        return false;
115
1.87k
    }
116
};
117
118
/** Handles reading of table entries */
119
class TableEntriesHandler
120
{
121
private:
122
    FontDataContainer const& mrFontDataContainer;
123
    const char* mpFirstPosition;
124
    sal_uInt16 mnNumberOfTables;
125
126
    const char* getTablePointer(const TableDirectoryEntry* pEntry, size_t nEntrySize)
127
183
    {
128
183
        size_t nSize = mrFontDataContainer.size();
129
183
        if (pEntry->offset > nSize)
130
64
        {
131
64
            SAL_WARN("vcl.fonts", "Table offset beyond end of available data");
132
64
            return nullptr;
133
64
        }
134
119
        if (nEntrySize > nSize - pEntry->offset)
135
33
        {
136
33
            SAL_WARN("vcl.fonts", "Insufficient available data for table entry");
137
33
            return nullptr;
138
33
        }
139
86
        return mrFontDataContainer.getPointer() + pEntry->offset;
140
119
    }
141
142
public:
143
    TableEntriesHandler(FontDataContainer const& rFontDataContainer)
144
783
        : mrFontDataContainer(rFontDataContainer)
145
783
    {
146
783
        const char* pData = mrFontDataContainer.getPointer();
147
783
        assert(mrFontDataContainer.size() >= sizeof(TableDirectory));
148
783
        mpFirstPosition = pData + sizeof(TableDirectory);
149
150
783
        const TableDirectory* pDirectory = reinterpret_cast<const TableDirectory*>(pData);
151
783
        mnNumberOfTables = pDirectory->nNumberOfTables;
152
153
783
        size_t nAvailableData = mrFontDataContainer.size() - sizeof(TableDirectory);
154
783
        size_t nMaxRecordsPossible = nAvailableData / sizeof(TableDirectoryEntry);
155
783
        if (mnNumberOfTables > nMaxRecordsPossible)
156
629
        {
157
629
            SAL_WARN("vcl.fonts", "Font claimed to have " << mnNumberOfTables
158
629
                                                          << " table records, but only space for "
159
629
                                                          << nMaxRecordsPossible);
160
629
            mnNumberOfTables = nMaxRecordsPossible;
161
629
        }
162
783
    }
163
164
    const TableDirectoryEntry* getEntry(sal_uInt32 nTag)
165
2.34k
    {
166
3.07M
        for (sal_uInt32 i = 0; i < mnNumberOfTables; i++)
167
3.06M
        {
168
3.06M
            const char* pPosition = mpFirstPosition + sizeof(TableDirectoryEntry) * i;
169
3.06M
            const auto* pEntry = reinterpret_cast<const TableDirectoryEntry*>(pPosition);
170
171
3.06M
            if (nTag == pEntry->tag)
172
652
                return pEntry;
173
3.06M
        }
174
1.69k
        return nullptr;
175
2.34k
    }
176
177
    const OS2Table* getOS2Table()
178
783
    {
179
783
        const auto* pEntry = getEntry(T_OS2);
180
783
        if (!pEntry)
181
700
            return nullptr;
182
83
        return reinterpret_cast<const OS2Table*>(getTablePointer(pEntry, sizeof(OS2Table)));
183
783
    }
184
185
    const HeadTable* getHeadTable()
186
783
    {
187
783
        const auto* pEntry = getEntry(T_head);
188
783
        if (!pEntry)
189
683
            return nullptr;
190
100
        return reinterpret_cast<const HeadTable*>(getTablePointer(pEntry, sizeof(HeadTable)));
191
783
    }
192
193
    const NameTable* getNameTable()
194
0
    {
195
0
        const auto* pEntry = getEntry(T_name);
196
0
        if (!pEntry)
197
0
            return nullptr;
198
0
        return reinterpret_cast<const NameTable*>(getTablePointer(pEntry, sizeof(NameTable)));
199
0
    }
200
201
    std::unique_ptr<NameTableHandler> getNameTableHandler()
202
783
    {
203
783
        const auto* pEntry = getEntry(T_name);
204
783
        if (!pEntry)
205
314
            return nullptr;
206
207
469
        return std::unique_ptr<NameTableHandler>(new NameTableHandler(mrFontDataContainer, pEntry));
208
783
    }
209
};
210
211
/** Entry point handler for the TTF Font */
212
class TTFFont
213
{
214
private:
215
    FontDataContainer const& mrFontDataContainer;
216
217
public:
218
    TTFFont(FontDataContainer const& rFontDataContainer)
219
791
        : mrFontDataContainer(rFontDataContainer)
220
791
    {
221
791
    }
222
223
    std::unique_ptr<TableEntriesHandler> getTableEntriesHandler()
224
791
    {
225
791
        size_t nSize = mrFontDataContainer.size();
226
791
        if (nSize < sizeof(TableDirectory))
227
8
        {
228
8
            SAL_WARN("vcl.fonts", "Font Data shorter than a TableDirectory");
229
8
            return nullptr;
230
8
        }
231
783
        return std::make_unique<TableEntriesHandler>(mrFontDataContainer);
232
791
    }
233
234
    /** Gets the string from a name table */
235
    OUString getNameTableString(sal_uInt64 nOffset, sal_uInt16 nLength)
236
496
    {
237
496
        size_t nSize = mrFontDataContainer.size();
238
496
        if (nOffset > nSize)
239
97
        {
240
97
            SAL_WARN("vcl.fonts", "String offset beyond end of available data");
241
97
            return OUString();
242
97
        }
243
399
        if (nLength > nSize - nOffset)
244
65
        {
245
65
            SAL_WARN("vcl.fonts", "Insufficient available data for string entry");
246
65
            return OUString();
247
65
        }
248
334
        const auto* pString = reinterpret_cast<const o3tl::sal_uInt16_BE*>(
249
334
            mrFontDataContainer.getPointer() + nOffset);
250
334
        OUStringBuffer aStringBuffer;
251
470k
        for (sal_uInt16 i = 0; i < (nLength / 2); i++)
252
470k
            aStringBuffer.append(sal_Unicode(pString[i]));
253
334
        return aStringBuffer.makeStringAndClear();
254
399
    }
255
};
256
}
257
258
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */