appendKanjiBytes static method

void appendKanjiBytes(
  1. String content,
  2. BitArray bits
)

Implementation

static void appendKanjiBytes(String content, BitArray bits) {
  var bytes = CharacterSetECI.SJIS.encoding.encode(content);
  if (bytes.length % 2 != 0) {
    throw WriterException('Kanji byte size not even');
  }
  var maxI = bytes.length - 1; // bytes.length must be even
  for (var i = 0; i < maxI; i += 2) {
    var byte1 = bytes[i] & 0xFF;
    var byte2 = bytes[i + 1] & 0xFF;
    var code = (byte1 << 8) | byte2;
    var subtracted = -1;
    if (code >= 0x8140 && code <= 0x9ffc) {
      subtracted = code - 0x8140;
    } else if (code >= 0xe040 && code <= 0xebbf) {
      subtracted = code - 0xc140;
    }
    if (subtracted == -1) {
      throw WriterException('Invalid byte sequence');
    }
    var encoded = ((subtracted >> 8) * 0xc0) + (subtracted & 0xff);
    bits.appendBits(encoded, 13);
  }
}