nehan
Version:
Html layout engine for paged-media written in Typescript
62 lines • 2.14 kB
JavaScript
import { DualChar, } from './public-api';
export class Hyphenator {
constructor() { }
hyphenate(context) {
const lexer = context.lexer;
const moveCount = this.getHyphenateCount(lexer);
if (moveCount < 0) {
const popCount = -moveCount;
if (popCount >= context.characters.length) {
return 0;
}
context.characters = context.characters.slice(0, -popCount);
context.text = context.characters.reduce((acm, ichar) => acm + ichar.text, "");
lexer.pushBack(popCount);
}
else if (moveCount > 0) {
for (let i = 0; i < moveCount; i++) {
context.addCharacter(lexer.getNext());
}
}
return moveCount;
}
getHyphenateCount(lexer) {
const tail = lexer.peek(-1);
const head = lexer.peek(0);
const next1 = lexer.peek(+1);
if (head instanceof DualChar && head.isHeadNg() && head.isHangEnable()) {
if (next1 === null || next1 instanceof DualChar === false) {
return +1;
}
if (next1 && next1 instanceof DualChar && next1.isHeadNg() === false) {
return +1;
}
if (tail && tail instanceof DualChar === false) {
return -1;
}
if (tail && tail instanceof DualChar && tail.isHeadNg() === false) {
return -1;
}
}
if (tail instanceof DualChar && tail.isTailNg()) {
let move = -1;
while (true) {
let prev = lexer.peek(move - 1);
if (!prev) {
break;
}
if (prev && prev instanceof DualChar === false) {
break;
}
if (prev && prev instanceof DualChar && prev.isTailNg() === false) {
break;
}
move--;
}
return move;
}
return 0;
}
}
Hyphenator.instance = new Hyphenator();
//# sourceMappingURL=hyphenator.js.map