@pairdraw/core 0.17.0 → 0.18.0

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.
Files changed (219) hide show
  1. package/dist/canvas/outline-stroke.d.ts.map +1 -1
  2. package/dist/canvas/outline-stroke.js.map +1 -1
  3. package/dist/canvas/overlays/prototype.d.ts +2 -5
  4. package/dist/canvas/overlays/prototype.d.ts.map +1 -1
  5. package/dist/canvas/overlays/prototype.js +3 -19
  6. package/dist/canvas/overlays/prototype.js.map +1 -1
  7. package/dist/canvas/renderer/fonts.js +2 -1
  8. package/dist/canvas/renderer/fonts.js.map +1 -1
  9. package/dist/canvas/renderer.d.ts +4 -0
  10. package/dist/canvas/renderer.d.ts.map +1 -1
  11. package/dist/canvas/renderer.js +4 -0
  12. package/dist/canvas/renderer.js.map +1 -1
  13. package/dist/canvas/scene.d.ts.map +1 -1
  14. package/dist/canvas/scene.js +61 -37
  15. package/dist/canvas/scene.js.map +1 -1
  16. package/dist/canvas/text/font-families.d.ts +4 -3
  17. package/dist/canvas/text/font-families.d.ts.map +1 -1
  18. package/dist/canvas/text/font-families.js +8 -7
  19. package/dist/canvas/text/font-families.js.map +1 -1
  20. package/dist/canvas/text/index.d.ts.map +1 -1
  21. package/dist/canvas/text/index.js +1 -1
  22. package/dist/canvas/text/index.js.map +1 -1
  23. package/dist/canvas/text/paragraph/index.js +2 -2
  24. package/dist/canvas/text/paragraph/index.js.map +1 -1
  25. package/dist/canvas/text/placed.js +2 -2
  26. package/dist/canvas/text/placed.js.map +1 -1
  27. package/dist/canvas/text/renderer.d.ts +2 -0
  28. package/dist/canvas/text/renderer.d.ts.map +1 -1
  29. package/dist/design-jsx/renderer.d.ts.map +1 -1
  30. package/dist/editor/bridges/undo.d.ts +1 -1
  31. package/dist/editor/create.d.ts +1 -1
  32. package/dist/figma-api/index.d.ts +43 -8
  33. package/dist/figma-api/index.d.ts.map +1 -1
  34. package/dist/figma-api/index.js +48 -6
  35. package/dist/figma-api/index.js.map +1 -1
  36. package/dist/figma-api/serialization.js +10 -0
  37. package/dist/figma-api/serialization.js.map +1 -1
  38. package/dist/geometry/connectors.js +2 -14
  39. package/dist/geometry/connectors.js.map +1 -1
  40. package/dist/geometry/index.d.ts +2 -2
  41. package/dist/geometry/scene.d.ts +3 -1
  42. package/dist/geometry/scene.d.ts.map +1 -1
  43. package/dist/geometry/scene.js +16 -1
  44. package/dist/geometry/scene.js.map +1 -1
  45. package/dist/geometry/types.d.ts +7 -1
  46. package/dist/geometry/types.d.ts.map +1 -1
  47. package/dist/io/formats/drawio/export.d.ts.map +1 -1
  48. package/dist/io/formats/drawio/export.js +10 -0
  49. package/dist/io/formats/drawio/export.js.map +1 -1
  50. package/dist/io/formats/drawio/index.d.ts.map +1 -1
  51. package/dist/io/formats/drawio/index.js +32 -1
  52. package/dist/io/formats/drawio/index.js.map +1 -1
  53. package/dist/io/formats/drawio/shapes.d.ts +10 -5
  54. package/dist/io/formats/drawio/shapes.d.ts.map +1 -1
  55. package/dist/io/formats/drawio/shapes.js +10 -6
  56. package/dist/io/formats/drawio/shapes.js.map +1 -1
  57. package/dist/io/formats/drawio/warnings.d.ts +11 -0
  58. package/dist/io/formats/drawio/warnings.d.ts.map +1 -0
  59. package/dist/io/formats/drawio/warnings.js +23 -0
  60. package/dist/io/formats/drawio/warnings.js.map +1 -0
  61. package/dist/io/formats/pdf/dom.d.ts +10 -1
  62. package/dist/io/formats/pdf/dom.d.ts.map +1 -1
  63. package/dist/io/formats/pdf/dom.js +31 -1
  64. package/dist/io/formats/pdf/dom.js.map +1 -1
  65. package/dist/io/formats/pdf/export.js +3 -2
  66. package/dist/io/formats/pdf/export.js.map +1 -1
  67. package/dist/io/formats/raster/headless.d.ts +2 -2
  68. package/dist/io/formats/raster/headless.d.ts.map +1 -1
  69. package/dist/io/formats/raster/headless.js +2 -2
  70. package/dist/io/formats/raster/headless.js.map +1 -1
  71. package/dist/io/formats/raster/render.d.ts +1 -1
  72. package/dist/io/formats/raster/render.d.ts.map +1 -1
  73. package/dist/io/formats/raster/render.js +2 -2
  74. package/dist/io/formats/raster/render.js.map +1 -1
  75. package/dist/io/formats.d.ts.map +1 -1
  76. package/dist/io/formats.js +3 -1
  77. package/dist/io/formats.js.map +1 -1
  78. package/dist/io/registry.d.ts +2 -0
  79. package/dist/io/registry.d.ts.map +1 -1
  80. package/dist/io/registry.js +5 -0
  81. package/dist/io/registry.js.map +1 -1
  82. package/dist/io/types.d.ts +2 -0
  83. package/dist/io/types.d.ts.map +1 -1
  84. package/dist/library/snapshot.d.ts.map +1 -1
  85. package/dist/library/snapshot.js +1 -0
  86. package/dist/library/snapshot.js.map +1 -1
  87. package/dist/lint/linter.d.ts.map +1 -1
  88. package/dist/lint/linter.js +11 -2
  89. package/dist/lint/linter.js.map +1 -1
  90. package/dist/rpc/variables-command.d.ts.map +1 -1
  91. package/dist/rpc/variables-command.js +4 -2
  92. package/dist/rpc/variables-command.js.map +1 -1
  93. package/dist/text/coverage.d.ts.map +1 -1
  94. package/dist/text/coverage.js +6 -3
  95. package/dist/text/coverage.js.map +1 -1
  96. package/dist/text/fallbacks.d.ts +8 -2
  97. package/dist/text/fallbacks.d.ts.map +1 -1
  98. package/dist/text/fallbacks.js +73 -7
  99. package/dist/text/fallbacks.js.map +1 -1
  100. package/dist/text/font/manager.d.ts +14 -6
  101. package/dist/text/font/manager.d.ts.map +1 -1
  102. package/dist/text/font/manager.js +40 -14
  103. package/dist/text/font/manager.js.map +1 -1
  104. package/dist/text/index.d.ts +2 -2
  105. package/dist/text/index.js +2 -2
  106. package/dist/text/outlines.js +1 -1
  107. package/dist/text/outlines.js.map +1 -1
  108. package/dist/text/path/caret.d.ts +2 -4
  109. package/dist/text/path/caret.d.ts.map +1 -1
  110. package/dist/text/path/caret.js.map +1 -1
  111. package/dist/text/prepare.d.ts +5 -1
  112. package/dist/text/prepare.d.ts.map +1 -1
  113. package/dist/text/prepare.js +19 -2
  114. package/dist/text/prepare.js.map +1 -1
  115. package/dist/text/web-font/assets.d.ts.map +1 -1
  116. package/dist/text/web-font/assets.js +13 -21
  117. package/dist/text/web-font/assets.js.map +1 -1
  118. package/dist/text/web-font/resolver.d.ts.map +1 -1
  119. package/dist/text/web-font/resolver.js +4 -1
  120. package/dist/text/web-font/resolver.js.map +1 -1
  121. package/dist/tools/create/components.d.ts +2 -1
  122. package/dist/tools/create/components.d.ts.map +1 -1
  123. package/dist/tools/create/components.js +27 -1
  124. package/dist/tools/create/components.js.map +1 -1
  125. package/dist/tools/create/icons.d.ts.map +1 -1
  126. package/dist/tools/create/icons.js +11 -5
  127. package/dist/tools/create/icons.js.map +1 -1
  128. package/dist/tools/create/vector.d.ts.map +1 -1
  129. package/dist/tools/create/vector.js +9 -6
  130. package/dist/tools/create/vector.js.map +1 -1
  131. package/dist/tools/create.d.ts +2 -2
  132. package/dist/tools/create.js +2 -2
  133. package/dist/tools/modify/effects.d.ts.map +1 -1
  134. package/dist/tools/modify/effects.js +5 -3
  135. package/dist/tools/modify/effects.js.map +1 -1
  136. package/dist/tools/modify/matching.d.ts.map +1 -1
  137. package/dist/tools/modify/matching.js +9 -6
  138. package/dist/tools/modify/matching.js.map +1 -1
  139. package/dist/tools/modify/paint.d.ts.map +1 -1
  140. package/dist/tools/modify/paint.js +7 -5
  141. package/dist/tools/modify/paint.js.map +1 -1
  142. package/dist/tools/modify/text.d.ts.map +1 -1
  143. package/dist/tools/modify/text.js +28 -8
  144. package/dist/tools/modify/text.js.map +1 -1
  145. package/dist/tools/read/libraries.d.ts.map +1 -1
  146. package/dist/tools/read/libraries.js +16 -4
  147. package/dist/tools/read/libraries.js.map +1 -1
  148. package/dist/tools/read/pages.d.ts.map +1 -1
  149. package/dist/tools/read/pages.js.map +1 -1
  150. package/dist/tools/registry/extended.d.ts.map +1 -1
  151. package/dist/tools/registry/extended.js +2 -1
  152. package/dist/tools/registry/extended.js.map +1 -1
  153. package/dist/tools/schema.d.ts +24 -5
  154. package/dist/tools/schema.d.ts.map +1 -1
  155. package/dist/tools/schema.js +80 -6
  156. package/dist/tools/schema.js.map +1 -1
  157. package/dist/tools/structure/batch.d.ts.map +1 -1
  158. package/dist/tools/structure/batch.js +4 -3
  159. package/dist/tools/structure/batch.js.map +1 -1
  160. package/dist/tools/variables/values.d.ts.map +1 -1
  161. package/dist/tools/variables/values.js +4 -2
  162. package/dist/tools/variables/values.js.map +1 -1
  163. package/dist/tools/vector/export.d.ts.map +1 -1
  164. package/dist/tools/vector/export.js +5 -2
  165. package/dist/tools/vector/export.js.map +1 -1
  166. package/package.json +9 -9
  167. package/src/canvas/outline-stroke.ts +2 -6
  168. package/src/canvas/overlays/prototype.ts +5 -27
  169. package/src/canvas/renderer/fonts.ts +2 -1
  170. package/src/canvas/renderer.ts +4 -0
  171. package/src/canvas/scene.ts +92 -63
  172. package/src/canvas/text/font-families.ts +8 -7
  173. package/src/canvas/text/index.ts +3 -1
  174. package/src/canvas/text/paragraph/index.ts +2 -2
  175. package/src/canvas/text/placed.ts +2 -2
  176. package/src/canvas/text/renderer.ts +2 -0
  177. package/src/figma-api/index.ts +90 -9
  178. package/src/figma-api/serialization.ts +14 -0
  179. package/src/geometry/connectors.ts +2 -12
  180. package/src/geometry/index.ts +1 -1
  181. package/src/geometry/scene.ts +19 -0
  182. package/src/geometry/types.ts +8 -0
  183. package/src/io/formats/drawio/export.ts +11 -0
  184. package/src/io/formats/drawio/index.ts +36 -1
  185. package/src/io/formats/drawio/shapes.ts +13 -9
  186. package/src/io/formats/drawio/warnings.ts +27 -0
  187. package/src/io/formats/pdf/dom.ts +29 -0
  188. package/src/io/formats/pdf/export.ts +3 -2
  189. package/src/io/formats/raster/headless.ts +10 -3
  190. package/src/io/formats/raster/render.ts +4 -2
  191. package/src/io/formats.ts +3 -1
  192. package/src/io/registry.ts +12 -0
  193. package/src/io/types.ts +2 -0
  194. package/src/library/snapshot.ts +1 -0
  195. package/src/lint/linter.ts +12 -2
  196. package/src/rpc/variables-command.ts +6 -5
  197. package/src/text/coverage.ts +17 -2
  198. package/src/text/fallbacks.ts +81 -7
  199. package/src/text/font/manager.ts +50 -17
  200. package/src/text/outlines.ts +1 -1
  201. package/src/text/path/caret.ts +3 -4
  202. package/src/text/prepare.ts +34 -2
  203. package/src/text/web-font/assets.ts +11 -15
  204. package/src/text/web-font/resolver.ts +7 -1
  205. package/src/tools/create/components.ts +33 -0
  206. package/src/tools/create/icons.ts +13 -5
  207. package/src/tools/create/vector.ts +16 -6
  208. package/src/tools/create.ts +1 -0
  209. package/src/tools/modify/effects.ts +11 -4
  210. package/src/tools/modify/matching.ts +10 -6
  211. package/src/tools/modify/paint.ts +13 -5
  212. package/src/tools/modify/text.ts +53 -20
  213. package/src/tools/read/libraries.ts +21 -4
  214. package/src/tools/read/pages.ts +1 -0
  215. package/src/tools/registry/extended.ts +2 -0
  216. package/src/tools/schema.ts +109 -5
  217. package/src/tools/structure/batch.ts +10 -5
  218. package/src/tools/variables/values.ts +6 -3
  219. package/src/tools/vector/export.ts +5 -1
