Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/graphite/src/GlyphCache.cpp
Line
Count
Source
1
// SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later
2
// Copyright 2012, SIL International, All rights reserved.
3
4
#include "graphite2/Font.h"
5
6
#include "inc/Main.h"
7
#include "inc/Face.h"     //for the tags
8
#include "inc/GlyphCache.h"
9
#include "inc/GlyphFace.h"
10
#include "inc/Endian.h"
11
#include "inc/bits.h"
12
13
using namespace graphite2;
14
15
namespace
16
{
17
    // Iterator over version 1 or 2 glat entries which consist of a series of
18
    //    +-+-+-+-+-+-+-+-+-+-+                +-+-+-+-+-+-+-+-+-+-+-+-+
19
    // v1 |k|n|v1 |v2 |...|vN |     or    v2   | k | n |v1 |v2 |...|vN |
20
    //    +-+-+-+-+-+-+-+-+-+-+                +-+-+-+-+-+-+-+-+-+-+-+-+
21
    // variable length structures.
22
23
    template<typename W>
24
    class _glat_iterator
25
    {
26
0
        unsigned short  key() const             { return uint16(be::peek<W>(_e) + _n); }
Unexecuted instantiation: GlyphCache.cpp:(anonymous namespace)::_glat_iterator<unsigned char>::key() const
Unexecuted instantiation: GlyphCache.cpp:(anonymous namespace)::_glat_iterator<unsigned short>::key() const
27
0
        unsigned int    run() const             { return be::peek<W>(_e+sizeof(W)); }
Unexecuted instantiation: GlyphCache.cpp:(anonymous namespace)::_glat_iterator<unsigned char>::run() const
Unexecuted instantiation: GlyphCache.cpp:(anonymous namespace)::_glat_iterator<unsigned short>::run() const
28
0
        void            advance_entry()         { _n = 0; _e = _v; be::skip<W>(_v,2); }
Unexecuted instantiation: GlyphCache.cpp:(anonymous namespace)::_glat_iterator<unsigned char>::advance_entry()
Unexecuted instantiation: GlyphCache.cpp:(anonymous namespace)::_glat_iterator<unsigned short>::advance_entry()
29
    public:
30
        using iterator_category = std::input_iterator_tag;
31
        using value_type = std::pair<sparse::key_type, sparse::mapped_type>;
32
        using difference_type = ptrdiff_t;
33
        using pointer = value_type *;
34
        using reference = value_type &;
35
36
0
        _glat_iterator(const void * glat=0) : _e(reinterpret_cast<const byte *>(glat)), _v(_e+2*sizeof(W)), _n(0) {}
Unexecuted instantiation: GlyphCache.cpp:(anonymous namespace)::_glat_iterator<unsigned char>::_glat_iterator(void const*)
Unexecuted instantiation: GlyphCache.cpp:(anonymous namespace)::_glat_iterator<unsigned short>::_glat_iterator(void const*)
37
38
0
        _glat_iterator<W> & operator ++ () {
39
0
            ++_n; be::skip<uint16>(_v);
40
0
            if (_n == run()) advance_entry();
41
0
            return *this;
42
0
        }
Unexecuted instantiation: GlyphCache.cpp:(anonymous namespace)::_glat_iterator<unsigned char>::operator++()
Unexecuted instantiation: GlyphCache.cpp:(anonymous namespace)::_glat_iterator<unsigned short>::operator++()
43
        _glat_iterator<W>   operator ++ (int)   { _glat_iterator<W> tmp(*this); operator++(); return tmp; }
44
45
        // This is strictly a >= operator. A true == operator could be
46
        // implemented that test for overlap but it would be more expensive a
47
        // test.
48
0
        bool operator == (const _glat_iterator<W> & rhs) { return _v >= rhs._e - 1; }
Unexecuted instantiation: GlyphCache.cpp:(anonymous namespace)::_glat_iterator<unsigned char>::operator==((anonymous namespace)::_glat_iterator<unsigned char> const&)
Unexecuted instantiation: GlyphCache.cpp:(anonymous namespace)::_glat_iterator<unsigned short>::operator==((anonymous namespace)::_glat_iterator<unsigned short> const&)
49
0
        bool operator != (const _glat_iterator<W> & rhs) { return !operator==(rhs); }
Unexecuted instantiation: GlyphCache.cpp:(anonymous namespace)::_glat_iterator<unsigned char>::operator!=((anonymous namespace)::_glat_iterator<unsigned char> const&)
Unexecuted instantiation: GlyphCache.cpp:(anonymous namespace)::_glat_iterator<unsigned short>::operator!=((anonymous namespace)::_glat_iterator<unsigned short> const&)
50
51
0
        value_type          operator * () const {
52
0
            return value_type(key(), be::peek<uint16>(_v));
53
0
        }
Unexecuted instantiation: GlyphCache.cpp:(anonymous namespace)::_glat_iterator<unsigned char>::operator*() const
Unexecuted instantiation: GlyphCache.cpp:(anonymous namespace)::_glat_iterator<unsigned short>::operator*() const
54
55
    protected:
56
        const byte     * _e, * _v;
57
        size_t        _n;
58
    };
59
60
    typedef _glat_iterator<uint8>   glat_iterator;
61
    typedef _glat_iterator<uint16>  glat2_iterator;
62
}
63
64
const SlantBox SlantBox::empty = {0,0,0,0};
65
66
67
class GlyphCache::Loader
68
{
69
public:
70
    Loader(const Face & face);    //return result indicates success. Do not use if failed.
71
72
    operator bool () const throw();
73
    unsigned short int units_per_em() const throw();
74
    unsigned short int num_glyphs() const throw();
75
    unsigned short int num_attrs() const throw();
76
    bool has_boxes() const throw();
77
78
    const GlyphFace * read_glyph(unsigned short gid, GlyphFace &, int *numsubs) const throw();
79
    GlyphBox * read_box(uint16 gid, GlyphBox *curr, const GlyphFace & face) const throw();
80
81
    CLASS_NEW_DELETE;
82
private:
83
    Face::Table _head,
84
                _hhea,
85
                _hmtx,
86
                _glyf,
87
                _loca,
88
                m_pGlat,
89
                m_pGloc;
90
91
    bool            _long_fmt;
92
    bool            _has_boxes;
93
    unsigned short  _num_glyphs_graphics,        //i.e. boundary box and advance
94
                    _num_glyphs_attributes,
95
                    _num_attrs;                    // number of glyph attributes per glyph
96
};
97
98
99
100
GlyphCache::GlyphCache(const Face & face, const uint32 face_options)
101
0
: _glyph_loader(new Loader(face)),
102
0
  _glyphs(_glyph_loader && *_glyph_loader && _glyph_loader->num_glyphs()
103
0
        ? grzeroalloc<const GlyphFace *>(_glyph_loader->num_glyphs()) : 0),
