EUCJPEncoder.js 2.5 KB

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