|  | 
|  | 1 | +import { labelFontSize } from "../constants"; | 
|  | 2 | + | 
|  | 3 | +// Helper functions for basic SVG components | 
|  | 4 | + | 
|  | 5 | +/** | 
|  | 6 | + * Given an array of SVG elements, group them as an SVG group using the `<g>` tag. | 
|  | 7 | + *  | 
|  | 8 | + * @param svgElems Array of SVG elements. | 
|  | 9 | + *  | 
|  | 10 | + * @returns SVG string for grouped elements. | 
|  | 11 | + */ | 
|  | 12 | +export const group = (...svgElems: (string | string[])[]): string => | 
|  | 13 | +    ['<g>', ...svgElems.flat(), '</g>'].join('\n'); | 
|  | 14 | + | 
|  | 15 | +/** | 
|  | 16 | + * Generate the SVG representation of a control dot used for controlled operations. | 
|  | 17 | + *  | 
|  | 18 | + * @param x      x coord of circle. | 
|  | 19 | + * @param y      y coord of circle. | 
|  | 20 | + * @param radius Radius of circle. | 
|  | 21 | + *  | 
|  | 22 | + * @returns SVG string for control dot. | 
|  | 23 | + */ | 
|  | 24 | +export const controlDot = (x: number, y: number, radius: number = 5): string => | 
|  | 25 | +    `<circle cx="${x}" cy="${y}" r="${radius}" stroke="black" fill="black" stroke-width="1"></circle>`; | 
|  | 26 | + | 
|  | 27 | +/** | 
|  | 28 | + * Generate an SVG line. | 
|  | 29 | + *  | 
|  | 30 | + * @param x1          x coord of starting point of line. | 
|  | 31 | + * @param y1          y coord of starting point of line. | 
|  | 32 | + * @param x2          x coord of ending point of line. | 
|  | 33 | + * @param y2          y coord fo ending point of line. | 
|  | 34 | + * @param strokeWidth Stroke width of line. | 
|  | 35 | + *  | 
|  | 36 | + * @returns SVG string for line. | 
|  | 37 | + */ | 
|  | 38 | +export const line = (x1: number, y1: number, x2: number, y2: number, strokeWidth: number = 1): string => | 
|  | 39 | +    `<line x1="${x1}" x2="${x2}" y1="${y1}" y2="${y2}" stroke="black" stroke-width="${strokeWidth}"></line>`; | 
|  | 40 | + | 
|  | 41 | +/** | 
|  | 42 | + * Generate the SVG representation of a unitary box that represents an arbitrary unitary operation. | 
|  | 43 | + *  | 
|  | 44 | + * @param x      x coord of box. | 
|  | 45 | + * @param y      y coord of box. | 
|  | 46 | + * @param width  Width of box. | 
|  | 47 | + * @param height Height of box. | 
|  | 48 | + *  | 
|  | 49 | + * @returns SVG string for unitary box. | 
|  | 50 | + */ | 
|  | 51 | +export const box = (x: number, y: number, width: number, height: number): string => | 
|  | 52 | +    `<rect x="${x}" y="${y}" width="${width}" height="${height}" stroke="black" fill="white" stroke-width="1"></rect>`; | 
|  | 53 | + | 
|  | 54 | +/** | 
|  | 55 | + * Generate the SVG text element from a given text string. | 
|  | 56 | + *  | 
|  | 57 | + * @param text String to render as SVG text. | 
|  | 58 | + * @param x    Middle x coord of text. | 
|  | 59 | + * @param y    Middle y coord of text. | 
|  | 60 | + * @param fs   Font size of text. | 
|  | 61 | + *  | 
|  | 62 | + * @returns SVG string for text. | 
|  | 63 | + */ | 
|  | 64 | +export const text = (text: string, x: number, y: number, fs: number = labelFontSize): string => | 
|  | 65 | +    `<text font-size="${fs}" font-family="Arial" x="${x}" y="${y}" dominant-baseline="middle" text-anchor="middle" fill="black">${text}</text>`; | 
|  | 66 | + | 
|  | 67 | +/** | 
|  | 68 | + * Generate the SVG representation of the arc used in the measurement box. | 
|  | 69 | + *  | 
|  | 70 | + * @param x  x coord of arc. | 
|  | 71 | + * @param y  y coord of arc. | 
|  | 72 | + * @param rx x radius of arc. | 
|  | 73 | + * @param ry y radius of arc. | 
|  | 74 | + *  | 
|  | 75 | + * @returns SVG string for arc. | 
|  | 76 | + */ | 
|  | 77 | +export const arc = (x: number, y: number, rx: number, ry: number): string => | 
|  | 78 | +    `<path d="M ${x + 2 * rx} ${y} A ${rx} ${ry} 0 0 0 ${x} ${y}" stroke="black" fill="none" stroke-width="1"></path>`; | 
|  | 79 | + | 
|  | 80 | +/** | 
|  | 81 | + * Generate a dashed SVG line. | 
|  | 82 | + *  | 
|  | 83 | + * @param x1 x coord of starting point of line. | 
|  | 84 | + * @param y1 y coord of starting point of line. | 
|  | 85 | + * @param x2 x coord of ending point of line. | 
|  | 86 | + * @param y2 y coord fo ending point of line. | 
|  | 87 | + *  | 
|  | 88 | + * @returns SVG string for dashed line. | 
|  | 89 | + */ | 
|  | 90 | +export const dashedLine = (x1: number, y1: number, x2: number, y2: number): string => | 
|  | 91 | +    `<line x1="${x1}" x2="${x2}" y1="${y1}" y2="${y2}" stroke="black" stroke-dasharray="8, 8" stroke-width="1"></line>`; | 
|  | 92 | + | 
|  | 93 | +/** | 
|  | 94 | + * Generate the SVG representation of the dashed box used for enclosing groups of operations controlled on a classical register. | 
|  | 95 | + *  | 
|  | 96 | + * @param x      x coord of box. | 
|  | 97 | + * @param y      y coord of box. | 
|  | 98 | + * @param width  Width of box. | 
|  | 99 | + * @param height Height of box. | 
|  | 100 | + *  | 
|  | 101 | + * @returns SVG string for dashed box. | 
|  | 102 | + */ | 
|  | 103 | +export const dashedBox = (x: number, y: number, width: number, height: number): string => | 
|  | 104 | +    `<rect x="${x}" y ="${y}" width="${width}" height="${height}" stroke="black" fill-opacity="0" stroke-dasharray="8, 8" stroke-width="1"></rect>`; | 
0 commit comments