@@ -1,6 +1,7 @@
1
1
  /* eslint-disable max-lines -- font loading and registration share FontManager lifecycle state */
2
2
 
3
3
  import type { CanvasKit, TypefaceFontProvider } from 'canvaskit-wasm'
4
+ import { uniq } from 'es-toolkit'
4
5
 
5
6
  import type { FontVariation, SceneGraph } from '@pairdraw/scene-graph'
6
7
  import {
@@ -12,7 +13,12 @@ import {
12
13
  } from '@pairdraw/scene-graph/font-style'
13
14
 
14
15
  import { DEFAULT_FONT_FAMILY, IS_BROWSER } from '#core/constants'
15
- import { fontFallbackEntry, type FontFallbackScript } from '#core/text/fallbacks'
16
+ import {
17
+ fontFallbackEntry,
18
+ LETTER_FALLBACK_SCRIPTS,
19
+ type FontFallbackScript,
20
+ type LetterFallbackScript
21
+ } from '#core/text/fallbacks'
16
22
  import { BundledFonts, type BundledFontLocator } from '#core/text/font/bundled'
17
23
  import { FamilyFaceCatalog, type HostFontFamily } from '#core/text/font/catalog'
18
24
  import { fontFaceRenderFamily, type FamilyFace } from '#core/text/font/faces'
@@ -89,8 +95,11 @@ export class FontManager {
89
95
  })
