Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(web): prototype KMX+ TouchLayout generator (in TS) #12305

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions web/src/engine/keyboard/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export * from "./keyEvent.js";
export { default as KeyMapping } from "./keyMapping.js";
export { OutputTarget } from "./outputTarget.interface.js";

export { LazySpan } from "./lazySpan.js";

export * from "@keymanapp/web-utils";

// At the top level, there should be no default export.
Expand Down
39 changes: 39 additions & 0 deletions web/src/engine/keyboard/src/lazySpan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* A span that does not cut out the substring until it absolutely has to!
*/
export class LazySpan {
jahorton marked this conversation as resolved.
Show resolved Hide resolved
private _source: string;
readonly start: number;
readonly end: number;
constructor(source: string, start: number, end: number) {
this._source = source;
this.start = start;
this.end = end;
}

get text(): string {
return this._source.substring(this.start, this.end);
}

get length(): number {
return this.end - this.start;
}

substring(start: number, end?: number): LazySpan {
if(end === undefined) {
end = start + this.length;
}

const maxIndex = this.start + this.length;
end = end > maxIndex ? maxIndex : end;

start = start < 0 ? 0 : start;
if(end < start) {
const temp = end;
end = start;
start = temp;
}

return new LazySpan(this._source, this.start + start, this.start + end);
}
}
84 changes: 84 additions & 0 deletions web/src/test/auto/headless/engine/keyboard/lazySpan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { assert } from 'chai';
import fs from 'fs';

import { LazySpan } from 'keyman/engine/keyboard';

const ldmlFixture = fs.readFileSync('../resources/fixtures/imperial_aramaic.kmx', 'binary');

const simpleString = "the quick brown fox jumped over the lazy dog";

const SINGLE_CHAR_RANGE = 256;
const ENCODED_NUM_BASE = 0;

// Little-endian, and we have to rebuild the number byte-by-byte.
export function decodeNumber(str, start, end) {
end ??= str.length;
let num = 0;

for(let i = end - 1; i >= start; i--) {
let val = str.charCodeAt(i);
num = num * SINGLE_CHAR_RANGE + val - ENCODED_NUM_BASE;
}

return num;
}
jahorton marked this conversation as resolved.
Show resolved Hide resolved

describe("LazySpan", () => {
it("handles simple, common-case init", () => {
const span = new LazySpan(simpleString, 4, 9);
assert.equal(span.text, 'quick');

const fullSpan = new LazySpan(simpleString, 0, simpleString.length);
assert.equal(fullSpan.text, simpleString);
});

it("correctly builds sub-spans", () => {
const fullSpan = new LazySpan(simpleString, 0, simpleString.length);
const quickSpan = fullSpan.substring(4, 9);
assert.equal(quickSpan.text, 'quick');

const latterHalf = new LazySpan(simpleString, simpleString.indexOf('jumped'), simpleString.length);
const overSpan = latterHalf.substring(7, 11);
assert.equal(overSpan.text, 'over');
});

it('ldml parsing scratchspace', () => {
jahorton marked this conversation as resolved.
Show resolved Hide resolved
const kmxPlusOffset = ldmlFixture.indexOf('sect');
const kmxPlusLength = decodeNumber(ldmlFixture, kmxPlusOffset + 8, kmxPlusOffset + 12);

// The KMX+ part of the fixture is the 'tail' of its file - once started, it is the remainder.
assert.equal(kmxPlusLength + kmxPlusOffset, ldmlFixture.length);

// Sections

const sectTableLength = decodeNumber(ldmlFixture, kmxPlusOffset + 4, kmxPlusOffset + 8);
const sectTableSpan = new LazySpan(ldmlFixture, kmxPlusOffset, kmxPlusOffset + sectTableLength);

const sectionMap = new Map();
sectionMap.set('sect', sectTableSpan);
jahorton marked this conversation as resolved.
Show resolved Hide resolved

const sectCount = decodeNumber(ldmlFixture, sectTableSpan.start + 12, sectTableSpan.start + 16);

// First entry's base offset: 16
const sectNames = [];
for(let i = 0; i < sectCount; i++) {
const tableRowOffset = sectTableSpan.start + 16 + 8*i;
const sectName = new LazySpan(ldmlFixture, tableRowOffset, tableRowOffset + 4);
sectNames.push(sectName);

const sectOffset = decodeNumber(ldmlFixture, tableRowOffset + 4, tableRowOffset + 8);

const nameFromTable = new LazySpan(ldmlFixture, kmxPlusOffset + sectOffset, kmxPlusOffset + sectOffset + 4);
const tableLength = decodeNumber(ldmlFixture, kmxPlusOffset + sectOffset + 4, kmxPlusOffset + sectOffset + 8);
const tableSpan = new LazySpan(ldmlFixture, kmxPlusOffset + sectOffset, kmxPlusOffset + sectOffset + tableLength)

sectionMap.set(sectName.text, tableSpan);

// Should result in a run-time error when converted to a proper func.
assert.equal(nameFromTable.text, sectName.text);
}

assert.sameMembers(sectNames.map((span) => span.text), ['elem', 'keys', 'layr', 'list', 'loca', 'meta', 'strs', 'uset', 'vars']);
assert.sameMembers(sectionMap.keys(), ['sect', 'elem', 'keys', 'layr', 'list', 'loca', 'meta', 'strs', 'uset', 'vars']);
});
});
Binary file not shown.
Loading