{ "version": 3, "sources": ["../../../../src/lib/shapes/shared/createTextSvgElementFromSpans.ts"], "sourcesContent": ["import {\n\tBox2d,\n\tBox2dModel,\n\tEditor,\n\tTLDefaultHorizontalAlignStyle,\n\tTLDefaultVerticalAlignStyle,\n} from '@tldraw/editor'\n\nfunction correctSpacesToNbsp(input: string) {\n\treturn input.replace(/\\s/g, '\\xa0')\n}\n\n/** Get an SVG element for a text shape. */\nexport function createTextSvgElementFromSpans(\n\teditor: Editor,\n\tspans: { text: string; box: Box2dModel }[],\n\topts: {\n\t\tfontSize: number\n\t\tfontFamily: string\n\t\ttextAlign: TLDefaultHorizontalAlignStyle\n\t\tverticalTextAlign: TLDefaultVerticalAlignStyle\n\t\tfontWeight: string\n\t\tfontStyle: string\n\t\tlineHeight: number\n\t\twidth: number\n\t\theight: number\n\t\tstroke?: string\n\t\tstrokeWidth?: number\n\t\tfill?: string\n\t\tpadding?: number\n\t\toffsetX?: number\n\t\toffsetY?: number\n\t}\n) {\n\tconst { padding = 0 } = opts\n\n\t// Create the text element\n\tconst textElm = document.createElementNS('http://www.w3.org/2000/svg', 'text')\n\ttextElm.setAttribute('font-size', opts.fontSize + 'px')\n\ttextElm.setAttribute('font-family', opts.fontFamily)\n\ttextElm.setAttribute('font-style', opts.fontStyle)\n\ttextElm.setAttribute('font-weight', opts.fontWeight)\n\ttextElm.setAttribute('line-height', opts.lineHeight * opts.fontSize + 'px')\n\ttextElm.setAttribute('dominant-baseline', 'mathematical')\n\ttextElm.setAttribute('alignment-baseline', 'mathematical')\n\n\tif (spans.length === 0) return textElm\n\n\tconst bounds = Box2d.From(spans[0].box)\n\tfor (const { box } of spans) {\n\t\tbounds.union(box)\n\t}\n\n\tconst offsetX = padding + (opts.offsetX ?? 0)\n\t// const offsetY = (Math.ceil(opts.height) - bounds.height + opts.fontSize) / 2 + (opts.offsetY ?? 0)\n\tconst offsetY =\n\t\t(opts.offsetY ?? 0) +\n\t\topts.fontSize / 2 +\n\t\t(opts.verticalTextAlign === 'start'\n\t\t\t? padding\n\t\t\t: opts.verticalTextAlign === 'end'\n\t\t\t? opts.height - padding - bounds.height\n\t\t\t: (Math.ceil(opts.height) - bounds.height) / 2)\n\n\t// Create text span elements for each word\n\tlet currentLineTop = null\n\tfor (const { text, box } of spans) {\n\t\t// if we broke a line, add a line break span. This helps tools like\n\t\t// figma import our exported svg correctly\n\t\tconst didBreakLine = currentLineTop !== null && box.y > currentLineTop\n\t\tif (didBreakLine) {\n\t\t\tconst lineBreakTspan = document.createElementNS('http://www.w3.org/2000/svg', 'tspan')\n\t\t\tlineBreakTspan.setAttribute('alignment-baseline', 'mathematical')\n\t\t\tlineBreakTspan.setAttribute('x', offsetX + 'px')\n\t\t\tlineBreakTspan.setAttribute('y', box.y + offsetY + 'px')\n\t\t\tlineBreakTspan.textContent = '\\n'\n\t\t\ttextElm.appendChild(lineBreakTspan)\n\t\t}\n\n\t\tconst tspan = document.createElementNS('http://www.w3.org/2000/svg', 'tspan')\n\t\ttspan.setAttribute('alignment-baseline', 'mathematical')\n\t\ttspan.setAttribute('x', box.x + offsetX + 'px')\n\t\ttspan.setAttribute('y', box.y + offsetY + 'px')\n\t\tconst cleanText = correctSpacesToNbsp(text)\n\t\ttspan.textContent = cleanText\n\t\ttextElm.appendChild(tspan)\n\n\t\tcurrentLineTop = box.y\n\t}\n\n\tif (opts.stroke && opts.strokeWidth) {\n\t\ttextElm.setAttribute('stroke', opts.stroke)\n\t\ttextElm.setAttribute('stroke-width', opts.strokeWidth + 'px')\n\t}\n\n\tif (opts.fill) {\n\t\ttextElm.setAttribute('fill', opts.fill)\n\t}\n\n\treturn textElm\n}\n"], "mappings": "AAAA;AAAA,EACC;AAAA,OAKM;AAEP,SAAS,oBAAoB,OAAe;AAC3C,SAAO,MAAM,QAAQ,OAAO,MAAM;AACnC;AAGO,SAAS,8BACf,QACA,OACA,MAiBC;AACD,QAAM,EAAE,UAAU,EAAE,IAAI;AAGxB,QAAM,UAAU,SAAS,gBAAgB,8BAA8B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