Add module for hyphenation

This commit is contained in:
Oleg Korshul
2023-08-23 22:11:00 +03:00
parent 1a95501cbb
commit b88b7db389
10 changed files with 372 additions and 29 deletions

View File

@ -1,6 +1,7 @@
#include "./TextMeasurerEmbed.h"
#include "./PointerEmbed.h"
#include "./../../fontengine/TextShaper.h"
#include "./../../fontengine/TextHyphen.h"
#define RAW_POINTER(value) ((CPointerEmbedObject*)value->toObject()->getNative())->Data
#define POINTER_OBJECT(value) ((CPointerEmbedObject*)value->toObject()->getNative())
@ -29,6 +30,17 @@ public:
}
};
CTextMeasurerEmbed::CTextMeasurerEmbed()
{
m_hyphen_engine = new NSHyphen::CEngine();
}
CTextMeasurerEmbed::~CTextMeasurerEmbed()
{
NSHyphen::CEngine* tmp = (NSHyphen::CEngine*)m_hyphen_engine;
delete tmp;
m_hyphen_engine = NULL;
}
JSSmart<CJSValue> CTextMeasurerEmbed::FT_Malloc(JSSmart<CJSValue> typed_array_or_len)
{
void* pData = NULL;
@ -192,3 +204,77 @@ JSSmart<CJSValue> CTextMeasurerEmbed::HB_FontFree(JSSmart<CJSValue> font)
return CJSContext::createUndefined();
}
#endif
JSSmart<CJSValue> CTextMeasurerEmbed::Hyphen_SetCacheSize(JSSmart<CJSValue> size)
{
((NSHyphen::CEngine*)m_hyphen_engine)->SetCacheSize(size->toInt32());
return CJSContext::createUndefined();
}
inline int GetUtf8SymbolLen(const unsigned char& c)
{
if (0x00 == (c & 0x80))
return 1;
else if (0x00 == (c & 0x20))
return 2;
else if (0x00 == (c & 0x10))
return 3;
else if (0x00 == (c & 0x0F))
return 4;
else if (0x00 == (c & 0x08))
return 4;
else if (0x00 == (c & 0x04))
return 5;
return 6;
}
JSSmart<CJSValue> CTextMeasurerEmbed::Hyphen_Word(JSSmart<CJSValue> lang, JSSmart<CJSValue> word)
{
std::string sWord = word->toStringA();
const char* curUnicode = sWord.c_str();
char* result = ((NSHyphen::CEngine*)m_hyphen_engine)->Process(lang->toInt32(), curUnicode, (int)sWord.length());
if (!result)
return CJSContext::createNull();
int nCount = 0;
char* tmp = result;
while (*tmp != 0)
{
if (1 == (*tmp & 1))
++nCount;
++tmp;
}
if (0 == nCount)
return CJSContext::createNull();
CJSArray* ret = CJSContext::createArray(nCount);
nCount = 0;
int pos = 0;
int posUnicode = 0;
int posUtf8 = 0;
while (result[pos] != 0)
{
if (1 == (result[pos] & 1))
{
while (posUtf8 <= pos)
{
++posUnicode;
posUtf8 += GetUtf8SymbolLen(curUnicode[posUtf8]);
}
ret->set(nCount++, CJSContext::createInt(posUnicode));
}
pos++;
}
return ret;
}
JSSmart<CJSValue> CTextMeasurerEmbed::Hyphen_IsDictionaryExist(JSSmart<CJSValue> lang)
{
return CJSContext::createBool(((NSHyphen::CEngine*)m_hyphen_engine)->IsDictionaryExist(lang->toInt32()));
}

View File

@ -10,13 +10,12 @@
using namespace NSJSBase;
class JS_DECL CTextMeasurerEmbed : public CJSEmbedObject
{
private:
void* m_hyphen_engine;
public:
CTextMeasurerEmbed()
{
}
~CTextMeasurerEmbed()
{
}
CTextMeasurerEmbed();
~CTextMeasurerEmbed();
public:
JSSmart<CJSValue> FT_Malloc(JSSmart<CJSValue> typed_array_or_len);
@ -50,6 +49,10 @@ public:
JSSmart<CJSValue> HB_FontFree(JSSmart<CJSValue> font);
#endif
JSSmart<CJSValue> Hyphen_SetCacheSize(JSSmart<CJSValue> size);
JSSmart<CJSValue> Hyphen_Word(JSSmart<CJSValue> lang, JSSmart<CJSValue> word);
JSSmart<CJSValue> Hyphen_IsDictionaryExist(JSSmart<CJSValue> lang);
DECLARE_EMBED_METHODS
};