GdiUtils.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace WMFConverter.Gdi
  7. {
  8. /// <summary>
  9. /// Utils functions.
  10. /// </summary>
  11. public class GdiUtils
  12. {
  13. #region Local Variables
  14. private static int[,] FBA_SHIFT_JIS = new int[,] { { 0x81, 0x9F }, { 0xE0, 0xFC } };
  15. private static int[,] FBA_HANGUL_CHARSET = new int[,] { { 0x80, 0xFF } };
  16. private static int[,] FBA_JOHAB_CHARSET = new int[,] { { 0x80, 0xFF } };
  17. private static int[,] FBA_GB2312_CHARSET = new int[,] { { 0x80, 0xFF } };
  18. private static int[,] FBA_CHINESEBIG5_CHARSET = new int[,] { { 0xA1, 0xFE } };
  19. #endregion
  20. #region Public Methods
  21. /// <summary>
  22. /// Convert byte array to string using specified charset.
  23. /// </summary>
  24. /// <param name="chars"></param>
  25. /// <param name="charset"></param>
  26. /// <returns></returns>
  27. public static string ConvertString(byte[] chars, int charset)
  28. {
  29. string str = null;
  30. int length = 0;
  31. while (length < chars.Count() && chars[length] != 0)
  32. {
  33. length++;
  34. }
  35. try
  36. {
  37. str = System.Text.Encoding.GetEncoding(GetCharset(charset)).GetString(chars).Substring(0, length);
  38. }
  39. catch (Exception ex)
  40. {
  41. try
  42. {
  43. str = System.Text.Encoding.GetEncoding("US-ASCII").GetString(chars).Substring(0, length);
  44. }
  45. catch (Exception ex2)
  46. {
  47. throw new Exception(ex.Message+ex2.Message);
  48. }
  49. }
  50. return str;
  51. }
  52. /// <summary>
  53. /// Get specified charset.
  54. /// </summary>
  55. /// <param name="charset"></param>
  56. /// <returns></returns>
  57. public static string GetCharset(int charset)
  58. {
  59. switch (charset)
  60. {
  61. case (int)FontCharsetEnum.ANSI_CHARSET:
  62. return "Cp1252";
  63. case (int)FontCharsetEnum.SYMBOL_CHARSET:
  64. return "Cp1252";
  65. case (int)FontCharsetEnum.MAC_CHARSET:
  66. return "MacRoman";
  67. case (int)FontCharsetEnum.SHIFTJIS_CHARSET:
  68. return "MS932";
  69. case (int)FontCharsetEnum.HANGUL_CHARSET:
  70. return "MS949";
  71. case (int)FontCharsetEnum.JOHAB_CHARSET:
  72. return "Johab";
  73. case (int)FontCharsetEnum.GB2312_CHARSET:
  74. return "MS936";
  75. case (int)FontCharsetEnum.CHINESEBIG5_CHARSET:
  76. return "MS950";
  77. case (int)FontCharsetEnum.GREEK_CHARSET:
  78. return "Cp1253";
  79. case (int)FontCharsetEnum.TURKISH_CHARSET:
  80. return "Cp1254";
  81. case (int)FontCharsetEnum.VIETNAMESE_CHARSET:
  82. return "Cp1258";
  83. case (int)FontCharsetEnum.HEBREW_CHARSET:
  84. return "Cp1255";
  85. case (int)FontCharsetEnum.ARABIC_CHARSET:
  86. return "Cp1256";
  87. case (int)FontCharsetEnum.BALTIC_CHARSET:
  88. return "Cp1257";
  89. case (int)FontCharsetEnum.RUSSIAN_CHARSET:
  90. return "Cp1251";
  91. case (int)FontCharsetEnum.THAI_CHARSET:
  92. return "MS874";
  93. case (int)FontCharsetEnum.EASTEUROPE_CHARSET:
  94. return "Cp1250";
  95. case (int)FontCharsetEnum.OEM_CHARSET:
  96. return "Cp1252";
  97. default:
  98. return "Cp1252";
  99. }
  100. }
  101. /// <summary>
  102. /// Get language using specified charset.
  103. /// </summary>
  104. /// <param name="charset"></param>
  105. /// <returns></returns>
  106. public static string GetLanguage(int charset)
  107. {
  108. switch (charset)
  109. {
  110. case (int)FontCharsetEnum.ANSI_CHARSET:
  111. return "en";
  112. case (int)FontCharsetEnum.SYMBOL_CHARSET:
  113. return "en";
  114. case (int)FontCharsetEnum.MAC_CHARSET:
  115. return "en";
  116. case (int)FontCharsetEnum.SHIFTJIS_CHARSET:
  117. return "ja";
  118. case (int)FontCharsetEnum.HANGUL_CHARSET:
  119. return "ko";
  120. case (int)FontCharsetEnum.JOHAB_CHARSET:
  121. return "ko";
  122. case (int)FontCharsetEnum.GB2312_CHARSET:
  123. return "zh-CN";
  124. case (int)FontCharsetEnum.CHINESEBIG5_CHARSET:
  125. return "zh-TW";
  126. case (int)FontCharsetEnum.GREEK_CHARSET:
  127. return "el";
  128. case (int)FontCharsetEnum.TURKISH_CHARSET:
  129. return "tr";
  130. case (int)FontCharsetEnum.VIETNAMESE_CHARSET:
  131. return "vi";
  132. case (int)FontCharsetEnum.HEBREW_CHARSET:
  133. return "iw";
  134. case (int)FontCharsetEnum.ARABIC_CHARSET:
  135. return "ar";
  136. case (int)FontCharsetEnum.BALTIC_CHARSET:
  137. return "bat";
  138. case (int)FontCharsetEnum.RUSSIAN_CHARSET:
  139. return "ru";
  140. case (int)FontCharsetEnum.THAI_CHARSET:
  141. return "th";
  142. case (int)FontCharsetEnum.EASTEUROPE_CHARSET:
  143. return null;
  144. case (int)FontCharsetEnum.OEM_CHARSET:
  145. return null;
  146. default:
  147. return null;
  148. }
  149. }
  150. /// <summary>
  151. /// Get the first byte area specified charset.
  152. /// </summary>
  153. /// <param name="charset"></param>
  154. /// <returns></returns>
  155. public static int[,] GetFirstByteArea(int charset)
  156. {
  157. switch (charset)
  158. {
  159. case (int)FontCharsetEnum.SHIFTJIS_CHARSET:
  160. return FBA_SHIFT_JIS;
  161. case (int)FontCharsetEnum.HANGUL_CHARSET:
  162. return FBA_HANGUL_CHARSET;
  163. case (int)FontCharsetEnum.JOHAB_CHARSET:
  164. return FBA_JOHAB_CHARSET;
  165. case (int)FontCharsetEnum.GB2312_CHARSET:
  166. return FBA_GB2312_CHARSET;
  167. case (int)FontCharsetEnum.CHINESEBIG5_CHARSET:
  168. return FBA_CHINESEBIG5_CHARSET;
  169. default:
  170. return null;
  171. }
  172. }
  173. #endregion
  174. }
  175. }