ShiftJISEncoder.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 ShiftJISEncoder = /** @class */ (function () {
  14. function ShiftJISEncoder(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. ShiftJISEncoder.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 or U+0080, return a
  27. // byte whose value is code point.
  28. if (terminology_1.isASCIICodePoint(code_point) || code_point === 0x0080)
  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 a byte whose value is code point − 0xFF61 + 0xA1.
  38. if (utilities_1.inRange(code_point, 0xFF61, 0xFF9F))
  39. return code_point - 0xFF61 + 0xA1;
  40. // 6. If code point is U+2212, set it to U+FF0D.
  41. if (code_point === 0x2212)
  42. code_point = 0xFF0D;
  43. // 7. Let pointer be the index Shift_JIS pointer for code point.
  44. var pointer = indexes_1.indexShiftJISPointerFor(code_point);
  45. // 8. If pointer is null, return error with code point.
  46. if (pointer === null)
  47. return encodings_1.encoderError(code_point);
  48. // 9. Let lead be Math.floor(pointer / 188).
  49. var lead = Math.floor(pointer / 188);
  50. // 10. Let lead offset be 0x81, if lead is less than 0x1F, and
  51. // 0xC1 otherwise.
  52. var lead_offset = (lead < 0x1F) ? 0x81 : 0xC1;
  53. // 11. Let trail be pointer % 188.
  54. var trail = pointer % 188;
  55. // 12. Let offset be 0x40, if trail is less than 0x3F, and 0x41
  56. // otherwise.
  57. var offset = (trail < 0x3F) ? 0x40 : 0x41;
  58. // 13. Return two bytes whose values are lead + lead offset and
  59. // trail + offset.
  60. return [lead + lead_offset, trail + offset];
  61. };
  62. return ShiftJISEncoder;
  63. }());
  64. exports.ShiftJISEncoder = ShiftJISEncoder;
  65. //# sourceMappingURL=ShiftJISEncoder.js.map