1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 /* 27 * 28 * 29 * (C) Copyright IBM Corp. 1998-2010 - All Rights Reserved 30 * 31 */ 32 33 #include "LETypes.h" 34 #include "OpenTypeTables.h" 35 #include "OpenTypeUtilities.h" 36 #include "ScriptAndLanguage.h" 37 #include "LESwaps.h" 38 39 U_NAMESPACE_BEGIN 40 41 LEReferenceTo<LangSysTable> ScriptTable::findLanguage(const LETableReference& base, LETag languageTag, LEErrorCode &success, le_bool exactMatch) const 42 { 43 le_uint16 count = SWAPW(langSysCount); 44 Offset langSysTableOffset = exactMatch? 0 : SWAPW(defaultLangSysTableOffset); 45 46 if (count > 0) { 47 LEReferenceToArrayOf<TagAndOffsetRecord> langSysRecords(base, success, langSysRecordArray, count); 48 Offset foundOffset = 49 OpenTypeUtilities::getTagOffset(languageTag, langSysRecords, success); 50 51 if (foundOffset != 0 && LE_SUCCESS(success)) { 52 langSysTableOffset = foundOffset; 53 } 54 } 55 56 if (langSysTableOffset != 0) { 57 return LEReferenceTo<LangSysTable>(base, success, langSysTableOffset); 58 } 59 60 return LEReferenceTo<LangSysTable>(); 61 } 62 63 LEReferenceTo<ScriptTable> ScriptListTable::findScript(const LETableReference &base, LETag scriptTag, LEErrorCode &success) const 64 { 65 if (LE_FAILURE(success) ) { 66 return LEReferenceTo<ScriptTable>(); // get out 67 } 68 /* 69 * There are some fonts that have a large, bogus value for scriptCount. To try 70 * and protect against this, we use the offset in the first scriptRecord, 71 * which we know has to be past the end of the scriptRecordArray, to compute 72 * a value which is greater than or equal to the actual script count. 73 * 74 * Note: normally, the first offset will point to just after the scriptRecordArray, 75 * but there's no guarantee of this, only that it's *after* the scriptRecordArray. 76 * Because of this, a binary serach isn't safe, because the new count may include 77 * data that's not actually in the scriptRecordArray and hence the array will appear 78 * to be unsorted. 79 */ 80 le_uint16 count = SWAPW(scriptCount); 81 82 if (count == 0) { 83 return LEReferenceTo<ScriptTable>(); // no items, no search 84 } 85 86 // attempt to construct a ref with at least one element 87 LEReferenceToArrayOf<ScriptRecord> oneElementTable(base, success, &scriptRecordArray[0], 1); 88 89 if( LE_FAILURE(success) ) { 90 return LEReferenceTo<ScriptTable>(); // couldn't even read the first record - bad font. 91 } 92 93 le_uint16 limit = ((SWAPW(scriptRecordArray[0].offset) - sizeof(ScriptListTable)) / sizeof(scriptRecordArray)) + ANY_NUMBER; 94 Offset scriptTableOffset = 0; 95 96 97 if (count > limit) { 98 // the scriptCount value is bogus; do a linear search 99 // because limit may still be too large. 100 LEReferenceToArrayOf<ScriptRecord> scriptRecordArrayRef(base, success, &scriptRecordArray[0], limit); 101 for(le_int32 s = 0; (s < limit)&&LE_SUCCESS(success); s += 1) { 102 if (SWAPT(scriptRecordArrayRef(s,success).tag) == scriptTag) { 103 scriptTableOffset = SWAPW(scriptRecordArrayRef(s,success).offset); 104 break; 105 } 106 } 107 } else { 108 LEReferenceToArrayOf<ScriptRecord> scriptRecordArrayRef(base, success, &scriptRecordArray[0], count); 109 scriptTableOffset = OpenTypeUtilities::getTagOffset(scriptTag, scriptRecordArrayRef, success); // TODO 110 } 111 112 if (scriptTableOffset != 0) { 113 return LEReferenceTo<ScriptTable>(base, success, scriptTableOffset); 114 } 115 116 return LEReferenceTo<ScriptTable>(); 117 } 118 119 LEReferenceTo<LangSysTable> ScriptListTable::findLanguage(const LETableReference &base, LETag scriptTag, LETag languageTag, LEErrorCode &success, le_bool exactMatch) const 120 { 121 const LEReferenceTo<ScriptTable> scriptTable = findScript(base, scriptTag, success); 122 123 if (scriptTable.isEmpty()) { 124 return LEReferenceTo<LangSysTable>(); 125 } 126 127 return scriptTable->findLanguage(scriptTable, languageTag, success, exactMatch).reparent(base); 128 } 129 130 U_NAMESPACE_END