90
96
  private cjkFallbackFamilies: string[] = []
91
97
  private cjkFallbackPromise: Promise<string[]> | null = null
92
- private arabicFallbackFamilies: string[] = []
93
- private arabicFallbackPromise: Promise<string[]> | null = null
98
+ /** The fallbacks of each script written in letters: Arabic, Hebrew, Thai, Devanagari. */
99
+ private readonly letterFallbackFamilies = new Map<LetterFallbackScript, string[]>(
100
+ LETTER_FALLBACK_SCRIPTS.map((script) => [script, []])
101
+ )
102
+ private readonly letterFallbackPromises = new Map<LetterFallbackScript, Promise<string[]>>()
94
103
 
95
104
  attachProvider(canvasKit: CanvasKit, provider: TypefaceFontProvider): void {
96
105
  this.fontProviders.add(provider)
@@ -623,12 +632,29 @@ export class FontManager {
623
632
  }
624
633
  }
625
634
 
626
- async ensureArabicFallback(): Promise<string[]> {
627
- if (this.arabicFallbackFamilies.length > 0) return this.arabicFallbackFamilies
628
- if (this.arabicFallbackPromise) return this.arabicFallbackPromise
635
+ private ensureLetterFallback(script: LetterFallbackScript): Promise<string[]> {
636
+ const families = this.letterFamilies(script)
637
+ if (families.length > 0) return Promise.resolve(families)
638
+ let pending = this.letterFallbackPromises.get(script)
639
+ if (!pending) {
640
+ pending = this.ensureFallbackFamilies(script, families)
641
+ this.letterFallbackPromises.set(script, pending)
642
+ }
643
+ return pending
644
+ }
645
+
646
+ private letterFamilies(script: LetterFallbackScript): string[] {
647
+ let families = this.letterFallbackFamilies.get(script)
648
+ if (!families) {
649
+ families = []
650
+ this.letterFallbackFamilies.set(script, families)
651
+ }
652
+ return families
653
+ }
629
654
 
630
- this.arabicFallbackPromise = this.ensureFallbackFamilies('arabic', this.arabicFallbackFamilies)
631
- return this.arabicFallbackPromise
655
+ /** The list a script's fallback families are kept in. */
656
+ private fallbackTarget(script: FontFallbackScript): string[] {
657
+ return isLetterFallbackScript(script) ? this.letterFamilies(script) : this.cjkFallbackFamilies
632
658
  }
633
659
 
634
660
  ensureFallbackPack(
@@ -649,11 +675,11 @@ export class FontManager {
649
675
  await Promise.all(
650
676
  scripts.map(async (script) => {
651
677
  signal?.throwIfAborted()
652
- const target = script === 'arabic' ? this.arabicFallbackFamilies : this.cjkFallbackFamilies
678
+ const target = this.fallbackTarget(script)
653
679
  if (signal) {
654
680
  result[script] = await this.ensureFallbackFamilies(script, target, characters, signal)
655
- } else if (script === 'arabic' && !characters) {
656
- result[script] = await this.ensureArabicFallback()
681
+ } else if (isLetterFallbackScript(script) && !characters) {
682
+ result[script] = await this.ensureLetterFallback(script)
657
683
  } else if (script === 'cjk' && !characters) {
658
684
  result[script] = await this.ensureCJKFallback()
659
685
  } else {
@@ -665,14 +691,17 @@ export class FontManager {
665
691
  return result
666
692
  }
667
693
 
668
- getArabicFallbackFamilies(): string[] {
669
- return this.arabicFallbackFamilies
694
+ /**
695
+ * The fallbacks for scripts written in letters, Arabic first, in the order the renderer tries
696
+ * them: each covers its own script, and all come before the CJK fallbacks.
697
+ */
698
+ getLetterFallbackFamilies(): string[] {
699
+ return uniq(LETTER_FALLBACK_SCRIPTS.flatMap((script) => this.letterFamilies(script)))
670
700
  }
671
701
 
672
- setArabicFallbackFamily(family: string): void {
673
- if (!this.arabicFallbackFamilies.includes(family)) {
674
- this.arabicFallbackFamilies.push(family)
675
- }
702
+ setLetterFallbackFamily(script: LetterFallbackScript, family: string): void {
703
+ const families = this.letterFamilies(script)
704
+ if (!families.includes(family)) families.push(family)
676
705
  }
677
706
 
678
707
  private async ensureFallbackFamilies(
@@ -879,3 +908,7 @@ export class FontManager {
879
908
  }
880
909
 
881
910
  export const fontManager = new FontManager()
911
+
912
+ function isLetterFallbackScript(script: FontFallbackScript): script is LetterFallbackScript {
913
+ return LETTER_FALLBACK_SCRIPTS.some((letter) => letter === script)
914
+ }
@@ -87,7 +87,7 @@ function resolvedGlyphStyle(style: TextStyle, char: string): TextStyle | null {
87
87
  }
88
88
 
89
89
  function fallbackFamilies(): string[] {
90
- return [...fontManager.getCJKFallbackFamilies(), ...fontManager.getArabicFallbackFamilies()]
90
+ return [...fontManager.getCJKFallbackFamilies(), ...fontManager.getLetterFallbackFamilies()]
91
91
  }
92
92
 
93
93
  /** `style` in a script fallback family, which has none of the text family's named faces. */
@@ -1,6 +1,8 @@
1
1
  import type { DerivedTextGlyph } from '@pairdraw/scene-graph'
2
2
  import type { Vector } from '@pairdraw/scene-graph/primitives'
3
3
 
4
+ import type { LineSegment } from '#core/geometry/types'
5
+
4
6
  /** How far the font reaches above and below the baseline, in ems. */
5
7
  export interface PathTextLineMetrics {
6
8
  ascent: number
@@ -23,10 +25,7 @@ interface PlacedGlyph {
23
25
  }
24
26
 
25
27
  /** A caret: a line square to the baseline from the descent to the ascent. */
26
- export interface PathTextCaret {
27
- from: Vector
28
- to: Vector
29
- }
28
+ export type PathTextCaret = LineSegment
30
29
 
31
30
  function placedGlyphs(
32
31
  glyphs: readonly DerivedTextGlyph[],
@@ -13,6 +13,7 @@ import { fontGlyphCoverageSync } from '#core/text/font/glyphs'
13
13
  import { fontManager } from '#core/text/font/manager'
14
14
  import { collectGraphFontRequirements, collectNodeFontFaces } from '#core/text/requirements/collect'
15
15
  import { missingGraphFontScripts } from '#core/text/requirements/resolved'
16
+ import { fontFaceDemand, fontResolver } from '#core/text/resolver'
16
17
 
17
18
  import { documentFontStatus, type DocumentFontStatus } from './font/status'
18
19
 
@@ -26,7 +27,7 @@ function laidOutEmojiCharacters(node: SceneNode): string {
26
27
  const faces = [
27
28
  ...collectNodeFontFaces(node),
28
29
  { family: DEFAULT_FONT_FAMILY, style: 'Regular' },
29
- ...[...fontManager.getArabicFallbackFamilies(), ...fontManager.getCJKFallbackFamilies()].map(
30
+ ...[...fontManager.getLetterFallbackFamilies(), ...fontManager.getCJKFallbackFamilies()].map(
30
31
  (family) => ({ family, style: 'Regular' })
31
32
  )
32
33
  ]
@@ -41,11 +42,29 @@ function laidOutEmojiCharacters(node: SceneNode): string {
41
42
  return characters
42
43
  }
43
44
 
45
+ /**
46
+ * A one-shot output cannot wait for a face no source has. Loading the substitute face and settling
47
+ * those demands is what lets the text draw in it instead of leaving the layer empty.
48
+ */
49
+ async function settleUnavailableFonts(
50
+ faces: readonly { family: string; style: string }[],
51
+ characters: string
52
+ ): Promise<void> {
53
+ const unavailable = faces.filter(({ family, style }) => !fontManager.isStyleLoaded(family, style))
54
+ if (unavailable.length === 0) return
55
+ await fontManager.loadFont(DEFAULT_FONT_FAMILY, 'Regular', characters)
56
+ for (const { family, style } of unavailable) {
57
+ fontResolver.exhaust(fontFaceDemand(family, style, characters))
58
+ }
59
+ }
60
+
44
61
  export async function prepareGraphFonts(
45
62
  graph: SceneGraph,
46
63
  nodeIds: readonly string[],
47
64
  /** Characters a writer draws besides the text's own, which the fonts must map too. */
48
- extraCharacters = ''
65
+ extraCharacters = '',
66
+ /** An output that draws once and cannot wait for a face to arrive; see `settleUnavailableFonts`. */
67
+ { settleUnavailable = false }: { settleUnavailable?: boolean } = {}
49
68
  ): Promise<DocumentFontStatus> {
50
69
  const collected = collectGraphFontRequirements(graph, nodeIds)
51
70
  const requirements = extraCharacters
@@ -62,6 +81,19 @@ export async function prepareGraphFonts(
62
81
  missingGraphFontScripts(requirements),
63
82
  requirements.characters
64
83
  )
84
+ if (settleUnavailable) await settleUnavailableFonts(faces, requirements.characters)
85
+ // A face the document asks for and no source has cannot draw; the text is drawn in the default
86
+ // face and the script fallbacks after it, so that face has to be loaded to keep the text
87
+ // visible instead of leaving the layer blank.
88
+ const unavailable = faces.filter(({ family, style }) => !fontManager.isStyleLoaded(family, style))
89
+ if (unavailable.length > 0) {
90
+ await fontManager.loadFont(DEFAULT_FONT_FAMILY, 'Regular', requirements.characters)
91
+ // Nothing else will load these faces in this export, so the render must not wait for them:
92
+ // settling them here is what turns the text into the substituted face it is drawn in.
93
+ for (const { family, style } of unavailable) {
94
+ fontResolver.exhaust(fontFaceDemand(family, style, requirements.characters))
95
+ }
96
+ }
65
97
  // Without a platform that draws emoji, emoji are drawn with Noto, loaded for just these: the
66
98
  // saved ones, and the ones the layout leaves to the emoji metrics font.
67
99
  const emoji =
@@ -1,6 +1,7 @@
1
1
  import type { RemoteFontSource, ResolveFontResult } from 'unifont'
2
2
 
3
3
  import type { WebFontFaceAsset, WebFontFaceRequest } from '@pairdraw/dom-css/export'
4
+ import { fontFileExtension, type FontFileExtension } from '@pairdraw/fig'
4
5
  import { faceLoadKey } from '@pairdraw/scene-graph/font-style'
5
6
 
6
7
  import { documentFontData } from '#core/text/font/document'
@@ -15,6 +16,14 @@ import {
15
16
  type WebFontProviderId
16
17
  } from '#core/text/web-font/resolver'
17
18
 
19
+ /** The `@font-face` format of each document font file. */
20
+ const FONT_FACE_FORMATS: Record<FontFileExtension, WebFontFaceAsset['format']> = {
21
+ ttf: 'truetype',
22
+ otf: 'opentype',
23
+ woff: 'woff',
24
+ woff2: 'woff2'
25
+ }
26
+
18
27
  /**
19
28
  * A document font's file extension and `@font-face` format, read from its signature: TrueType,
20
29
  * OpenType, WOFF, or WOFF2. Null for bytes no document font holds, which registration rejects.
@@ -22,21 +31,8 @@ import {
22
31
  function documentFontFile(
23
32
  data: Uint8Array
24
33
  ): { extension: string; format: WebFontFaceAsset['format'] } | null {
25
- if (data.byteLength < 4) return null
26
- const signature = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]
27
- switch (signature) {
28
- case 0x00010000:
29
- case 0x74727565:
30
- return { extension: 'ttf', format: 'truetype' }
31
- case 0x4f54544f:
32
- return { extension: 'otf', format: 'opentype' }
33
- case 0x774f4646:
34
- return { extension: 'woff', format: 'woff' }
35
- case 0x774f4632:
36
- return { extension: 'woff2', format: 'woff2' }
37
- default:
38
- return null
39
- }
34
+ const extension = fontFileExtension(data)
35
+ return extension ? { extension, format: FONT_FACE_FORMATS[extension] } : null
40
36
  }
41
37
 
42
38
  /**
@@ -6,9 +6,12 @@ import { parseFontStyle } from '@pairdraw/scene-graph/font-style'
6
6
  import { IS_BROWSER } from '#core/constants'
7
7
  import {
8
8
  ARABIC_CHARACTER,
9
+ DEVANAGARI_CHARACTER,
9
10
  HAN_CHARACTER,
10
11
  HANGUL_CHARACTER,
11
- KANA_CHARACTER
12
+ HEBREW_CHARACTER,
13
+ KANA_CHARACTER,
14
+ THAI_CHARACTER
12
15
  } from '#core/text/fallbacks'
13
16
  import { standardFace, type FamilyFace } from '#core/text/font/faces'
14
17
  import { sfntFont } from '#core/text/font/woff2'
@@ -74,6 +77,9 @@ export function normalizedCoverageText(text: string): string {
74
77
  export function webFontSubsetsForText(text: string): string[] {
75
78
  const subsets = new Set(DEFAULT_WEB_FONT_SUBSETS)
76
79
  if (ARABIC_CHARACTER.test(text)) subsets.add('arabic')
80
+ if (HEBREW_CHARACTER.test(text)) subsets.add('hebrew')
81
+ if (THAI_CHARACTER.test(text)) subsets.add('thai')
82
+ if (DEVANAGARI_CHARACTER.test(text)) subsets.add('devanagari')
77
83
  if (HANGUL_CHARACTER.test(text)) subsets.add('korean')
78
84
  if (KANA_CHARACTER.test(text)) subsets.add('japanese')
79
85
  if (HAN_CHARACTER.test(text)) {
@@ -105,3 +105,36 @@ export const exposeInstanceSwap = defineTool({
105
105
  }
106
106
  }
107
107
  })
108
+
109
+ export const detachInstance = defineTool({
110
+ name: 'detach_instance',
111
+
112
+ description:
113
+ 'Detach instances into plain frames, keeping their children, overrides, and position.',
114
+ execution: { kind: 'sync', mutation: 'document' },
115
+ input: v.strictObject({
116
+ ids: v.pipe(v.array(v.string()), v.minLength(1), v.description('Instance node IDs to detach'))
117
+ }),
118
+ execute: (figma, { ids }) => {
119
+ // Validate every id before the first detach so a refused call changes nothing.
120
+ // A repeated id is detached once; detaching it again would hit a frame that
121
+ // no longer exists as an instance and fail halfway through the batch.
122
+ const instances: FigmaNodeProxy[] = []
123
+ const seen = new Set<string>()
124
+ for (const id of ids) {
125
+ if (seen.has(id)) continue
126
+ seen.add(id)
127
+ const node = figma.getNodeById(id)
128
+ if (!node) return { error: `Node "${id}" not found` }
129
+ if (node.type !== 'INSTANCE') {
130
+ return { error: `Node "${id}" is not an instance (${node.type})` }
131
+ }
132
+ instances.push(node)
133
+ }
134
+ try {
135
+ return { ids: instances.map((instance) => instance.detachInstance().id) }
136
+ } catch (error) {
137
+ return { error: error instanceof Error ? error.message : String(error) }
138
+ }
139
+ }
140
+ })
@@ -1,10 +1,9 @@
1
1
  import * as v from 'valibot'
2
2
 
3
- import { parseColor } from '@pairdraw/scene-graph/color'
4
3
  import { iconOptionsSchema, type IconOptions } from '@pairdraw/scene-graph/icons'
5
4
 
6
5
  import { placeIcon } from '#core/icons'
7
- import { toolNumber, defineTool } from '#core/tools/schema'
6
+ import { defineTool, parseToolColor, toolNumber } from '#core/tools/schema'
8
7
 
9
8
  export const fetchIconsTool = defineTool({
10
9
  name: 'fetch_icons',
@@ -120,6 +119,8 @@ export const insertIcon = defineTool({
120
119
  v.pipe(v.string(), v.description('Icon color hex (replaces currentColor). Default: #000000'))
121
120
  ),
122
121
  parent_id: v.optional(v.pipe(v.string(), v.description('Parent node ID for all icons'))),
122
+ x: v.optional(toolNumber(v.pipe(v.number(), v.description('X position in the parent')))),
123
+ y: v.optional(toolNumber(v.pipe(v.number(), v.description('Y position in the parent')))),
123
124
  ...iconOptionInputs
124
125
  }),
125
126
  execute: async (figma, args) => {
@@ -127,8 +128,8 @@ export const insertIcon = defineTool({
127
128
  if (names.length === 0) return { error: 'Provide "names" (array) or "name" (string)' }
128
129
 
129
130
  const size = args.size ?? 24
130
- const color = args.color ?? '#000000'
131
- const parsedColor = parseColor(color)
131
+ const parsedColor = parseToolColor(args.color ?? '#000000')
132
+ if ('error' in parsedColor) return parsedColor
132
133
  const options = iconOptions(args)
133
134
 
134
135
  let icons
@@ -148,7 +149,14 @@ export const insertIcon = defineTool({
148
149
  continue
149
150
  }
150
151
  const parentId = args.parent_id ?? figma.currentPage.id
151
- const frame = placeIcon(figma.graph, parentId, icon, { size, color: parsedColor })
152
+ // A position puts the icon there; without one, its parent's layout places it.
153
+ const overrides =
154
+ args.x === undefined && args.y === undefined ? {} : { x: args.x ?? 0, y: args.y ?? 0 }
155
+ const frame = placeIcon(figma.graph, parentId, icon, {
156
+ size,
157
+ color: parsedColor,
158
+ overrides
159
+ })
152
160
  inserted.push({ id: frame.id, name: frame.name, icon: name })
153
161
  }
154
162
 
@@ -1,11 +1,16 @@
1
1
  import * as v from 'valibot'
2
2
 
3
3
  import type { VectorNetwork } from '@pairdraw/scene-graph'
4
- import { parseColor } from '@pairdraw/scene-graph/color'
5
4
  import { parseSVGPath } from '@pairdraw/scene-graph/parse-path'
6
5
  import { transformVectorNetwork } from '@pairdraw/scene-graph/vector-network'
7
6
 
8
- import { defineTool, nodeSummary, positionInputs, toolNumber } from '#core/tools/schema'
7
+ import {
8
+ defineTool,
9
+ nodeSummary,
10
+ parseOptionalToolColor,
11
+ positionInputs,
12
+ toolNumber
13
+ } from '#core/tools/schema'
9
14
  import { parseVectorNetworkJSON } from '#core/tools/vector/network'
10
15
  import { computeAccurateBounds } from '#core/vector/curve-math'
11
16
 
@@ -68,6 +73,11 @@ export const createVector = defineTool({
68
73
  if ('error' in parsed) return parsed
69
74
  parsedPath = parsed
70
75
  }
76
+ // Both colours are read before the node exists, so a bad one creates nothing.
77
+ const fill = parseOptionalToolColor(args.fill)
78
+ if ('error' in fill) return fill
79
+ const stroke = parseOptionalToolColor(args.stroke)
80
+ if ('error' in stroke) return stroke
71
81
 
72
82
  const node = figma.createVector()
73
83
  node.x = args.x
@@ -77,15 +87,15 @@ export const createVector = defineTool({
77
87
  if (parsedPath.size) node.resize(parsedPath.size.width, parsedPath.size.height)
78
88
  figma.graph.updateNode(node.id, { vectorNetwork: parsedPath.network })
79
89
  }
80
- if (args.fill) {
81
- node.fills = [{ type: 'SOLID', color: parseColor(args.fill) }]
90
+ if (fill.color) {
91
+ node.fills = [{ type: 'SOLID', color: fill.color }]
82
92
  }
83
- if (args.stroke) {
93
+ if (stroke.color) {
84
94
  figma.graph.updateNode(node.id, {
85
95
  strokes: [
86
96
  {
87
97
  type: 'SOLID',
88
- color: parseColor(args.stroke),
98
+ color: stroke.color,
89
99
  weight: args.stroke_weight ?? 1,
90
100
  opacity: 1,
91
101
  visible: true,
@@ -4,6 +4,7 @@ export {
4
4
  combineAsVariants,
5
5
  createComponent,
6
6
  createInstance,
7
+ detachInstance,
7
8
  exposeInstanceSwap
8
9
  } from './create/components'
9
10
  export { fetchIconsTool, insertIcon, searchIconsTool } from './create/icons'
@@ -1,10 +1,14 @@
1
1
  import * as v from 'valibot'
2
2
 
3
- import { parseColor } from '@pairdraw/scene-graph/color'
4
-
5
3
  import { DEFAULT_SHADOW_COLOR } from '#core/constants'
6
4
  import type { FigmaEffect } from '#core/figma-api/effects'
7
- import { toolNumber, nodeIdInputSchema, defineTool, nodeNotFound } from '#core/tools/schema'
5
+ import {
6
+ defineTool,
7
+ nodeIdInputSchema,
8
+ nodeNotFound,
9
+ parseOptionalToolColor,
10
+ toolNumber
11
+ } from '#core/tools/schema'
8
12
 
9
13
  export const setEffects = defineTool({
10
14
  name: 'set_effects',
@@ -30,6 +34,9 @@ export const setEffects = defineTool({
30
34
  execute: (figma, args) => {
31
35
  const node = figma.getNodeById(args.id)
32
36
  if (!node) return nodeNotFound(args.id)
37
+ const parsedColor = parseOptionalToolColor(args.color)
38
+ if ('error' in parsedColor) return parsedColor
39
+ const shadowColor = parsedColor.color ?? { ...DEFAULT_SHADOW_COLOR }
33
40
 
34
41
  const effect: FigmaEffect =
35
42
  args.type === 'FOREGROUND_BLUR' || args.type === 'BACKGROUND_BLUR'
@@ -41,7 +48,7 @@ export const setEffects = defineTool({
41
48
  }
42
49
  : {
43
50
  type: args.type,
44
- color: args.color ? parseColor(args.color) : { ...DEFAULT_SHADOW_COLOR },
51
+ color: shadowColor,
45
52
  offset: { x: args.offset_x, y: args.offset_y },
46
53
  radius: args.radius,
47
54
  spread: args.spread,
@@ -2,7 +2,6 @@ import { mapValues, pick } from 'es-toolkit'
2
2
  import * as v from 'valibot'
3
3
 
4
4
  import type { Fill, NodeType, SceneNode } from '@pairdraw/scene-graph'
5
- import { tryParseColor } from '@pairdraw/scene-graph/color'
6
5
  import { copyStroke } from '@pairdraw/scene-graph/copy'
7
6
  import type { Color } from '@pairdraw/scene-graph/primitives'
8
7
 
@@ -12,7 +11,7 @@ import { editNode } from '#core/figma-api/accessor-utils'
12
11
  import type { FigmaFontNameInput } from '#core/figma-api/fonts/names'
13
12
  import { getStyledTextSegments } from '#core/figma-api/text/style'
14
13
  import { collectNodeFontFaces } from '#core/text/requirements/collect'
15
- import { defineTool, toolFailure, toolNumber } from '#core/tools/schema'
14
+ import { defineTool, parseToolColor, toolFailure, toolNumber } from '#core/tools/schema'
16
15
 
17
16
  import { textFonts } from './text'
18
17
 
@@ -84,6 +83,13 @@ function channels(color: Color): string {
84
83
  return [color.r, color.g, color.b].map((value) => Math.round(value * 255)).join(',')
85
84
  }
86
85
 
86
+ /** A colour as a tool reads it, so a bad value fails with the message other tools give. */
87
+ function colorOf(value: string): Color {
88
+ const parsed = parseToolColor(value)
89
+ if ('error' in parsed) throw new Error(parsed.error)
90
+ return parsed
91
+ }
92
+
87
93
  interface ColorSwap {
88
94
  match: (paint: Fill) => boolean
89
95
  apply: (paint: Fill) => Fill
@@ -91,10 +97,8 @@ interface ColorSwap {
91
97
 
92
98
  /** The swap a colour pair makes; throws on a value that is not a colour. */
93
99
  function colorSwap(pair: ColorPair): ColorSwap {
94
- const from = tryParseColor(pair.from)
95
- const to = tryParseColor(pair.to)
96
- if (!from) throw new Error(`"${pair.from}" is not a color`)
97
- if (!to) throw new Error(`"${pair.to}" is not a color`)
100
+ const from = colorOf(pair.from)
101
+ const to = colorOf(pair.to)
98
102
  const key = channels(from)
99
103
  const percent = (value: number) => Math.round(value * 100)
100
104
  return {
@@ -1,12 +1,17 @@
1
1
  import { isValid, toUint8Array } from 'js-base64'
2
2
  import * as v from 'valibot'
3
3
 
4
- import { parseColor } from '@pairdraw/scene-graph/color'
5
4
  import { gradientTransformRows, linearGradientTransform } from '@pairdraw/scene-graph/gradient'
6
5
  import type { Vector } from '@pairdraw/scene-graph/primitives'
7
6
 
8
7
  import { storeImage } from '#core/figma-api/images'
9
- import { defineTool, nodeIdInputSchema, toolFailure, toolNumber } from '#core/tools/schema'
8
+ import {
9
+ defineTool,
10
+ nodeIdInputSchema,
11
+ parseToolColor,
12
+ toolFailure,
13
+ toolNumber
14
+ } from '#core/tools/schema'
10
15
 
11
16
  /**
12
17
  * Stretch as Figma's API writes it: a crop whose window is the whole image, which the image fill
@@ -40,10 +45,12 @@ export const setFill = defineTool({
40
45
  const node = figma.getNodeById(id)
41
46
  if (!node) return { error: `Node "${id}" not found` }
42
47
 
43
- const c = parseColor(color)
48
+ const c = parseToolColor(color)
49
+ if ('error' in c) return c
44
50
 
45
51
  if (gradient && color_end) {
46
- const cEnd = parseColor(color_end)
52
+ const cEnd = parseToolColor(color_end)
53
+ if ('error' in cEnd) return cEnd
47
54
  // Edge middles of the layer's unit square that each direction runs between.
48
55
  const ends: Record<string, [Vector, Vector]> = {
49
56
  'top-bottom': [
@@ -104,7 +111,8 @@ export const setStroke = defineTool({
104
111
  const node = figma.getNodeById(id)
105
112
  if (!node) return { error: `Node "${id}" not found` }
106
113
 
107
- const c = parseColor(color)
114
+ const c = parseToolColor(color)
115
+ if ('error' in c) return c
108
116
  node.strokes = [{ type: 'SOLID', color: c }]
109
117
  node.strokeWeight = weight
110
118
  node.strokeAlign = align