PAText UTF8

From Qt Wiki
Jump to navigation Jump to search
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.


PAText (UTF-8 Version)

Pointing to the previous multi-byte version (link) the new version comes with improvements and better functionality.

Changes

2.0.0

  • Process UTF-8 encoded bytes
  • Changed comparison method from "byte to byte" to "byte(s) to table", so even translated chars would be reformatted
  • Added isolator function which replaces ligature words
  • Fixed the character "ی" problems with Persian layout
  • No longer in QPlugs namespace

2.1.0

  • Added another Persian 'ی' character to the map table
  • Added "detranslate()" function for a reverse action

Source Code

"patext.h"

/*''''''
 PAText (UTF-8) by Mohsen Kh.Akhlaghi
 Ver 2.1.0 Release 2013/01/26
 www.madagon.com
*/
#ifndef PAText_H
#define PAText_H
#include <QString>

class PAText
{
public:
 static QString translate(QByteArray pText);
 static QString detranslate(QByteArray pText);
};

#endif // PAText_H

"patext.cpp"

/'''
 PAText (UTF-8) by Mohsen Kh.Akhlaghi
 Ver 2.1.0 Release 2013/01/26
 www.madagon.com
''''''*/
#include "PAText.h"
#include <QList>

static const int mapsize=42;
static ushort PAMap[42][5]=
{ //Base Final Initial Medial Isolated
 {0x630, 0xfeac, 0xfeab, 0xfeac, 0xfeab}, //ذ 0
 {0x62f, 0xfeaa, 0xfea9, 0xfeaa, 0xfea9}, //د 1
 {0x62c, 0xfe9e, 0xfe9f, 0xfea0, 0xfe9d}, //ج 2
 {0x62d, 0xfea2, 0xfea3, 0xfea4, 0xfea1}, //ح 3
 {0x62e, 0xfea6, 0xfea7, 0xfea8, 0xfea5}, //خ 4
 {0x647, 0xfeea, 0xfeeb, 0xfeec, 0xfee9}, //ه 5
 {0x639, 0xfeca, 0xfecb, 0xfecc, 0xfec9}, //ع 6
 {0x63a, 0xfece, 0xfecf, 0xfed0, 0xfecd}, //غ 7
 {0x641, 0xfed2, 0xfed3, 0xfed4, 0xfed1}, //ف 8
 {0x642, 0xfed6, 0xfed7, 0xfed8, 0xfed5}, //ق 9
 {0x62b, 0xfe9a, 0xfe9b, 0xfe9c, 0xfe99}, //ث 10
 {0x635, 0xfeba, 0xfebb, 0xfebc, 0xfeb9}, //ص 11
 {0x636, 0xfebe, 0xfebf, 0xfec0, 0xfebd}, //ض 12
 {0x637, 0xfec2, 0xfec3, 0xfec4, 0xfec1}, //ط 13
 {0x643, 0xfeda, 0xfedb, 0xfedc, 0xfed9}, //ك 14
 {0x645, 0xfee2, 0xfee3, 0xfee4, 0xfee1}, //م 15
 {0x646, 0xfee6, 0xfee7, 0xfee8, 0xfee5}, //ن 16
 {0x62a, 0xfe96, 0xfe97, 0xfe98, 0xfe95}, //ت 17
 {0x627, 0xfe8e, 0xfe8d, 0xfe8e, 0xfe8d}, //ا 18
 {0x644, 0xfede, 0xfedf, 0xfee0, 0xfedd}, //ل 19
 {0x628, 0xfe90, 0xfe91, 0xfe92, 0xfe8f}, //ب 20
 {0x64a, 0xfef2, 0xfef3, 0xfef4, 0xfef1}, //21 ي
 {0x633, 0xfeb2, 0xfeb3, 0xfeb4, 0xfeb1}, //س 22
 {0x634, 0xfeb6, 0xfeb7, 0xfeb8, 0xfeb5}, //ش 23
 {0x638, 0xfec6, 0xfec7, 0xfec8, 0xfec5}, //ظ 24
 {0x632, 0xfeb0, 0xfeaf, 0xfeb0, 0xfeaf}, //ز 25
 {0x625, 0xfe88, 0xfe87, 0xfe88, 0xfe87}, //إ 26
 {0x6A9, 0xfb8f, 0xfb90, 0xfb91, 0xfb8e}, //ک 27
 {0x6Af, 0xfb93, 0xfb94, 0xfb95, 0xfb92}, //گ 28
 {0x698, 0xfb8b, 0xfb8a, 0xfb8b, 0xfb8a}, //ژ 29
 {0x686, 0xfb7b, 0xfb7c, 0xfb7d, 0xfb7a}, //چ 30
 {0x67E, 0xfb57, 0xfb58, 0xfb59, 0xfb56}, //پ 31
 {0x648, 0xfeee, 0xfeed, 0xfeee, 0xfeed}, //و 32
 {0x629, 0xfe94, 0xfe93, 0xfe93, 0xfe93}, //ة 33
 {0x649, 0xfef0, 0xfef3, 0xfef4, 0xfeef}, //ى 34
 {0x631, 0xfeae, 0xfead, 0xfeae, 0xfead}, //ر 35
 {0x624, 0xfe86, 0xfe85, 0xfe86, 0xfe85}, //ؤ 36
 {0x621, 0xfe80, 0xfe80, 0xfe80, 0xfe7f}, //ء 37
 {0x626, 0xfe8a, 0xfe8b, 0xfe8c, 0xfe89}, //ئ 38
 {0x623, 0xfe84, 0xfe83, 0xfe84, 0xfe83}, //أ 39
 {0x622, 0xfe82, 0xfe81, 0xfe82, 0xfe81}, //آ 40
 {0x6cc, 0xfbfd, 0xfbfe, 0xfbff, 0xfbfc}, //ى 41
};

static int charInMap(QCharRef &pChar)
{
 int j;
 for (int i=0;i<mapsize;i+''){
 for (j=0;j<5;j){
 if (pChar==PAMap[i][j]) return i;
 }
 }

 return 1;
}

static bool charC(QCharRef &pChar)
{
 static const int sp_sz=13;
 static int sp[]={0,1,18,25,26,29,32,33,35,36,37,39,40};

 int j;

 for (int i=0;i<mapsize;i){
 for (j=0;j<5;j){
 if (pCharPAMap[i][j]){
                for (j=0;j<sp_sz;j++) if (sp[j]i) return true;
 return false;
 }
 }
 }

 return true;
}

static QString &isolate(QString &str, bool reverse=false)
{
 static QString find[]={
 QString().append(PAMap[19][2]).append(PAMap[18][1]),//ﻻ
 QString().append(PAMap[19][3]).append(PAMap[18][1]),//ﻼ
 QString().append(PAMap[19][2]).append(PAMap[26][1]),//ﻹ
 QString().append(PAMap[19][3]).append(PAMap[26][1]),//ﻺ
 QString().append(PAMap[19][2]).append(PAMap[39][1]),//ﻷ
 QString().append(PAMap[19][3]).append(PAMap[39][1]),//ﻸ
 QString().append(PAMap[18][0]).append(PAMap[19][2]).append(PAMap[19][3]).append(PAMap[5][1]) //ﷲ
 };

 static QString replace[]={
 QString(0xfefb),
 QString(0xfefc),
 QString(0xfef9),
 QString(0xfefa),
 QString(0xfef7),
 QString(0xfef8),
 QString(0xfdf2)
 };

 static const int sz=7;

 if (!reverse){
 for (int i=0; i<sz; i) str.replace(find[i],replace[i]);
 } else{
 for (int i=0; i<sz; i) str.replace(replace[i],find[i]);
 }

 return str;
}

QString PAText::translate(QByteArray pText)
{
 QString text=isolate(QString::fromUtf8(pText),true);
 int sz=text.size();
 QString r;
 int cpoint;
 QList<int> cursor;

 for (int i=0; i<sz; i){
 cpoint=charInMap(text[i]);

 cursor << cpoint;
 if (cpoint<0){
 r.append(text[i]);
 continue;
 }

 if (i==0){
 r.append(PAMap[cpoint][0]);
 } else{
 QCharRef prev=text[i-1];
 if (charC(prev)){ //prev special
 r.append(PAMap[cpoint][0]);
 } else{
 if (cursor[i-1]>0){
 if (i>1){
 QCharRef pprev=text[i-2];
 if (charC(pprev)){ //pprev special
 r[i-1]=PAMap[cursor[i-1]][2];
 } else{
 r[i-1]=PAMap[cursor[i-1]][3];
 }
 } else{
 r[i-1]=PAMap[cursor[i-1]][2];
 }

 r.append(PAMap[cpoint][1]);
 } else{
 r.append(PAMap[cpoint][0]);
 }

 }
 }
 }

 return isolate®;
}

QString PAText::detranslate(QByteArray pText)
{
 QString text=isolate(QString::fromUtf8(pText),true);
 QString r;
 int sz=text.size();
 int cpoint;

 for (int i=0; i<sz; i''+){
 cpoint=charInMap(text[i]);

if (cpoint<0){
 r.append(text[i]);
 continue;
 }

r.append(PAMap[cpoint][4]);
 }

return r;
}

Sample Code

  1. include "patext.h"

. . qDebug() << PAText::translate("سلام");