nehan
Version:
Html layout engine for paged-media written in Typescript
60 lines • 2.58 kB
JavaScript
import { Config, LayoutResult, RubyReducer, LogicalNodeGenerator, } from './public-api';
export class RubyNodeGenerator {
constructor(context, reducer = RubyReducer.instance) {
this.context = context;
this.reducer = reducer;
this.generator = this.createGenerator();
}
getNext() {
const next = this.generator.next();
return next.done ? undefined : next.value;
}
*createGenerator() {
if (Config.debugLayout) {
console.group("ruby");
}
let childElement = this.context.env.element.firstChild;
while (childElement !== null) {
const childGen = LogicalNodeGenerator.createChild(childElement, this.context);
this.context.child = childGen.generator;
while (true) {
const value = this.context.child.getNext();
if (!value) {
break;
}
switch (value.type) {
case "ruby-base":
this.context.addRubyBase(value.body);
break;
case "ruby-text":
this.context.addRubyText(value.body);
break;
}
}
childElement = childGen.nextElement;
}
for (let rubyGroup of this.context.rubyGroups) {
const ruby = this.context.acceptLayoutReducer(this.reducer, rubyGroup);
const rubyNode = ruby.body;
const rubyLineExtent = rubyNode.lineExtent;
if (rubyNode.size.measure > this.context.maxMeasure) {
console.warn("too large ruby, can't be included, ignored:%o(maxM=%d)", ruby, this.context.maxMeasure);
continue;
}
while (this.context.restExtent < rubyLineExtent) {
yield LayoutResult.pageBreak(this.context, `restExtent is not enough for rubyNode.lineExtent(${rubyLineExtent})`);
}
while (this.context.restMeasure < rubyNode.measure) {
yield LayoutResult.lineBreak(this.context, "restMeasure is not enough for rubyNode.measure");
}
while (this.context.restExtent < rubyLineExtent) {
yield LayoutResult.pageBreak(this.context, `restExtent is not enough for rubyNode.lineExtent(${rubyLineExtent}) even after pageBreak`);
}
yield ruby;
}
if (Config.debugLayout) {
console.groupEnd();
}
}
}
//# sourceMappingURL=ruby-node-generator.js.map