104
0
  _boxes(_glyph_loader && _glyph_loader->has_boxes() && _glyph_loader->num_glyphs()
105
0
        ? grzeroalloc<GlyphBox *>(_glyph_loader->num_glyphs()) : 0),
106
0
  _num_glyphs(_glyphs ? _glyph_loader->num_glyphs() : 0),
107
0
  _num_attrs(_glyphs ? _glyph_loader->num_attrs() : 0),
108
0
  _upem(_glyphs ? _glyph_loader->units_per_em() : 0)
109
0
{
110
0
    if ((face_options & gr_face_preloadGlyphs) && _glyph_loader && _glyphs)
111
0
    {
112
0
        int numsubs = 0;
113
0
        GlyphFace * const glyphs = new GlyphFace [_num_glyphs];
114
0
        if (!glyphs)
115
0
            return;
116
117
        // The 0 glyph is definately required.
118
0
        _glyphs[0] = _glyph_loader->read_glyph(0, glyphs[0], &numsubs);
119
120
        // glyphs[0] has the same address as the glyphs array just allocated,
121
        //  thus assigning the &glyphs[0] to _glyphs[0] means _glyphs[0] points
122
        //  to the entire array.
123
0
        const GlyphFace * loaded = _glyphs[0];
124
0
        for (uint16 gid = 1; loaded && gid != _num_glyphs; ++gid)
125
0
            _glyphs[gid] = loaded = _glyph_loader->read_glyph(gid, glyphs[gid], &numsubs);
126
127
0
        if (!loaded)
128
0
        {
129
0
            _glyphs[0] = 0;
130
0
            delete [] glyphs;
131
0
        }
132
0
        else if (numsubs > 0 && _boxes)
133
0
        {
134
0
            GlyphBox * boxes = (GlyphBox *)gralloc<char>(_num_glyphs * sizeof(GlyphBox) + numsubs * 8 * sizeof(float));
135
0
            GlyphBox * currbox = boxes;
136
137
0
            for (uint16 gid = 0; currbox && gid != _num_glyphs; ++gid)
138
0
            {
139
0
                _boxes[gid] = currbox;
140
0
                currbox = _glyph_loader->read_box(gid, currbox, *_glyphs[gid]);
141
0
            }
142
0
            if (!currbox)
143
0
            {
144
0
                free(boxes);
145
0
                _boxes[0] = 0;
146
0
            }
147
0
        }
148
0
        delete _glyph_loader;
149
0
        _glyph_loader = 0;
150
  // coverity[leaked_storage : FALSE] - calling read_glyph on index 0 saved
151
  // glyphs as _glyphs[0]. Setting _glyph_loader to nullptr here flags that
152
  // the dtor needs to call delete[] on _glyphs[0] to release what was allocated
153
  // as glyphs
154
0
    }
155
156
0
    if (_glyphs && glyph(0) == 0)
157
0
    {
158
0
        free(_glyphs);
159
0
        _glyphs = 0;
160
0
        if (_boxes)
161
0
        {
162
0
            free(_boxes);
163
0
            _boxes = 0;
164
0
        }
165
0
        _num_glyphs = _num_attrs = _upem = 0;
166
0
    }
167
0
}
168
169
170
GlyphCache::~GlyphCache()
171
0
{
172
0
    if (_glyphs)
173
0
    {
174
0
        if (_glyph_loader)
175
0
        {
176
0
            const GlyphFace *  * g = _glyphs;
177
0
            for(unsigned short n = _num_glyphs; n; --n, ++g)
178
0
                delete *g;
179
0
        }
180
0
        else
181
0
            delete [] _glyphs[0];
182
0
        free(_glyphs);
183
0
    }
184
0
    if (_boxes)
185
0
    {
186
0
        if (_glyph_loader)
187
0
        {
188
0
            GlyphBox *  * g = _boxes;
189
0
            for (uint16 n = _num_glyphs; n; --n, ++g)
190
0
                free(*g);
191
0
        }
192
0
        else
193
0
            free(_boxes[0]);
194
0
        free(_boxes);
195
0
    }
196
0
    delete _glyph_loader;
197
0
}
198
199
const GlyphFace *GlyphCache::glyph(unsigned short glyphid) const      //result may be changed by subsequent call with a different glyphid
200
0
{
201
0
    if (glyphid >= numGlyphs())
202
0
        return _glyphs[0];
203
0
    const GlyphFace * & p = _glyphs[glyphid];
204
0
    if (p == 0 && _glyph_loader)
205
0
    {
206
0
        int numsubs = 0;
207
0
        GlyphFace * g = new GlyphFace();
208
0
        if (g)  p = _glyph_loader->read_glyph(glyphid, *g, &numsubs);
209
0
        if (!p)
210
0
        {
211
0
            delete g;
212
0
            return *_glyphs;
213
0
        }
214
0
        if (_boxes)
215
0
        {
216
0
            _boxes[glyphid] = (GlyphBox *)gralloc<char>(sizeof(GlyphBox) + 8 * numsubs * sizeof(float));
217
0
            if (!_glyph_loader->read_box(glyphid, _boxes[glyphid], *_glyphs[glyphid]))
218
0
            {
219
0
                free(_boxes[glyphid]);
220
0
                _boxes[glyphid] = 0;
221
0
            }
222
0
        }
223
0
    }
224
0
    return p;
225
0
}
226
227
228
229
GlyphCache::Loader::Loader(const Face & face)
230
0
: _head(face, Tag::head),
231
0
  _hhea(face, Tag::hhea),
