Coverage Report

Created: 2026-11-06 11:29

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
471
    {
32
471
        size_t nSize = mrFontDataContainer.size();
33
471
        if (pEntry->offset > nSize)
34
28
        {
35
28
            SAL_WARN("vcl.fonts", "Table offset beyond end of available data");
36
28
            return nullptr;
37
28
        }
38
443
        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
439
        return mrFontDataContainer.getPointer() + pEntry->offset;
44
443
    }
45
46
public:
47
    NameTableHandler(FontDataContainer const& rFontDataContainer,
48
                     const TableDirectoryEntry* pTableDirectoryEntry)
49
471
        : mrFontDataContainer(rFontDataContainer)
50
471
        , mpTableDirectoryEntry(pTableDirectoryEntry)
51
471
        , mpNameTablePointer(getTablePointer(mpTableDirectoryEntry, sizeof(NameTable)))
52
471
        , mpNameTable(reinterpret_cast<const NameTable*>(mpNameTablePointer))
53
471
        , mnNumberOfRecords(0)
54
471
    {
55
471
        if (mpNameTable)
56
439
        {
57
439
            mnNumberOfRecords = mpNameTable->nCount;
58
59
439
            const char* pEnd = mrFontDataContainer.getPointer() + mrFontDataContainer.size();
60
439
            const char* pStart = mpNameTablePointer + sizeof(NameTable);
61
439
            size_t nAvailableData = pEnd - pStart;
62
439
            size_t nMaxRecordsPossible = nAvailableData / sizeof(NameRecord);
63
439
            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
439
        }
72
471
    }
73
74
502
    sal_uInt32 getTableOffset() { return mpTableDirectoryEntry->offset; }
75
76
502
    const NameTable* getNameTable() { return mpNameTable; }
77
78
    /** Number of tables */
79
1.88k
    sal_uInt16 getNumberOfRecords() { return mnNumberOfRecords; }
80
81
    /** Get a name table record for index */
82
    const NameRecord* getNameRecord(sal_uInt32 index)
83
2.85M
    {
84
2.85M
        const char* pPointer = mpNameTablePointer + sizeof(NameTable);
85
2.85M
        pPointer += sizeof(NameRecord) * index;
86
2.85M
        return reinterpret_cast<const NameRecord*>(pPointer);
87
2.85M
    }
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.88k
    {
96
1.88k
        rOffset = 0;
97
1.88k
        rLength = 0;
98
99
2.85M
        for (sal_uInt16 n = 0, nCount = getNumberOfRecords(); n < nCount; n++)
100
2.85M
        {
101
2.85M
            const font::NameRecord* pNameRecord = getNameRecord(n);
102
103
2.85M
            if (pNameRecord->nPlatformID == 3 // Windows
104
19.9k
                && pNameRecord->nEncodingID == 1 // Unicode BMP
105
16.2k
                && pNameRecord->nLanguageID == 0x0409 // en-us
106
7.25k
                && pNameRecord->nNameID == sal_uInt16(eNameID))
107
502
            {
108
502
                rLength = pNameRecord->nLength;
109
502
                rOffset = getTableOffset() + getNameTable()->nStorageOffset
110
502
                          + pNameRecord->nStringOffset;
111
502
                return true;
112
502
            }
113
2.85M
        }
114
1.38k
        return false;
115
1.88k
    }
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
187
    {
128
187
        size_t nSize = mrFontDataContainer.size();
129
187
        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
121
        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
88
        return mrFontDataContainer.getPointer() + pEntry->offset;
140
121
    }
141
142
public:
143
    TableEntriesHandler(FontDataContainer const& rFontDataContainer)
144
793
        : mrFontDataContainer(rFontDataContainer)
