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
13using namespace graphite2;
14
15namespace
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 unsigned short key() const { return uint16(be::peek<W>(_e) + _n); }
27 unsigned int run() const { return be::peek<W>(_e+sizeof(W)); }
28 void advance_entry() { _n = 0; _e = _v; be::skip<W>(_v,2); }
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 _glat_iterator(const void * glat=0) : _e(reinterpret_cast<const byte *>(glat)), _v(_e+2*sizeof(W)), _n(0) {}
37
38 _glat_iterator<W> & operator ++ () {
39 ++_n; be::skip<uint16>(_v);
40 if (_n == run()) advance_entry();
41 return *this;
42 }
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 bool operator == (const _glat_iterator<W> & rhs) { return _v >= rhs._e - 1; }
49 bool operator != (const _glat_iterator<W> & rhs) { return !operator==(rhs); }
50
51 value_type operator * () const {
52 return value_type(key(), be::peek<uint16>(_v));
53 }
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
64const SlantBox SlantBox::empty = {0,0,0,0};
65
66
67class GlyphCache::Loader
68{
69public:
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;
82private:
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
100GlyphCache::GlyphCache(const Face & face, const uint32 face_options)
101: _glyph_loader(new Loader(face)),
102 _glyphs(_glyph_loader && *_glyph_loader && _glyph_loader->num_glyphs()
103 ? grzeroalloc<const GlyphFace *>(_glyph_loader->num_glyphs()) : 0),
104 _boxes(_glyph_loader && _glyph_loader->has_boxes() && _glyph_loader->num_glyphs()
105 ? grzeroalloc<GlyphBox *>(_glyph_loader->num_glyphs()) : 0),
106 _num_glyphs(_glyphs ? _glyph_loader->num_glyphs() : 0),
107 _num_attrs(_glyphs ? _glyph_loader->num_attrs() : 0),
108 _upem(_glyphs ? _glyph_loader->units_per_em() : 0)
109{
110 if ((face_options & gr_face_preloadGlyphs) && _glyph_loader && _glyphs)
111 {
112 int numsubs = 0;
113 GlyphFace * const glyphs = new GlyphFace [_num_glyphs];
114 if (!glyphs)
115 return;
116
117 // The 0 glyph is definately required.
118 _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 const GlyphFace * loaded = _glyphs[0];
124 for (uint16 gid = 1; loaded && gid != _num_glyphs; ++gid)
125 _glyphs[gid] = loaded = _glyph_loader->read_glyph(gid, glyphs[gid], &numsubs);
126
127 if (!loaded)
128 {
129 _glyphs[0] = 0;
130 delete [] glyphs;
131 }
132 else if (numsubs > 0 && _boxes)
133 {
134 GlyphBox * boxes = (GlyphBox *)gralloc<char>(_num_glyphs * sizeof(GlyphBox) + numsubs * 8 * sizeof(float));
135 GlyphBox * currbox = boxes;
136
137 for (uint16 gid = 0; currbox && gid != _num_glyphs; ++gid)
138 {
139 _boxes[gid] = currbox;
140 currbox = _glyph_loader->read_box(gid, currbox, *_glyphs[gid]);
141 }
142 if (!currbox)
143 {
144 free(boxes);
145 _boxes[0] = 0;
146 }
147 }
148 delete _glyph_loader;
149 _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 }
155
156 if (_glyphs && glyph(0) == 0)
157 {
158 free(_glyphs);
159 _glyphs = 0;
160 if (_boxes)
161 {
162 free(_boxes);
163 _boxes = 0;
164 }
165 _num_glyphs = _num_attrs = _upem = 0;
166 }
167}
168
169
170GlyphCache::~GlyphCache()
171{
172 if (_glyphs)
173 {
174 if (_glyph_loader)
175 {
176 const GlyphFace * * g = _glyphs;
177 for(unsigned short n = _num_glyphs; n; --n, ++g)
178 delete *g;
179 }
180 else
181 delete [] _glyphs[0];
182 free(_glyphs);
183 }
184 if (_boxes)
185 {
186 if (_glyph_loader)
187 {
188 GlyphBox * * g = _boxes;
189 for (uint16 n = _num_glyphs; n; --n, ++g)
190 free(*g);
191 }
192 else
193 free(_boxes[0]);
194 free(_boxes);
195 }
196 delete _glyph_loader;
197}
198
199const GlyphFace *GlyphCache::glyph(unsigned short glyphid) const //result may be changed by subsequent call with a different glyphid
200{
201 if (glyphid >= numGlyphs())
202 return _glyphs[0];
203 const GlyphFace * & p = _glyphs[glyphid];
204 if (p == 0 && _glyph_loader)
205 {
206 int numsubs = 0;
207 GlyphFace * g = new GlyphFace();
208 if (g) p = _glyph_loader->read_glyph(glyphid, *g, &numsubs);
209 if (!p)
210 {
211 delete g;
212 return *_glyphs;
213 }
214 if (_boxes)
215 {
216 _boxes[glyphid] = (GlyphBox *)gralloc<char>(sizeof(GlyphBox) + 8 * numsubs * sizeof(float));
217 if (!_glyph_loader->read_box(glyphid, _boxes[glyphid], *_glyphs[glyphid]))
218 {
219 free(_boxes[glyphid]);
220 _boxes[glyphid] = 0;
221 }
222 }
223 }
224 return p;
225}
226
227
228
229GlyphCache::Loader::Loader(const Face & face)
230: _head(face, Tag::head),
231 _hhea(face, Tag::hhea),
232 _hmtx(face, Tag::hmtx),
233 _glyf(face, Tag::glyf),
234 _loca(face, Tag::loca),
235 _long_fmt(false),
236 _has_boxes(false),
237 _num_glyphs_graphics(0),
238 _num_glyphs_attributes(0),
239 _num_attrs(0)
240{
241 if (!operator bool())
242 return;
243
244 const Face::Table maxp = Face::Table(face, Tag::maxp);
245 if (!maxp) { _head = Face::Table(); return; }
246
247 _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 if (_glyf && TtfUtil::LocaLookup(_num_glyphs_graphics-1, _loca, _loca.size(), _head) == size_t(-2))
250 {
251 _head = Face::Table();
252 return;
253 }
254
255 if ((m_pGlat = Face::Table(face, Tag::Glat, 0x00030000)) == NULL
256 || (m_pGloc = Face::Table(face, Tag::Gloc)) == NULL
257 || m_pGloc.size() < 8)
258 {
259 _head = Face::Table();
260 return;
261 }
262 const byte * p = m_pGloc;
263 int version = be::read<uint32>(p);
264 const uint16 flags = be::read<uint16>(p);
265 _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 _long_fmt = flags & 1;
270 ptrdiff_t tmpnumgattrs = (m_pGloc.size()
271 - (p - m_pGloc)
272 - sizeof(uint16)*(flags & 0x2 ? _num_attrs : 0))
273 / (_long_fmt ? sizeof(uint32) : sizeof(uint16)) - 1;
274
275 if (version >= 0x00020000 || tmpnumgattrs < 0 || tmpnumgattrs > 65535
276 || _num_attrs == 0 || _num_attrs > 0x3000 // is this hard limit appropriate?
277 || _num_glyphs_graphics > tmpnumgattrs
278 || m_pGlat.size() < 4)
279 {
280 _head = Face::Table();
281 return;
282 }
283
284 _num_glyphs_attributes = static_cast<unsigned short>(tmpnumgattrs);
285 p = m_pGlat;
286 version = be::read<uint32>(p);
287 if (version >= 0x00040000 || (version >= 0x00030000 && m_pGlat.size() < 8)) // reject Glat tables that are too new
288 {
289 _head = Face::Table();
290 return;
291 }
292 else if (version >= 0x00030000)
293 {
294 unsigned int glatflags = be::read<uint32>(p);
295 _has_boxes = glatflags & 1;
296 // delete this once the compiler is fixed
297 _has_boxes = true;
298 }
299}
300
301inline
302GlyphCache::Loader::operator bool () const throw()
303{
304 return _head && _hhea && _hmtx && !(bool(_glyf) != bool(_loca));
305}
306
307inline
308unsigned short int GlyphCache::Loader::units_per_em() const throw()
309{
310 return _head ? TtfUtil::DesignUnits(_head) : 0;
311}
312
313inline
314unsigned short int GlyphCache::Loader::num_glyphs() const throw()
315{
316 return max(_num_glyphs_graphics, _num_glyphs_attributes);
317}
318
319inline
320unsigned short int GlyphCache::Loader::num_attrs() const throw()
321{
322 return _num_attrs;
323}
324
325inline
326bool GlyphCache::Loader::has_boxes () const throw()
327{
328 return _has_boxes;
329}
330
331const GlyphFace * GlyphCache::Loader::read_glyph(unsigned short glyphid, GlyphFace & glyph, int *numsubs) const throw()
332{
333 Rect bbox;
334 Position advance;
335
336 if (glyphid < _num_glyphs_graphics)
337 {
338 int nLsb;
339 unsigned int nAdvWid;
340 if (_glyf)
341 {
342 int xMin, yMin, xMax, yMax;
343 size_t locidx = TtfUtil::LocaLookup(glyphid, _loca, _loca.size(), _head);
344 void *pGlyph = TtfUtil::GlyfLookup(_glyf, locidx, _glyf.size());
345
346 if (pGlyph && TtfUtil::GlyfBox(pGlyph, xMin, yMin, xMax, yMax))
347 {
348 if ((xMin > xMax) || (yMin > yMax))
349 return 0;
350 bbox = Rect(Position(static_cast<float>(xMin), static_cast<float>(yMin)),
351 Position(static_cast<float>(xMax), static_cast<float>(yMax)));
352 }
353 }
354 if (TtfUtil::HorMetrics(glyphid, _hmtx, _hmtx.size(), _hhea, nLsb, nAdvWid))
355 advance = Position(static_cast<float>(nAdvWid), 0);
356 }
357
358 if (glyphid < _num_glyphs_attributes)
359 {
360 const byte * gloc = m_pGloc;
361 size_t glocs = 0, gloce = 0;
362
363 be::skip<uint32>(gloc);
364 be::skip<uint16>(gloc,2);
365 if (_long_fmt)
366 {
367 if (8 + glyphid * sizeof(uint32) > m_pGloc.size())
368 return 0;
369 be::skip<uint32>(gloc, glyphid);
370 glocs = be::read<uint32>(gloc);
371 gloce = be::peek<uint32>(gloc);
372 }
373 else
374 {
375 if (8 + glyphid * sizeof(uint16) > m_pGloc.size())
376 return 0;
377 be::skip<uint16>(gloc, glyphid);
378 glocs = be::read<uint16>(gloc);
379 gloce = be::peek<uint16>(gloc);
380 }
381
382 if (glocs >= m_pGlat.size() - 1 || gloce > m_pGlat.size())
383 return 0;
384
385 const uint32 glat_version = be::peek<uint32>(m_pGlat);
386 if (glat_version >= 0x00030000)
387 {
388 if (glocs >= gloce)
389 return 0;
390 const byte * p = m_pGlat + glocs;
391 uint16 bmap = be::read<uint16>(p);
392 int num = bit_set_count((uint32)bmap);
393 if (numsubs) *numsubs += num;
394 glocs += 6 + 8 * num;
395 if (glocs > gloce)
396 return 0;
397 }
398 if (glat_version < 0x00020000)
399 {
400 if (gloce - glocs < 2*sizeof(byte)+sizeof(uint16)
401 || gloce - glocs > _num_attrs*(2*sizeof(byte)+sizeof(uint16)))
402 return 0;
403 new (&glyph) GlyphFace(bbox, advance, glat_iterator(m_pGlat + glocs), glat_iterator(m_pGlat + gloce));
404 }
405 else
406 {
407 if (gloce - glocs < 3*sizeof(uint16) // can a glyph have no attributes? why not?
408 || gloce - glocs > _num_attrs*3*sizeof(uint16)
409 || glocs > m_pGlat.size() - 2*sizeof(uint16))
410 return 0;
411 new (&glyph) GlyphFace(bbox, advance, glat2_iterator(m_pGlat + glocs), glat2_iterator(m_pGlat + gloce));
412 }
413 if (!glyph.attrs() || glyph.attrs().capacity() > _num_attrs)
414 return 0;
415 }
416 return &glyph;
417}
418
419inline float scale_to(uint8 t, float zmin, float zmax)
420{
421 return (zmin + t * (zmax - zmin) / 255);
422}
423
424Rect readbox(Rect &b, uint8 zxmin, uint8 zymin, uint8 zxmax, uint8 zymax)
425{
426 return Rect(Position(scale_to(zxmin, b.bl.x, b.tr.x), scale_to(zymin, b.bl.y, b.tr.y)),
427 Position(scale_to(zxmax, b.bl.x, b.tr.x), scale_to(zymax, b.bl.y, b.tr.y)));
428}
429
430GlyphBox * GlyphCache::Loader::read_box(uint16 gid, GlyphBox *curr, const GlyphFace & glyph) const throw()
431{
432 if (gid >= _num_glyphs_attributes) return 0;
433
434 const byte * gloc = m_pGloc;
435 size_t glocs = 0, gloce = 0;
436
437 be::skip<uint32>(gloc);
438 be::skip<uint16>(gloc,2);
439 if (_long_fmt)
440 {
441 be::skip<uint32>(gloc, gid);
442 glocs = be::read<uint32>(gloc);
443 gloce = be::peek<uint32>(gloc);
444 }
445 else
446 {
447 be::skip<uint16>(gloc, gid);
448 glocs = be::read<uint16>(gloc);
449 gloce = be::peek<uint16>(gloc);
450 }
451
452 if (gloce > m_pGlat.size() || glocs + 6 >= gloce)
453 return 0;
454
455 const byte * p = m_pGlat + glocs;
456 uint16 bmap = be::read<uint16>(p);
457 int num = bit_set_count((uint32)bmap);
458
459 Rect bbox = glyph.theBBox();
460 Rect diamax(Position(bbox.bl.x + bbox.bl.y, bbox.bl.x - bbox.tr.y),
461 Position(bbox.tr.x + bbox.tr.y, bbox.tr.x - bbox.bl.y));
462 Rect diabound = readbox(diamax, p[0], p[2], p[1], p[3]);
463 ::new (curr) GlyphBox(num, bmap, &diabound);
464 be::skip<uint8>(p, 4);
465 if (glocs + 6 + num * 8 >= gloce)
466 return 0;
467
468 for (int i = 0; i < num * 2; ++i)
469 {
470 Rect box = readbox((i & 1) ? diamax : bbox, p[0], p[2], p[1], p[3]);
471 curr->addSubBox(i >> 1, i & 1, &box);
472 be::skip<uint8>(p, 4);
473 }
474 return (GlyphBox *)((char *)(curr) + sizeof(GlyphBox) + 2 * num * sizeof(Rect));
475}
476