Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "sft.hxx"
21 :
22 : #include "gsub.h"
23 :
24 : #include <osl/diagnose.h>
25 :
26 : #include <vector>
27 : #include <map>
28 : #include <algorithm>
29 :
30 : namespace vcl
31 : {
32 :
33 : typedef sal_uIntPtr sal_uLong;
34 : typedef sal_uInt8 FT_Byte;
35 :
36 : typedef std::map<sal_uInt16,sal_uInt16> GlyphSubstitution;
37 :
38 271244 : inline sal_uInt32 NEXT_Long( const unsigned char* &p )
39 : {
40 271244 : sal_uInt32 nVal = (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3];
41 271244 : p += 4;
42 271244 : return nVal;
43 : }
44 :
45 851522 : inline sal_uInt16 NEXT_UShort( const unsigned char* &p )
46 : {
47 851522 : sal_uInt16 nVal = (p[0]<<8) + p[1];
48 851522 : p += 2;
49 851522 : return nVal;
50 : }
51 :
52 : #define MKTAG(s) ((((((s[0]<<8)+s[1])<<8)+s[2])<<8)+s[3])
53 :
54 8039 : bool ReadGSUB( struct _TrueTypeFont* pTTFile,
55 : int nRequestedScript, int nRequestedLangsys )
56 : {
57 8039 : const FT_Byte* pGsubBase = (FT_Byte*)pTTFile->tables[ O_gsub ];
58 8039 : if( !pGsubBase )
59 648 : return false;
60 :
61 : // #129682# check offsets inside GSUB table
62 7391 : const FT_Byte* pGsubLimit = pGsubBase + pTTFile->tlens[ O_gsub ];
63 :
64 : // parse GSUB header
65 7391 : const FT_Byte* pGsubHeader = pGsubBase;
66 7391 : const sal_uLong nVersion = NEXT_Long( pGsubHeader );
67 7391 : const sal_uInt16 nOfsScriptList = NEXT_UShort( pGsubHeader );
68 7391 : const sal_uInt16 nOfsFeatureTable = NEXT_UShort( pGsubHeader );
69 7391 : const sal_uInt16 nOfsLookupList = NEXT_UShort( pGsubHeader );
70 :
71 : // sanity check the GSUB header
72 7391 : if( nVersion != 0x00010000 )
73 0 : if( nVersion != 0x00001000 ) // workaround for SunBatang etc.
74 0 : return false; // unknown format or broken
75 :
76 : typedef std::vector<sal_uLong> ReqFeatureTagList;
77 7391 : ReqFeatureTagList aReqFeatureTagList;
78 :
79 7391 : aReqFeatureTagList.push_back( MKTAG("vert") );
80 :
81 : typedef std::vector<sal_uInt16> UshortList;
82 14782 : UshortList aFeatureIndexList;
83 :
84 : // parse Script Table
85 7391 : const FT_Byte* pScriptHeader = pGsubBase + nOfsScriptList;
86 7391 : const sal_uInt16 nCntScript = NEXT_UShort( pScriptHeader );
87 7391 : if( pGsubLimit < pScriptHeader + 6 * nCntScript )
88 0 : return false;
89 41183 : for( sal_uInt16 nScriptIndex = 0; nScriptIndex < nCntScript; ++nScriptIndex )
90 : {
91 33792 : const sal_uLong nTag = NEXT_Long( pScriptHeader ); // e.g. hani/arab/kana/hang
92 33792 : const sal_uInt16 nOfsScriptTable= NEXT_UShort( pScriptHeader );
93 33792 : if( (nTag != (sal_uInt16)nRequestedScript) && (nRequestedScript != 0) )
94 0 : continue;
95 :
96 33792 : const FT_Byte* pScriptTable = pGsubBase + nOfsScriptList + nOfsScriptTable;
97 33792 : if( pGsubLimit < pScriptTable + 4 )
98 0 : return false;
99 33792 : const sal_uInt16 nDefaultLangsysOfs = NEXT_UShort( pScriptTable );
100 33792 : const sal_uInt16 nCntLangSystem = NEXT_UShort( pScriptTable );
101 33792 : sal_uInt16 nLangsysOffset = 0;
102 33792 : if( pGsubLimit < pScriptTable + 6 * nCntLangSystem )
103 0 : return false;
104 33792 : for( sal_uInt16 nLangsysIndex = 0; nLangsysIndex < nCntLangSystem; ++nLangsysIndex )
105 : {
106 9499 : const sal_uLong nInnerTag = NEXT_Long( pScriptTable ); // e.g. KOR/ZHS/ZHT/JAN
107 9499 : const sal_uInt16 nOffset= NEXT_UShort( pScriptTable );
108 9499 : if( (nInnerTag != (sal_uInt16)nRequestedLangsys) && (nRequestedLangsys != 0) )
109 0 : continue;
110 9499 : nLangsysOffset = nOffset;
111 9499 : break;
112 : }
113 :
114 33792 : if( (nDefaultLangsysOfs != 0) && (nDefaultLangsysOfs != nLangsysOffset) )
115 : {
116 30535 : const FT_Byte* pLangSys = pGsubBase + nOfsScriptList + nOfsScriptTable + nDefaultLangsysOfs;
117 30535 : if( pGsubLimit < pLangSys + 6 )
118 0 : return false;
119 30535 : /*const sal_uInt16 nLookupOrder =*/ NEXT_UShort( pLangSys );
120 30535 : const sal_uInt16 nReqFeatureIdx = NEXT_UShort( pLangSys );
121 30535 : const sal_uInt16 nCntFeature = NEXT_UShort( pLangSys );
122 30535 : if( pGsubLimit < pLangSys + 2 * nCntFeature )
123 0 : return false;
124 30535 : aFeatureIndexList.push_back( nReqFeatureIdx );
125 207517 : for( sal_uInt16 i = 0; i < nCntFeature; ++i )
126 : {
127 176982 : const sal_uInt16 nFeatureIndex = NEXT_UShort( pLangSys );
128 176982 : aFeatureIndexList.push_back( nFeatureIndex );
129 : }
130 : }
131 :
132 33792 : if( nLangsysOffset != 0 )
133 : {
134 9499 : const FT_Byte* pLangSys = pGsubBase + nOfsScriptList + nOfsScriptTable + nLangsysOffset;
135 9499 : if( pGsubLimit < pLangSys + 6 )
136 0 : return false;
137 9499 : /*const sal_uInt16 nLookupOrder =*/ NEXT_UShort( pLangSys );
138 9499 : const sal_uInt16 nReqFeatureIdx = NEXT_UShort( pLangSys );
139 9499 : const sal_uInt16 nCntFeature = NEXT_UShort( pLangSys );
140 9499 : if( pGsubLimit < pLangSys + 2 * nCntFeature )
141 0 : return false;
142 9499 : aFeatureIndexList.push_back( nReqFeatureIdx );
143 75842 : for( sal_uInt16 i = 0; i < nCntFeature; ++i )
144 : {
145 66343 : const sal_uInt16 nFeatureIndex = NEXT_UShort( pLangSys );
146 66343 : aFeatureIndexList.push_back( nFeatureIndex );
147 : }
148 : }
149 : }
150 :
151 7391 : if( aFeatureIndexList.empty() )
152 0 : return true;
153 :
154 14782 : UshortList aLookupIndexList;
155 14782 : UshortList aLookupOffsetList;
156 :
157 : // parse Feature Table
158 7391 : const FT_Byte* pFeatureHeader = pGsubBase + nOfsFeatureTable;
159 7391 : if( pGsubLimit < pFeatureHeader + 2 )
160 0 : return false;
161 7391 : const sal_uInt16 nCntFeature = NEXT_UShort( pFeatureHeader );
162 7391 : if( pGsubLimit < pFeatureHeader + 6 * nCntFeature )
163 0 : return false;
164 227953 : for( sal_uInt16 nFeatureIndex = 0; nFeatureIndex < nCntFeature; ++nFeatureIndex )
165 : {
166 220562 : const sal_uLong nTag = NEXT_Long( pFeatureHeader ); // e.g. locl/vert/trad/smpl/liga/fina/...
167 220562 : const sal_uInt16 nOffset= NEXT_UShort( pFeatureHeader );
168 :
169 : // ignore unneeded feature lookups
170 220562 : if( aFeatureIndexList[0] != nFeatureIndex ) // do not ignore the required feature
171 : {
172 220264 : const int nRequested = std::count( aFeatureIndexList.begin(), aFeatureIndexList.end(), nFeatureIndex);
173 220264 : if( !nRequested ) // ignore features that are not requested
174 281672 : continue;
175 158856 : const int nAvailable = std::count( aReqFeatureTagList.begin(), aReqFeatureTagList.end(), nTag);
176 158856 : if( !nAvailable ) // some fonts don't provide features they request!
177 158856 : continue;
178 : }
179 :
180 298 : const FT_Byte* pFeatureTable = pGsubBase + nOfsFeatureTable + nOffset;
181 298 : if( pGsubLimit < pFeatureTable + 2 )
182 0 : return false;
183 298 : const sal_uInt16 nCntLookups = NEXT_UShort( pFeatureTable );
184 298 : if( pGsubLimit < pFeatureTable + 2 * nCntLookups )
185 0 : return false;
186 298 : for( sal_uInt16 i = 0; i < nCntLookups; ++i )
187 : {
188 0 : const sal_uInt16 nLookupIndex = NEXT_UShort( pFeatureTable );
189 0 : aLookupIndexList.push_back( nLookupIndex );
190 : }
191 298 : if( nCntLookups == 0 ) //### hack needed by Mincho/Gothic/Mingliu/Simsun/...
192 298 : aLookupIndexList.push_back( 0 );
193 : }
194 :
195 : // parse Lookup List
196 7391 : const FT_Byte* pLookupHeader = pGsubBase + nOfsLookupList;
197 7391 : if( pGsubLimit < pLookupHeader + 2 )
198 0 : return false;
199 7391 : const sal_uInt16 nCntLookupTable = NEXT_UShort( pLookupHeader );
200 7391 : if( pGsubLimit < pLookupHeader + 2 * nCntLookupTable )
201 0 : return false;
202 118511 : for( sal_uInt16 nLookupIdx = 0; nLookupIdx < nCntLookupTable; ++nLookupIdx )
203 : {
204 111120 : const sal_uInt16 nOffset = NEXT_UShort( pLookupHeader );
205 111120 : if( std::count( aLookupIndexList.begin(), aLookupIndexList.end(), nLookupIdx ) )
206 298 : aLookupOffsetList.push_back( nOffset );
207 : }
208 :
209 7391 : UshortList::const_iterator it = aLookupOffsetList.begin();
210 7689 : for(; it != aLookupOffsetList.end(); ++it )
211 : {
212 298 : const sal_uInt16 nOfsLookupTable = *it;
213 298 : const FT_Byte* pLookupTable = pGsubBase + nOfsLookupList + nOfsLookupTable;
214 298 : if( pGsubLimit < pLookupTable + 6 )
215 0 : return false;
216 298 : const sal_uInt16 eLookupType = NEXT_UShort( pLookupTable );
217 298 : /*const sal_uInt16 eLookupFlag =*/ NEXT_UShort( pLookupTable );
218 298 : const sal_uInt16 nCntLookupSubtable = NEXT_UShort( pLookupTable );
219 :
220 : // TODO: switch( eLookupType )
221 298 : if( eLookupType != 1 ) // TODO: once we go beyond SingleSubst
222 298 : continue;
223 :
224 0 : if( pGsubLimit < pLookupTable + 2 * nCntLookupSubtable )
225 0 : return false;
226 0 : for( sal_uInt16 nSubTableIdx = 0; nSubTableIdx < nCntLookupSubtable; ++nSubTableIdx )
227 : {
228 0 : const sal_uInt16 nOfsSubLookupTable = NEXT_UShort( pLookupTable );
229 0 : const FT_Byte* pSubLookup = pGsubBase + nOfsLookupList + nOfsLookupTable + nOfsSubLookupTable;
230 0 : if( pGsubLimit < pSubLookup + 6 )
231 0 : return false;
232 0 : const sal_uInt16 nFmtSubstitution = NEXT_UShort( pSubLookup );
233 0 : const sal_uInt16 nOfsCoverage = NEXT_UShort( pSubLookup );
234 :
235 : typedef std::pair<sal_uInt16,sal_uInt16> GlyphSubst;
236 : typedef std::vector<GlyphSubst> SubstVector;
237 0 : SubstVector aSubstVector;
238 :
239 : const FT_Byte* pCoverage = pGsubBase
240 0 : + nOfsLookupList + nOfsLookupTable + nOfsSubLookupTable + nOfsCoverage;
241 0 : if( pGsubLimit < pCoverage + 4 )
242 0 : return false;
243 0 : const sal_uInt16 nFmtCoverage = NEXT_UShort( pCoverage );
244 0 : switch( nFmtCoverage )
245 : {
246 : case 1: // Coverage Format 1
247 : {
248 0 : const sal_uInt16 nCntGlyph = NEXT_UShort( pCoverage );
249 0 : if( pGsubLimit < pCoverage + 2 * nCntGlyph )
250 : // TODO? nCntGlyph = (pGsubLimit - pCoverage) / 2;
251 0 : return false;
252 0 : aSubstVector.reserve( nCntGlyph );
253 0 : for( sal_uInt16 i = 0; i < nCntGlyph; ++i )
254 : {
255 0 : const sal_uInt16 nGlyphId = NEXT_UShort( pCoverage );
256 0 : aSubstVector.push_back( GlyphSubst( nGlyphId, 0 ) );
257 : }
258 : }
259 0 : break;
260 :
261 : case 2: // Coverage Format 2
262 : {
263 0 : const sal_uInt16 nCntRange = NEXT_UShort( pCoverage );
264 0 : if( pGsubLimit < pCoverage + 6 * nCntRange )
265 : // TODO? nCntGlyph = (pGsubLimit - pCoverage) / 6;
266 0 : return false;
267 0 : for( int i = nCntRange; --i >= 0; )
268 : {
269 0 : const sal_uInt32 nGlyph0 = NEXT_UShort( pCoverage );
270 0 : const sal_uInt32 nGlyph1 = NEXT_UShort( pCoverage );
271 0 : const sal_uInt16 nCovIdx = NEXT_UShort( pCoverage );
272 0 : for( sal_uInt32 j = nGlyph0; j <= nGlyph1; ++j )
273 0 : aSubstVector.push_back( GlyphSubst( static_cast<sal_uInt16>(j + nCovIdx), 0 ) );
274 : }
275 : }
276 0 : break;
277 : }
278 :
279 0 : SubstVector::iterator subst_it( aSubstVector.begin() );
280 :
281 0 : switch( nFmtSubstitution )
282 : {
283 : case 1: // Single Substitution Format 1
284 : {
285 0 : const sal_uInt16 nDeltaGlyphId = NEXT_UShort( pSubLookup );
286 :
287 0 : for(; subst_it != aSubstVector.end(); ++subst_it )
288 0 : (*subst_it).second = (*subst_it).first + nDeltaGlyphId;
289 : }
290 0 : break;
291 :
292 : case 2: // Single Substitution Format 2
293 : {
294 0 : const sal_uInt16 nCntGlyph = NEXT_UShort( pSubLookup );
295 0 : for( int i = nCntGlyph; (subst_it != aSubstVector.end()) && (--i>=0); ++subst_it )
296 : {
297 0 : if( pGsubLimit < pSubLookup + 2 )
298 0 : return false;
299 0 : const sal_uInt16 nGlyphId = NEXT_UShort( pSubLookup );
300 0 : (*subst_it).second = nGlyphId;
301 : }
302 : }
303 0 : break;
304 : }
305 :
306 : // now apply the glyph substitutions that have been collected in this subtable
307 0 : if( !aSubstVector.empty() )
308 : {
309 0 : GlyphSubstitution* pGSubstitution = new GlyphSubstitution;
310 0 : pTTFile->pGSubstitution = (void*)pGSubstitution;
311 0 : for( subst_it = aSubstVector.begin(); subst_it != aSubstVector.end(); ++subst_it )
312 0 : (*pGSubstitution)[ (*subst_it).first ] = (*subst_it).second;
313 : }
314 0 : }
315 : }
316 14782 : return true;
317 : }
318 :
319 8039 : void ReleaseGSUB(struct _TrueTypeFont* pTTFile)
320 : {
321 8039 : GlyphSubstitution* pGlyphSubstitution = (GlyphSubstitution*)pTTFile->pGSubstitution;
322 8039 : if( pGlyphSubstitution )
323 0 : delete pGlyphSubstitution;
324 8039 : }
325 :
326 0 : int UseGSUB( struct _TrueTypeFont* pTTFile, int nGlyph )
327 : {
328 0 : GlyphSubstitution* pGlyphSubstitution = (GlyphSubstitution*)pTTFile->pGSubstitution;
329 0 : if( pGlyphSubstitution != 0 )
330 : {
331 0 : GlyphSubstitution::const_iterator it( pGlyphSubstitution->find( sal::static_int_cast<sal_uInt16>(nGlyph) ) );
332 0 : if( it != pGlyphSubstitution->end() )
333 0 : nGlyph = (*it).second;
334 : }
335 :
336 0 : return nGlyph;
337 : }
338 :
339 8039 : int HasVerticalGSUB( struct _TrueTypeFont* pTTFile )
340 : {
341 8039 : GlyphSubstitution* pGlyphSubstitution = (GlyphSubstitution*)pTTFile->pGSubstitution;
342 8039 : return pGlyphSubstitution ? +1 : 0;
343 : }
344 :
345 : }
346 :
347 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|