All files / src/types/kvks kvks-file-reader.ts

89.67% Statements 165/184
72.34% Branches 34/47
100% Functions 8/8
89.67% Lines 165/184

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 1851x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 1x 1x 1x     9x 9x 9x 9x 9x 9x 9x 1x 1x 9x 1x 1x 9x 1x 1x 1x 1x 1x 1x 1x 1x 2873x 1498x     2873x 1375x 1375x 1375x     1375x 2873x 2873x 4335x 57x 1423x 1423x 1423x 1423x 4335x 1441x 1441x 4335x 2873x 1x 1x 3x     3x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 3x 3x 8x     8x 7x 7x 8x     8x 8x 8x 8x 8x 8x 8x 35x 35x 1189x 1189x           1189x 1189x 1189x 1189x 1189x 1189x 1189x 1189x 1189x 12x 12x 1189x 1189x 35x 8x 8x 8x 8x 1x 1x 12x 12x 12x 348564x 348564x 12x 12x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 39x 39x 9x 9x 9x 1x 1x 1x 35x 35x 35x 35x 284x 35x 35x 284x     1x  
import { SchemaValidators as SV, KvkFile, util, Constants } from '@keymanapp/common-types';
import { KeymanXMLReader } from '../../index.js'
import KVKSourceFile from './kvks-file.js';
const SchemaValidators = SV.default;
import boxXmlArray = util.boxXmlArray;
import USVirtualKeyCodes = Constants.USVirtualKeyCodes;
import { VisualKeyboard as VK } from '@keymanapp/common-types';
import DEFAULT_KVK_FONT = VK.DEFAULT_KVK_FONT;
import VisualKeyboard = VK.VisualKeyboard;
import VisualKeyboardHeaderFlags = VK.VisualKeyboardHeaderFlags;
import VisualKeyboardKey = VK.VisualKeyboardKey;
import VisualKeyboardKeyFlags = VK.VisualKeyboardKeyFlags;
import VisualKeyboardLegalShiftStates = VK.VisualKeyboardLegalShiftStates;
import VisualKeyboardShiftState = VK.VisualKeyboardShiftState;
import BUILDER_KVK_HEADER_VERSION = KvkFile.BUILDER_KVK_HEADER_VERSION;
import KVK_HEADER_IDENTIFIER_BYTES = KvkFile.KVK_HEADER_IDENTIFIER_BYTES;
 
 
export default class KVKSFileReader {
  public read(file: Uint8Array): KVKSourceFile {
    let source: KVKSourceFile;
 
    try {
      source = new KeymanXMLReader('kvks')
        .parse(new TextDecoder().decode(file)) as KVKSourceFile;
    } catch(e) {
      if(file.byteLength > 4 && file.subarray(0,3).every((v,i) => v == KVK_HEADER_IDENTIFIER_BYTES[i])) {
        throw new Error('File appears to be a binary .kvk file', {cause: e});
      }
      throw e;
    }
    if(source) {
      source = this.boxArrays(source);
      this.cleanupFlags(source);
      this.cleanupUnderscore('visualkeyboard', source.visualkeyboard);
    }
    return source;
  }
 
  private cleanupFlags(source: any) {
    if(source.visualkeyboard?.header?.flags === '') {
      source.visualkeyboard.header.flags = {};
    }
  }
 
  /**
   * The only element that allows spaces is <key>. Remove
   * all other empty whitespace-only values.
   * @param root
   * @param source
   */
  private cleanupUnderscore(root: string, source: any) {
    if(root != 'key') {
      if(source?.['_']?.trim() === '') {
        delete source['_'];
      }
    } else {
      // If key text is pure whitespace, replace with empty string,
      // which matches kmcomp reader
      if(source?.['_']?.match(/^( +)$/)) {
        source['_'] = '';
      }
    }
 
    for(const key of Object.keys(source)) {
      if(Array.isArray(source[key])) {
        for(const item of source[key]) {
          if(typeof(item) === 'object') {
            this.cleanupUnderscore(key, item);
          }
        }
      } else if(typeof source[key] === 'object') {
        this.cleanupUnderscore(key, source[key]);
      }
    }
  }
 
