1 /* 2 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) 3 * 4 * This is part of HarfBuzz, an OpenType Layout engine library. 5 * 6 * Permission is hereby granted, without written agreement and without 7 * license or royalty fees, to use, copy, modify, and distribute this 8 * software and its documentation for any purpose, provided that the 9 * above copyright notice and the following two paragraphs appear in 10 * all copies of this software. 11 * 12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 16 * DAMAGE. 17 * 18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 23 */ 24 25 #include "harfbuzz-shaper.h" 26 #include "harfbuzz-shaper-private.h" 27 28 #include <assert.h> 29 30 /* 31 tibetan syllables are of the form: 32 head position consonant 33 first sub-joined consonant 34 ....intermediate sub-joined consonants (if any) 35 last sub-joined consonant 36 sub-joined vowel (a-chung U+0F71) 37 standard or compound vowel sign (or 'virama' for devanagari transliteration) 38 */ 39 40 typedef enum { 41 TibetanOther, 42 TibetanHeadConsonant, 43 TibetanSubjoinedConsonant, 44 TibetanSubjoinedVowel, 45 TibetanVowel 46 } TibetanForm; 47 48 /* this table starts at U+0f40 */ 49 static const unsigned char tibetanForm[0x80] = { 50 TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, 51 TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, 52 TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, 53 TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, 54 55 TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, 56 TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, 57 TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, 58 TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, 59 60 TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, 61 TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, 62 TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, 63 TibetanOther, TibetanOther, TibetanOther, TibetanOther, 64 65 TibetanOther, TibetanVowel, TibetanVowel, TibetanVowel, 66 TibetanVowel, TibetanVowel, TibetanVowel, TibetanVowel, 67 TibetanVowel, TibetanVowel, TibetanVowel, TibetanVowel, 68 TibetanVowel, TibetanVowel, TibetanVowel, TibetanVowel, 69 70 TibetanVowel, TibetanVowel, TibetanVowel, TibetanVowel, 71 TibetanVowel, TibetanVowel, TibetanVowel, TibetanVowel, 72 TibetanOther, TibetanOther, TibetanOther, TibetanOther, 73 TibetanOther, TibetanOther, TibetanOther, TibetanOther, 74 75 TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, 76 TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, 77 TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, 78 TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, 79 80 TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, 81 TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, 82 TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, 83 TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, 84 85 TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, 86 TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, 87 TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, 88 TibetanSubjoinedConsonant, TibetanOther, TibetanOther, TibetanOther 89 }; 90 91 92 #define tibetan_form(c) \ 93 (TibetanForm)tibetanForm[c - 0x0f40] 94 95 static const HB_OpenTypeFeature tibetan_features[] = { 96 { HB_MAKE_TAG('c', 'c', 'm', 'p'), CcmpProperty }, 97 { HB_MAKE_TAG('a', 'b', 'v', 's'), AboveSubstProperty }, 98 { HB_MAKE_TAG('b', 'l', 'w', 's'), BelowSubstProperty }, 99 { HB_MAKE_TAG('c', 'a', 'l', 't'), CaltProperty }, 100 {0, 0} 101 }; 102 103 static HB_Bool tibetan_shape_syllable(HB_Bool openType, HB_ShaperItem *item, HB_Bool invalid) 104 { 105 hb_uint32 i; 106 const HB_UChar16 *str = item->string + item->item.pos; 107 int len = item->item.length; 108 #ifndef NO_OPENTYPE 109 const int availableGlyphs = item->num_glyphs; 110 #endif 111 HB_Bool haveGlyphs; 112 HB_STACKARRAY(HB_UChar16, reordered, len + 4); 113 114 if (item->num_glyphs < item->item.length + 4) { 115 item->num_glyphs = item->item.length + 4; 116 return FALSE; 117 } 118 119 if (invalid) { 120 *reordered = 0x25cc; 121 memcpy(reordered+1, str, len*sizeof(HB_UChar16)); 122 len++; 123 str = reordered; 124 } 125 126 haveGlyphs = item->font->klass->convertStringToGlyphIndices(item->font, 127 str, len, 128 item->glyphs, &item->num_glyphs, 129 item->item.bidiLevel % 2); 130 131 HB_FREE_STACKARRAY(reordered); 132 133 if (!haveGlyphs) 134 return FALSE; 135 136 for (i = 0; i < item->item.length; i++) { 137 item->attributes[i].mark = FALSE; 138 item->attributes[i].clusterStart = FALSE; 139 item->attributes[i].justification = 0; 140 item->attributes[i].zeroWidth = FALSE; 141 /* IDEBUG(" %d: %4x", i, str[i]); */ 142 } 143 144 /* now we have the syllable in the right order, and can start running it through open type. */ 145 146 #ifndef NO_OPENTYPE 147 if (openType) { 148 HB_OpenTypeShape(item, /*properties*/0); 149 if (!HB_OpenTypePosition(item, availableGlyphs, /*doLogClusters*/FALSE)) 150 return FALSE; 151 } else { 152 HB_HeuristicPosition(item); 153 } 154 #endif 155 156 item->attributes[0].clusterStart = TRUE; 157 return TRUE; 158 } 159 160 161 static int tibetan_nextSyllableBoundary(const HB_UChar16 *s, int start, int end, HB_Bool *invalid) 162 { 163 const HB_UChar16 *uc = s + start; 164 165 int pos = 0; 166 TibetanForm state = tibetan_form(*uc); 167 168 /* qDebug("state[%d]=%d (uc=%4x)", pos, state, uc[pos]);*/ 169 pos++; 170 171 if (state != TibetanHeadConsonant) { 172 if (state != TibetanOther) 173 *invalid = TRUE; 174 goto finish; 175 } 176 177 while (pos < end - start) { 178 TibetanForm newState = tibetan_form(uc[pos]); 179 switch(newState) { 180 case TibetanSubjoinedConsonant: 181 case TibetanSubjoinedVowel: 182 if (state != TibetanHeadConsonant && 183 state != TibetanSubjoinedConsonant) 184 goto finish; 185 state = newState; 186 break; 187 case TibetanVowel: 188 if (state != TibetanHeadConsonant && 189 state != TibetanSubjoinedConsonant && 190 state != TibetanSubjoinedVowel) 191 goto finish; 192 break; 193 case TibetanOther: 194 case TibetanHeadConsonant: 195 goto finish; 196 } 197 pos++; 198 } 199 200 finish: 201 *invalid = FALSE; 202 return start+pos; 203 } 204 205 HB_Bool HB_TibetanShape(HB_ShaperItem *item) 206 { 207 208 HB_Bool openType = FALSE; 209 unsigned short *logClusters = item->log_clusters; 210 211 HB_ShaperItem syllable = *item; 212 int first_glyph = 0; 213 214 int sstart = item->item.pos; 215 int end = sstart + item->item.length; 216 217 assert(item->item.script == HB_Script_Tibetan); 218 219 #ifndef QT_NO_OPENTYPE 220 openType = HB_SelectScript(item, tibetan_features); 221 #endif 222 223 while (sstart < end) { 224 HB_Bool invalid; 225 int i; 226 int send = tibetan_nextSyllableBoundary(item->string, sstart, end, &invalid); 227 /* IDEBUG("syllable from %d, length %d, invalid=%s", sstart, send-sstart, 228 invalid ? "TRUE" : "FALSE"); */ 229 syllable.item.pos = sstart; 230 syllable.item.length = send-sstart; 231 syllable.glyphs = item->glyphs + first_glyph; 232 syllable.attributes = item->attributes + first_glyph; 233 syllable.offsets = item->offsets + first_glyph; 234 syllable.advances = item->advances + first_glyph; 235 syllable.num_glyphs = item->num_glyphs - first_glyph; 236 if (!tibetan_shape_syllable(openType, &syllable, invalid)) { 237 item->num_glyphs += syllable.num_glyphs; 238 return FALSE; 239 } 240 /* fix logcluster array */ 241 for (i = sstart; i < send; ++i) 242 logClusters[i-item->item.pos] = first_glyph; 243 sstart = send; 244 first_glyph += syllable.num_glyphs; 245 } 246 item->num_glyphs = first_glyph; 247 return TRUE; 248 } 249 250 void HB_TibetanAttributes(HB_Script script, const HB_UChar16 *text, hb_uint32 from, hb_uint32 len, HB_CharAttributes *attributes) 251 { 252 int end = from + len; 253 const HB_UChar16 *uc = text + from; 254 hb_uint32 i = 0; 255 HB_UNUSED(script); 256 attributes += from; 257 while (i < len) { 258 HB_Bool invalid; 259 hb_uint32 boundary = tibetan_nextSyllableBoundary(text, from+i, end, &invalid) - from; 260 261 attributes[i].charStop = TRUE; 262 263 if (boundary > len-1) boundary = len; 264 i++; 265 while (i < boundary) { 266 attributes[i].charStop = FALSE; 267 ++uc; 268 ++i; 269 } 270 assert(i == boundary); 271 } 272 } 273 274 275