EUCJPEncoder.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. import { inRange } from "../../encoding/utilities";
  6. /**
  7. * @constructor
  8. * @implements {Encoder}
  9. * @param {{fatal: boolean}} options
  10. */
  11. var EUCJPEncoder = /** @class */ (function () {
  12. function EUCJPEncoder(options) {
  13. this.fatal = options.fatal;
  14. }
  15. /**
  16. * @param {Stream} stream Input stream.
  17. * @param {number} code_point Next code point read from the stream.
  18. * @return {(number|!Array.<number>)} Byte(s) to emit.
  19. */
  20. EUCJPEncoder.prototype.handler = function (stream, code_point) {
  21. // 1. If code point is end-of-stream, return finished.
  22. if (code_point === end_of_stream)
  23. return finished;
  24. // 2. If code point is an ASCII code point, return a byte whose
  25. // value is code point.
  26. if (isASCIICodePoint(code_point))
  27. return code_point;
  28. // 3. If code point is U+00A5, return byte 0x5C.
  29. if (code_point === 0x00A5)
  30. return 0x5C;
  31. // 4. If code point is U+203E, return byte 0x7E.
  32. if (code_point === 0x203E)
  33. return 0x7E;
  34. // 5. If code point is in the range U+FF61 to U+FF9F, inclusive,
  35. // return two bytes whose values are 0x8E and code point −
  36. // 0xFF61 + 0xA1.
  37. if (inRange(code_point, 0xFF61, 0xFF9F))
  38. return [0x8E, code_point - 0xFF61 + 0xA1];
  39. // 6. If code point is U+2212, set it to U+FF0D.
  40. if (code_point === 0x2212)
  41. code_point = 0xFF0D;
  42. // 7. Let pointer be the index pointer for code point in index
  43. // jis0208.
  44. var pointer = indexPointerFor(code_point, index('jis0208'));
  45. // 8. If pointer is null, return error with code point.
  46. if (pointer === null)
  47. return encoderError(code_point);
  48. // 9. Let lead be Math.floor(pointer / 94) + 0xA1.
  49. var lead = Math.floor(pointer / 94) + 0xA1;
  50. // 10. Let trail be pointer % 94 + 0xA1.
  51. var trail = pointer % 94 + 0xA1;
  52. // 11. Return two bytes whose values are lead and trail.
  53. return [lead, trail];
  54. };
  55. return EUCJPEncoder;
  56. }());
  57. export { EUCJPEncoder };
  58. //# sourceMappingURL=EUCJPEncoder.js.map