#include "harfbuzz-shaper.h"
#include "harfbuzz-shaper-private.h"
#include <assert.h>
#include <stdio.h>
enum KhmerCharClassValues {
CC_RESERVED = 0,
CC_CONSONANT = 1,
CC_CONSONANT2 = 2,
CC_CONSONANT3 = 3,
CC_ZERO_WIDTH_NJ_MARK = 4,
CC_CONSONANT_SHIFTER = 5,
CC_ROBAT = 6,
CC_COENG = 7,
CC_DEPENDENT_VOWEL = 8,
CC_SIGN_ABOVE = 9,
CC_SIGN_AFTER = 10,
CC_ZERO_WIDTH_J_MARK = 11,
CC_COUNT = 12
};
enum KhmerCharClassFlags {
CF_CLASS_MASK = 0x0000FFFF,
CF_CONSONANT = 0x01000000,
CF_SPLIT_VOWEL = 0x02000000,
CF_DOTTED_CIRCLE = 0x04000000,
CF_COENG = 0x08000000,
CF_SHIFTER = 0x10000000,
CF_ABOVE_VOWEL = 0x20000000,
CF_POS_BEFORE = 0x00080000,
CF_POS_BELOW = 0x00040000,
CF_POS_ABOVE = 0x00020000,
CF_POS_AFTER = 0x00010000,
CF_POS_MASK = 0x000f0000
};
enum KhmerChar {
C_SIGN_ZWNJ = 0x200C,
C_SIGN_ZWJ = 0x200D,
C_RO = 0x179A,
C_VOWEL_AA = 0x17B6,
C_SIGN_NIKAHIT = 0x17C6,
C_VOWEL_E = 0x17C1,
C_COENG = 0x17D2
};
enum {
_xx = CC_RESERVED,
_sa = CC_SIGN_ABOVE | CF_DOTTED_CIRCLE | CF_POS_ABOVE,
_sp = CC_SIGN_AFTER | CF_DOTTED_CIRCLE| CF_POS_AFTER,
_c1 = CC_CONSONANT | CF_CONSONANT,
_c2 = CC_CONSONANT2 | CF_CONSONANT,
_c3 = CC_CONSONANT3 | CF_CONSONANT,
_rb = CC_ROBAT | CF_POS_ABOVE | CF_DOTTED_CIRCLE,
_cs = CC_CONSONANT_SHIFTER | CF_DOTTED_CIRCLE | CF_SHIFTER,
_dl = CC_DEPENDENT_VOWEL | CF_POS_BEFORE | CF_DOTTED_CIRCLE,
_db = CC_DEPENDENT_VOWEL | CF_POS_BELOW | CF_DOTTED_CIRCLE,
_da = CC_DEPENDENT_VOWEL | CF_POS_ABOVE | CF_DOTTED_CIRCLE | CF_ABOVE_VOWEL,
_dr = CC_DEPENDENT_VOWEL | CF_POS_AFTER | CF_DOTTED_CIRCLE,
_co = CC_COENG | CF_COENG | CF_DOTTED_CIRCLE,
_va = _da | CF_SPLIT_VOWEL,
_vr = _dr | CF_SPLIT_VOWEL
};
typedef unsigned long KhmerCharClass;
static const KhmerCharClass khmerCharClasses[] = {
_c1, _c1, _c1, _c3, _c1, _c1, _c1, _c1, _c3, _c1, _c1, _c1, _c1, _c3, _c1, _c1,
_c1, _c1, _c1, _c1, _c3, _c1, _c1, _c1, _c1, _c3, _c2, _c1, _c1, _c1, _c3, _c3,
_c1, _c3, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1,
_c1, _c1, _c1, _c1, _dr, _dr, _dr, _da, _da, _da, _da, _db, _db, _db, _va, _vr,
_vr, _dl, _dl, _dl, _vr, _vr, _sa, _sp, _sp, _cs, _cs, _sa, _rb, _sa, _sa, _sa,
_sa, _sa, _co, _sa, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _sa, _xx, _xx
};
enum KhmerCharClassesRange {
KhmerFirstChar = 0x1780,
KhmerLastChar = 0x17df
};
static KhmerCharClass getKhmerCharClass(HB_UChar16 uc)
{
if (uc == C_SIGN_ZWJ) {
return CC_ZERO_WIDTH_J_MARK;
}
if (uc == C_SIGN_ZWNJ) {
return CC_ZERO_WIDTH_NJ_MARK;
}
if (uc < KhmerFirstChar || uc > KhmerLastChar) {
return CC_RESERVED;
}
return khmerCharClasses[uc - KhmerFirstChar];
}
static const signed char khmerStateTable[][CC_COUNT] =
{
{ 1, 2, 2, 2, 1, 1, 1, 6, 1, 1, 1, 2},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, 3, 4, 5, 6, 16, 17, 1, -1},
{-1, -1, -1, -1, -1, 4, -1, -1, 16, -1, -1, -1},
{-1, -1, -1, -1, 15, -1, -1, 6, 16, 17, 1, 14},
{-1, -1, -1, -1, -1, -1, -1, -1, 20, -1, 1, -1},
{-1, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, 12, 13, -1, 10, 16, 17, 1, 14},
{-1, -1, -1, -1, 12, 13, -1, -1, 16, 17, 1, 14},
{-1, -1, -1, -1, 12, 13, -1, 10, 16, 17, 1, 14},
{-1, 11, 11, 11, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, 15, -1, -1, -1, 16, 17, 1, 14},
{-1, -1, -1, -1, -1, 13, -1, -1, 16, -1, -1, -1},
{-1, -1, -1, -1, 15, -1, -1, -1, 16, 17, 1, 14},
{-1, -1, -1, -1, -1, -1, -1, -1, 16, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, 16, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, 17, 1, 18},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 18},
{-1, -1, -1, -1, -1, -1, -1, 19, -1, -1, -1, -1},
{-1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1},
};
#ifdef KHMER_DEBUG
#define KHDEBUG qDebug
#else
#define KHDEBUG if(0) printf
#endif
static int khmer_nextSyllableBoundary(const HB_UChar16 *s, int start, int end, HB_Bool *invalid)
{
const HB_UChar16 *uc = s + start;
int state = 0;
int pos = start;
*invalid = FALSE;
while (pos < end) {
KhmerCharClass charClass = getKhmerCharClass(*uc);
if (pos == start) {
*invalid = (charClass > 0) && ! (charClass & CF_CONSONANT);
}
state = khmerStateTable[state][charClass & CF_CLASS_MASK];
KHDEBUG("state[%d]=%d class=%8lx (uc=%4x)", pos - start, state,
charClass, *uc );
if (state < 0) {
break;
}
++uc;
++pos;
}
return pos;
}
#ifndef NO_OPENTYPE
static const HB_OpenTypeFeature khmer_features[] = {
{ HB_MAKE_TAG( 'p', 'r', 'e', 'f' ), PreFormProperty },
{ HB_MAKE_TAG( 'b', 'l', 'w', 'f' ), BelowFormProperty },
{ HB_MAKE_TAG( 'a', 'b', 'v', 'f' ), AboveFormProperty },
{ HB_MAKE_TAG( 'p', 's', 't', 'f' ), PostFormProperty },
{ HB_MAKE_TAG( 'p', 'r', 'e', 's' ), PreSubstProperty },
{ HB_MAKE_TAG( 'b', 'l', 'w', 's' ), BelowSubstProperty },
{ HB_MAKE_TAG( 'a', 'b', 'v', 's' ), AboveSubstProperty },
{ HB_MAKE_TAG( 'p', 's', 't', 's' ), PostSubstProperty },
{ HB_MAKE_TAG( 'c', 'l', 'i', 'g' ), CligProperty },
{ 0, 0 }
};
#endif
static HB_Bool khmer_shape_syllable(HB_Bool openType, HB_ShaperItem *item)
{
int len = 0;
int syllableEnd = item->item.pos + item->item.length;
unsigned short reordered[16];
unsigned char properties[16];
enum {
AboveForm = 0x01,
PreForm = 0x02,
PostForm = 0x04,
BelowForm = 0x08
};
#ifndef NO_OPENTYPE
const int availableGlyphs = item->num_glyphs;
#endif
int coengRo;
int i;
assert(item->item.length < 13);
memset(properties, 0, 16*sizeof(unsigned char));
#ifdef KHMER_DEBUG
qDebug("original:");
for (int i = from; i < syllableEnd; i++) {
qDebug(" %d: %4x", i, string[i]);
}
#endif
coengRo = -1;
for (i = item->item.pos; i < syllableEnd; i += 1) {
KhmerCharClass charClass = getKhmerCharClass(item->string[i]);
if (charClass & CF_SPLIT_VOWEL) {
reordered[len] = C_VOWEL_E;
properties[len] = PreForm;
++len;
break;
}
if (charClass & CF_POS_BEFORE) {
reordered[len] = item->string[i];
properties[len] = PreForm;
++len;
break;
}
if ( (charClass & CF_COENG) && (i + 1 < syllableEnd) &&
( (getKhmerCharClass(item->string[i+1]) & CF_CLASS_MASK) == CC_CONSONANT2) ) {
coengRo = i;
}
}
if (coengRo > -1) {
reordered[len] = C_COENG;
properties[len] = PreForm;
++len;
reordered[len] = C_RO;
properties[len] = PreForm;
++len;
}
if (getKhmerCharClass(item->string[item->item.pos]) & CF_DOTTED_CIRCLE) {
reordered[len] = C_DOTTED_CIRCLE;
++len;
}
for (i = item->item.pos; i < syllableEnd; i += 1) {
HB_UChar16 uc = item->string[i];
KhmerCharClass charClass = getKhmerCharClass(uc);
if (charClass & CF_POS_BEFORE) {
continue;
}
if (i == coengRo) {
i += 1;
continue;
}
switch (charClass & CF_POS_MASK)
{
case CF_POS_ABOVE :
reordered[len] = uc;
properties[len] = AboveForm;
++len;
break;
case CF_POS_AFTER :
reordered[len] = uc;
properties[len] = PostForm;
++len;
break;
case CF_POS_BELOW :
reordered[len] = uc;
properties[len] = BelowForm;
++len;
break;
default:
if ( (charClass & CF_COENG) && i + 1 < syllableEnd ) {
unsigned char property = (getKhmerCharClass(item->string[i+1]) & CF_CLASS_MASK) == CC_CONSONANT3 ?
PostForm : BelowForm;
reordered[len] = uc;
properties[len] = property;
++len;
i += 1;
reordered[len] = item->string[i];
properties[len] = property;
++len;
break;
}
if ( (charClass & CF_SHIFTER) && (i + 1 < syllableEnd) ) {
if (getKhmerCharClass(item->string[i+1]) & CF_ABOVE_VOWEL ) {
reordered[len] = uc;
properties[len] = BelowForm;
++len;
break;
}
if (i + 2 < syllableEnd &&
(item->string[i+1] == C_VOWEL_AA) &&
(item->string[i+2] == C_SIGN_NIKAHIT) )
{
reordered[len] = uc;
properties[len] = BelowForm;
++len;
break;
}
if (i + 3 < syllableEnd && (getKhmerCharClass(item->string[i+3]) & CF_ABOVE_VOWEL) ) {
reordered[len] = uc;
properties[len] = BelowForm;
++len;
break;
}
if (i + 4 < syllableEnd &&
(item->string[i+3] == C_VOWEL_AA) &&
(item->string[i+4] == C_SIGN_NIKAHIT) )
{
reordered[len] = uc;
properties[len] = BelowForm;
++len;
break;
}
}
reordered[len] = uc;
++len;
break;
}
}
if (!item->font->klass->convertStringToGlyphIndices(item->font,
reordered, len,
item->glyphs, &item->num_glyphs,
item->item.bidiLevel % 2))
return FALSE;
KHDEBUG("after shaping: len=%d", len);
for (i = 0; i < len; i++) {
item->attributes[i].mark = FALSE;
item->attributes[i].clusterStart = FALSE;
item->attributes[i].justification = 0;
item->attributes[i].zeroWidth = FALSE;
KHDEBUG(" %d: %4x property=%x", i, reordered[i], properties[i]);
}
#ifndef NO_OPENTYPE
if (openType) {
hb_uint32 where[16];
for (i = 0; i < len; ++i) {
where[i] = ~(PreSubstProperty
| BelowSubstProperty
| AboveSubstProperty
| PostSubstProperty
| CligProperty
| PositioningProperties);
if (properties[i] == PreForm)
where[i] &= ~PreFormProperty;
else if (properties[i] == BelowForm)
where[i] &= ~BelowFormProperty;
else if (properties[i] == AboveForm)
where[i] &= ~AboveFormProperty;
else if (properties[i] == PostForm)
where[i] &= ~PostFormProperty;
}
HB_OpenTypeShape(item, where);
if (!HB_OpenTypePosition(item, availableGlyphs, FALSE))
return FALSE;
} else
#endif
{
KHDEBUG("Not using openType");
HB_HeuristicPosition(item);
}
item->attributes[0].clusterStart = TRUE;
return TRUE;
}
HB_Bool HB_KhmerShape(HB_ShaperItem *item)
{
HB_Bool openType = FALSE;
unsigned short *logClusters = item->log_clusters;
int i;
HB_ShaperItem syllable = *item;
int first_glyph = 0;
int sstart = item->item.pos;
int end = sstart + item->item.length;
assert(item->item.script == HB_Script_Khmer);
#ifndef NO_OPENTYPE
openType = HB_SelectScript(item, khmer_features);
#endif
KHDEBUG("khmer_shape: from %d length %d", item->item.pos, item->item.length);
while (sstart < end) {
HB_Bool invalid;
int send = khmer_nextSyllableBoundary(item->string, sstart, end, &invalid);
KHDEBUG("syllable from %d, length %d, invalid=%s", sstart, send-sstart,
invalid ? "TRUE" : "FALSE");
syllable.item.pos = sstart;
syllable.item.length = send-sstart;
syllable.glyphs = item->glyphs + first_glyph;
syllable.attributes = item->attributes + first_glyph;
syllable.offsets = item->offsets + first_glyph;
syllable.advances = item->advances + first_glyph;
syllable.num_glyphs = item->num_glyphs - first_glyph;
if (!khmer_shape_syllable(openType, &syllable)) {
KHDEBUG("syllable shaping failed, syllable requests %d glyphs", syllable.num_glyphs);
item->num_glyphs += syllable.num_glyphs;
return FALSE;
}
KHDEBUG("syllable:");
for (i = first_glyph; i < first_glyph + (int)syllable.num_glyphs; ++i)
KHDEBUG(" %d -> glyph %x", i, item->glyphs[i]);
KHDEBUG(" logclusters:");
for (i = sstart; i < send; ++i) {
KHDEBUG(" %d -> glyph %d", i, first_glyph);
logClusters[i-item->item.pos] = first_glyph;
}
sstart = send;
first_glyph += syllable.num_glyphs;
}
item->num_glyphs = first_glyph;
return TRUE;
}
void HB_KhmerAttributes(HB_Script script, const HB_UChar16 *text, hb_uint32 from, hb_uint32 len, HB_CharAttributes *attributes)
{
int end = from + len;
const HB_UChar16 *uc = text + from;
hb_uint32 i = 0;
HB_UNUSED(script);
attributes += from;
while ( i < len ) {
HB_Bool invalid;
hb_uint32 boundary = khmer_nextSyllableBoundary( text, from+i, end, &invalid ) - from;
attributes[i].charStop = TRUE;
if ( boundary > len-1 ) boundary = len;
i++;
while ( i < boundary ) {
attributes[i].charStop = FALSE;
++uc;
++i;
}
assert( i == boundary );
}
}