appendKanjiBytes static method

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

Implementation

static void appendKanjiBytes(String content, BitArray bits) {
  final bytes = StringUtils.shiftJisCharset.encode(content);
  if (bytes.length % 2 != 0) {
    throw WriterException('Kanji byte size not even');
  }
  final maxI = bytes.length - 1; // bytes.length must be even
  for (int i = 0; i < maxI; i += 2) {
    final byte1 = bytes[i] & 0xFF;
    final byte2 = bytes[i + 1] & 0xFF;
    final code = ((byte1 << 8) & 0xFFFFFFFF) | byte2;
    int 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');
    }
    final encoded = ((subtracted >> 8) * 0xc0) + (subtracted & 0xff);
    bits.appendBits(encoded, 13);
  }
}