encodeString(str) {
    const buffers = [];
    const currentBuf = [];

    // buffers will contain: encoded, non-encoded, encoded, ...
    // currentBuf is pushed in buffers each time there is a change.
    // So when buffers.length is odd then the last string is an encoded one
    // and currentBuf contains non-encoded chars.
    const hasCurrentBufErrors = () => buffers.length % 2 === 1;

    const getCharCode =
      this.toUnicode instanceof IdentityToUnicodeMap
        ? unicode => this.toUnicode.charCodeOf(unicode)
        : unicode => this.toUnicode.charCodeOf(String.fromCodePoint(unicode));

    for (let i = 0, ii = str.length; i < ii; i++) {
      const unicode = str.codePointAt(i);
      if (unicode > 0xd7ff && (unicode < 0xe000 || unicode > 0xfffd)) {
        // unicode is represented by two uint16
        i++;
      }
      if (this.toUnicode) {
        const charCode = getCharCode(unicode);
        if (charCode !== -1) {
          if (hasCurrentBufErrors()) {
            buffers.push(currentBuf.join(""));
            currentBuf.length = 0;
          }
          const charCodeLength = this.cMap
            ? this.cMap.getCharCodeLength(charCode)
            : 1;
          for (let j = charCodeLength - 1; j >= 0; j--) {
            currentBuf.push(String.fromCharCode((charCode >> (8 * j)) & 0xff));
          }
          continue;
        }
      }

      // unicode can't be encoded
      if (!hasCurrentBufErrors()) {
        buffers.push(currentBuf.join(""));
        currentBuf.length = 0;
      }
      currentBuf.push(String.fromCodePoint(unicode));
    }

    buffers.push(currentBuf.join(""));

    return buffers;
  }
Encode a js string using font encoding.
The resulting array contains an encoded string at even positions
(can be empty) and a non-encoded one at odd positions.
@param {String} a js string.
@returns {Array<String>} an array of encoded strings or non-encoded ones.