kanji-data.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. var Mode = require('./mode')
  2. var Utils = require('./utils')
  3. function KanjiData (data) {
  4. this.mode = Mode.KANJI
  5. this.data = data
  6. }
  7. KanjiData.getBitsLength = function getBitsLength (length) {
  8. return length * 13
  9. }
  10. KanjiData.prototype.getLength = function getLength () {
  11. return this.data.length
  12. }
  13. KanjiData.prototype.getBitsLength = function getBitsLength () {
  14. return KanjiData.getBitsLength(this.data.length)
  15. }
  16. KanjiData.prototype.write = function (bitBuffer) {
  17. var i
  18. // In the Shift JIS system, Kanji characters are represented by a two byte combination.
  19. // These byte values are shifted from the JIS X 0208 values.
  20. // JIS X 0208 gives details of the shift coded representation.
  21. for (i = 0; i < this.data.length; i++) {
  22. var value = Utils.toSJIS(this.data[i])
  23. // For characters with Shift JIS values from 0x8140 to 0x9FFC:
  24. if (value >= 0x8140 && value <= 0x9FFC) {
  25. // Subtract 0x8140 from Shift JIS value
  26. value -= 0x8140
  27. // For characters with Shift JIS values from 0xE040 to 0xEBBF
  28. } else if (value >= 0xE040 && value <= 0xEBBF) {
  29. // Subtract 0xC140 from Shift JIS value
  30. value -= 0xC140
  31. } else {
  32. throw new Error(
  33. 'Invalid SJIS character: ' + this.data[i] + '\n' +
  34. 'Make sure your charset is UTF-8')
  35. }
  36. // Multiply most significant byte of result by 0xC0
  37. // and add least significant byte to product
  38. value = (((value >>> 8) & 0xff) * 0xC0) + (value & 0xff)
  39. // Convert result to a 13-bit binary string
  40. bitBuffer.put(value, 13)
  41. }
  42. }
  43. module.exports = KanjiData