3 Copyright 2010, SIL International
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.
29 #include "inc/CmapCache.h"
31 #include "inc/TtfTypes.h"
32 #include "inc/TtfUtil.h"
35 using namespace graphite2
;
37 const void * bmp_subtable(const Face::Table
& cmap
)
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()))
50 const void * smp_subtable(const Face::Table
& cmap
)
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()))
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
)
65 uint32 codePoint
= NextCodePoint(cst
, 0, &rangeKey
),
67 while (codePoint
< limit
)
69 unsigned int block
= codePoint
>> 8;
72 blocks
[block
] = grzeroalloc
<uint16
>(0x100);
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
);
87 CachedCmap::CachedCmap(const Face
& face
)
91 const Face::Table
cmap(face
, Tag::cmap
);
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))
105 if (m_blocks
&& bmp_cmap
)
107 if (!cache_subtable
<TtfUtil::CmapSubtable4NextCodepoint
, TtfUtil::CmapSubtable4Lookup
>(m_blocks
, bmp_cmap
, 0xFFFF))
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
++)
121 uint16
CachedCmap::operator [] (const uint32 usv
) const throw()
123 if ((m_isBmpOnly
&& usv
> 0xFFFF) || (usv
> 0x10FFFF))
125 const uint32 block
= 0xFFFF & (usv
>> 8);
127 return m_blocks
[block
][usv
& 0xFF];
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()
147 ? (_smp
? TtfUtil::CmapSubtable12Lookup(_smp
, usv
, 0) : 0)
148 : TtfUtil::CmapSubtable4Lookup(_bmp
, usv
, 0);
151 DirectCmap::operator bool () const throw()
153 return _cmap
&& _bmp
;