JisCode.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #region Using ステートメント
  2. using System.Collections.Generic;
  3. using System.Text;
  4. #endregion
  5. namespace WpfFontPipeline
  6. {
  7. /// <summary>
  8. /// JISコードで定義されている文字を取得する為のヘルパークラス
  9. /// ここではJIS基本漢字(JIS X 0208)で定義されている文字を返す
  10. /// http://www.asahi-net.or.jp/~ax2s-kmtn/ref/jisx0208.html
  11. /// </summary>
  12. static class JisCode
  13. {
  14. #region パブリックメソッド
  15. /// <summary>
  16. /// 記号、特殊記号、148字の取得
  17. /// </summary>
  18. public static IEnumerable<char> GetSpecialCharacters()
  19. {
  20. var list = new List<char>(148);
  21. AddRow(1, list);
  22. AddJisCodes(0x2220, 0x222e, list);
  23. AddJisCodes(0x223a, 0x2241, list);
  24. AddJisCodes(0x224a, 0x2250, list);
  25. AddJisCodes(0x225c, 0x226a, list);
  26. AddJisCodes(0x2272, 0x2279, list);
  27. AddJisCodes(0x227e, 0x227e, list);
  28. return list;
  29. }
  30. /// <summary>
  31. /// 全角英数字、62字の取得
  32. /// </summary>
  33. public static IEnumerable<char> GetLatinLetters()
  34. {
  35. var list = new List<char>(62);
  36. AddJisCodes(0x2330, 0x2339, list);
  37. AddJisCodes(0x2341, 0x235a, list);
  38. AddJisCodes(0x2361, 0x237a, list);
  39. return list;
  40. }
  41. /// <summary>
  42. /// ひらがな、83字の取得
  43. /// </summary>
  44. public static IEnumerable<char> GetHiragana()
  45. {
  46. var list = new List<char>(83);
  47. AddJisCodes(0x2421, 0x2473, list);
  48. return list;
  49. }
  50. /// <summary>
  51. /// カタカナ、86字の取得
  52. /// </summary>
  53. public static IEnumerable<char> GetKatakana()
  54. {
  55. var list = new List<char>(86);
  56. AddJisCodes(0x2521, 0x2576, list);
  57. return list;
  58. }
  59. /// <summary>
  60. /// ギリシャ文字、48字の取得
  61. /// </summary>
  62. public static IEnumerable<char> GetGreekLetters()
  63. {
  64. var list = new List<char>(48);
  65. AddJisCodes(0x2621, 0x2638, list);
  66. AddJisCodes(0x2641, 0x2658, list);
  67. return list;
  68. }
  69. /// <summary>
  70. /// キリル文字、66字の取得
  71. /// </summary>
  72. public static IEnumerable<char> GetCyrillicLetters()
  73. {
  74. var list = new List<char>(66);
  75. AddJisCodes(0x2721, 0x2741, list);
  76. AddJisCodes(0x2751, 0x2771, list);
  77. return list;
  78. }
  79. /// <summary>
  80. /// 罫線文字、32字の取得
  81. /// </summary>
  82. public static IEnumerable<char> GetBoxDrawingCharacters()
  83. {
  84. var list = new List<char>(32);
  85. AddJisCodes(0x2821, 0x2840, list);
  86. return list;
  87. }
  88. /// <summary>
  89. /// 第1水準漢字、2,965字の取得
  90. /// </summary>
  91. public static IEnumerable<char> GetKanjiLevel1()
  92. {
  93. var list = new List<char>(2965);
  94. for (int row = 16; row <= 46; ++row)
  95. {
  96. AddRow(row, list);
  97. }
  98. AddJisCodes(0x4f21, 0x4f53, list);
  99. return list;
  100. }
  101. /// <summary>
  102. /// 第2水準漢字、3,990字の取得
  103. /// </summary>
  104. public static IEnumerable<char> GetKanjiLevel2()
  105. {
  106. var list = new List<char>(3390);
  107. for (int row = 48; row <= 83; ++row)
  108. {
  109. AddRow(row, list);
  110. }
  111. AddJisCodes(0x7421, 0x7426, list);
  112. return list;
  113. }
  114. #endregion
  115. #region プライベートメソッド
  116. /// <summary>
  117. /// 指定した区の文字取得
  118. /// </summary>
  119. /// <param name="row">区番号</param>
  120. /// <param name="list">出力先</param>
  121. private static void AddRow(int row, List<char> list)
  122. {
  123. int offset = 0x2000 + row * 0x100;
  124. AddJisCodes(offset + 0x21, offset + 0x7e, list);
  125. }
  126. /// <summary>
  127. /// 指定されたJISコード領域の文字取得
  128. /// </summary>
  129. /// <param name="start">開始JISコード</param>
  130. /// <param name="end">終了JISコード</param>
  131. /// <param name="list">出力先</param>
  132. private static void AddJisCodes(int start, int end, List<char> list)
  133. {
  134. int idx = 0;
  135. buffer[idx++] = 0x1b; // 2バイト文字コードへのエスケープシーケンス
  136. buffer[idx++] = 0x24;
  137. buffer[idx++] = 0x42;
  138. for (int jisCode = start; jisCode <= end; ++jisCode)
  139. {
  140. buffer[idx++] = (byte)((jisCode >> 8) & 0xff);
  141. buffer[idx++] = (byte)(jisCode & 0xff);
  142. }
  143. list.AddRange(encoding.GetChars(buffer, 0, idx));
  144. }
  145. #endregion
  146. #region プライベートフィールド
  147. // JISコードからUnicode変換用のEncoding
  148. static Encoding encoding = Encoding.GetEncoding("iso-2022-jp");
  149. // JISコード格納用バッファ、
  150. // ひとつの区内の96文字+エスケープシーケンス分のサイズ
  151. static byte[] buffer = new byte[16 * 6 * 2 + 3];
  152. #endregion
  153. }
  154. }