|
1 /* GRAPHITE2 LICENSING |
|
2 |
|
3 Copyright 2010, SIL International |
|
4 All rights reserved. |
|
5 |
|
6 This library is free software; you can redistribute it and/or modify |
|
7 it under the terms of the GNU Lesser General Public License as published |
|
8 by the Free Software Foundation; either version 2.1 of License, or |
|
9 (at your option) any later version. |
|
10 |
|
11 This program is distributed in the hope that it will be useful, |
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 Lesser General Public License for more details. |
|
15 |
|
16 You should also have received a copy of the GNU Lesser General Public |
|
17 License along with this library in the file named "LICENSE". |
|
18 If not, write to the Free Software Foundation, 51 Franklin Street, |
|
19 Suite 500, Boston, MA 02110-1335, USA or visit their web page on the |
|
20 internet at http://www.fsf.org/licenses/lgpl.html. |
|
21 |
|
22 Alternatively, the contents of this file may be used under the terms of the |
|
23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public |
|
24 License, as published by the Free Software Foundation, either version 2 |
|
25 of the License or (at your option) any later version. |
|
26 */ |
|
27 #include "inc/GlyphFaceCache.h" |
|
28 #include "graphite2/Font.h" |
|
29 #include "inc/Face.h" //for the tags |
|
30 #include "inc/Endian.h" |
|
31 |
|
32 using namespace graphite2; |
|
33 |
|
34 /*virtual*/ bool GlyphFaceCacheHeader::initialize(const Face & face, const bool dumb_font) //return result indicates success. Do not use if failed. |
|
35 { |
|
36 if ((m_pLoca = face.getTable(Tag::loca, &m_lLoca)) == NULL) return false; |
|
37 if ((m_pHead = face.getTable(Tag::head)) == NULL) return false; |
|
38 if ((m_pGlyf = face.getTable(Tag::glyf, &m_lGlyf)) == NULL) return false; |
|
39 if ((m_pHmtx = face.getTable(Tag::hmtx, &m_lHmtx)) == NULL) return false; |
|
40 if ((m_pHHea = face.getTable(Tag::hhea)) == NULL) return false; |
|
41 |
|
42 const void* pMaxp = face.getTable(Tag::maxp); |
|
43 if (pMaxp == NULL) return false; |
|
44 m_nGlyphs = m_nGlyphsWithGraphics = (unsigned short)TtfUtil::GlyphCount(pMaxp); |
|
45 if (TtfUtil::LocaLookup(m_nGlyphs-1, m_pLoca, m_lLoca, m_pHead) == size_t(-1)) |
|
46 return false; // This will fail if m_nGlyphs is wildly out of range. |
|
47 |
|
48 if (!dumb_font) |
|
49 { |
|
50 if ((m_pGlat = face.getTable(Tag::Glat, &m_lGlat)) == NULL) return false; |
|
51 m_fGlat = be::peek<uint32>(m_pGlat); |
|
52 size_t lGloc; |
|
53 if ((m_pGloc = face.getTable(Tag::Gloc, &lGloc)) == NULL) return false; |
|
54 if (lGloc < 6) return false; |
|
55 int version = be::read<uint32>(m_pGloc); |
|
56 if (version != 0x00010000) return false; |
|
57 |
|
58 const uint16 locFlags = be::read<uint16>(m_pGloc); |
|
59 m_numAttrs = be::read<uint16>(m_pGloc); |
|
60 if (m_numAttrs > 0x1000) return false; // is this hard limit appropriate? |
|
61 |
|
62 if (locFlags & 1) |
|
63 { |
|
64 m_locFlagsUse32Bit = true; |
|
65 m_nGlyphsWithAttributes = (unsigned short)((lGloc - 12) / 4); |
|
66 } |
|
67 else |
|
68 { |
|
69 m_locFlagsUse32Bit = false; |
|
70 m_nGlyphsWithAttributes = (unsigned short)((lGloc - 10) / 2); |
|
71 } |
|
72 |
|
73 if (m_nGlyphsWithAttributes > m_nGlyphs) |
|
74 m_nGlyphs = m_nGlyphsWithAttributes; |
|
75 } |
|
76 |
|
77 return true; |
|
78 } |
|
79 |
|
80 GlyphFaceCache* GlyphFaceCache::makeCache(const GlyphFaceCacheHeader& hdr) |
|
81 { |
|
82 return new (hdr) GlyphFaceCache(hdr); |
|
83 } |
|
84 |
|
85 GlyphFaceCache::GlyphFaceCache(const GlyphFaceCacheHeader& hdr) |
|
86 : GlyphFaceCacheHeader(hdr) |
|
87 { |
|
88 unsigned int nGlyphs = numGlyphs(); |
|
89 |
|
90 for (unsigned int i = 0; i < nGlyphs; i++) |
|
91 { |
|
92 *glyphPtrDirect(i) = NULL; |
|
93 } |
|
94 } |
|
95 |
|
96 GlyphFaceCache::~GlyphFaceCache() |
|
97 { |
|
98 unsigned int nGlyphs = numGlyphs(); |
|
99 int deltaPointers = (*glyphPtrDirect(nGlyphs-1u) - *glyphPtrDirect(0u)); |
|
100 if ((nGlyphs > 0u) && (deltaPointers == static_cast<int>(nGlyphs - 1))) |
|
101 { |
|
102 for (unsigned int i=0 ; i<nGlyphs; ++i) |
|
103 { |
|
104 GlyphFace *p = *glyphPtrDirect(i); |
|
105 assert (p); |
|
106 p->~GlyphFace(); |
|
107 } |
|
108 free (*glyphPtrDirect(0)); |
|
109 } |
|
110 else |
|
111 { |
|
112 for (unsigned int i=0 ; i<nGlyphs; ++i) |
|
113 { |
|
114 GlyphFace *p = *glyphPtrDirect(i); |
|
115 if (p) |
|
116 { |
|
117 p->~GlyphFace(); |
|
118 free(p); |
|
119 } |
|
120 } |
|
121 } |
|
122 } |
|
123 |
|
124 void GlyphFaceCache::loadAllGlyphs() |
|
125 { |
|
126 unsigned int nGlyphs = numGlyphs(); |
|
127 // size_t sparse_size = 0; |
|
128 GlyphFace * glyphs = gralloc<GlyphFace>(nGlyphs); |
|
129 for (unsigned short glyphid = 0; glyphid < nGlyphs; glyphid++) |
|
130 { |
|
131 GlyphFace **p = glyphPtrDirect(glyphid); |
|
132 *p = &(glyphs[glyphid]); |
|
133 new(*p) GlyphFace(*this, glyphid); |
|
134 // sparse_size += (*p)->m_attrs._sizeof(); |
|
135 } |
|
136 // const size_t flat_size = nGlyphs*(sizeof(uint16*) + sizeof(uint16)*numAttrs()); |
|
137 // assert(sparse_size <= flat_size); |
|
138 } |
|
139 |
|
140 /*virtual*/ const GlyphFace *GlyphFaceCache::glyph(unsigned short glyphid) const //result may be changed by subsequent call with a different glyphid |
|
141 { |
|
142 GlyphFace **p = glyphPtrDirect(glyphid); |
|
143 if (*p) |
|
144 return *p; |
|
145 |
|
146 *p = (GlyphFace*)malloc(sizeof(GlyphFace)); |
|
147 new(*p) GlyphFace(*this, glyphid); |
|
148 return *p; |
|
149 } |