145
793
    {
146
793
        const char* pData = mrFontDataContainer.getPointer();
147
793
        assert(mrFontDataContainer.size() >= sizeof(TableDirectory));
148
793
        mpFirstPosition = pData + sizeof(TableDirectory);
149
150
793
        const TableDirectory* pDirectory = reinterpret_cast<const TableDirectory*>(pData);
151
793
        mnNumberOfTables = pDirectory->nNumberOfTables;
152
153
793
        size_t nAvailableData = mrFontDataContainer.size() - sizeof(TableDirectory);
154
793
        size_t nMaxRecordsPossible = nAvailableData / sizeof(TableDirectoryEntry);
155
793
        if (mnNumberOfTables > nMaxRecordsPossible)
156
641
        {
157
641
            SAL_WARN("vcl.fonts", "Font claimed to have " << mnNumberOfTables
158
641
                                                          << " table records, but only space for "
159
641
                                                          << nMaxRecordsPossible);
160
641
            mnNumberOfTables = nMaxRecordsPossible;
161
641
        }
162
793
    }
163
164
    const TableDirectoryEntry* getEntry(sal_uInt32 nTag)
165
2.37k
    {
166
2.82M
        for (sal_uInt32 i = 0; i < mnNumberOfTables; i++)
167
2.82M
        {
168
2.82M
            const char* pPosition = mpFirstPosition + sizeof(TableDirectoryEntry) * i;
169
2.82M
            const auto* pEntry = reinterpret_cast<const TableDirectoryEntry*>(pPosition);
170
171
2.82M
            if (nTag == pEntry->tag)
172
658
                return pEntry;
173
2.82M
        }
174
1.72k
        return nullptr;
175
2.37k
    }
176
177
    const OS2Table* getOS2Table()
178
793
    {
179
793
        const auto* pEntry = getEntry(T_OS2);
180
793
        if (!pEntry)
181
708
            return nullptr;
182
85
        return reinterpret_cast<const OS2Table*>(getTablePointer(pEntry, sizeof(OS2Table)));
183
793
    }
184
185
    const HeadTable* getHeadTable()
186
793
    {
187
793
        const auto* pEntry = getEntry(T_head);
188
793
        if (!pEntry)
189
691
            return nullptr;
190
102
        return reinterpret_cast<const HeadTable*>(getTablePointer(pEntry, sizeof(HeadTable)));
191
793
    }
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
793
    {
203
793
        const auto* pEntry = getEntry(T_name);
204
793
        if (!pEntry)
205
322
            return nullptr;
206
207
471
        return std::unique_ptr<NameTableHandler>(new NameTableHandler(mrFontDataContainer, pEntry));
208
793
    }
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
801
        : mrFontDataContainer(rFontDataContainer)
220
801
    {
221
801
    }
222
223
    std::unique_ptr<TableEntriesHandler> getTableEntriesHandler()
224
801
    {
225
801
        size_t nSize = mrFontDataContainer.size();
226
801
        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
793
        return std::make_unique<TableEntriesHandler>(mrFontDataContainer);
232
801
    }
233
234
    /** Gets the string from a name table */
235
    OUString getNameTableString(sal_uInt64 nOffset, sal_uInt16 nLength)
236
502
    {
237
502
        size_t nSize = mrFontDataContainer.size();
238
502
        if (nOffset > nSize)
239
100
        {
240
100
            SAL_WARN("vcl.fonts", "String offset beyond end of available data");
241
100
            return OUString();
242
100
        }
243
402
        if (nLength > nSize - nOffset)
244
69
        {
245
69
            SAL_WARN("vcl.fonts", "Insufficient available data for string entry");
246
69
            return OUString();
247
69
        }
248
333
        const auto* pString = reinterpret_cast<const o3tl::sal_uInt16_BE*>(
249
333
            mrFontDataContainer.getPointer() + nOffset);
250
333
        OUStringBuffer aStringBuffer;
251
464k
        for (sal_uInt16 i = 0; i < (nLength / 2); i++)
252
464k
            aStringBuffer.append(sal_Unicode(pString[i]));
253
333
        return aStringBuffer.makeStringAndClear();
254
402
    }
255
};
256
}
257
258
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */