Bug 1857841 - pt 3. Add a new page kind named "fresh" r=glandium
[gecko.git] / gfx / graphite2 / src / CmapCache.cpp
blobd070019a3405c37db3428b7f5af4f35d5b31aead
1 /* GRAPHITE2 LICENSING
3 Copyright 2010, SIL International
4 All rights reserved.
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.
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.
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.
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.
28 #include "inc/Main.h"
29 #include "inc/CmapCache.h"
30 #include "inc/Face.h"
31 #include "inc/TtfTypes.h"
32 #include "inc/TtfUtil.h"
35 using namespace graphite2;
37 const void * bmp_subtable(const Face::Table & cmap)
39 const void * stbl;
40 if (!cmap.size()) return 0;
41 if (TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 3, 1, cmap.size()), cmap + cmap.size())
42 || TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 3, cmap.size()), cmap + cmap.size())
43 || TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 2, cmap.size()), cmap + cmap.size())
44 || TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 1, cmap.size()), cmap + cmap.size())
45 || TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 0, cmap.size()), cmap + cmap.size()))
46 return stbl;
47 return 0;
50 const void * smp_subtable(const Face::Table & cmap)
52 const void * stbl;
53 if (!cmap.size()) return 0;
54 if (TtfUtil::CheckCmapSubtable12(stbl = TtfUtil::FindCmapSubtable(cmap, 3, 10, cmap.size()), cmap + cmap.size())
55 || TtfUtil::CheckCmapSubtable12(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 4, cmap.size()), cmap + cmap.size()))
56 return stbl;
57 return 0;
60 template <unsigned int (*NextCodePoint)(const void *, unsigned int, int *),
61 uint16 (*LookupCodePoint)(const void *, unsigned int, int)>
62 bool cache_subtable(uint16 * blocks[], const void * cst, const unsigned int limit)
64 int rangeKey = 0;
65 uint32 codePoint = NextCodePoint(cst, 0, &rangeKey),
66 prevCodePoint = 0;
67 while (codePoint < limit)
69 unsigned int block = codePoint >> 8;
70 if (!blocks[block])
72 blocks[block] = grzeroalloc<uint16>(0x100);
73 if (!blocks[block])
74 return false;
76 blocks[block][codePoint & 0xFF] = LookupCodePoint(cst, codePoint, rangeKey);
77 // prevent infinite loop
78 if (codePoint <= prevCodePoint)
79 codePoint = prevCodePoint + 1;
80 prevCodePoint = codePoint;
81 codePoint = NextCodePoint(cst, codePoint, &rangeKey);
83 return true;
87 CachedCmap::CachedCmap(const Face & face)
88 : m_isBmpOnly(true),
89 m_blocks(0)
91 const Face::Table cmap(face, Tag::cmap);
92 if (!cmap) return;
94 const void * bmp_cmap = bmp_subtable(cmap);
95 const void * smp_cmap = smp_subtable(cmap);
96 m_isBmpOnly = !smp_cmap;
98 m_blocks = grzeroalloc<uint16 *>(m_isBmpOnly ? 0x100 : 0x1100);
99 if (m_blocks && smp_cmap)
101 if (!cache_subtable<TtfUtil::CmapSubtable12NextCodepoint, TtfUtil::CmapSubtable12Lookup>(m_blocks, smp_cmap, 0x10FFFF))
102 return;
105 if (m_blocks && bmp_cmap)
107 if (!cache_subtable<TtfUtil::CmapSubtable4NextCodepoint, TtfUtil::CmapSubtable4Lookup>(m_blocks, bmp_cmap, 0xFFFF))
108 return;
112 CachedCmap::~CachedCmap() throw()
114 if (!m_blocks) return;
115 unsigned int numBlocks = (m_isBmpOnly)? 0x100 : 0x1100;
116 for (unsigned int i = 0; i < numBlocks; i++)
117 free(m_blocks[i]);
118 free(m_blocks);
121 uint16 CachedCmap::operator [] (const uint32 usv) const throw()
123 if ((m_isBmpOnly && usv > 0xFFFF) || (usv > 0x10FFFF))
124 return 0;
125 const uint32 block = 0xFFFF & (usv >> 8);
126 if (m_blocks[block])
127 return m_blocks[block][usv & 0xFF];
128 return 0;
131 CachedCmap::operator bool() const throw()
133 return m_blocks != 0;
137 DirectCmap::DirectCmap(const Face & face)
138 : _cmap(face, Tag::cmap),
139 _smp(smp_subtable(_cmap)),
140 _bmp(bmp_subtable(_cmap))
144 uint16 DirectCmap::operator [] (const uint32 usv) const throw()
146 return usv > 0xFFFF
147 ? (_smp ? TtfUtil::CmapSubtable12Lookup(_smp, usv, 0) : 0)
148 : TtfUtil::CmapSubtable4Lookup(_bmp, usv, 0);
151 DirectCmap::operator bool () const throw()
153 return _cmap && _bmp;