Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* GRAPHITE2 LICENSING |
michael@0 | 2 | |
michael@0 | 3 | Copyright 2010, SIL International |
michael@0 | 4 | All rights reserved. |
michael@0 | 5 | |
michael@0 | 6 | This library is free software; you can redistribute it and/or modify |
michael@0 | 7 | it under the terms of the GNU Lesser General Public License as published |
michael@0 | 8 | by the Free Software Foundation; either version 2.1 of License, or |
michael@0 | 9 | (at your option) any later version. |
michael@0 | 10 | |
michael@0 | 11 | This program is distributed in the hope that it will be useful, |
michael@0 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
michael@0 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
michael@0 | 14 | Lesser General Public License for more details. |
michael@0 | 15 | |
michael@0 | 16 | You should also have received a copy of the GNU Lesser General Public |
michael@0 | 17 | License along with this library in the file named "LICENSE". |
michael@0 | 18 | If not, write to the Free Software Foundation, 51 Franklin Street, |
michael@0 | 19 | Suite 500, Boston, MA 02110-1335, USA or visit their web page on the |
michael@0 | 20 | internet at http://www.fsf.org/licenses/lgpl.html. |
michael@0 | 21 | |
michael@0 | 22 | Alternatively, the contents of this file may be used under the terms of the |
michael@0 | 23 | Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public |
michael@0 | 24 | License, as published by the Free Software Foundation, either version 2 |
michael@0 | 25 | of the License or (at your option) any later version. |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | #include "inc/Main.h" |
michael@0 | 29 | #include "inc/CmapCache.h" |
michael@0 | 30 | #include "inc/Face.h" |
michael@0 | 31 | #include "inc/TtfTypes.h" |
michael@0 | 32 | #include "inc/TtfUtil.h" |
michael@0 | 33 | |
michael@0 | 34 | |
michael@0 | 35 | using namespace graphite2; |
michael@0 | 36 | |
michael@0 | 37 | const void * bmp_subtable(const Face::Table & cmap) |
michael@0 | 38 | { |
michael@0 | 39 | const void * stbl; |
michael@0 | 40 | if (!cmap.size()) return 0; |
michael@0 | 41 | if (TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 3, 1, cmap.size())) |
michael@0 | 42 | || TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 3, cmap.size())) |
michael@0 | 43 | || TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 2, cmap.size())) |
michael@0 | 44 | || TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 1, cmap.size())) |
michael@0 | 45 | || TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 0, cmap.size()))) |
michael@0 | 46 | return stbl; |
michael@0 | 47 | return 0; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | const void * smp_subtable(const Face::Table & cmap) |
michael@0 | 51 | { |
michael@0 | 52 | const void * stbl; |
michael@0 | 53 | if (!cmap.size()) return 0; |
michael@0 | 54 | if (TtfUtil::CheckCmapSubtable12(stbl = TtfUtil::FindCmapSubtable(cmap, 3, 10, cmap.size())) |
michael@0 | 55 | || TtfUtil::CheckCmapSubtable12(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 4, cmap.size()))) |
michael@0 | 56 | return stbl; |
michael@0 | 57 | return 0; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | template <unsigned int (*NextCodePoint)(const void *, unsigned int, int *), |
michael@0 | 61 | uint16 (*LookupCodePoint)(const void *, unsigned int, int)> |
michael@0 | 62 | bool cache_subtable(uint16 * blocks[], const void * cst, const unsigned int limit) |
michael@0 | 63 | { |
michael@0 | 64 | int rangeKey = 0; |
michael@0 | 65 | uint32 codePoint = NextCodePoint(cst, 0, &rangeKey), |
michael@0 | 66 | prevCodePoint = 0; |
michael@0 | 67 | while (codePoint != limit) |
michael@0 | 68 | { |
michael@0 | 69 | unsigned int block = codePoint >> 8; |
michael@0 | 70 | if (!blocks[block]) |
michael@0 | 71 | { |
michael@0 | 72 | blocks[block] = grzeroalloc<uint16>(0x100); |
michael@0 | 73 | if (!blocks[block]) |
michael@0 | 74 | return false; |
michael@0 | 75 | } |
michael@0 | 76 | blocks[block][codePoint & 0xFF] = LookupCodePoint(cst, codePoint, rangeKey); |
michael@0 | 77 | // prevent infinite loop |
michael@0 | 78 | if (codePoint <= prevCodePoint) |
michael@0 | 79 | codePoint = prevCodePoint + 1; |
michael@0 | 80 | prevCodePoint = codePoint; |
michael@0 | 81 | codePoint = NextCodePoint(cst, codePoint, &rangeKey); |
michael@0 | 82 | } |
michael@0 | 83 | return true; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | |
michael@0 | 87 | CachedCmap::CachedCmap(const Face & face) |
michael@0 | 88 | : m_isBmpOnly(true), |
michael@0 | 89 | m_blocks(0) |
michael@0 | 90 | { |
michael@0 | 91 | const Face::Table cmap(face, Tag::cmap); |
michael@0 | 92 | if (!cmap) return; |
michael@0 | 93 | |
michael@0 | 94 | const void * bmp_cmap = bmp_subtable(cmap); |
michael@0 | 95 | const void * smp_cmap = smp_subtable(cmap); |
michael@0 | 96 | m_isBmpOnly = !smp_cmap; |
michael@0 | 97 | |
michael@0 | 98 | m_blocks = grzeroalloc<uint16 *>(m_isBmpOnly ? 0x100 : 0x1100); |
michael@0 | 99 | if (m_blocks && smp_cmap) |
michael@0 | 100 | { |
michael@0 | 101 | if (!cache_subtable<TtfUtil::CmapSubtable12NextCodepoint, TtfUtil::CmapSubtable12Lookup>(m_blocks, smp_cmap, 0x10FFFF)) |
michael@0 | 102 | return; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | if (m_blocks && bmp_cmap) |
michael@0 | 106 | { |
michael@0 | 107 | if (!cache_subtable<TtfUtil::CmapSubtable4NextCodepoint, TtfUtil::CmapSubtable4Lookup>(m_blocks, bmp_cmap, 0xFFFF)) |
michael@0 | 108 | return; |
michael@0 | 109 | } |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | CachedCmap::~CachedCmap() throw() |
michael@0 | 113 | { |
michael@0 | 114 | if (!m_blocks) return; |
michael@0 | 115 | unsigned int numBlocks = (m_isBmpOnly)? 0x100 : 0x1100; |
michael@0 | 116 | for (unsigned int i = 0; i < numBlocks; i++) |
michael@0 | 117 | free(m_blocks[i]); |
michael@0 | 118 | free(m_blocks); |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | uint16 CachedCmap::operator [] (const uint32 usv) const throw() |
michael@0 | 122 | { |
michael@0 | 123 | if ((m_isBmpOnly && usv > 0xFFFF) || (usv > 0x10FFFF)) |
michael@0 | 124 | return 0; |
michael@0 | 125 | const uint32 block = 0xFFFF & (usv >> 8); |
michael@0 | 126 | if (m_blocks[block]) |
michael@0 | 127 | return m_blocks[block][usv & 0xFF]; |
michael@0 | 128 | return 0; |
michael@0 | 129 | }; |
michael@0 | 130 | |
michael@0 | 131 | CachedCmap::operator bool() const throw() |
michael@0 | 132 | { |
michael@0 | 133 | return m_blocks != 0; |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | |
michael@0 | 137 | DirectCmap::DirectCmap(const Face & face) |
michael@0 | 138 | : _cmap(face, Tag::cmap), |
michael@0 | 139 | _smp(smp_subtable(_cmap)), |
michael@0 | 140 | _bmp(bmp_subtable(_cmap)) |
michael@0 | 141 | { |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | uint16 DirectCmap::operator [] (const uint32 usv) const throw() |
michael@0 | 145 | { |
michael@0 | 146 | return usv > 0xFFFF |
michael@0 | 147 | ? (_smp ? TtfUtil::CmapSubtable12Lookup(_smp, usv, 0) : 0) |
michael@0 | 148 | : TtfUtil::CmapSubtable4Lookup(_bmp, usv, 0); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | DirectCmap::operator bool () const throw() |
michael@0 | 152 | { |
michael@0 | 153 | return _cmap && _bmp; |
michael@0 | 154 | } |
michael@0 | 155 |