ISO2022JPEncoder.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { encoderError } from "../../encoding/encodings";
  2. import { finished } from "../../encoding/finished";
  3. import { index, indexPointerFor } from "../../encoding/indexes";
  4. import { end_of_stream, isASCIICodePoint } from "../../encoding/terminology";
  5. var states;
  6. (function (states) {
  7. states[states["ASCII"] = 0] = "ASCII";
  8. states[states["Roman"] = 1] = "Roman";
  9. states[states["jis0208"] = 2] = "jis0208";
  10. })(states || (states = {}));
  11. /**
  12. * @constructor
  13. * @implements {Encoder}
  14. * @param {{fatal: boolean}} options
  15. */
  16. export class ISO2022JPEncoder {
  17. constructor(options) {
  18. this.fatal = options.fatal;
  19. // iso-2022-jp's encoder has an associated iso-2022-jp encoder
  20. // state which is one of ASCII, Roman, and jis0208 (initially
  21. // ASCII).
  22. /** @type {number} */ this.iso2022jp_state = states.ASCII;
  23. }
  24. /**
  25. * @param {Stream} stream Input stream.
  26. * @param {number} code_point Next code point read from the stream.
  27. * @return {(number|!Array.<number>)} Byte(s) to emit.
  28. */
  29. handler(stream, code_point) {
  30. // 1. If code point is end-of-stream and iso-2022-jp encoder
  31. // state is not ASCII, prepend code point to stream, set
  32. // iso-2022-jp encoder state to ASCII, and return three bytes
  33. // 0x1B 0x28 0x42.
  34. if (code_point === end_of_stream &&
  35. this.iso2022jp_state !== states.ASCII) {
  36. stream.prepend(code_point);
  37. this.iso2022jp_state = states.ASCII;
  38. return [0x1B, 0x28, 0x42];
  39. }
  40. // 2. If code point is end-of-stream and iso-2022-jp encoder
  41. // state is ASCII, return finished.
  42. if (code_point === end_of_stream && this.iso2022jp_state === states.ASCII)
  43. return finished;
  44. // 3. If ISO-2022-JP encoder state is ASCII or Roman, and code
  45. // point is U+000E, U+000F, or U+001B, return error with U+FFFD.
  46. if ((this.iso2022jp_state === states.ASCII ||
  47. this.iso2022jp_state === states.Roman) &&
  48. (code_point === 0x000E || code_point === 0x000F ||
  49. code_point === 0x001B)) {
  50. return encoderError(0xFFFD);
  51. }
  52. // 4. If iso-2022-jp encoder state is ASCII and code point is an
  53. // ASCII code point, return a byte whose value is code point.
  54. if (this.iso2022jp_state === states.ASCII &&
  55. isASCIICodePoint(code_point))
  56. return code_point;
  57. // 5. If iso-2022-jp encoder state is Roman and code point is an
  58. // ASCII code point, excluding U+005C and U+007E, or is U+00A5
  59. // or U+203E, run these substeps:
  60. if (this.iso2022jp_state === states.Roman &&
  61. ((isASCIICodePoint(code_point) &&
  62. code_point !== 0x005C && code_point !== 0x007E) ||
  63. (code_point == 0x00A5 || code_point == 0x203E))) {
  64. // 1. If code point is an ASCII code point, return a byte
  65. // whose value is code point.
  66. if (isASCIICodePoint(code_point))
  67. return code_point;
  68. // 2. If code point is U+00A5, return byte 0x5C.
  69. if (code_point === 0x00A5)
  70. return 0x5C;
  71. // 3. If code point is U+203E, return byte 0x7E.
  72. if (code_point === 0x203E)
  73. return 0x7E;
  74. }
  75. // 6. If code point is an ASCII code point, and iso-2022-jp
  76. // encoder state is not ASCII, prepend code point to stream, set
  77. // iso-2022-jp encoder state to ASCII, and return three bytes
  78. // 0x1B 0x28 0x42.
  79. if (isASCIICodePoint(code_point) &&
  80. this.iso2022jp_state !== states.ASCII) {
  81. stream.prepend(code_point);
  82. this.iso2022jp_state = states.ASCII;
  83. return [0x1B, 0x28, 0x42];
  84. }
  85. // 7. If code point is either U+00A5 or U+203E, and iso-2022-jp
  86. // encoder state is not Roman, prepend code point to stream, set
  87. // iso-2022-jp encoder state to Roman, and return three bytes
  88. // 0x1B 0x28 0x4A.
  89. if ((code_point === 0x00A5 || code_point === 0x203E) &&
  90. this.iso2022jp_state !== states.Roman) {
  91. stream.prepend(code_point);
  92. this.iso2022jp_state = states.Roman;
  93. return [0x1B, 0x28, 0x4A];
  94. }
  95. // 8. If code point is U+2212, set it to U+FF0D.
  96. if (code_point === 0x2212)
  97. code_point = 0xFF0D;
  98. // 9. Let pointer be the index pointer for code point in index
  99. // jis0208.
  100. const pointer = indexPointerFor(code_point, index('jis0208'));
  101. // 10. If pointer is null, return error with code point.
  102. if (pointer === null)
  103. return encoderError(code_point);
  104. // 11. If iso-2022-jp encoder state is not jis0208, prepend code
  105. // point to stream, set iso-2022-jp encoder state to jis0208,
  106. // and return three bytes 0x1B 0x24 0x42.
  107. if (this.iso2022jp_state !== states.jis0208) {
  108. stream.prepend(code_point);
  109. this.iso2022jp_state = states.jis0208;
  110. return [0x1B, 0x24, 0x42];
  111. }
  112. // 12. Let lead be Math.floor(pointer / 94) + 0x21.
  113. const lead = Math.floor(pointer / 94) + 0x21;
  114. // 13. Let trail be pointer % 94 + 0x21.
  115. const trail = pointer % 94 + 0x21;
  116. // 14. Return two bytes whose values are lead and trail.
  117. return [lead, trail];
  118. }
  119. }
  120. //# sourceMappingURL=ISO2022JPEncoder.js.map