@skillpet/circuit
Version:
Circuit diagram library — render electrical schematics from JSON, with interactive SVG, themes, and Vue/React components
54 lines (53 loc) • 2.37 kB
TypeScript
/**
* User label placement — subset aligned with Python `Element._place_label` / `Label`.
*/
import { Point, type XY } from "./geometry/point.js";
import type { Element } from "./element.js";
export interface ElementLabelSpec {
/** Single line or list (list → distributed along `loc` like Python). */
text: string | readonly string[];
loc?: string | null;
ofst?: number | XY;
halign?: "left" | "center" | "right";
valign?: "top" | "center" | "bottom" | "base";
/** `true` = rotate with element (`theta`); number = extra local degrees. */
rotate?: boolean | number;
fontsize?: number;
font?: string;
mathfont?: string;
color?: string;
href?: string | null;
decoration?: string | null;
}
type Halign = "left" | "center" | "right";
type Valign = "top" | "center" | "bottom" | "base";
/**
* Python `Element._position_label`: when drawing angle is in (90°, 270°], swap edge `loc`
* so a logical "top" label stays visually above the body (see `elements.py`).
*/
export declare function swapLabelLocForTheta(loc: string, thetaDeg: number): string;
/** Python `_position_label` rotation part (degrees) for `_align_label` (`th = theta - label.rotate`). */
export declare function resolveLabelRotateDeg(spec: ElementLabelSpec, thetaDeg: number): number;
export type BBoxLike = {
xmin: number;
xmax: number;
ymin: number;
ymax: number;
};
/**
* Python `Element._align_label` (subset): suggested `halign`/`valign`/`ofst` Point in local coords.
* Caller supplies `loc` after `_position_label` (e.g. `swapLabelLocForTheta`), bbox without text, and
* `ofstIn` after flip/reverse on tuple and default fill (scalar lblofst when undefined).
*/
export declare function alignLabelForPlace(el: Element, loc: string, thetaDeg: number, labelRotateDeg: number, ofstIn: number | XY, bb: BBoxLike): {
halign: Halign;
valign: Valign;
ofst: Point;
};
/**
* Append `SegmentText` for user labels (local coords). Caller must truncate segments first.
*/
export declare function placeElementLabels(el: Element, thetaDeg: number, specs: readonly ElementLabelSpec[]): void;
/** Build `ElementLabelSpec[]` from constructor params (Python `toplabel` / `label` / …). */
export declare function buildLabelSpecsFromParams(params: Record<string, unknown>, _thetaDeg: number): ElementLabelSpec[];
export {};