232
0
  _hmtx(face, Tag::hmtx),
233
0
  _glyf(face, Tag::glyf),
234
0
  _loca(face, Tag::loca),
235
0
  _long_fmt(false),
236
0
  _has_boxes(false),
237
0
  _num_glyphs_graphics(0),
238
0
  _num_glyphs_attributes(0),
239
0
  _num_attrs(0)
240
0
{
241
0
    if (!operator bool())
242
0
        return;
243
244
0
    const Face::Table maxp = Face::Table(face, Tag::maxp);
245
0
    if (!maxp) { _head = Face::Table(); return; }
246
247
0
    _num_glyphs_graphics = static_cast<unsigned short>(TtfUtil::GlyphCount(maxp));
248
    // This will fail if the number of glyphs is wildly out of range.
249
0
    if (_glyf && TtfUtil::LocaLookup(_num_glyphs_graphics-1, _loca, _loca.size(), _head) == size_t(-2))
250
0
    {
251
0
        _head = Face::Table();
252
0
        return;
253
0
    }
254
255
0
    if ((m_pGlat = Face::Table(face, Tag::Glat, 0x00030000)) == NULL
256
0
        || (m_pGloc = Face::Table(face, Tag::Gloc)) == NULL
257
0
        || m_pGloc.size() < 8)
258
0
    {
259
0
        _head = Face::Table();
260
0
        return;
261
0
    }
262
0
    const byte    * p = m_pGloc;
263
0
    int       version = be::read<uint32>(p);
264
0
    const uint16    flags = be::read<uint16>(p);
265
0
    _num_attrs = be::read<uint16>(p);
266
    // We can accurately calculate the number of attributed glyphs by
267
    //  subtracting the length of the attribids array (numAttribs long if present)
268
    //  and dividing by either 2 or 4 depending on shor or lonf format
269
0
    _long_fmt              = flags & 1;
270
0
    ptrdiff_t tmpnumgattrs       = (m_pGloc.size()
271
0
                               - (p - m_pGloc)
272
0
                               - sizeof(uint16)*(flags & 0x2 ? _num_attrs : 0))
273
0
                                   / (_long_fmt ? sizeof(uint32) : sizeof(uint16)) - 1;
274
275
0
    if (version >= 0x00020000 || tmpnumgattrs < 0 || tmpnumgattrs > 65535
276
0
        || _num_attrs == 0 || _num_attrs > 0x3000  // is this hard limit appropriate?
277
0
        || _num_glyphs_graphics > tmpnumgattrs
278
0
        || m_pGlat.size() < 4)
279
0
    {
280
0
        _head = Face::Table();
281
0
        return;
282
0
    }
283
284
0
    _num_glyphs_attributes = static_cast<unsigned short>(tmpnumgattrs);
285
0
    p = m_pGlat;
286
0
    version = be::read<uint32>(p);
287
0
    if (version >= 0x00040000 || (version >= 0x00030000 && m_pGlat.size() < 8))       // reject Glat tables that are too new
288
0
    {
289
0
        _head = Face::Table();
290
0
        return;
291
0
    }
292
0
    else if (version >= 0x00030000)
293
0
    {
294
0
        unsigned int glatflags = be::read<uint32>(p);
295
0
        _has_boxes = glatflags & 1;
296
        // delete this once the compiler is fixed
297
0
        _has_boxes = true;
298
0
    }
299
0
}
300
301
inline
302
GlyphCache::Loader::operator bool () const throw()
303
0
{
304
0
    return _head && _hhea && _hmtx && !(bool(_glyf) != bool(_loca));
305
0
}
306
307
inline
308
unsigned short int GlyphCache::Loader::units_per_em() const throw()
309
0
{
310
0
    return _head ? TtfUtil::DesignUnits(_head) : 0;
311
0
}
312
313
inline
314
unsigned short int GlyphCache::Loader::num_glyphs() const throw()
315
0
{
316
0
    return max(_num_glyphs_graphics, _num_glyphs_attributes);
317
0
}
318
319
inline
320
unsigned short int GlyphCache::Loader::num_attrs() const throw()
321
0
{
322
0
    return _num_attrs;
323
0
}
324
325
inline
326
bool GlyphCache::Loader::has_boxes () const throw()
327
0
{
328
0
    return _has_boxes;
329
0
}
330
331
const GlyphFace * GlyphCache::Loader::read_glyph(unsigned short glyphid, GlyphFace & glyph, int *numsubs) const throw()
332
0
{
333
0
    Rect        bbox;
334
0
    Position    advance;
335
336
0
    if (glyphid < _num_glyphs_graphics)
337
0
    {
338
0
        int nLsb;
339
0
        unsigned int nAdvWid;
340
0
        if (_glyf)
341
0
        {
342
0
            int xMin, yMin, xMax, yMax;
343
0
            size_t locidx = TtfUtil::LocaLookup(glyphid, _loca, _loca.size(), _head);
344
0
            void *pGlyph = TtfUtil::GlyfLookup(_glyf, locidx, _glyf.size());
345
346
0
            if (pGlyph && TtfUtil::GlyfBox(pGlyph, xMin, yMin, xMax, yMax))
347
0
            {
348
0
                if ((xMin > xMax) || (yMin > yMax))
349
0
                    return 0;
350
0
                bbox = Rect(Position(static_cast<float>(xMin), static_cast<float>(yMin)),
351
0
                    Position(static_cast<float>(xMax), static_cast<float>(yMax)));
352
0
            }
353
0
        }
354
0
        if (TtfUtil::HorMetrics(glyphid, _hmtx, _hmtx.size(), _hhea, nLsb, nAdvWid))
355
0
            advance = Position(static_cast<float>(nAdvWid), 0);
356
0
    }
357
358
0
    if (glyphid < _num_glyphs_attributes)
359
0
    {
360
0
        const byte * gloc = m_pGloc;
361
0
        size_t      glocs = 0, gloce = 0;
362
363
0
        be::skip<uint32>(gloc);
364
0
        be::skip<uint16>(gloc,2);
365
0
        if (_long_fmt)
366
0
        {
367
0
            if (8 + glyphid * sizeof(uint32) > m_pGloc.size())
368
0
                return 0;
369
0
            be::skip<uint32>(gloc, glyphid);
370
0
            glocs = be::read<uint32>(gloc);
371
0
            gloce = be::peek<uint32>(gloc);
372
0
        }
373
0
        else
374
0
        {
375
0
            if (8 + glyphid * sizeof(uint16) > m_pGloc.size())
376
0
                return 0;
377
0
            be::skip<uint16>(gloc, glyphid);
378
0
            glocs = be::read<uint16>(gloc);
379
0
            gloce = be::peek<uint16>(gloc);
380
0
        }
381
382
0
        if (glocs >= m_pGlat.size() - 1 || gloce > m_pGlat.size())
383
0
            return 0;
384
385
0
        const uint32 glat_version = be::peek<uint32>(m_pGlat);
386
0
        if (glat_version >= 0x00030000)
387
0
        {
388
0
            if (glocs >= gloce)
389
0
                return 0;
390
0
            const byte * p = m_pGlat + glocs;
391
0
            uint16 bmap = be::read<uint16>(p);
392
0
            int num = bit_set_count((uint32)bmap);
393
0
            if (numsubs) *numsubs += num;
394
0
            glocs += 6 + 8 * num;
395
0
            if (glocs > gloce)
396
0
                return 0;
397
0
        }
398
0
        if (glat_version < 0x00020000)
399
0
        {
400
0
            if (gloce - glocs < 2*sizeof(byte)+sizeof(uint16)
401
0
                || gloce - glocs > _num_attrs*(2*sizeof(byte)+sizeof(uint16)))
402
0
                    return 0;
403
0
            new (&glyph) GlyphFace(bbox, advance, glat_iterator(m_pGlat + glocs), glat_iterator(m_pGlat + gloce));
404
0
        }
405
0
        else
406
0
        {
407
0
            if (gloce - glocs < 3*sizeof(uint16)        // can a glyph have no attributes? why not?
408
0
                || gloce - glocs > _num_attrs*3*sizeof(uint16)
409
0
                || glocs > m_pGlat.size() - 2*sizeof(uint16))
410
0
                    return 0;
411
0
            new (&glyph) GlyphFace(bbox, advance, glat2_iterator(m_pGlat + glocs), glat2_iterator(m_pGlat + gloce));
412
0
        }
413
0
        if (!glyph.attrs() || glyph.attrs().capacity() > _num_attrs)
414
0
            return 0;
415
0
    }
416
0
    return &glyph;
417
0
}
418
419
inline float scale_to(uint8 t, float zmin, float zmax)
420
0
{
421
0
    return (zmin + t * (zmax - zmin) / 255);
422
0
}
423
424
Rect readbox(Rect &b, uint8 zxmin, uint8 zymin, uint8 zxmax, uint8 zymax)
425
0
{
426
0
    return Rect(Position(scale_to(zxmin, b.bl.x, b.tr.x), scale_to(zymin, b.bl.y, b.tr.y)),
427
0
                Position(scale_to(zxmax, b.bl.x, b.tr.x), scale_to(zymax, b.bl.y, b.tr.y)));
428
0
}
429
430
GlyphBox * GlyphCache::Loader::read_box(uint16 gid, GlyphBox *curr, const GlyphFace & glyph) const throw()
431
0
{
432
0
    if (gid >= _num_glyphs_attributes) return 0;
433
434
0
    const byte * gloc = m_pGloc;
435
0
    size_t      glocs = 0, gloce = 0;
436
437
0
    be::skip<uint32>(gloc);
438
0
    be::skip<uint16>(gloc,2);
439
0
    if (_long_fmt)
440
0
    {
441
0
        be::skip<uint32>(gloc, gid);
442
0
        glocs = be::read<uint32>(gloc);
443
0
        gloce = be::peek<uint32>(gloc);
444
0
    }
445
0
    else
446
0
    {
447
0
        be::skip<uint16>(gloc, gid);
448
0
        glocs = be::read<uint16>(gloc);
449
0
        gloce = be::peek<uint16>(gloc);
450
0
    }
451
452
0
    if (gloce > m_pGlat.size() || glocs + 6 >= gloce)
453
0
        return 0;
454
455
0
    const byte * p = m_pGlat + glocs;
456
0
    uint16 bmap = be::read<uint16>(p);
457
0
    int num = bit_set_count((uint32)bmap);
458
459
0
    Rect bbox = glyph.theBBox();
460
0
    Rect diamax(Position(bbox.bl.x + bbox.bl.y, bbox.bl.x - bbox.tr.y),
461
0
                Position(bbox.tr.x + bbox.tr.y, bbox.tr.x - bbox.bl.y));
462
0
    Rect diabound = readbox(diamax, p[0], p[2], p[1], p[3]);
463
0
    ::new (curr) GlyphBox(num, bmap, &diabound);
464
0
    be::skip<uint8>(p, 4);
465
0
    if (glocs + 6 + num * 8 >= gloce)
466
0
        return 0;
467
468
0
    for (int i = 0; i < num * 2; ++i)
469
0
    {
470
0
        Rect box = readbox((i & 1) ? diamax : bbox, p[0], p[2], p[1], p[3]);
471
0
        curr->addSubBox(i >> 1, i & 1, &box);
472
0
        be::skip<uint8>(p, 4);
473
0
    }
474
0
    return (GlyphBox *)((char *)(curr) + sizeof(GlyphBox) + 2 * num * sizeof(Rect));
475
0
}