UNPKG

nehan

Version:

Html layout engine for paged-media written in Typescript

77 lines 2.7 kB
import { FlowFormatContext, ReplacedElement, PhysicalSize, LogicalBoxEdge, } from "./public-api"; function getAtomExtent(atomElement, lineExtent, writingMode) { if (atomElement.isTextElement()) { return lineExtent; } if (ReplacedElement.isReplacedElement(atomElement)) { return PhysicalSize.load(atomElement).getLogicalSize(writingMode).extent; } return 0; } export class DynamicStyleUtils { static requiredExtent(requiredExtent) { return (ctx) => { if (!ctx.parentContext) { return undefined; } if (ctx.parentContext.restExtent < requiredExtent) { return { "page-break-before": "always" }; } return { "page-break-before": "auto" }; }; } static smartHeader(ctx) { let parentCtx = ctx.parentContext; if (parentCtx instanceof FlowFormatContext && parentCtx.env.display.isInlineLevel()) { parentCtx = parentCtx.parentBlock; } if (!parentCtx) { return undefined; } const marginBefore = 2 * ctx.remSize - 0.14285714 * ctx.emSize; const style = { marginBefore: marginBefore + "px", marginStart: "0px", marginEnd: "0px", marginAfter: "1rem" }; if (parentCtx.globalPos.before === 0 || parentCtx.restExtent < marginBefore) { style.marginBefore = "0px"; } return style; } static smartBorderBreak(ctx) { const edge = LogicalBoxEdge.load(ctx.element); if (edge.border.width.before <= 0) { return undefined; } if (!ctx.parentContext) { return undefined; } const firstAtomElement = ctx.element.firstAtomChild; if (!firstAtomElement) { return undefined; } const restExtent = ctx.parentContext.restExtent; const lineExtent = ctx.parentContext.env.font.lineExtent; const writingMode = ctx.parentContext.env.writingMode; const minExtent = getAtomExtent(firstAtomElement, lineExtent, writingMode) + edge.before; if (restExtent < minExtent) { return { "page-break-before": "always" }; } return undefined; } static breakPoint(ctx) { debugger; return undefined; } static replaceContent(fn_replace) { return (ctx) => { let old_content = ctx.element.textContent; let new_content = fn_replace(old_content, ctx); ctx.element.innerHTML = new_content; return undefined; }; } } //# sourceMappingURL=dynamic-style-utils.js.map