Coverage Report

Created: 2026-06-30 11:14

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
468
    {
32
468
        size_t nSize = mrFontDataContainer.size();
33
468
        if (pEntry->offset > nSize)
34
32
        {
35
32
            SAL_WARN("vcl.fonts", "Table offset beyond end of available data");
36
32
            return nullptr;
37
32
        }
38
436
        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
432
        return mrFontDataContainer.getPointer() + pEntry->offset;
44
436
    }
45
46
public:
47
    NameTableHandler(FontDataContainer const& rFontDataContainer,
48
                     const TableDirectoryEntry* pTableDirectoryEntry)
49
468
        : mrFontDataContainer(rFontDataContainer)
50
468
        , mpTableDirectoryEntry(pTableDirectoryEntry)
51
468
        , mpNameTablePointer(getTablePointer(mpTableDirectoryEntry, sizeof(NameTable)))
52
468
        , mpNameTable(reinterpret_cast<const NameTable*>(mpNameTablePointer))
53
468
        , mnNumberOfRecords(0)
54
468
    {
55
468
        if (mpNameTable)
56
432
        {
57
432
            mnNumberOfRecords = mpNameTable->nCount;
58
59
432
            const char* pEnd = mrFontDataContainer.getPointer() + mrFontDataContainer.size();
60
432
            const char* pStart = mpNameTablePointer + sizeof(NameTable);
61
432
            size_t nAvailableData = pEnd - pStart;
62
432
            size_t nMaxRecordsPossible = nAvailableData / sizeof(NameRecord);
63
432
            if (mnNumberOfRecords > nMaxRecordsPossible)
64
376
            {
65
376
                SAL_WARN("vcl.fonts", "Font claimed to have "
66
376
                                          << mnNumberOfRecords
67
376
                                          << " name records, but only space for "
68
376
                                          << nMaxRecordsPossible);
69
376
                mnNumberOfRecords = nMaxRecordsPossible;
70
376
            }
71
432
        }
72
468
    }
73
74
480
    sal_uInt32 getTableOffset() { return mpTableDirectoryEntry->offset; }
75
76
480
    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.39M
    {
84
2.39M
        const char* pPointer = mpNameTablePointer + sizeof(NameTable);
85
2.39M
        pPointer += sizeof(NameRecord) * index;
86
2.39M
        return reinterpret_cast<const NameRecord*>(pPointer);
87
2.39M
    }
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.39M
        for (sal_uInt16 n = 0, nCount = getNumberOfRecords(); n < nCount; n++)
100
2.39M
        {
101
2.39M
            const font::NameRecord* pNameRecord = getNameRecord(n);
102
103
2.39M
            if (pNameRecord->nPlatformID == 3 // Windows
104
21.0k
                && pNameRecord->nEncodingID == 1 // Unicode BMP
105
17.2k
                && pNameRecord->nLanguageID == 0x0409 // en-us
106
7.07k
                && pNameRecord->nNameID == sal_uInt16(eNameID))
107
480
            {
108
480
                rLength = pNameRecord->nLength;
109
480
                rOffset = getTableOffset() + getNameTable()->nStorageOffset
110
480
                          + pNameRecord->nStringOffset;
111
480
                return true;
112
480
            }
113
2.39M
        }
114
1.39k
        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
186
    {
128
186
        size_t nSize = mrFontDataContainer.size();
129
186
        if (pEntry->offset > nSize)
130
66
        {
131
66
            SAL_WARN("vcl.fonts", "Table offset beyond end of available data");
132
66
            return nullptr;
133
66
        }
134
120
        if (nEntrySize > nSize - pEntry->offset)
135
31
        {
136
31
            SAL_WARN("vcl.fonts", "Insufficient available data for table entry");
137
31
            return nullptr;
138
31
        }
139
89
        return mrFontDataContainer.getPointer() + pEntry->offset;
140
120
    }
141
142
public:
143
    TableEntriesHandler(FontDataContainer const& rFontDataContainer)
144
789
        : mrFontDataContainer(rFontDataContainer)
