/** * Scene shapes as SVG elements: `@embedpdf/web`'s element descriptions (`svgShapesOf`), one * element each, a clipping `svg` with its children. The annotation and form layers draw their * scenes through `` inside an ``. */ import { ChangeDetectionStrategy, Component, Directive, effect, ElementRef, inject, input, } from '@angular/core'; import type { SvgShape } from '@embedpdf/web'; import { injectFontMounts } from './font-mounts'; /** * An SVG element's attributes from a record of SVG names (`stroke-width`), as `@embedpdf/web` * describes a scene node (templates can't spread). Set again when the record changes; an * attribute that left the record is removed. */ @Directive({ selector: '[epdfSvgAttributes]' }) export class EpdfSvgAttributes { readonly attributes = input.required>>({ alias: 'epdfSvgAttributes', }); constructor() { const element = inject>(ElementRef).nativeElement; let shown: Readonly> = {}; effect(() => { const next = this.attributes(); for (const name of Object.keys(shown)) { if (!(name in next)) element.removeAttribute(name); } for (const [name, value] of Object.entries(next)) { if (shown[name] !== value) element.setAttribute(name, String(value)); } shown = next; }); } } /** The shapes, in drawing order, in the `` it's on, and the faces their text draws in. */ @Component({ selector: 'g[epdfSvgShapes]', imports: [EpdfSvgAttributes], changeDetection: ChangeDetectionStrategy.OnPush, template: ` @for (shape of shapes(); track $index) { @switch (shape.tag) { @case ('rect') { } @case ('ellipse') { } @case ('line') { } @case ('path') { } @case ('polygon') { } @case ('polyline') { } @case ('text') { } @case ('svg') { } } } `, }) export class EpdfSvgShapes { readonly shapes = input.required({ alias: 'epdfSvgShapes' }); constructor() { // The faces of this level's text, while it shows: a clipping `svg` mounts its children's. injectFontMounts(() => this.shapes().flatMap((shape) => (shape.program ? [shape.program] : [])), ); } }