feat(anatomy): composeViewSvg with tree-level highlight and halo, review drawing on the shared composer
This commit is contained in:
1 parent
4c6c5151f8
commit
694092610d
7 files changed
+521
-14
No files matched your search
@@ -1,14 +1,14 @@
|
||||
import { DOMParser, XMLSerializer } from '@xmldom/xmldom'
|
||||
import type { AnatomicalSheetDocumentInput } from '@faunosteo/domain'
|
||||
import { createLayerElement } from '../render/dom'
|
||||
import { renderAnnotationLayer, viewBoxOf } from '../render/layer'
|
||||
import { composeViewSvg } from '../render/compose'
|
||||
|
||||
/**
|
||||
* The drawing of one view exactly as the application renders it: the
|
||||
* cleaned asset framed on the view's box plus the annotation layer the app
|
||||
* generates from the document. Written under draft/review/app-<view>.svg
|
||||
* so the markers and the framing can be compared with the legacy files
|
||||
* before and after a change, outside the browser.
|
||||
* before and after a change, outside the browser. Parse and serialise
|
||||
* here; the composition itself is the shared `composeViewSvg`.
|
||||
*/
|
||||
|
||||
export interface AppReviewInput {
|
||||
@@ -21,16 +21,6 @@ export function renderAppReviewDrawing(input: AppReviewInput): string {
|
||||
const doc = new DOMParser().parseFromString(input.cleanedSvg, 'image/svg+xml')
|
||||
const root = doc.documentElement
|
||||
if (!root || root.tagName !== 'svg') throw new Error('not an SVG document')
|
||||
const view = input.document.views.find((v) => v.id === input.viewId)
|
||||
if (!view) throw new Error(`unknown view "${input.viewId}"`)
|
||||
const box = viewBoxOf(view)
|
||||
if (box) root.setAttribute('viewBox', box)
|
||||
root.removeAttribute('width')
|
||||
root.removeAttribute('height')
|
||||
const layer = renderAnnotationLayer({
|
||||
document: input.document,
|
||||
viewId: view.id,
|
||||
})
|
||||
root.appendChild(createLayerElement(doc, layer) as never)
|
||||
composeViewSvg({ root, document: input.document, viewId: input.viewId })
|
||||
return new XMLSerializer().serializeToString(doc)
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
import type { AnatomicalSheetDocumentInput } from '@faunosteo/domain'
|
||||
import {
|
||||
createLayerElement,
|
||||
SVG_NS,
|
||||
type MinimalDocument,
|
||||
type MinimalElement,
|
||||
type MinimalNode,
|
||||
} from './dom'
|
||||
import { renderAnnotationLayer, viewBoxOf, type LayerElement } from './layer'
|
||||
|
||||
/**
|
||||
* Composes the drawing of one view: the parsed asset framed on the view's
|
||||
* box plus the annotation layer rendered from the document. With a
|
||||
* `highlight`, the layer keeps only the selected annotations, styled for
|
||||
* print (legacy picture: accent fill, halo on the main view, no data
|
||||
* attributes). Works on any DOM Level 2 implementation (a Node parser or
|
||||
* the browser DOM): the caller parses and serialises.
|
||||
*/
|
||||
|
||||
export const HALO_FILTER_ID = 'osteo-halo'
|
||||
export const HIGHLIGHT_FILL = '#b85a3f'
|
||||
export const HIGHLIGHT_FILL_OPACITY = '0.85'
|
||||
|
||||
/** DOM Level 2 surface the composer needs on the `<svg>` root. */
|
||||
export interface ComposeElement extends MinimalElement {
|
||||
readonly ownerDocument: MinimalDocument | null
|
||||
readonly firstChild: MinimalNode | null
|
||||
getAttribute(name: string): string | null
|
||||
removeAttribute(name: string): void
|
||||
insertBefore(node: MinimalNode, reference: MinimalNode | null): unknown
|
||||
getElementsByTagName(tag: string): ComposeElementList
|
||||
}
|
||||
|
||||
export interface ComposeElementList {
|
||||
readonly length: number
|
||||
item(index: number): ComposeElement | null
|
||||
}
|
||||
|
||||
export interface ComposeHighlight {
|
||||
/** Annotation ids kept on the layer; every other marker is dropped. */
|
||||
annotationIds: ReadonlySet<string>
|
||||
/** Wrap the kept markers in the `osteo-halo` filter (main view). */
|
||||
halo: boolean
|
||||
}
|
||||
|
||||
export interface ComposeInput<Root extends ComposeElement = ComposeElement> {
|
||||
root: Root
|
||||
document: AnatomicalSheetDocumentInput
|
||||
viewId: string
|
||||
highlight?: ComposeHighlight
|
||||
}
|
||||
|
||||
/** Frames `root` on the view, appends the layer `<g>` last, returns `root`. */
|
||||
export function composeViewSvg<Root extends ComposeElement>(
|
||||
input: ComposeInput<Root>,
|
||||
): Root {
|
||||
const { root, document, viewId, highlight } = input
|
||||
const view = document.views.find((v) => v.id === viewId)
|
||||
if (!view) throw new Error(`unknown view "${viewId}"`)
|
||||
const box = viewBoxOf(view)
|
||||
if (box) root.setAttribute('viewBox', box)
|
||||
root.removeAttribute('width')
|
||||
root.removeAttribute('height')
|
||||
if (highlight?.halo) ensureHaloFilter(root)
|
||||
const rendered = renderAnnotationLayer({ document, viewId })
|
||||
const layer = highlight ? highlightLayer(rendered, highlight) : rendered
|
||||
root.appendChild(createLayerElement(documentOf(root), layer))
|
||||
return root
|
||||
}
|
||||
|
||||
/** Typed nullable by the DOM; an element taken from a parsed document has one. */
|
||||
function documentOf(root: ComposeElement): MinimalDocument {
|
||||
if (!root.ownerDocument) throw new Error('root has no owner document')
|
||||
return root.ownerDocument
|
||||
}
|
||||
|
||||
/**
|
||||
* The layer reduced to the selected annotations (both elements of a point),
|
||||
* each with the accent fill and, when asked, the halo filter. No `data-*`
|
||||
* attribute survives: the result is a picture, not an interactive layer.
|
||||
*/
|
||||
export function highlightLayer(
|
||||
layer: LayerElement,
|
||||
highlight: ComposeHighlight,
|
||||
): LayerElement {
|
||||
const children = layer.children
|
||||
.filter((child) =>
|
||||
highlight.annotationIds.has(child.attrs['data-anno-id'] ?? ''),
|
||||
)
|
||||
.map((child) => highlightElement(child, highlight.halo))
|
||||
return { ...layer, attrs: withoutDataAttrs(layer.attrs), children }
|
||||
}
|
||||
|
||||
function highlightElement(node: LayerElement, halo: boolean): LayerElement {
|
||||
const attrs: Record<string, string> = {
|
||||
...withoutDataAttrs(node.attrs),
|
||||
fill: HIGHLIGHT_FILL,
|
||||
'fill-opacity': HIGHLIGHT_FILL_OPACITY,
|
||||
}
|
||||
if (halo) attrs.filter = `url(#${HALO_FILTER_ID})`
|
||||
return { ...node, attrs }
|
||||
}
|
||||
|
||||
function withoutDataAttrs(
|
||||
attrs: Record<string, string>,
|
||||
): Record<string, string> {
|
||||
return Object.fromEntries(
|
||||
Object.entries(attrs).filter(([name]) => !name.startsWith('data-')),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the Gaussian-blur "halo" filter so the highlighted markers stay
|
||||
* visible on the whole-animal view once rasterised at print scale. One
|
||||
* filter per root: a second call finds it and leaves the tree alone.
|
||||
*/
|
||||
function ensureHaloFilter(root: ComposeElement): void {
|
||||
if (hasElementWithId(root, 'filter', HALO_FILTER_ID)) return
|
||||
const defs = root.getElementsByTagName('defs').item(0) ?? createDefs(root)
|
||||
defs.appendChild(haloFilter(documentOf(root)))
|
||||
}
|
||||
|
||||
function hasElementWithId(
|
||||
root: ComposeElement,
|
||||
tag: string,
|
||||
id: string,
|
||||
): boolean {
|
||||
const list = root.getElementsByTagName(tag)
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
if (list.item(i)?.getAttribute('id') === id) return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/** `<defs>` as the first child: the asset's own filters sit at the root. */
|
||||
function createDefs(root: ComposeElement): MinimalElement {
|
||||
const defs = documentOf(root).createElementNS(SVG_NS, 'defs')
|
||||
root.insertBefore(defs, root.firstChild)
|
||||
return defs
|
||||
}
|
||||
|
||||
function svgElement(
|
||||
doc: MinimalDocument,
|
||||
tag: string,
|
||||
attrs: Record<string, string>,
|
||||
): MinimalElement {
|
||||
const element = doc.createElementNS(SVG_NS, tag)
|
||||
for (const [name, value] of Object.entries(attrs)) {
|
||||
element.setAttribute(name, value)
|
||||
}
|
||||
return element
|
||||
}
|
||||
|
||||
function haloFilter(doc: MinimalDocument): MinimalElement {
|
||||
const filter = svgElement(doc, 'filter', {
|
||||
id: HALO_FILTER_ID,
|
||||
x: '-100%',
|
||||
y: '-100%',
|
||||
width: '300%',
|
||||
height: '300%',
|
||||
filterUnits: 'userSpaceOnUse',
|
||||
})
|
||||
filter.appendChild(gaussianBlur(doc, '18', 'haloWide'))
|
||||
filter.appendChild(gaussianBlur(doc, '8', 'haloTight'))
|
||||
filter.appendChild(haloMerge(doc))
|
||||
return filter
|
||||
}
|
||||
|
||||
function gaussianBlur(
|
||||
doc: MinimalDocument,
|
||||
stdDeviation: string,
|
||||
result: string,
|
||||
): MinimalElement {
|
||||
return svgElement(doc, 'feGaussianBlur', {
|
||||
in: 'SourceGraphic',
|
||||
stdDeviation,
|
||||
result,
|
||||
})
|
||||
}
|
||||
|
||||
/** Three wide blurs, two tight ones, then the marker itself on top. */
|
||||
function haloMerge(doc: MinimalDocument): MinimalElement {
|
||||
const merge = svgElement(doc, 'feMerge', {})
|
||||
const inputs = [
|
||||
'haloWide',
|
||||
'haloWide',
|
||||
'haloWide',
|
||||
'haloTight',
|
||||
'haloTight',
|
||||
'SourceGraphic',
|
||||
]
|
||||
for (const input of inputs) {
|
||||
merge.appendChild(svgElement(doc, 'feMergeNode', { in: input }))
|
||||
}
|
||||
return merge
|
||||
}
|
||||
@@ -20,3 +20,14 @@ export {
|
||||
type MinimalNode,
|
||||
} from './dom'
|
||||
export { serializeLayer } from './serialize'
|
||||
export {
|
||||
composeViewSvg,
|
||||
HALO_FILTER_ID,
|
||||
HIGHLIGHT_FILL,
|
||||
HIGHLIGHT_FILL_OPACITY,
|
||||
highlightLayer,
|
||||
type ComposeElement,
|
||||
type ComposeElementList,
|
||||
type ComposeHighlight,
|
||||
type ComposeInput,
|
||||
} from './compose'
|
||||
Reference in new issue
Block a user