  public validate(source: KVKSourceFile): void {
    if(!SchemaValidators.kvks(source)) {
      throw new Error(JSON.stringify((<any>SchemaValidators.kvks).errors));
    }
  }
 
  public transform(source: KVKSourceFile, invalidVkeys?: string[]): VisualKeyboard {
    // NOTE: at this point, the xml should have been validated
    // and matched the schema result so we can assume properties exist
 
    const result: VisualKeyboard = {
      header: {
        version: BUILDER_KVK_HEADER_VERSION,
        flags: 0,
        ansiFont: {...DEFAULT_KVK_FONT},
        unicodeFont: {...DEFAULT_KVK_FONT},
        associatedKeyboard: source.visualkeyboard?.header?.kbdname,
        underlyingLayout: source.visualkeyboard?.header?.layout,
      },
      keys: []
    };
 
    if(source.visualkeyboard?.header?.flags?.displayunderlying !== undefined) {
      result.header.flags |= VisualKeyboardHeaderFlags.kvkhDisplayUnderlying;
    }
    if(source.visualkeyboard?.header?.flags?.key102 !== undefined) {
      result.header.flags |= VisualKeyboardHeaderFlags.kvkh102;
    }
    if(source.visualkeyboard?.header?.flags?.usealtgr !== undefined) {
      result.header.flags |= VisualKeyboardHeaderFlags.kvkhAltGr;
    }
    if(source.visualkeyboard?.header?.flags?.useunderlying !== undefined) {
      result.header.flags |= VisualKeyboardHeaderFlags.kvkhUseUnderlying;
    }
 
    for(const encoding of source.visualkeyboard.encoding) {
      const isUnicode = (encoding.$?.name == 'unicode'),
        font = isUnicode ? result.header.unicodeFont : result.header.ansiFont;
      font.name = encoding.$?.fontname ?? DEFAULT_KVK_FONT.name;
      font.size = parseInt(encoding.$?.fontsize ?? DEFAULT_KVK_FONT.size.toString(), 10);
      for(const layer of encoding.layer) {
        const shift = this.kvksShiftToKvkShift(layer.$?.shift);
        for(const sourceKey of layer.key) {
          const vkey = (USVirtualKeyCodes as any)[sourceKey.$?.vkey];
          if(!vkey) {
            if(typeof invalidVkeys !== 'undefined') {
              invalidVkeys.push(sourceKey.$?.vkey);
            }
            continue;
          }
          const key: VisualKeyboardKey = {
            flags:
              (isUnicode ? VisualKeyboardKeyFlags.kvkkUnicode : 0) |
              (sourceKey.bitmap ? VisualKeyboardKeyFlags.kvkkBitmap : 0),
            shift: shift,
            text: sourceKey.bitmap ? '' : (sourceKey._ ?? ''),
            vkey: vkey
          };
          if(sourceKey.bitmap) {
            key.bitmap = this.base64ToArray(sourceKey.bitmap);
          }
          result.keys.push(key);
        }
      }
    }
 
    return result;
  }
 
  private base64ToArray(source: string): Uint8Array {
    const binary = atob(source);
    const bytes = new Uint8Array(binary.length);
    for (let i = 0; i < binary.length; i++) {
      bytes[i] = binary.charCodeAt(i);
    }
    return bytes;
  }
 
  /**
   * xml2js will not place single-entry objects into arrays.
   * Easiest way to fix this is to box them ourselves as needed
   * @param source KVKSourceFile
   */
  private boxArrays(source: KVKSourceFile) {
    boxXmlArray(source.visualkeyboard, 'encoding');
    for(const encoding of source.visualkeyboard.encoding) {
      boxXmlArray(encoding, 'layer');
      for(const layer of encoding.layer) {
        boxXmlArray(layer, 'key');
      }
    }
    return source;
  }
 
 
  public kvksShiftToKvkShift(shift: string): VisualKeyboardShiftState {
    shift = shift.toUpperCase();
 
    // TODO-LDML(lowpri): make a map of this?
    for(const state of VisualKeyboardLegalShiftStates) {
      if(state.name == shift) {
        return state.shift;
      }
    }
    return 0;
  }
}