private int GetnChar(FontIndex index) { switch (index) { case FontIndex.iUTF: return(3); case FontIndex.iUNI: case FontIndex.iUTH: case FontIndex.iVWF: case FontIndex.iNOSIGN: case FontIndex.iVISCII: case FontIndex.iVPS: case FontIndex.iBKHCM1: return(1); case FontIndex.iNCR: return(7); case FontIndex.iNCRHex: return(8); case FontIndex.iCString: return(6); default: return(2); } }
/// <summary> /// Lấy ký hiệu nhận dạng mã theo kiểu số /// </summary> /// <param name="code"></param> /// <returns></returns> /// <summary> /// Kiểm tra một chuỗi có phải tiếng việt không. /// Dùng nếu cần quan tâm mã của chuỗi là gì /// </summary> /// <param name="s">set string to work</param> /// <param name="code">get vietnamese int code if string is vietnamese</param> /// <returns></returns> public bool isVietnamese(String s, ref FontIndex code) { code = getCode(s); if (code > FontIndex.iNotKnown) { return(true); } return(false); }
protected internal bool AddFont(string alias, string filePath, bool localToApp) { System.IO.FileInfo file; file = localToApp ? System.Reflection.Assembly.GetExecutingAssembly().GetAppFile(filePath) : new System.IO.FileInfo(filePath); if (!file.Exists) { return(false); } FontIndex.AddFamily(file, alias); return(true); }
private int GetnChar(FontIndex index) { switch (index) { case FontIndex.iUTF: return(3); case FontIndex.iUNI: //case FontIndex.iUTH: case FontIndex.iVWF: case FontIndex.iNOSIGN: case FontIndex.iVISCII: case FontIndex.iVPS: case FontIndex.iBKHCM1: return(1); case FontIndex.iNCR: return(7); case FontIndex.iNCRHex: return(8); case FontIndex.iCString: return(6); default: return(2); } //if (index == FontIndex.iUTF) //{ // return 3; //} //else if (index == FontIndex.iUNI || index == FontIndex.iUTH || // index == FontIndex.iVWF || index == FontIndex.iNOSIGN ) //{ // return 1; //} //else if (index == FontIndex.iNCR) //{ // return 7; //} //else //{ // return 2; //} }
/* * * /// <summary> * /// Với những mã mà 1 ký tự bên mã gốc chỉ tương ứng với 1 ký tự bên mã đích thì dùng cách replace là nhanh nhất * /// </summary> * private bool ConvertByReplace(ref string strConv, int iSrc, int iDest) * { * if (string.IsNullOrEmpty(strConv)) * { * return false; * } * StringBuilder strBulder = new StringBuilder(strConv); * for (int i = 1; i < countVnChar; i++) * { * strBulder.Replace(Code[i, iSrc], Code[i, iDest]); * } * strConv = strBulder.ToString(); * return true; * } * * private bool CanReplace(int iCode) * { * for (int i = 0; i < arrCorresponseCode.Length; i++) * { * if (iCode == arrCorresponseCode[i]) * { * return true; * } * } * return false; * } * * private void ConvertTCVNToUNI(ref string strConv) * { * return ConvertByReplace(ref strConv, iTCV, iUNI); * } * * private void ConvertUNIToTCVN(ref string strConv) * { * return ConvertByReplace(ref strConv, iUNI, iTCV); * } * * private void ConvertUNIToUTH(ref string strConv) * { * return ConvertByReplace(ref strConv, iUNI, iUTH); * } * * private void ConvertUTHToUNI(ref string strConv) * { * return ConvertByReplace(ref strConv, iUTH, iUNI); * } * * private void ConvertUTHToUNI(ref string strConv) * { * return ConvertByReplace(ref strConv, iUTH, iUNI); * } */ #endregion #region Public method /// <summary> /// Chuyển mã một chuỗi văn bản tiếng việt với một số mã thông dụng /// </summary> /// <param name="strConv"> /// Chuỗi văn bản tiếng việt cần chuyển đổi /// </param> /// <param name="iSrc"> /// Mã gốc (int) của chuỗi tiếng việt đưa vào. Các mã thuộc struct FontIndex /// </param> /// <param name="iDest"> /// Mã đích (int) của chuỗi tiếng việt đưa vào. Các mã thuộc struct FontIndex /// </param> /// <returns>Nếu có thực hiện chuyển đổi thì return true, ngược lại return false</returns> public bool Convert(ref String strConv, FontIndex iSource, FontIndex iDestination) { int iSrc = (int)iSource; int iDest = (int)iDestination; if (strConv.Trim() == "") { return(false); } if (iSrc == iDest) { return(false); } String s = "", s1 = ""; if (iSrc == (int)FontIndex.iNotKnown) //Nếu không tìm thấy tên mã đưa vào { //Tự động tìm mã vào dựa theo các ký tự dấu tiếng việt int intcode = (int)getCode(strConv); if (intcode > -1) { iSrc = intcode; } else { return(false); } } //Mac dinh ma xuat ra la Unicode if (iDest == -1) { iDest = 0; } ////Xét xem có thể dùng cách replace cho các mã này không? //if (CanReplace(iSrc) && CanReplace(iDest)) //{ // return ConvertByReplace(ref strConv, iSrc, iDest); //} //Một số mã, 1 ký tự có dấu có thể > 1 ký tự ASNSI //mChar là minimun char number, nChar là Maximun char number trong 1 bảng mã //Ví dụ: Mã TCVN có thể có 1, hoặc 2 ký tự, khi đó mChar=1, nChar=2 //Lấy số ký tự lớn nhất có thể tổ hợp cho 1 ký tự có dấu trong mã nguồn int nChar = GetnChar((FontIndex)int.Parse(Code[0, iSrc])); int mChar = nChar > 1 ? nChar - 1 : 1; //CHuỗi chứa kết quả sau khi chuyển đổi StringBuilder sResult = new StringBuilder(); sResult.Append(""); //Cho biết có sự chuyển đổi xảy ra trên chuỗi đưa vào không. bool hasconvert = false; //Mã VietwareF có ký tự "̣" = "ò" và "́" = "ì". Ta chuyển nó về chung 1 kiểu để tiện chuyển mã if (iSource == FontIndex.iVWF) { StringBuilder strBd = new StringBuilder(strConv); strBd.Replace("̣", "ò").Replace("́", "ì"); strConv = strBd.ToString(); } int Len = strConv.Length; //Thêm vào 7 ký tự trắng vào chuỗi cần chuyển đổi //Vì mã NCR Decimal có thể tổ hợp 7 ký tự cho 1 ký tự có dấu strConv = strConv.PadRight(Len + countVnChar, ' ');//THêm vào để tránh lỗi khi xử lý chuỗi Len = strConv.Length; //Khai báo biến chứa chuỗi làm việc int i = 0; int j, k; string tmp = ""; bool IsFirstCharOfWord = false; //Bat dau chuyen ma while (i < Len - countVnChar) { #region Kiểm tra xem ký tự đầu tiên có phải là ký tự đầu tiên của 1 từ không //Trường hợp này chỉ chuyển ký tự đầu tiên của chuỗi //hoặc những ký tự mà trước nó là khoảng trắng, hoặc tab (ký tự đầu tiên mỗi từ //thành chữ hoa if (m_CharCase == FontCase.UpperCaseFirstChar) { if (i == 0) { IsFirstCharOfWord = true; } else if (i > 0) { tmp = strConv.Substring(i - 1, 1); if (SplitFirstChar.Contains(tmp)) { IsFirstCharOfWord = true; } else { IsFirstCharOfWord = false; } } } #endregion //De phong cac ma to hop (mot ky co dau=2->3 ky tu khong dau) //nChar, mChar thể hiện sự giao động số ký tự trong 1 bảng mã. for (j = nChar; j >= mChar; j--) { s1 = ""; // Luu giu ma kt dich sau khi chuyen if (strConv.Substring(i, 1).Equals(" ")) //Nếu hiện tại là khoảng trắng thì chắc chắn ko có mã nào rồi { s = " "; break; } s = strConv.Substring(i, j); //Lay ky tu hien tai trong chuoi //Vong lap tim ma cac kt tu co dau for (k = 1; k < countVnChar; k++) //duyet qua ma nguon, tim vi tri kt trong ma { if (s == Code[k, iSrc]) //neu thay { //Giải quyết trường hợp chuyển sang hoa thường if (m_CharCase == FontCase.UpperCase && k < DISTANCE_CASE + 1) { //Nếu ký tự hiện tại là chữ thường mới xét s1 = Code[k + DISTANCE_CASE, iDest]; } else if (m_CharCase == FontCase.LowerCase && k >= DISTANCE_CASE + 1) { s1 = Code[k - DISTANCE_CASE, iDest]; } else if (m_CharCase == FontCase.UpperCaseFirstChar) { if (IsFirstCharOfWord && k < DISTANCE_CASE + 1) { s1 = Code[k + DISTANCE_CASE, iDest]; } else if (!IsFirstCharOfWord && k >= DISTANCE_CASE + 1) { s1 = Code[k - DISTANCE_CASE, iDest]; } else { s1 = Code[k, iDest]; } } else { s1 = Code[k, iDest]; //Lưu ký tự tương ứng trong mã đích, trường hợp không xét hoa thường; } i += j; //Tang i tuy theo truong hop cua j break; } } //Vì j chỉ > 3 khi mã là NCR Decimal, mà mã NCR Decimal chỉ có thể là 6, hoặc 7 ký ự //Tức là trường hợp j = 5 sẽ không xảy ra //Khi đó, ta chắc chắn mã hiện tại là NCR Decimal và trường hợp j=5 không bao giờ xảy ra //==> ta không cần cho j giảm nữa, để tối ưu về thời gian if ((!s1.Equals("")) || (j == 5)) { break; //neu tim thay thi thoi lap } } //s1 chứa ký tự có dấu của mã đích tìm được //Nếu s1 khác "" nghĩa là có sự chuyển đổi if (!s1.Equals("")) { sResult.Append(s1);//dua ky tu chuyen duoc vao chuoi ket qua hasconvert = true; } else { if (m_CharCase == FontCase.Normal) { sResult.Append(s.Substring(0, 1)); } else { if (m_CharCase == FontCase.UpperCase) { sResult.Append(s.Substring(0, 1).ToUpper());//Đưa ký tự không dấu (không được chuyển đổi) vào chuỗi kết quả } else if (m_CharCase == FontCase.LowerCase) { sResult.Append(s.Substring(0, 1).ToLower());//Đưa ký tự không dấu (không được chuyển đổi) vào chuỗi kết quả } else if (m_CharCase == FontCase.UpperCaseFirstChar) { if (IsFirstCharOfWord) { sResult.Append(s.Substring(0, 1).ToUpper()); } else { sResult.Append(s.Substring(0, 1).ToLower()); } } } //Trường hợp có sự chuyển đổi, i đã được tăng ở trên //Trường hợp không có sự chuyển đổi ở ký tự hiện tại thì tăng i để duyệt ký tự kế tiếp i++; } } //Nếu không có sự chuyển đổi nào xảy ra if (!hasconvert) { //Loại bỏ 7 khoảng trắng đã thêm vào ban đầu strConv = strConv.Remove(Len - countVnChar, countVnChar); if (m_CharCase == FontCase.UpperCase) { strConv = strConv.ToUpper(); return(true); } else if (m_CharCase == FontCase.LowerCase) { strConv = strConv.ToLower(); return(true); } else if (m_CharCase == FontCase.UpperCaseFirstChar) { if (strConv.Length > 1) { strConv = strConv.Substring(0, 1).ToUpper() + strConv.Substring(1); } else if (strConv.Length == 1) { strConv = strConv.Substring(0, 1).ToUpper(); } } return(false); } else { strConv = sResult.ToString(); return(true); } }
public override int GetHashCode() { return(FontIndex.GetHashCode() ^ FillIndex.GetHashCode()); }
public bool Convert(ref string strConv, FontIndex iSource, FontIndex iDestination) { int num = (int) iSource; int num2 = (int) iDestination; if (strConv.Trim() == "") { return false; } if (num == num2) { return false; } string str = ""; string str2 = ""; if (num == -1) { int num3 = (int) this.getCode(strConv); if (num3 <= -1) { return false; } num = num3; } if (num2 == -1) { num2 = 0; } int nChar = this.GetnChar((FontIndex) int.Parse(this.Code[0, num])); int num5 = (nChar > 1) ? (nChar - 1) : 1; string str3 = ""; bool flag = false; strConv = strConv + " "; int startIndex = 0; while (startIndex < (strConv.Length - 7)) { for (int i = nChar; i >= num5; i--) { str2 = ""; if (strConv.Substring(startIndex, 1) == " ") { str = " "; break; } str = strConv.Substring(startIndex, i); for (int j = 1; j < 0x87; j++) { if (str == this.Code[j, num]) { if ((this.m_CharCase == FontCase.UpperCase) && (j < 0x44)) { str2 = this.Code[j + 0x43, num2]; } else if ((this.m_CharCase == FontCase.LowerCase) && (j >= 0x44)) { str2 = this.Code[j - 0x43, num2]; } else { str2 = this.Code[j, num2]; } startIndex += i; break; } } if ((str2 != "") || (i == 5)) { break; } } if (str2 != "") { str3 = str3 + str2; flag = true; } else { if (this.m_CharCase == FontCase.UpperCase) { str3 = str3 + str.Substring(0, 1).ToUpper(); } else if (this.m_CharCase == FontCase.LowerCase) { str3 = str3 + str.Substring(0, 1).ToLower(); } else { str3 = str3 + str.Substring(0, 1); } startIndex++; } } if (!flag) { strConv = strConv.Remove(strConv.Length - 7, 7); if (this.m_CharCase == FontCase.UpperCase) { strConv = strConv.ToUpper(); return true; } if (this.m_CharCase == FontCase.LowerCase) { strConv = strConv.ToLower(); return true; } return false; } strConv = str3.TrimEnd(new char[0]); return true; }
private int GetnChar(FontIndex index) { if (index == FontIndex.iUTF) { return 3; } if (((index == FontIndex.iUNI) || (index == FontIndex.iUTH)) || (index == FontIndex.iNOSIGN)) { return 1; } if (index == FontIndex.iNCR) { return 7; } return 2; }
public bool isVietnamese(string s, ref FontIndex code) { code = this.getCode(s); return (code > FontIndex.iNotKnown); }
public bool Convert(ref string strConv, FontIndex iSource, FontIndex iDestination) { int num = (int)iSource; int num2 = (int)iDestination; bool result; if (strConv.Trim() == "") { result = false; } else { if (num == num2) { result = false; } else { string text = ""; string text2 = ""; if (num == -1) { int code = (int)this.getCode(strConv); if (code <= -1) { result = false; return result; } num = code; } if (num2 == -1) { num2 = 0; } int num3 = this.GetnChar((FontIndex)int.Parse(this.Code[0, num])); int num4 = (num3 > 1) ? (num3 - 1) : 1; string text3 = ""; bool flag = false; strConv += " "; int i = 0; while (i < strConv.Length - 7) { for (int j = num3; j >= num4; j--) { text2 = ""; if (strConv.Substring(i, 1) == " ") { text = " "; break; } text = strConv.Substring(i, j); for (int k = 1; k < 135; k++) { if (text == this.Code[k, num]) { if (this.m_CharCase == FontCase.UpperCase && k < 68) { text2 = this.Code[k + 67, num2]; } else { if (this.m_CharCase == FontCase.LowerCase && k >= 68) { text2 = this.Code[k - 67, num2]; } else { text2 = this.Code[k, num2]; } } i += j; break; } } if (text2 != "" || j == 5) { break; } } if (text2 != "") { text3 += text2; flag = true; } else { if (this.m_CharCase == FontCase.UpperCase) { text3 += text.Substring(0, 1).ToUpper(); } else { if (this.m_CharCase == FontCase.LowerCase) { text3 += text.Substring(0, 1).ToLower(); } else { text3 += text.Substring(0, 1); } } i++; } } if (!flag) { strConv = strConv.Remove(strConv.Length - 7, 7); if (this.m_CharCase == FontCase.UpperCase) { strConv = strConv.ToUpper(); result = true; } else { if (this.m_CharCase == FontCase.LowerCase) { strConv = strConv.ToLower(); result = true; } else { result = false; } } } else { strConv = text3.TrimEnd(new char[0]); result = true; } } } return result; }
private int GetnChar(FontIndex index) { int result; if (index == FontIndex.iUTF) { result = 3; } else { if (index == FontIndex.iUNI || index == FontIndex.iUTH || index == FontIndex.iNOSIGN) { result = 1; } else { if (index == FontIndex.iNCR) { result = 7; } else { result = 2; } } } return result; }
public ConvertBase(FontIndex ToFontCode) { toCode = (FontIndex)ToFontCode; InitData(); }