circuit-to-svg 0.0.415 → 0.0.417

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,6 +1,35 @@
1
+ // lib/utils/stringify-svg.ts
2
+ import base64Font from "@tscircuit/alphabet/base64font";
3
+ import { stringify } from "svgson";
4
+ var ALPHABET_FONT_FAMILY = "TscircuitAlphabet";
5
+ var FONT_STYLE_ATTRIBUTE = "data-tscircuit-alphabet-font";
6
+ function stringifySvg(svg) {
7
+ const withoutEmbeddedFont = (node) => ({
8
+ ...node,
9
+ children: (node.children ?? []).filter((child) => !(FONT_STYLE_ATTRIBUTE in (child.attributes ?? {}))).map(withoutEmbeddedFont)
10
+ });
11
+ const root = withoutEmbeddedFont(svg);
12
+ root.children.push({
13
+ name: "style",
14
+ type: "element",
15
+ value: "",
16
+ attributes: { [FONT_STYLE_ATTRIBUTE]: "", type: "text/css" },
17
+ children: [
18
+ {
19
+ name: "",
20
+ type: "text",
21
+ value: `@font-face { font-family: '${ALPHABET_FONT_FAMILY}'; src: url('data:font/ttf;base64,${base64Font}') format('truetype'); font-weight: normal; font-style: normal; }`,
22
+ attributes: {},
23
+ children: []
24
+ }
25
+ ]
26
+ });
27
+ return stringify(root);
28
+ }
29
+
1
30
  // lib/pcb/convert-circuit-json-to-pcb-svg.ts
2
31
  import "circuit-json";
