All files / tools/src/utilities/drawing textBoxOverlapRegistry.ts

75% Statements 6/8
50% Branches 2/4
100% Functions 3/3
75% Lines 6/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61                                          430x             9039x                     135x 135x       135x                           135x    
/**
 * Per-viewport registry that tracks rendered text-box bounding rectangles.
 *
 * During each annotation render cycle the registry is:
 *   1. Cleared at the start            (via `clearTextBoxRegistry`)
 *   2. Populated after each text box    (via `registerTextBox` – called from
 *      drawTextBox.ts)
 *   3. Queried before placing a new box (via `getRegisteredTextBoxes` – used
 *      by getTextBoxCoordsCanvas.ts to avoid overlap)
 *
 * The registry is keyed by the viewport SVG-layer element which is unique per
 * viewport and naturally scoped to a single render frame.
 */
 
export interface TextBoxRect {
  x: number;
  y: number;
  width: number;
  height: number;
}
 
const registry = new WeakMap<Element, TextBoxRect[]>();
 
/**
 * Clear all tracked text boxes for a viewport.
 * Called at the beginning of each render cycle (draw.ts).
 */
export function clearTextBoxRegistry(svgLayerElement: Element): void {
  registry.set(svgLayerElement, []);
}
 
/**
 * Register a rendered text box so subsequent placements can avoid it.
 * Called from drawTextBox.ts after each text box is drawn.
 */
export function registerTextBox(
  svgLayerElement: Element,
  rect: TextBoxRect
): void {
  let boxes = registry.get(svgLayerElement);
  Iif (!boxes) {
    boxes = [];
    registry.set(svgLayerElement, boxes);
  }
  boxes.push({
    x: rect.x,
    y: rect.y,
    width: rect.width,
    height: rect.height,
  });
}
 
/**
 * Return all text boxes registered so far for the given viewport.
 */
export function getRegisteredTextBoxes(
  svgLayerElement: Element
): TextBoxRect[] {
  return registry.get(svgLayerElement) || [];
}