UNPKG

@keymanapp/common-types

Version:

Keyman Developer keyboard file types

80 lines (78 loc) 3.28 kB
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},n=(new Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="3bbaef71-e096-51cd-8639-e5f7356d7f7d")}catch(e){}}(); import KVKFile, { BUILDER_KVK_HEADER_IDENTIFIER, BUILDER_KVK_HEADER_VERSION } from "./kvk-file.js"; export default class KvkFileWriter { /** * Writes the visual keyboard to a binary .kvk format byte array. * @param source VisualKeyboard * @returns Uint8Array, the .kvk file */ write(source) { const binary = this.build(source); const kvk = new KVKFile(); const file = new Uint8Array(kvk.KVK_FILE.size(binary)); const data = kvk.KVK_FILE.toBuffer(binary); file.set(data, 0); return file; } build(source) { /** * The visual keyboard text color is included in .kvk files but is never * defined in .kvks files. Thus, it is always written by the legacy .kvk * binary writer VisualKeyboardSaverBinary.pas as TColor.clWindowText (SystemColor | 8), * or in DWORD form 0xFF000008, and we maintain this value for consistency * here. */ const VISUAL_KEYBOARD_TEXT_COLOR = 0xFF000008; const binary = { header: { identifier: BUILDER_KVK_HEADER_IDENTIFIER, version: BUILDER_KVK_HEADER_VERSION, associatedKeyboard: { len: 0, str: '' }, flags: source.header.flags, ansiFont: { color: VISUAL_KEYBOARD_TEXT_COLOR, size: source.header.ansiFont.size, name: { len: 0, str: '' } }, unicodeFont: { color: VISUAL_KEYBOARD_TEXT_COLOR, size: source.header.unicodeFont.size, name: { len: 0, str: '' } }, }, keyCount: source.keys.length, keys: [] }; this.setString(binary.header.associatedKeyboard, source.header.associatedKeyboard); this.setString(binary.header.ansiFont.name, source.header.ansiFont.name); this.setString(binary.header.unicodeFont.name, source.header.unicodeFont.name); for (let sourceKey of source.keys) { const binaryKey = { flags: sourceKey.flags, vkey: sourceKey.vkey, shift: sourceKey.shift, text: { len: 0, str: '' }, bitmapSize: sourceKey.bitmap ? sourceKey.bitmap.byteLength : 0, bitmapData: sourceKey.bitmap ? Array.from(sourceKey.bitmap) : [] }; this.setString(binaryKey.text, sourceKey.text || ''); binary.keys.push(binaryKey); } return binary; } /** * Fills a kvk string from a source string. Note that the format includes both * a length word and zero termination. * * @param str * @param value * @returns number */ setString(str, value) { str.len = value.length + 1; str.str = value; } } ; //# sourceMappingURL=kvk-file-writer.js.map //# debugId=3bbaef71-e096-51cd-8639-e5f7356d7f7d