3
- import { stringify } from "svgson";
32
+ import "svgson";
4
33
  import {
5
34
  applyToPoint as applyToPoint47,
6
35
  compose as compose7,
@@ -937,7 +966,7 @@ function createSvgObjectsFromPcbFabricationNoteText(pcbFabNoteText, ctx) {
937
966
  attributes: {
938
967
  x: "0",
939
968
  y: "0",
940
- "font-family": "Arial, sans-serif",
969
+ "font-family": ALPHABET_FONT_FAMILY,
941
970
  "font-size": transformedFontSize.toString(),
942
971
  "text-anchor": textAnchor,
943
972
  "dominant-baseline": dominantBaseline,
@@ -1275,7 +1304,7 @@ function createSvgObjectsFromPcbFabricationNoteDimension(dimension, ctx) {
1275
1304
  y: textY.toString(),
1276
1305
  fill: lineColor,
1277
1306
  "font-size": transformedFontSize.toString(),
1278
- "font-family": "Arial, sans-serif",
1307
+ "font-family": ALPHABET_FONT_FAMILY,
1279
1308
  "text-anchor": "middle",
1280
1309
  "dominant-baseline": "central",
1281
1310
  class: "pcb-fabrication-note-dimension-text",
@@ -3140,7 +3169,6 @@ import {
3140
3169
  scale,
3141
3170
  toString as matrixToString2
3142
3171
  } from "transformation-matrix";
3143
- import { lineAlphabet as lineAlphabet2 } from "@tscircuit/alphabet";
3144
3172
 
3145
3173
  // lib/pcb/svg-object-fns/create-pcb-alphabet-text-geometry.ts
3146
3174
  import { lineAlphabet } from "@tscircuit/alphabet";
@@ -3263,22 +3291,85 @@ function segmentsToPathData(segments) {
3263
3291
  ).join(" ");
3264
3292
  }
3265
3293
 
3266
- // lib/pcb/svg-object-fns/create-svg-objects-from-pcb-silkscreen-text.ts
3267
- var alphabetBounds = (() => {
3268
- let maxX = 0;
3269
- let minY = Infinity;
3270
- let maxY = -Infinity;
3271
- for (const segments of Object.values(lineAlphabet2)) {
3272
- for (const seg of segments) {
3273
- maxX = Math.max(maxX, seg.x1, seg.x2);
3274
- minY = Math.min(minY, seg.y1, seg.y2);
3275
- maxY = Math.max(maxY, seg.y1, seg.y2);
3294
+ // lib/pcb/svg-object-fns/create-alphabet-knockout-text.ts
3295
+ import { lineAlphabet as lineAlphabet2 } from "@tscircuit/alphabet";
3296
+ var ASCENDER = 0.94;
3297
+ var STROKE_RADIUS = 0.09 / 2;
3298
+ var glyphBounds = Object.fromEntries(
3299
+ Object.entries(lineAlphabet2).map(([char, segments]) => {
3300
+ const xs = segments.flatMap((s) => [s.x1, s.x2]);
3301
+ const ys = segments.flatMap((s) => [s.y1, s.y2]);
3302
+ return [
3303
+ char,
3304
+ segments.length ? {
3305
+ minX: Math.min(...xs) - STROKE_RADIUS,
3306
+ maxX: Math.max(...xs) + STROKE_RADIUS,
3307
+ minY: -(Math.max(...ys) + STROKE_RADIUS) * ASCENDER,
3308
+ maxY: -(Math.min(...ys) - STROKE_RADIUS) * ASCENDER
3309
+ } : null
3310
+ ];
3311
+ })
3312
+ );
3313
+ var CHAR_ADVANCE = Math.round(
3314
+ Math.max(
3315
+ ...Object.values(glyphBounds).map((b) => b ? b.maxX - b.minX : 0)
3316
+ ) * 1e3
3317
+ ) / 1e3;
3318
+ function createAlphabetKnockoutText(text, fontSize) {
3319
+ let bounds = null;
3320
+ const lines = text.split("\n");
3321
+ const children = lines.map((line, index) => {
3322
+ const chars = Array.from(line);
3323
+ const x = -(chars.length * CHAR_ADVANCE * fontSize) / 2;
3324
+ const y = index * fontSize * 1.1;
3325
+ chars.forEach((char, charIndex) => {
3326
+ const glyph = glyphBounds[char];
3327
+ if (!glyph) return;
3328
+ const offset = x + charIndex * CHAR_ADVANCE * fontSize;
3329
+ const ink = {
3330
+ minX: offset + glyph.minX * fontSize,
3331
+ maxX: offset + glyph.maxX * fontSize,
3332
+ minY: y + glyph.minY * fontSize,
3333
+ maxY: y + glyph.maxY * fontSize
3334
+ };
3335
+ bounds = bounds ? {
3336
+ minX: Math.min(bounds.minX, ink.minX),
3337
+ maxX: Math.max(bounds.maxX, ink.maxX),
3338
+ minY: Math.min(bounds.minY, ink.minY),
3339
+ maxY: Math.max(bounds.maxY, ink.maxY)
3340
+ } : ink;
3341
+ });
3342
+ return {
3343
+ name: "tspan",
3344
+ type: "element",
3345
+ value: "",
3346
+ attributes: { x: x.toString(), y: y.toString() },
3347
+ children: [
3348
+ { name: "", type: "text", value: line, attributes: {}, children: [] }
3349
+ ]
3350
+ };
3351
+ });
3352
+ return {
3353
+ bounds,
3354
+ textNode: {
3355
+ name: "text",
3356
+ type: "element",
3357
+ value: "",
3358
+ attributes: {
3359
+ "font-family": ALPHABET_FONT_FAMILY,
3360
+ "font-size": fontSize.toString(),
3361
+ "text-anchor": "start",
3362
+ "dominant-baseline": "alphabetic",
3363
+ "xml:space": "preserve",
3364
+ fill: "black",
3365
+ stroke: "none"
3366
+ },
3367
+ children
3276
3368
  }
3277
- }
3278
- return { width: maxX, height: maxY - minY };
3279
- })();
3280
- var INTER_CHAR_SPACING_RATIO = 0.2;
3281
- var LINE_HEIGHT_MULTIPLIER = 1.1;
3369
+ };
3370
+ }
3371
+
3372
+ // lib/pcb/svg-object-fns/create-svg-objects-from-pcb-silkscreen-text.ts
3282
3373
  var silkscreenMaskIdCounter = 0;
3283
3374
  function createSvgObjectsFromPcbSilkscreenText(pcbSilkscreenText, ctx) {
3284
3375
  const { transform, layer: layerFilter, colorMap: colorMap2 } = ctx;
@@ -3310,33 +3401,16 @@ function createSvgObjectsFromPcbSilkscreenText(pcbSilkscreenText, ctx) {
3310
3401
  const isBottom = layer === "bottom";
3311
3402
  const applyMirror = isBottom ? true : is_mirrored === true;
3312
3403
  if (is_knockout) {
3313
- const scaledFontSize = font_size * (2 / 3) / alphabetBounds.height;
3314
- const charSpacing = alphabetBounds.width * INTER_CHAR_SPACING_RATIO;
3315
- const geometry = createPcbAlphabetTextGeometry({
3316
- text,
3317
- anchorAlignment: anchor_alignment,
3318
- fontSize: scaledFontSize,
3319
- charAdvance: (alphabetBounds.width + charSpacing) * scaledFontSize,
3320
- spaceAdvance: (alphabetBounds.width + charSpacing) * scaledFontSize * 0.6,
3321
- trailingSpacing: charSpacing * scaledFontSize,
3322
- lineHeight: scaledFontSize * alphabetBounds.height * LINE_HEIGHT_MULTIPLIER,
3323
- mapSegment: (segment, offsetX, offsetY, fontSize) => ({
3324
- x1: offsetX + segment.x1 * fontSize,
3325
- y1: offsetY + (1 - segment.y1) * fontSize,
3326
- x2: offsetX + segment.x2 * fontSize,
3327
- y2: offsetY + (1 - segment.y2) * fontSize
3328
- })
3329
- });
3330
- if (!geometry.bounds || !geometry.pathData) return [];
3331
- const padLeft = knockout_padding?.left ?? scaledFontSize * 0.5;
3332
- const padRight = knockout_padding?.right ?? scaledFontSize * 0.5;
3333
- const padTop = knockout_padding?.top ?? scaledFontSize * 0.3;
3334
- const padBottom = knockout_padding?.bottom ?? scaledFontSize * 0.3;
3404
+ const geometry = createAlphabetKnockoutText(text, font_size);
3405
+ if (!geometry.bounds) return [];
3406
+ const padLeft = knockout_padding?.left ?? font_size * 0.5;
3407
+ const padRight = knockout_padding?.right ?? font_size * 0.5;
3408
+ const padTop = knockout_padding?.top ?? font_size * 0.3;
3409
+ const padBottom = knockout_padding?.bottom ?? font_size * 0.3;
3335
3410
  const rectX = geometry.bounds.minX - padLeft;
3336
3411
  const rectY = geometry.bounds.minY - padTop;
3337
3412
  const rectW = geometry.bounds.maxX - geometry.bounds.minX + padLeft + padRight;
3338
3413
  const rectH = geometry.bounds.maxY - geometry.bounds.minY + padTop + padBottom;
3339
- const strokeWidth = scaledFontSize * 0.15;
3340
3414
  const knockoutBounds = {
3341
3415
  minX: rectX,
3342
3416
  minY: rectY,
@@ -3363,20 +3437,7 @@ function createSvgObjectsFromPcbSilkscreenText(pcbSilkscreenText, ctx) {
3363
3437
  },
3364
3438
  children: []
3365
3439
  },
3366
- {
3367
- name: "path",
3368
- type: "element",
3369
- value: "",
3370
- attributes: {
3371
- d: geometry.pathData,
3372
- fill: "none",
3373
- stroke: "black",
3374
- "stroke-width": strokeWidth.toString(),
3375
- "stroke-linecap": "round",
3376
- "stroke-linejoin": "round"
3377
- },
3378
- children: []
3379
- }
3440
+ geometry.textNode
3380
3441
  ];
3381
3442
  const hasKnockoutAnchorOffset = knockoutAnchorOffset.x !== 0 || knockoutAnchorOffset.y !== 0;
3382
3443
  const maskChildren = hasKnockoutAnchorOffset ? [
@@ -3522,7 +3583,7 @@ function createSvgObjectsFromPcbSilkscreenText(pcbSilkscreenText, ctx) {
3522
3583
  dx: "0",
3523
3584
  dy: "0",
3524
3585
  fill: silkscreenColor,
3525
- "font-family": "Arial, sans-serif",
3586
+ "font-family": ALPHABET_FONT_FAMILY,
3526
3587
  "font-size": transformedFontSize.toString(),
3527
3588
  "text-anchor": textAnchor,
3528
3589
  "dominant-baseline": dominantBaseline,
@@ -7745,7 +7806,7 @@ function getSoftwareUsedString(circuitJson) {
7745
7806
  var package_default = {
7746
7807
  name: "circuit-to-svg",
7747
7808
  type: "module",
7748
- version: "0.0.414",
7809
+ version: "0.0.416",
7749
7810
  repository: {
7750
7811
  type: "git",
7751
7812
  url: "https://github.com/tscircuit/circuit-to-svg.git"
@@ -7768,7 +7829,7 @@ var package_default = {
7768
7829
  devDependencies: {
7769
7830
  "@biomejs/biome": "^1.9.4",
7770
7831
  "@resvg/resvg-js": "^2.6.2",
7771
- "@tscircuit/alphabet": "^0.0.25",
7832
+ "@tscircuit/alphabet": "^0.0.27",
7772
7833
  "@tscircuit/checks-latest": "npm:@tscircuit/checks@0.0.179",
7773
7834
  "@types/bun": "^1.2.8",
7774
7835
  "@types/debug": "^4.1.12",
@@ -9335,7 +9396,7 @@ function convertCircuitJsonToPcbSvg(circuitJson, options) {
9335
9396
  children: children.filter((child) => child !== null)
9336
9397
  };
9337
9398
  try {
9338
- return stringify(svgObject);
9399
+ return stringifySvg(svgObject);
9339
9400
  } catch (error) {
9340
9401
  console.error("Error stringifying SVG object:", error);
9341
9402
  throw error;
@@ -9513,7 +9574,7 @@ function createSvgObjectFromPcbBoundary(transform, minX, minY, maxX, maxY) {
9513
9574
  var circuitJsonToPcbSvg = convertCircuitJsonToPcbSvg;
9514
9575
 
9515
9576
  // lib/assembly/convert-circuit-json-to-assembly-svg.ts
9516
- import { stringify as stringify2 } from "svgson";
9577
+ import "svgson";
9517
9578
  import { su as su3 } from "@tscircuit/circuit-json-util";
9518
9579
  import {
9519
9580
  applyToPoint as applyToPoint54,
@@ -10366,7 +10427,7 @@ function convertCircuitJsonToAssemblySvg(soup, options) {
10366
10427
  value: "",
10367
10428
  children
10368
10429
  };
10369
- return stringify2(svgObject);
10430
+ return stringifySvg(svgObject);
10370
10431
  }
10371
10432
  function createSvgObjects2(elm, ctx, soup) {
10372
10433
  const sourceComponents = su3(soup).source_component.list();
@@ -10427,7 +10488,7 @@ function createSvgObjectFromAssemblyBoundary(transform, minX, minY, maxX, maxY)
10427
10488
  }
10428
10489
 
10429
10490
  // lib/pinout/convert-circuit-json-to-pinout-svg.ts
10430
- import { stringify as stringify3 } from "svgson";
10491
+ import "svgson";
10431
10492
  import {
10432
10493
  compose as compose9,
10433
10494
  scale as matrixScale,
@@ -11700,7 +11761,7 @@ function convertCircuitJsonToPinoutSvg(soup, options) {
11700
11761
  value: "",
11701
11762
  children
11702
11763
  };
11703
- return stringify3(svgObject);
11764
+ return stringifySvg(svgObject);
11704
11765
  }
11705
11766
  function createSvgObjects3(elm, ctx, soup) {
11706
11767
  switch (elm.type) {
@@ -11725,7 +11786,6 @@ function createSvgObjects3(elm, ctx, soup) {
11725
11786
  }
11726
11787
 
11727
11788
  // lib/sch/convert-circuit-json-to-schematic-svg.ts
11728
- import { stringify as stringify4 } from "svgson";
11729
11789
  import {
11730
11790
  applyToPoint as applyToPoint89,
11731
11791
  fromTriangles,
@@ -15164,7 +15224,9 @@ var createSvgObjectsFromSchematicComponentWithPrimitives = ({
15164
15224
  for (const elm of circuitJson) {
15165
15225
  if (!("schematic_component_id" in elm) || elm.schematic_component_id !== compId)
15166
15226
  continue;
15167
- if (elm.type === "schematic_line") {
15227
+ if (elm.type === "schematic_text" && schComponent.is_box_with_pins === false) {
15228
+ svgObjects.push(createSvgSchText({ elm, transform, colorMap: colorMap2 }));
15229
+ } else if (elm.type === "schematic_line") {
15168
15230
  svgObjects.push(
15169
15231
  ...createSvgObjectsFromSchematicLine({
15170
15232
  schLine: elm,
@@ -16950,7 +17012,7 @@ function convertCircuitJsonToSchematicSvg(circuitJson, options) {
16950
17012
  ],
16951
17013
  value: ""
16952
17014
  };
16953
- return stringify4(svgObject);
17015
+ return stringifySvg(svgObject);
16954
17016
  }
16955
17017
  var circuitJsonToSchematicSvg = convertCircuitJsonToSchematicSvg;
16956
17018
  function getSchematicGraphicViewport({
@@ -17007,7 +17069,7 @@ function clampGraphicDimension(requestedPixels, availablePixels) {
17007
17069
  }
17008
17070
 
17009
17071
  // lib/sch/convert-circuit-json-to-stacked-schematic-sheets-svg.ts
17010
- import { parseSync as parseSync3, stringify as stringify5 } from "svgson";
17072
+ import { parseSync as parseSync3 } from "svgson";
17011
17073
  import { applyToPoint as applyToPoint90, fromString } from "transformation-matrix";
17012
17074
  var DEFAULT_SHEET_WIDTH = 1e3;
17013
17075
  var DEFAULT_LABEL_HEIGHT = 28;
@@ -17107,7 +17169,7 @@ function convertCircuitJsonToStackedSchematicSheetsSvg(circuitJson, options) {
17107
17169
  ...children
17108
17170
  ]
17109
17171
  };
17110
- return stringify5(svgObject);
17172
+ return stringifySvg(svgObject);
17111
17173
  }
17112
17174
  function getSheetLabelPosition({
17113
17175
  transformStr,
@@ -17128,10 +17190,7 @@ function getSheetLabelPosition({
17128
17190
  }
17129
17191
 
17130
17192
  // lib/convert-circuit-json-to-schematic-simulation-svg.ts
17131
- import { parseSync as parseSync4, stringify as stringify7 } from "svgson";
17132
-
17133
- // lib/sim/convert-circuit-json-to-simulation-graph-svg.ts
17134
- import { stringify as stringify6 } from "svgson";
17193
+ import { parseSync as parseSync4 } from "svgson";
17135
17194
 
17136
17195
  // lib/sim/normalize-simulation-analysis-results.ts
17137
17196
  var getResultName = ({
@@ -18567,7 +18626,7 @@ function convertCircuitJsonToSimulationGraphSvg({
18567
18626
  },
18568
18627
  svgChildren
18569
18628
  );
18570
- return stringify6(svgObject);
18629
+ return stringifySvg(svgObject);
18571
18630
  }
18572
18631
  var buildLogarithmicAxisInfo = (logarithmicCoordinates) => {
18573
18632
  const axisInfo = buildAxisInfo(logarithmicCoordinates);
@@ -18682,7 +18741,7 @@ function convertCircuitJsonToSchematicSimulationSvg({
18682
18741
  },
18683
18742
  children: combinedChildren
18684
18743
  };
18685
- return stringify7(svgObject);
18744
+ return stringifySvg(svgObject);
18686
18745
  }
18687
18746
  function getSvgViewBoxSize(node) {
18688
18747
  const viewBox = node.attributes?.viewBox;
@@ -18705,7 +18764,7 @@ function clamp01(value) {
18705
18764
 
18706
18765
  // lib/pcb/convert-circuit-json-to-solder-paste-mask.ts
18707
18766
  import { distance as distance8 } from "circuit-json";
18708
- import { stringify as stringify8 } from "svgson";
18767
+ import "svgson";
18709
18768
  import {
18710
18769
  applyToPoint as applyToPoint91,
18711
18770
  compose as compose16,
@@ -18821,7 +18880,7 @@ function convertCircuitJsonToSolderPasteMask(circuitJson, options) {
18821
18880
  children
18822
18881
  };
18823
18882
  try {
18824
- return stringify8(svgObject);
18883
+ return stringifySvg(svgObject);
18825
18884
  } catch (error) {
18826
18885
  console.error("Error stringifying SVG object:", error);
18827
18886
  throw error;