145
789
    {
146
789
        const char* pData = mrFontDataContainer.getPointer();
147
789
        assert(mrFontDataContainer.size() >= sizeof(TableDirectory));
148
789
        mpFirstPosition = pData + sizeof(TableDirectory);
149
150
789
        const TableDirectory* pDirectory = reinterpret_cast<const TableDirectory*>(pData);
151
789
        mnNumberOfTables = pDirectory->nNumberOfTables;
152
153
789
        size_t nAvailableData = mrFontDataContainer.size() - sizeof(TableDirectory);
154
789
        size_t nMaxRecordsPossible = nAvailableData / sizeof(TableDirectoryEntry);
155
789
        if (mnNumberOfTables > nMaxRecordsPossible)
156
633
        {
157
633
            SAL_WARN("vcl.fonts", "Font claimed to have " << mnNumberOfTables
158
633
                                                          << " table records, but only space for "
159
633
                                                          << nMaxRecordsPossible);
160
633
            mnNumberOfTables = nMaxRecordsPossible;
161
633
        }
162
789
    }
163
164
    const TableDirectoryEntry* getEntry(sal_uInt32 nTag)
165
2.36k
    {
166
2.76M
        for (sal_uInt32 i = 0; i < mnNumberOfTables; i++)
167
2.75M
        {
168
2.75M
            const char* pPosition = mpFirstPosition + sizeof(TableDirectoryEntry) * i;
169
2.75M
            const auto* pEntry = reinterpret_cast<const TableDirectoryEntry*>(pPosition);
170
171
2.75M
            if (nTag == pEntry->tag)
172
654
                return pEntry;
173
2.75M
        }
174
1.71k
        return nullptr;
175
2.36k
    }
176
177
    const OS2Table* getOS2Table()
178
789
    {
179
789
        const auto* pEntry = getEntry(T_OS2);
180
789
        if (!pEntry)
181
706
            return nullptr;
182
83
        return reinterpret_cast<const OS2Table*>(getTablePointer(pEntry, sizeof(OS2Table)));
183
789
    }
184
185
    const HeadTable* getHeadTable()
186
789
    {
187
789
        const auto* pEntry = getEntry(T_head);
188
789
        if (!pEntry)
189
686
            return nullptr;
190
103
        return reinterpret_cast<const HeadTable*>(getTablePointer(pEntry, sizeof(HeadTable)));
191
789
    }
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
789
    {
203
789
        const auto* pEntry = getEntry(T_name);
204
789
        if (!pEntry)
205
321
            return nullptr;
206
207
468
        return std::unique_ptr<NameTableHandler>(new NameTableHandler(mrFontDataContainer, pEntry));
208
789
    }
209
};
210
211
/** Entry point handler for the TTF Font */
212
class TTFFont
213
{
214
private:
215
    FontDataContainer const& mrFontDataContainer;
216
217
    const char* getTablePointer(const TableDirectoryEntry* pEntry)
218
0
    {
219
0
        return mrFontDataContainer.getPointer() + pEntry->offset;
220
0
    }
221
222
public:
223
    TTFFont(FontDataContainer const& rFontDataContainer)
224
797
        : mrFontDataContainer(rFontDataContainer)
225
797
    {
226
797
    }
227
228
    std::unique_ptr<TableEntriesHandler> getTableEntriesHandler()
229
797
    {
230
797
        size_t nSize = mrFontDataContainer.size();
231
797
        if (nSize < sizeof(TableDirectory))
232
8
        {
233
8
            SAL_WARN("vcl.fonts", "Font Data shorter than a TableDirectory");
234
8
            return nullptr;
235
8
        }
236
789
        return std::make_unique<TableEntriesHandler>(mrFontDataContainer);
237
797
    }
238
239
    /** Gets the string from a name table */
240
    OUString getNameTableString(sal_uInt64 nOffset, sal_uInt16 nLength)
241
480
    {
242
480
        size_t nSize = mrFontDataContainer.size();
243
480
        if (nOffset > nSize)
244
107
        {
245
107
            SAL_WARN("vcl.fonts", "String offset beyond end of available data");
246
107
            return OUString();
247
107
        }
248
373
        if (nLength > nSize - nOffset)
249
61
        {
250
61
            SAL_WARN("vcl.fonts", "Insufficient available data for string entry");
251
61
            return OUString();
252
61
        }
253
312
        const auto* pString = reinterpret_cast<const o3tl::sal_uInt16_BE*>(
254
312
            mrFontDataContainer.getPointer() + nOffset);
255
312
        OUStringBuffer aStringBuffer;
256
463k
        for (sal_uInt16 i = 0; i < (nLength / 2); i++)
257
463k
            aStringBuffer.append(sal_Unicode(pString[i]));
258
312
        return aStringBuffer.makeStringAndClear();
259
373
    }
260
};
261
}
262
263
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */