Update contrib.
4 * (C) Copyright IBM Corp. 1998-2005 - All Rights Reserved
10 #include "LELanguages.h"
12 #include "LayoutEngine.h"
13 #include "ArabicLayoutEngine.h"
14 #include "CanonShaping.h"
15 #include "HanLayoutEngine.h"
16 #include "IndicLayoutEngine.h"
17 #include "KhmerLayoutEngine.h"
18 #include "ThaiLayoutEngine.h"
19 #include "GXLayoutEngine.h"
20 #include "ScriptAndLanguageTags.h"
21 #include "CharSubstitutionFilter.h"
23 #include "LEGlyphStorage.h"
25 #include "OpenTypeUtilities.h"
26 #include "GlyphSubstitutionTables.h"
27 #include "MorphTables.h"
29 #include "DefaultCharMapper.h"
31 #include "KernTable.h"
35 #define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
37 const LEUnicode32 DefaultCharMapper::controlChars[] = {
38 0x0009, 0x000A, 0x000D,
39 /*0x200C, 0x200D,*/ 0x200E, 0x200F,
40 0x2028, 0x2029, 0x202A, 0x202B, 0x202C, 0x202D, 0x202E,
41 0x206A, 0x206B, 0x206C, 0x206D, 0x206E, 0x206F
44 const le_int32 DefaultCharMapper::controlCharsCount = ARRAY_SIZE(controlChars);
46 const LEUnicode32 DefaultCharMapper::mirroredChars[] = {
47 0x0028, 0x0029, // ascii paired punctuation
51 0x2045, 0x2046, // math symbols (not complete)
55 0x3008, 0x3009, // chinese paired punctuation
66 const le_int32 DefaultCharMapper::mirroredCharsCount = ARRAY_SIZE(mirroredChars);
68 LEUnicode32 DefaultCharMapper::mapChar(LEUnicode32 ch) const
70 if (fFilterControls) {
71 le_int32 index = OpenTypeUtilities::search((le_uint32)ch, (le_uint32 *)controlChars, controlCharsCount);
73 if (controlChars[index] == ch) {
79 le_int32 index = OpenTypeUtilities::search((le_uint32) ch, (le_uint32 *)mirroredChars, mirroredCharsCount);
81 if (mirroredChars[index] == ch) {
82 le_int32 mirrorOffset = ((index & 1) == 0) ? 1 : -1;
84 return mirroredChars[index + mirrorOffset];
91 // This is here to get it out of LEGlyphFilter.h.
92 // No particular reason to put it here, other than
93 // this is a good central location...
94 LEGlyphFilter::~LEGlyphFilter()
99 CharSubstitutionFilter::CharSubstitutionFilter(const LEFontInstance *fontInstance)
100 : fFontInstance(fontInstance)
105 CharSubstitutionFilter::~CharSubstitutionFilter()
111 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LayoutEngine)
113 static const LETag emptyTag = 0x00000000;
115 static const LETag ccmpFeatureTag = LE_CCMP_FEATURE_TAG;
117 static const LETag canonFeatures[] = {ccmpFeatureTag, emptyTag};
119 LayoutEngine::LayoutEngine(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode, le_int32 typoFlags)
120 : fGlyphStorage(NULL), fFontInstance(fontInstance), fScriptCode(scriptCode), fLanguageCode(languageCode),
121 fTypoFlags(typoFlags), fGidOfRA(0)
123 fGlyphStorage = new LEGlyphStorage();
126 le_bool LayoutEngine::isBogus()
128 return fGlyphStorage? FALSE : TRUE;
131 le_int32 LayoutEngine::getGlyphCount() const
133 return fGlyphStorage->getGlyphCount();
136 void LayoutEngine::getCharIndices(le_int32 charIndices[], le_int32 indexBase, LEErrorCode &success) const
138 fGlyphStorage->getCharIndices(charIndices, indexBase, success);
141 void LayoutEngine::getCharIndices(le_int32 charIndices[], LEErrorCode &success) const
143 fGlyphStorage->getCharIndices(charIndices, success);
146 // Copy the glyphs into caller's (32-bit) glyph array, OR in extraBits
147 void LayoutEngine::getGlyphs(le_uint32 glyphs[], le_uint32 extraBits, LEErrorCode &success) const
149 fGlyphStorage->getGlyphs(glyphs, extraBits, success);
152 void LayoutEngine::getGlyphs(LEGlyphID glyphs[], LEErrorCode &success) const
154 fGlyphStorage->getGlyphs(glyphs, success);
158 void LayoutEngine::getGlyphPositions(float positions[], LEErrorCode &success) const
160 fGlyphStorage->getGlyphPositions(positions, success);
163 void LayoutEngine::getGlyphPosition(le_int32 glyphIndex, float &x, float &y, LEErrorCode &success) const
165 fGlyphStorage->getGlyphPosition(glyphIndex, x, y, success);
168 le_int32 LayoutEngine::characterProcessing(const LEUnicode chars[], le_int32 offset, le_int32 count, le_int32 max, le_bool rightToLeft,
169 LEUnicode *&outChars, LEGlyphStorage &glyphStorage, LEErrorCode &success)
171 if (LE_FAILURE(success)) {
175 if (offset < 0 || count < 0 || max < 0 || offset >= max || offset + count > max) {
176 success = LE_ILLEGAL_ARGUMENT_ERROR;
180 const GlyphSubstitutionTableHeader *canonGSUBTable = (GlyphSubstitutionTableHeader *) CanonShaping::glyphSubstitutionTable;
181 LETag scriptTag = OpenTypeLayoutEngine::getScriptTag(fScriptCode);
182 LETag langSysTag = OpenTypeLayoutEngine::getLangSysTag(fLanguageCode);
183 le_int32 i, dir = 1, out = 0, outCharCount = count;
185 if (canonGSUBTable->coversScript(scriptTag)) {
186 CharSubstitutionFilter *substitutionFilter = new CharSubstitutionFilter(fFontInstance);
187 if (!substitutionFilter) {
188 success = LE_MEMORY_ALLOCATION_ERROR;
192 const LEUnicode *inChars = &chars[offset];
193 LEUnicode *reordered = NULL;
195 // This is the cheapest way to get mark reordering only for Hebrew.
196 // We could just do the mark reordering for all scripts, but most
197 // of them probably don't need it...
198 if (fScriptCode == hebrScriptCode) {
199 reordered = LE_NEW_ARRAY(LEUnicode, count);
201 if (reordered == NULL) {
202 success = LE_MEMORY_ALLOCATION_ERROR;
203 delete substitutionFilter;
207 CanonShaping::reorderMarks(&chars[offset], count, rightToLeft, reordered, glyphStorage);
211 glyphStorage.allocateGlyphArray(count, rightToLeft, success);
212 glyphStorage.allocateAuxData(success);
214 if (LE_FAILURE(success)) {
215 delete substitutionFilter;
224 for (i = 0; i < count; i += 1, out += dir) {
225 glyphStorage[out] = (LEGlyphID) inChars[i];
226 glyphStorage.setAuxData(out, (void *) canonFeatures, success);
229 if (reordered != NULL) {
230 LE_DELETE_ARRAY(reordered);
233 outCharCount = canonGSUBTable->process(glyphStorage, rightToLeft,
234 scriptTag, langSysTag, NULL, success, substitutionFilter, NULL);
236 if (LE_FAILURE(success)) {
237 delete substitutionFilter;
241 out = (rightToLeft? count - 1 : 0);
243 outChars = LE_NEW_ARRAY(LEUnicode, outCharCount);
245 delete substitutionFilter;
246 success = LE_MEMORY_ALLOCATION_ERROR;
249 for (i = 0; i < outCharCount; i += 1, out += dir) {
250 outChars[out] = (LEUnicode) LE_GET_GLYPH(glyphStorage[i]);
253 delete substitutionFilter;
259 le_int32 LayoutEngine::computeGlyphs(const LEUnicode chars[], le_int32 offset, le_int32 count, le_int32 max, le_bool rightToLeft,
260 LEGlyphStorage &glyphStorage, LEErrorCode &success)
262 if (LE_FAILURE(success)) {
266 if (chars == NULL || offset < 0 || count < 0 || max < 0 || offset >= max || offset + count > max) {
267 success = LE_ILLEGAL_ARGUMENT_ERROR;
271 LEUnicode *outChars = NULL;
272 le_int32 outCharCount = characterProcessing(chars, offset, count, max, rightToLeft, outChars, glyphStorage, success);
274 if (outChars != NULL) {
275 mapCharsToGlyphs(outChars, 0, outCharCount, rightToLeft, rightToLeft, glyphStorage, success);
276 LE_DELETE_ARRAY(outChars); // FIXME: a subclass may have allocated this, in which case this delete might not work...
278 mapCharsToGlyphs(chars, offset, count, rightToLeft, rightToLeft, glyphStorage, success);
281 return glyphStorage.getGlyphCount();
286 void LayoutEngine::positionGlyphs(LEGlyphStorage &glyphStorage, float x, float y, LEErrorCode &success)
288 if (LE_FAILURE(success)) {
292 glyphStorage.allocatePositions(success);
294 if (LE_FAILURE(success)) {
298 le_int32 i, glyphCount = glyphStorage.getGlyphCount();
300 for (i = 0; i < glyphCount; i += 1) {
303 glyphStorage.setPosition(i, x, y, success);
305 fFontInstance->getGlyphAdvance(glyphStorage[i], advance);
310 glyphStorage.setPosition(glyphCount, x, y, success);
313 void LayoutEngine::adjustGlyphPositions(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool /*reverse*/,
314 LEGlyphStorage &glyphStorage, LEErrorCode &success)
316 if (LE_FAILURE(success)) {
320 if (chars == NULL || offset < 0 || count < 0) {
321 success = LE_ILLEGAL_ARGUMENT_ERROR;
325 if (fTypoFlags & 0x1) { /* kerning enabled */
326 static const le_uint32 kernTableTag = LE_KERN_TABLE_TAG;
328 KernTable kt(fFontInstance, getFontTable(kernTableTag));
329 kt.process(glyphStorage);
332 // default is no adjustments
336 void LayoutEngine::adjustMarkGlyphs(LEGlyphStorage &glyphStorage, LEGlyphFilter *markFilter, LEErrorCode &success)
339 le_int32 p, glyphCount = glyphStorage.getGlyphCount();
341 if (LE_FAILURE(success)) {
345 if (markFilter == NULL) {
346 success = LE_ILLEGAL_ARGUMENT_ERROR;
352 glyphStorage.getGlyphPosition(0, prev, ignore, success);
354 for (p = 0; p < glyphCount; p += 1) {
355 float next, xAdvance;
357 glyphStorage.getGlyphPosition(p + 1, next, ignore, success);
359 xAdvance = next - prev;
360 glyphStorage.adjustPosition(p, xAdjust, 0, success);
362 if (markFilter->accept(glyphStorage[p])) {
369 glyphStorage.adjustPosition(glyphCount, xAdjust, 0, success);
372 void LayoutEngine::adjustMarkGlyphs(const LEUnicode chars[], le_int32 charCount, le_bool reverse, LEGlyphStorage &glyphStorage, LEGlyphFilter *markFilter, LEErrorCode &success)
375 le_int32 c = 0, direction = 1, p;
376 le_int32 glyphCount = glyphStorage.getGlyphCount();
378 if (LE_FAILURE(success)) {
382 if (markFilter == NULL) {
383 success = LE_ILLEGAL_ARGUMENT_ERROR;
394 glyphStorage.getGlyphPosition(0, prev, ignore, success);
396 for (p = 0; p < charCount; p += 1, c += direction) {
397 float next, xAdvance;
399 glyphStorage.getGlyphPosition(p + 1, next, ignore, success);
401 xAdvance = next - prev;
402 glyphStorage.adjustPosition(p, xAdjust, 0, success);
404 if (markFilter->accept(chars[c])) {
411 glyphStorage.adjustPosition(glyphCount, xAdjust, 0, success);
414 const void *LayoutEngine::getFontTable(LETag tableTag) const
416 return fFontInstance->getFontTable(tableTag);
419 void LayoutEngine::mapCharsToGlyphs(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool reverse, le_bool mirror,
420 LEGlyphStorage &glyphStorage, LEErrorCode &success)
422 if (LE_FAILURE(success)) {
426 glyphStorage.allocateGlyphArray(count, reverse, success);
428 DefaultCharMapper charMapper(TRUE, mirror);
430 fFontInstance->mapCharsToGlyphs(chars, offset, count, reverse, &charMapper, glyphStorage);
433 // Input: characters, font?
434 // Output: glyphs, positions, char indices
435 // Returns: number of glyphs
436 le_int32 LayoutEngine::layoutChars(const LEUnicode chars[], le_int32 offset, le_int32 count, le_int32 max, le_bool rightToLeft,
437 float x, float y, LEErrorCode &success)
439 if (LE_FAILURE(success)) {
443 if (chars == NULL || offset < 0 || count < 0 || max < 0 || offset >= max || offset + count > max) {
444 success = LE_ILLEGAL_ARGUMENT_ERROR;
448 // 1922 mlyl: get the glyph ID of RAKAR -->
449 const LEUnicode RAKAR[] = {0xFFFF, 0xD15, 0xD4D, 0xD30, 0xFFFF};
450 if (fGidOfRA == 0 && mlymScriptCode == fScriptCode) {
451 fGidOfRA = -1; // 0xFFFFFFFF
452 TInt noOfGlyphs = layoutChars(
453 RAKAR, // chars - the input character context
454 1, // the offset of the first character to process
455 3, // count - the number of characters to process // offset
456 5, // max - the number of characters in the input context // size of text
457 FALSE, // rightToLeft - TRUE if the characters are in a right to left directional run
460 success); // result code
462 if (!LE_FAILURE(success)) {
463 fGidOfRA = (*fGlyphStorage)[1];
464 (*fGlyphStorage).reset();
471 glyphCount = computeGlyphs(chars, offset, count, max, rightToLeft, *fGlyphStorage, success);
473 // 1922 mlyl: put RAKAR left of the preceding glyph -->
474 if (fGidOfRA != 0 && fGidOfRA != -1 && mlymScriptCode == fScriptCode) {
475 fGlyphStorage->forMlylRakar(fGidOfRA);
479 positionGlyphs(*fGlyphStorage, x, y, success);
480 adjustGlyphPositions(chars, offset, count, rightToLeft, *fGlyphStorage, success);
485 void LayoutEngine::reset()
487 fGlyphStorage->reset();
490 LayoutEngine *LayoutEngine::layoutEngineFactory(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode, LEErrorCode &success)
492 // 3 -> kerning and ligatures
493 return LayoutEngine::layoutEngineFactory(fontInstance, scriptCode, languageCode, 3, success);
496 LayoutEngine *LayoutEngine::layoutEngineFactory(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode, le_int32 typoFlags, LEErrorCode &success)
498 static const le_uint32 gsubTableTag = LE_GSUB_TABLE_TAG;
499 static const le_uint32 mortTableTag = LE_MORT_TABLE_TAG;
501 if (LE_FAILURE(success)) {
505 const GlyphSubstitutionTableHeader *gsubTable = (const GlyphSubstitutionTableHeader *) fontInstance->getFontTable(gsubTableTag);
506 LayoutEngine *result = NULL;
507 LETag scriptTag = 0x00000000;
508 LETag languageTag = 0x00000000;
510 if (gsubTable != NULL && gsubTable->coversScript(scriptTag = OpenTypeLayoutEngine::getScriptTag(scriptCode))) {
511 switch (scriptCode) {
521 result = new IndicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable);
525 result = new ArabicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable);
529 languageTag = OpenTypeLayoutEngine::getLangSysTag(languageCode);
531 switch (languageCode) {
532 case korLanguageCode:
533 case janLanguageCode:
534 case zhtLanguageCode:
535 case zhsLanguageCode:
536 if (gsubTable->coversScriptAndLanguage(scriptTag, languageTag, TRUE)) {
537 result = new HanOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable);
541 // note: falling through to default case.
543 result = new OpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable);
550 result = new KhmerOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable);
554 result = new OpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable);
558 const MorphTableHeader *morphTable = (MorphTableHeader *) fontInstance->getFontTable(mortTableTag);
560 if (morphTable != NULL) {
561 result = new GXLayoutEngine(fontInstance, scriptCode, languageCode, morphTable);
563 switch (scriptCode) {
574 result = new IndicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags);
579 //case hebrScriptCode:
580 result = new UnicodeArabicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags);
583 //case hebrScriptCode:
584 // return new HebrewOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags);
587 result = new ThaiLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags);
591 result = new LayoutEngine(fontInstance, scriptCode, languageCode, typoFlags);
597 if (result && result->isBogus()) {
601 if (result == NULL) {
602 success = LE_MEMORY_ALLOCATION_ERROR;
608 LayoutEngine::~LayoutEngine() {
609 delete fGlyphStorage;