@open-pencil/scene-graph 0.17.0-beta.4 → 0.17.0-beta.6
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/buffered-events.js +1 -1
- package/dist/buffered-events.js.map +1 -1
- package/dist/clipping.d.ts +7 -1
- package/dist/clipping.d.ts.map +1 -1
- package/dist/clipping.js +23 -19
- package/dist/clipping.js.map +1 -1
- package/dist/comments/schema.d.ts.map +1 -1
- package/dist/comments/schema.js +1 -0
- package/dist/comments/schema.js.map +1 -1
- package/dist/comments/threads.d.ts +2 -0
- package/dist/comments/threads.d.ts.map +1 -1
- package/dist/comments/threads.js +11 -5
- package/dist/comments/threads.js.map +1 -1
- package/dist/components/index.d.ts +2 -2
- package/dist/components/index.js +2 -2
- package/dist/components/properties.d.ts +1 -3
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +3 -10
- package/dist/components/properties.js.map +1 -1
- package/dist/constants.d.ts +12 -2
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +42 -1
- package/dist/constants.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +2 -0
- package/dist/copy.js.map +1 -1
- package/dist/corners.d.ts +22 -0
- package/dist/corners.d.ts.map +1 -0
- package/dist/corners.js +52 -0
- package/dist/corners.js.map +1 -0
- package/dist/css/grid.d.ts +4 -1
- package/dist/css/grid.d.ts.map +1 -1
- package/dist/css/grid.js +40 -7
- package/dist/css/grid.js.map +1 -1
- package/dist/css/index.d.ts +3 -3
- package/dist/css/index.js +3 -3
- package/dist/css/layout.d.ts +13 -1
- package/dist/css/layout.d.ts.map +1 -1
- package/dist/css/layout.js +26 -1
- package/dist/css/layout.js.map +1 -1
- package/dist/css/values.d.ts +23 -1
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +39 -1
- package/dist/css/values.js.map +1 -1
- package/dist/font-style.d.ts +2 -1
- package/dist/font-style.d.ts.map +1 -1
- package/dist/font-style.js +31 -6
- package/dist/font-style.js.map +1 -1
- package/dist/geometry.d.ts +68 -3
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +159 -25
- package/dist/geometry.js.map +1 -1
- package/dist/gradient.d.ts +9 -2
- package/dist/gradient.d.ts.map +1 -1
- package/dist/gradient.js +29 -1
- package/dist/gradient.js.map +1 -1
- package/dist/graph.d.ts +9 -2
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +46 -10
- package/dist/graph.js.map +1 -1
- package/dist/group-bounds.d.ts.map +1 -1
- package/dist/group-bounds.js +4 -3
- package/dist/group-bounds.js.map +1 -1
- package/dist/guides.d.ts +11 -2
- package/dist/guides.d.ts.map +1 -1
- package/dist/guides.js +30 -1
- package/dist/guides.js.map +1 -1
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +57 -5
- package/dist/hit-test.js.map +1 -1
- package/dist/icons/fit.d.ts +17 -0
- package/dist/icons/fit.d.ts.map +1 -0
- package/dist/icons/fit.js +21 -0
- package/dist/icons/fit.js.map +1 -0
- package/dist/icons/glyph.d.ts.map +1 -1
- package/dist/icons/glyph.js +5 -4
- package/dist/icons/glyph.js.map +1 -1
- package/dist/icons/index.d.ts +2 -1
- package/dist/icons/index.js +2 -1
- package/dist/ids.js +1 -1
- package/dist/ids.js.map +1 -1
- package/dist/images.d.ts +14 -4
- package/dist/images.d.ts.map +1 -1
- package/dist/images.js +45 -5
- package/dist/images.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/assignments.js +39 -6
- package/dist/instances/assignments.js.map +1 -1
- package/dist/instances/index.d.ts +6 -1
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +58 -9
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/kept-overrides.js +2 -2
- package/dist/instances/kept-overrides.js.map +1 -1
- package/dist/instances/matching.js +68 -0
- package/dist/instances/matching.js.map +1 -0
- package/dist/instances/overrides.d.ts +4 -1
- package/dist/instances/overrides.d.ts.map +1 -1
- package/dist/instances/overrides.js +13 -1
- package/dist/instances/overrides.js.map +1 -1
- package/dist/instances/protection.js +67 -0
- package/dist/instances/protection.js.map +1 -0
- package/dist/instances/swap.js +7 -5
- package/dist/instances/swap.js.map +1 -1
- package/dist/instances/sync.js +34 -116
- package/dist/instances/sync.js.map +1 -1
- package/dist/layout/guides.d.ts +17 -1
- package/dist/layout/guides.d.ts.map +1 -1
- package/dist/layout/guides.js +31 -11
- package/dist/layout/guides.js.map +1 -1
- package/dist/layout/index.d.ts +5 -2
- package/dist/layout/index.js +5 -2
- package/dist/layout/limits.d.ts +40 -0
- package/dist/layout/limits.d.ts.map +1 -0
- package/dist/layout/limits.js +54 -0
- package/dist/layout/limits.js.map +1 -0
- package/dist/layout/sizing.d.ts.map +1 -1
- package/dist/layout/sizing.js +0 -1
- package/dist/layout/sizing.js.map +1 -1
- package/dist/layout/spacing.d.ts +10 -0
- package/dist/layout/spacing.d.ts.map +1 -0
- package/dist/layout/spacing.js +12 -0
- package/dist/layout/spacing.js.map +1 -0
- package/dist/layout/strokes.d.ts +10 -0
- package/dist/layout/strokes.d.ts.map +1 -0
- package/dist/layout/strokes.js +30 -0
- package/dist/layout/strokes.js.map +1 -0
- package/dist/masks.d.ts +28 -0
- package/dist/masks.d.ts.map +1 -0
- package/dist/masks.js +53 -0
- package/dist/masks.js.map +1 -0
- package/dist/matrix.d.ts +2 -1
- package/dist/matrix.d.ts.map +1 -1
- package/dist/matrix.js +14 -1
- package/dist/matrix.js.map +1 -1
- package/dist/node-defaults.d.ts +9 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +65 -38
- package/dist/node-defaults.js.map +1 -1
- package/dist/outline/flatten.d.ts.map +1 -1
- package/dist/outline/flatten.js +24 -17
- package/dist/outline/flatten.js.map +1 -1
- package/dist/outline/hit.d.ts +2 -8
- package/dist/outline/hit.d.ts.map +1 -1
- package/dist/outline/hit.js +16 -25
- package/dist/outline/hit.js.map +1 -1
- package/dist/outline/index.d.ts +3 -3
- package/dist/outline/index.js +3 -3
- package/dist/outline/shapes.d.ts +3 -1
- package/dist/outline/shapes.d.ts.map +1 -1
- package/dist/outline/shapes.js +18 -39
- package/dist/outline/shapes.js.map +1 -1
- package/dist/paints/index.d.ts +3 -1
- package/dist/paints/index.js +3 -1
- package/dist/paints/pattern.d.ts +13 -0
- package/dist/paints/pattern.d.ts.map +1 -0
- package/dist/paints/pattern.js +47 -0
- package/dist/paints/pattern.js.map +1 -0
- package/dist/paints/shows.d.ts +11 -0
- package/dist/paints/shows.d.ts.map +1 -0
- package/dist/paints/shows.js +16 -0
- package/dist/paints/shows.js.map +1 -0
- package/dist/parse-path.d.ts +4 -1
- package/dist/parse-path.d.ts.map +1 -1
- package/dist/parse-path.js +15 -1
- package/dist/parse-path.js.map +1 -1
- package/dist/paths/arc.d.ts +32 -0
- package/dist/paths/arc.d.ts.map +1 -0
- package/dist/paths/arc.js +49 -0
- package/dist/paths/arc.js.map +1 -0
- package/dist/paths/index.d.ts +5 -0
- package/dist/paths/index.js +5 -0
- package/dist/paths/network.d.ts +63 -0
- package/dist/paths/network.d.ts.map +1 -0
- package/dist/paths/network.js +315 -0
- package/dist/paths/network.js.map +1 -0
- package/dist/paths/sink.d.ts +20 -0
- package/dist/paths/sink.d.ts.map +1 -0
- package/dist/paths/sink.js +32 -0
- package/dist/paths/sink.js.map +1 -0
- package/dist/paths/smooth-corners.d.ts +25 -0
- package/dist/paths/smooth-corners.d.ts.map +1 -0
- package/dist/paths/smooth-corners.js +196 -0
- package/dist/paths/smooth-corners.js.map +1 -0
- package/dist/plugin-data/fields.d.ts +4 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +2 -0
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/polygon.d.ts +1 -3
- package/dist/polygon.d.ts.map +1 -1
- package/dist/polygon.js +1 -16
- package/dist/polygon.js.map +1 -1
- package/dist/primitives/index.d.ts +1 -7
- package/dist/primitives/index.d.ts.map +1 -1
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +5 -5
- package/dist/resize.js.map +1 -1
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +25 -3
- package/dist/scaling/node.js.map +1 -1
- package/dist/shaders/model.d.ts.map +1 -1
- package/dist/shaders/model.js +1 -1
- package/dist/shaders/model.js.map +1 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +2 -9
- package/dist/shared-styles.js.map +1 -1
- package/dist/strokes.d.ts +39 -0
- package/dist/strokes.d.ts.map +1 -0
- package/dist/strokes.js +104 -0
- package/dist/strokes.js.map +1 -0
- package/dist/text/auto-resize.d.ts +15 -0
- package/dist/text/auto-resize.d.ts.map +1 -0
- package/dist/text/auto-resize.js +23 -0
- package/dist/text/auto-resize.js.map +1 -0
- package/dist/text/index.d.ts +5 -3
- package/dist/text/index.js +5 -3
- package/dist/text/lengths.d.ts +6 -1
- package/dist/text/lengths.d.ts.map +1 -1
- package/dist/text/lengths.js +10 -1
- package/dist/text/lengths.js.map +1 -1
- package/dist/text/overflow.d.ts +14 -5
- package/dist/text/overflow.d.ts.map +1 -1
- package/dist/text/overflow.js +24 -6
- package/dist/text/overflow.js.map +1 -1
- package/dist/text/path.js +10 -0
- package/dist/text/path.js.map +1 -0
- package/dist/text/picture.js +5 -5
- package/dist/text/picture.js.map +1 -1
- package/dist/text/runs.d.ts +29 -0
- package/dist/text/runs.d.ts.map +1 -0
- package/dist/text/runs.js +72 -0
- package/dist/text/runs.js.map +1 -0
- package/dist/transfer/dependencies.d.ts.map +1 -1
- package/dist/transfer/dependencies.js +3 -2
- package/dist/transfer/dependencies.js.map +1 -1
- package/dist/transfer/nodes.js +2 -1
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/types.d.ts +56 -10
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/variables/index.d.ts +4 -3
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +4 -3
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/lookup.d.ts +20 -0
- package/dist/variables/lookup.d.ts.map +1 -0
- package/dist/variables/lookup.js +34 -0
- package/dist/variables/lookup.js.map +1 -0
- package/dist/variables/paints.d.ts +35 -4
- package/dist/variables/paints.d.ts.map +1 -1
- package/dist/variables/paints.js +67 -16
- package/dist/variables/paints.js.map +1 -1
- package/dist/variables/resolution.d.ts +3 -1
- package/dist/variables/resolution.d.ts.map +1 -1
- package/dist/variables/resolution.js +8 -3
- package/dist/variables/resolution.js.map +1 -1
- package/dist/variables/subtree.js +1 -1
- package/dist/variables/subtree.js.map +1 -1
- package/dist/vector-network.d.ts.map +1 -1
- package/dist/vector-network.js +13 -79
- package/dist/vector-network.js.map +1 -1
- package/package.json +28 -7
- package/src/buffered-events.ts +2 -1
- package/src/clipping.ts +11 -8
- package/src/comments/schema.ts +1 -0
- package/src/comments/threads.ts +13 -5
- package/src/components/properties.ts +3 -14
- package/src/constants.ts +25 -1
- package/src/copy.ts +2 -0
- package/src/corners.ts +93 -0
- package/src/css/grid.ts +49 -7
- package/src/css/index.ts +13 -2
- package/src/css/layout.ts +36 -0
- package/src/css/values.ts +49 -0
- package/src/font-style.ts +38 -4
- package/src/geometry.ts +223 -28
- package/src/gradient.ts +23 -1
- package/src/graph.ts +35 -10
- package/src/group-bounds.ts +7 -3
- package/src/guides.ts +26 -1
- package/src/hit-test.ts +68 -10
- package/src/icons/fit.ts +24 -0
- package/src/icons/glyph.ts +6 -4
- package/src/icons/index.ts +1 -0
- package/src/ids.ts +1 -1
- package/src/images.ts +67 -7
- package/src/instances/assignments.ts +65 -7
- package/src/instances/index.ts +77 -13
- package/src/instances/kept-overrides.ts +2 -5
- package/src/instances/matching.ts +101 -0
- package/src/instances/overrides.ts +18 -0
- package/src/instances/protection.ts +103 -0
- package/src/instances/swap.ts +12 -9
- package/src/instances/sync.ts +52 -184
- package/src/layout/guides.ts +42 -9
- package/src/layout/index.ts +3 -0
- package/src/layout/limits.ts +64 -0
- package/src/layout/sizing.ts +0 -2
- package/src/layout/spacing.ts +11 -0
- package/src/layout/strokes.ts +23 -0
- package/src/masks.ts +51 -0
- package/src/matrix.ts +15 -2
- package/src/node-defaults.ts +71 -36
- package/src/outline/flatten.ts +31 -18
- package/src/outline/hit.ts +16 -33
- package/src/outline/index.ts +2 -2
- package/src/outline/shapes.ts +19 -36
- package/src/paints/index.ts +2 -0
- package/src/paints/pattern.ts +45 -0
- package/src/paints/shows.ts +13 -0
- package/src/parse-path.ts +14 -0
- package/src/paths/arc.ts +59 -0
- package/src/paths/index.ts +4 -0
- package/src/paths/network.ts +411 -0
- package/src/paths/sink.ts +50 -0
- package/src/paths/smooth-corners.ts +357 -0
- package/src/plugin-data/fields.ts +2 -0
- package/src/polygon.ts +0 -17
- package/src/primitives/index.ts +0 -5
- package/src/resize.ts +6 -7
- package/src/scaling/node.ts +29 -3
- package/src/shaders/model.ts +2 -1
- package/src/shared-styles.ts +3 -4
- package/src/strokes.ts +121 -0
- package/src/text/auto-resize.ts +25 -0
- package/src/text/index.ts +2 -0
- package/src/text/lengths.ts +11 -0
- package/src/text/overflow.ts +32 -6
- package/src/text/path.ts +13 -0
- package/src/text/picture.ts +5 -5
- package/src/text/runs.ts +80 -0
- package/src/transfer/dependencies.ts +4 -2
- package/src/transfer/nodes.ts +2 -1
- package/src/types.ts +62 -8
- package/src/variables/index.ts +1 -0
- package/src/variables/lookup.ts +31 -0
- package/src/variables/paints.ts +101 -20
- package/src/variables/resolution.ts +15 -2
- package/src/variables/subtree.ts +1 -1
- package/src/vector-network.ts +19 -142
- package/dist/random.d.ts +0 -7
- package/dist/random.d.ts.map +0 -1
- package/dist/random.js +0 -25
- package/dist/random.js.map +0 -1
- package/src/random.ts +0 -28
package/dist/font-style.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"font-style.js","names":[],"sources":["../src/font-style.ts"],"sourcesContent":["export interface FontFamilyStyle {\n family: string\n style: string\n}\n\nexport interface ParsedFontStyle {\n weight: number\n italic: boolean\n}\n\n/**\n * The standard weights by the names Google Fonts gives them, which most fonts and Figma's font\n * list use; a font's own names can differ (\"Semi Bold\", \"Book\", \"Heavy\").\n */\nexport const FONT_WEIGHT_NAMES: Record<number, string> = {\n 100: 'Thin',\n 200: 'ExtraLight',\n 300: 'Light',\n 400: 'Regular',\n 500: 'Medium',\n 600: 'SemiBold',\n 700: 'Bold',\n 800: 'ExtraBold',\n 900: 'Black'\n}\n\nconst FONT_WEIGHT_ALIASES = [\n { weight: 100, names: ['thin', 'hairline', 'extrathin', 'ultrathin'] },\n { weight: 200, names: ['extralight', 'ultralight'] },\n { weight: 300, names: ['light'] },\n { weight: 400, names: ['regular', 'normal', 'book', 'roman', 'plain'] },\n { weight: 500, names: ['medium'] },\n { weight: 600, names: ['semibold', 'demibold'] },\n { weight: 700, names: ['bold'] },\n { weight: 800, names: ['extrabold', 'ultrabold'] },\n { weight: 900, names: ['black', 'heavy'] }\n] as const\n\nconst FONT_WEIGHT_BY_STYLE: ReadonlyMap<string, number> = new Map(\n FONT_WEIGHT_ALIASES.flatMap(({ names, weight }) => names.map((name) => [name, weight] as const))\n)\n\nfunction normalizeFontStyleName(style: string): string {\n return style\n .toLowerCase()\n .replace(/italic|oblique/u, '')\n .replace(/[^a-z0-9]+/gu, '')\n}\n\
|
|
1
|
+
{"version":3,"file":"font-style.js","names":[],"sources":["../src/font-style.ts"],"sourcesContent":["export interface FontFamilyStyle {\n family: string\n style: string\n}\n\nexport interface ParsedFontStyle {\n weight: number\n italic: boolean\n}\n\n/**\n * The standard weights by the names Google Fonts gives them, which most fonts and Figma's font\n * list use; a font's own names can differ (\"Semi Bold\", \"Book\", \"Heavy\").\n */\nexport const FONT_WEIGHT_NAMES: Record<number, string> = {\n 100: 'Thin',\n 200: 'ExtraLight',\n 300: 'Light',\n 400: 'Regular',\n 500: 'Medium',\n 600: 'SemiBold',\n 700: 'Bold',\n 800: 'ExtraBold',\n 900: 'Black'\n}\n\nconst FONT_WEIGHT_ALIASES = [\n { weight: 100, names: ['thin', 'hairline', 'extrathin', 'ultrathin'] },\n { weight: 200, names: ['extralight', 'ultralight'] },\n { weight: 300, names: ['light'] },\n { weight: 400, names: ['regular', 'normal', 'book', 'roman', 'plain'] },\n { weight: 500, names: ['medium'] },\n { weight: 600, names: ['semibold', 'demibold'] },\n { weight: 700, names: ['bold'] },\n { weight: 800, names: ['extrabold', 'ultrabold'] },\n { weight: 900, names: ['black', 'heavy'] }\n] as const\n\nconst FONT_WEIGHT_BY_STYLE: ReadonlyMap<string, number> = new Map(\n FONT_WEIGHT_ALIASES.flatMap(({ names, weight }) => names.map((name) => [name, weight] as const))\n)\n\nfunction normalizeFontStyleName(style: string): string {\n return style\n .toLowerCase()\n .replace(/italic|oblique/u, '')\n .replace(/[^a-z0-9]+/gu, '')\n}\n\nconst FONT_WIDTH_NAMES = [\n 'ultracondensed',\n 'ultracompressed',\n 'extracompressed',\n 'extracondensed',\n 'compressed',\n 'condensed',\n 'narrow',\n 'semicompressed',\n 'semicondensed',\n 'demicondensed',\n 'semiexpanded',\n 'semiextended',\n 'expanded',\n 'extended',\n 'wide',\n 'extraexpanded',\n 'extraextended',\n 'ultraexpanded',\n 'ultraextended'\n]\n\nconst FONT_WIDTH_NAME = new RegExp(\n FONT_WIDTH_NAMES.toSorted((a, b) => b.length - a.length).join('|'),\n 'u'\n)\n\nfunction weightOfStyleName(name: string): number {\n const numericWeight = name.match(/(?:^|[^0-9])([1-9]00)(?:[^0-9]|$)/u)?.[1]\n return numericWeight ? Number(numericWeight) : (FONT_WEIGHT_BY_STYLE.get(name) ?? 400)\n}\n\nexport function parseFontStyle(style: string | undefined): ParsedFontStyle {\n const raw = style ?? ''\n const normalized = normalizeFontStyleName(raw)\n return {\n weight: weightOfStyleName(normalized.replace(FONT_WIDTH_NAME, '')),\n italic: /(?:italic|oblique)/iu.test(raw)\n }\n}\n\nexport function namesWidth(style: string): boolean {\n return FONT_WIDTH_NAME.test(normalizeFontStyleName(style))\n}\n\nexport function styleToWeight(style: string | undefined): number {\n return parseFontStyle(style).weight\n}\n\nexport function normalizeFontFamily(family: string): string {\n return family.replace(/\\s+(Variable|\\d+(?:pt|px|em))$/i, '')\n}\n\nexport function faceLoadKey(weight: number, italic = false): string {\n const rounded = Math.round(weight / 100) * 100\n const label = FONT_WEIGHT_NAMES[rounded] ?? 'Regular'\n return italic ? `${label} Italic` : label\n}\n\n/**\n * The name Google Fonts gives the face at `weight` and slant (\"SemiBold Italic\", and \"Italic\"\n * for the regular weight), for a face whose font has not said its own.\n */\nexport function standardStyleName(weight: number, italic = false): string {\n const label = FONT_WEIGHT_NAMES[Math.round(weight / 100) * 100] ?? 'Regular'\n if (!italic) return label\n return label === 'Regular' ? 'Italic' : `${label} Italic`\n}\n\n/** A style name compared without case or spaces, so \"Semi Bold\" and \"SemiBold\" name one face. */\nexport function styleSpelling(style: string): string {\n return style.toLowerCase().replace(/\\s+/gu, '')\n}\n\n/**\n * Whether `style` only names a weight and slant (\"Bold Italic\", \"Semi Bold\"), so any face of that\n * weight is it; a name such as \"Book\" or \"Condensed Bold\" is one face of its own.\n */\nexport function namesWeight(style: string): boolean {\n const { weight, italic } = parseFontStyle(style)\n const spelling = styleSpelling(style)\n return [faceLoadKey(weight, italic), standardStyleName(weight, italic)]\n .map(styleSpelling)\n .includes(spelling)\n}\n\n/** A face as text sets it: a weight and slant, and the name it was given, if any. */\nexport interface NamedFace {\n fontWeight: number\n italic: boolean\n fontStyle: string | null\n}\n\n/**\n * The style a face loads and draws as. A name that only spells its weight and slant (\"Semi Bold\",\n * \"Italic\") loads as the weight's key, so whichever font of the family draws that weight draws\n * it; any other name (\"Book\", \"Condensed Bold\", \"Retina\") is a face the weight cannot pick, and\n * it loads and draws by that name.\n */\nexport function faceStyleKey({ fontWeight, italic, fontStyle }: NamedFace): string {\n const key = faceLoadKey(fontWeight, italic)\n if (!fontStyle) return key\n const spelling = styleSpelling(fontStyle)\n const weightNames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,aAAa,EAAE,YAAY,QAAQ,aAAgC;CACjF,MAAM,MAAM,YAAY,YAAY,MAAM;CAC1C,IAAI,CAAC,WAAW,OAAO;CACvB,MAAM,WAAW,cAAc,SAAS;CAExC,OADoB,CAAC,KAAK,kBAAkB,YAAY,MAAM,CAAC,CAAC,CAAC,IAAI,aACpD,CAAC,CAAC,SAAS,QAAQ,IAAI,MAAM;AAChD"}
|
package/dist/geometry.d.ts
CHANGED
|
@@ -1,9 +1,24 @@
|
|
|
1
1
|
import { Rect, Vector } from "./primitives/index.js";
|
|
2
|
-
import { Effect, SceneNode, Stroke, StrokeCap, StrokeJoin, VectorNetwork } from "./types.js";
|
|
2
|
+
import { ArcData, Effect, SceneNode, Stroke, StrokeCap, StrokeJoin, VectorNetwork } from "./types.js";
|
|
3
3
|
import { cubicPoint } from "./cubic.js";
|
|
4
4
|
//#region src/geometry.d.ts
|
|
5
5
|
declare function degToRad(degrees: number): number;
|
|
6
6
|
declare function radToDeg(radians: number): number;
|
|
7
|
+
/**
|
|
8
|
+
* Whether a layer's shadows take their spread. Figma spreads those of boxes and ellipses, arcs
|
|
9
|
+
* among them, and frames, and counts it in their render bounds; a text, boolean, star, polygon or
|
|
10
|
+
* vector never spreads (Figma desktop, live).
|
|
11
|
+
*/
|
|
12
|
+
declare function shadowSpreads(node: Pick<SceneNode, 'type'>): boolean;
|
|
13
|
+
/** The spread a shadow on `node` takes: its own, or none on a layer Figma never spreads. */
|
|
14
|
+
declare function shadowSpreadOf(node: Pick<SceneNode, 'type'>, effect: Pick<Effect, 'spread'>): number;
|
|
15
|
+
/**
|
|
16
|
+
* Whether a sweep from `start` to `end` is a whole turn: less than 0.001 rad short of one draws the
|
|
17
|
+
* whole ellipse in Figma (desktop, live: 0.00095 short is whole, 0.001 short an arc).
|
|
18
|
+
*/
|
|
19
|
+
declare function sweepsWholeTurn(start: number, end: number): boolean;
|
|
20
|
+
/** Whether an arc sweeps a whole turn, so it is drawn and edited as a whole ellipse or ring. */
|
|
21
|
+
declare function isWholeArc(arc: Pick<ArcData, 'startingAngle' | 'endingAngle'>): boolean;
|
|
7
22
|
declare function rotatePoint(px: number, py: number, cx: number, cy: number, rad: number): Vector;
|
|
8
23
|
declare function rotatedCorners(cx: number, cy: number, hw: number, hh: number, rotationDeg: number): [Vector, Vector, Vector, Vector];
|
|
9
24
|
declare function rotatedBBox(x: number, y: number, w: number, h: number, rotationDeg: number): {
|
|
@@ -30,11 +45,41 @@ declare function polygonVertices(node: {
|
|
|
30
45
|
type: string;
|
|
31
46
|
starInnerRadius: number;
|
|
32
47
|
}): Vector[];
|
|
48
|
+
/** A rectangle's stroke weights, side by side: top, right, bottom, left. */
|
|
49
|
+
type SideWeights = readonly [top: number, right: number, bottom: number, left: number];
|
|
50
|
+
type SideStrokeNode = Pick<SceneNode, 'independentStrokeWeights' | 'borderTopWeight' | 'borderRightWeight' | 'borderBottomWeight' | 'borderLeftWeight'>;
|
|
51
|
+
/** What deciding on per-side strokes reads: the side weights, and the node's type when known. */
|
|
52
|
+
type StrokeSidesInput = Partial<SideStrokeNode> & {
|
|
53
|
+
type?: string;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Whether `node` strokes each side by its own weight. A line or vector keeps one weight whatever
|
|
57
|
+
* the flag says, since its outline has no sides.
|
|
58
|
+
*/
|
|
59
|
+
declare function drawsStrokeSides(node: StrokeSidesInput): boolean;
|
|
60
|
+
/** The share of a stroke's weight that lies outside the shape it outlines. */
|
|
61
|
+
declare function strokeAlignShare(align: Stroke['align']): number;
|
|
62
|
+
/** A layer's own side weights: top, right, bottom, left. */
|
|
63
|
+
declare function borderWeights(node: SideStrokeNode): SideWeights;
|
|
64
|
+
/** The weight of each side of `node` under `stroke`, its own when its sides have their own. */
|
|
65
|
+
declare function strokeSideWeights(node: SideStrokeNode & {
|
|
66
|
+
type?: string;
|
|
67
|
+
}, stroke: Pick<Stroke, 'weight'>): SideWeights;
|
|
68
|
+
/** A stroke's weight, or with sides of their own the widest side, for what is not drawn per side. */
|
|
69
|
+
declare function strokeWeightOf(node: StrokeSidesInput, stroke: Pick<Stroke, 'weight'>): number;
|
|
70
|
+
/**
|
|
71
|
+
* How far a layer's visible strokes reach past each side of its box: top, right, bottom, left.
|
|
72
|
+
* With sides of their own each side reaches by its own weight (Figma desktop, live: a 1px outside
|
|
73
|
+
* stroke with a 20px right side bounds a 100 × 100 rectangle at 121 × 102).
|
|
74
|
+
*/
|
|
75
|
+
declare function strokeSideReach(node: SideStrokeNode & Pick<SceneNode, 'strokes'> & {
|
|
76
|
+
type?: string;
|
|
77
|
+
}): SideWeights;
|
|
33
78
|
/**
|
|
34
79
|
* What shapes how far a layer's stroke reaches past its box: its type, since a line is stroked to
|
|
35
80
|
* one side, and its join and miter limit, for the tips mitered path corners reach.
|
|
36
81
|
*/
|
|
37
|
-
interface StrokeShape {
|
|
82
|
+
interface StrokeShape extends Partial<SideStrokeNode> {
|
|
38
83
|
type?: string;
|
|
39
84
|
strokeJoin?: StrokeJoin;
|
|
40
85
|
strokeMiterLimit?: number;
|
|
@@ -56,8 +101,28 @@ declare function widestBlurRadius(effect: Pick<Effect, 'radius' | 'progressive'>
|
|
|
56
101
|
* SVG export writes radius / 2, which is wider (`packages/core/tests/canvas/blur.test.ts`).
|
|
57
102
|
*/
|
|
58
103
|
declare function blurSigma(radius: number): number;
|
|
104
|
+
/**
|
|
105
|
+
* How far past what it blurs a Gaussian of `sigma` leaves ink on screen: where Skia's kernel
|
|
106
|
+
* ends, and one more pixel holds its rounding.
|
|
107
|
+
*/
|
|
108
|
+
declare function kernelReach(sigma: number): number;
|
|
109
|
+
/** How far past what it blurs a Figma blur of `radius` leaves ink on screen (`kernelReach`). */
|
|
110
|
+
declare function blurReach(radius: number): number;
|
|
111
|
+
/**
|
|
112
|
+
* How far past its shape a drop shadow of `radius` and colour alpha `alpha` leaves ink on
|
|
113
|
+
* screen. Skia blurs the shape into an 8-bit mask and scales it by `alpha`, so a mask level shows
|
|
114
|
+
* once `alpha` times it rounds to a level: an opaque or half-opaque shadow shows its faintest
|
|
115
|
+
* level, out to where the kernel ends, and a fainter one ends sooner. Measured on CPU raster, the
|
|
116
|
+
* ink ends at 3.0 sigma for alpha 1 and 0.5, 2.4 for 0.25, and 2.1 for 0.1.
|
|
117
|
+
*/
|
|
118
|
+
declare function shadowReach(radius: number, alpha: number): number;
|
|
59
119
|
/** The Figma blur radius that draws a Gaussian of `sigma`, the inverse of `blurSigma`. */
|
|
60
120
|
declare function blurRadiusForSigma(sigma: number): number;
|
|
121
|
+
/**
|
|
122
|
+
* How far a layer's shadows and blurs reach past each side as Figma's render bounds count them:
|
|
123
|
+
* a blur by its radius, a shadow by its radius and spread, moved by its offset. Ink on screen
|
|
124
|
+
* reaches further, to `blurReach`.
|
|
125
|
+
*/
|
|
61
126
|
declare function effectOverflow(effects?: Effect[]): {
|
|
62
127
|
left: number;
|
|
63
128
|
right: number;
|
|
@@ -92,5 +157,5 @@ declare function geometryBlobBounds(paths: Array<{
|
|
|
92
157
|
/** How far a text node's underline or strikethrough reaches below its box. */
|
|
93
158
|
declare function textDecorationOverflow(node: Pick<SceneNode, 'type' | 'fontSize' | 'textDecoration' | 'textUnderlineOffset' | 'textDecorationThickness'>): number;
|
|
94
159
|
//#endregion
|
|
95
|
-
export { GeometryCommand, StrokeShape, VisualBounds, blurRadiusForSigma, blurSigma, computeAbsoluteBounds, computeBounds, cubicPoint, degToRad, drawnBlurRadius, effectOverflow, geometryBlobBounds, intersectVisualBounds, polygonVertices, radToDeg, readGeometryBlob, rotatePoint, rotatedBBox, rotatedCorners, strokeOverflow, textDecorationOverflow, unionVisualBounds, visualBoundsToRect, widestBlurRadius };
|
|
160
|
+
export { GeometryCommand, SideWeights, StrokeShape, VisualBounds, blurRadiusForSigma, blurReach, blurSigma, borderWeights, computeAbsoluteBounds, computeBounds, cubicPoint, degToRad, drawnBlurRadius, drawsStrokeSides, effectOverflow, geometryBlobBounds, intersectVisualBounds, isWholeArc, kernelReach, polygonVertices, radToDeg, readGeometryBlob, rotatePoint, rotatedBBox, rotatedCorners, shadowReach, shadowSpreadOf, shadowSpreads, strokeAlignShare, strokeOverflow, strokeSideReach, strokeSideWeights, strokeWeightOf, sweepsWholeTurn, textDecorationOverflow, unionVisualBounds, visualBoundsToRect, widestBlurRadius };
|
|
96
161
|
//# sourceMappingURL=geometry.d.ts.map
|
package/dist/geometry.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"geometry.d.ts","names":[],"sources":["../src/geometry.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"geometry.d.ts","names":[],"sources":["../src/geometry.ts"],"mappings":";;;;iBAegB,SAAS;iBAIT,SAAS;;;;;;iBAoBT,cAAc,MAAM,KAAK;;iBAKzB,eACd,MAAM,KAAK,oBACX,QAAQ,KAAK;;;;;iBASC,gBAAgB,eAAe;;iBAK/B,WAAW,KAAK,KAAK;iBAIrB,YAAY,YAAY,YAAY,YAAY,YAAY,cAAc;iBAS1E,eACd,YACA,YACA,YACA,YACA,uBACE,QAAQ,QAAQ,QAAQ;iBAYZ,YACd,WACA,WACA,WACA,WACA;EACG;EAAc;EAAe;EAAa;EAAgB;EAAiB;;UAyB/D;EACf;EACA;EACA;EACA;;;iBAsBc,mBAAmB,QAAQ,eAAe;iBAa1C,cAAc,OAAO,SAAS,QAAQ;iBAMtC,gBAAgB;EAC9B;EACA;EACA;EACA;EACA;IACE;;KAuBQ,wBAAwB,aAAa,eAAe,gBAAgB;KAE3E,iBAAiB,KACpB;;KASG,mBAAmB,QAAQ;EAAoB;;;;;;iBAiBpC,iBAAiB,MAAM;;iBAKvB,iBAAiB,OAAO;;iBAOxB,cAAc,MAAM,iBAAiB;;iBAUrC,kBACd,MAAM;EAAmB;GACzB,QAAQ,KAAK,oBACZ;;iBAMa,eAAe,MAAM,kBAAkB,QAAQ,KAAK;;;;;;iBAepD,gBACd,MAAM,iBAAiB,KAAK;EAA0B;IACrD;;;;;UAoBc,oBAAoB,QAAQ;EAC3C;EACA,aAAa;EACb;;iBAec,eACd,UAAU,UACV,cAAc,WACd,UAAU,sBACV,QAAQ;;;;;iBA8CM,gBAAgB;;;;;iBAQhB,iBAAiB,QAAQ,KAAK;;;;;;iBAW9B,UAAU;;;;;iBAWV,YAAY;;iBAKZ,UAAU;;;;;;;;iBAiCV,YAAY,gBAAgB;;iBAW5B,mBAAmB;;;;;;iBASnB,eAAe,UAAU;;;;;;iBAyBzB,sBACd,OAAO;EAAW;EAAY;EAAe;IAC7C,sBAAsB,eAAe,SACpC;iBASa,kBACd,GAAG,qBACH,GAAG,sBACF;iBAWa,sBAAsB,GAAG,cAAc,GAAG,eAAe;;cAS5D;;;;;;;KACD,0BAA0B,8BAA8B;;;;;;iBAmBpD,iBACd,MAAM,YACN,QAAQ,SAAS,iBAAiB,2BAA2B;iBAmB/C,mBAAmB,OAAO;EAAQ,cAAc;KAAgB;;iBAYhE,uBACd,MAAM,KACJ"}
|
package/dist/geometry.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { cubicPoint } from "./cubic.js";
|
|
2
2
|
import { arrowCapOverflow } from "./arrow-caps.js";
|
|
3
|
+
import { joinedSegmentPairs, leavingDirection } from "./paths/network.js";
|
|
3
4
|
//#region src/geometry.ts
|
|
4
5
|
function degToRad(degrees) {
|
|
5
6
|
return degrees * Math.PI / 180;
|
|
@@ -7,6 +8,38 @@ function degToRad(degrees) {
|
|
|
7
8
|
function radToDeg(radians) {
|
|
8
9
|
return radians * 180 / Math.PI;
|
|
9
10
|
}
|
|
11
|
+
const FULL_TURN = 2 * Math.PI;
|
|
12
|
+
/** Layers whose shadows Figma never spreads: a text, boolean, star, polygon or vector. */
|
|
13
|
+
const UNSPREAD_TYPES = /* @__PURE__ */ new Set([
|
|
14
|
+
"TEXT",
|
|
15
|
+
"BOOLEAN_OPERATION",
|
|
16
|
+
"STAR",
|
|
17
|
+
"POLYGON",
|
|
18
|
+
"VECTOR"
|
|
19
|
+
]);
|
|
20
|
+
/**
|
|
21
|
+
* Whether a layer's shadows take their spread. Figma spreads those of boxes and ellipses, arcs
|
|
22
|
+
* among them, and frames, and counts it in their render bounds; a text, boolean, star, polygon or
|
|
23
|
+
* vector never spreads (Figma desktop, live).
|
|
24
|
+
*/
|
|
25
|
+
function shadowSpreads(node) {
|
|
26
|
+
return !UNSPREAD_TYPES.has(node.type);
|
|
27
|
+
}
|
|
28
|
+
/** The spread a shadow on `node` takes: its own, or none on a layer Figma never spreads. */
|
|
29
|
+
function shadowSpreadOf(node, effect) {
|
|
30
|
+
return shadowSpreads(node) ? effect.spread : 0;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Whether a sweep from `start` to `end` is a whole turn: less than 0.001 rad short of one draws the
|
|
34
|
+
* whole ellipse in Figma (desktop, live: 0.00095 short is whole, 0.001 short an arc).
|
|
35
|
+
*/
|
|
36
|
+
function sweepsWholeTurn(start, end) {
|
|
37
|
+
return FULL_TURN - Math.abs(end - start) < .001;
|
|
38
|
+
}
|
|
39
|
+
/** Whether an arc sweeps a whole turn, so it is drawn and edited as a whole ellipse or ring. */
|
|
40
|
+
function isWholeArc(arc) {
|
|
41
|
+
return sweepsWholeTurn(arc.startingAngle, arc.endingAngle);
|
|
42
|
+
}
|
|
10
43
|
function rotatePoint(px, py, cx, cy, rad) {
|
|
11
44
|
const cos = Math.cos(rad);
|
|
12
45
|
const sin = Math.sin(rad);
|
|
@@ -123,6 +156,84 @@ function polygonVertices(node) {
|
|
|
123
156
|
};
|
|
124
157
|
});
|
|
125
158
|
}
|
|
159
|
+
/** The layers the canvas strokes side by side when their sides have weights of their own. */
|
|
160
|
+
const SIDE_STROKE_TYPES = /* @__PURE__ */ new Set([
|
|
161
|
+
"FRAME",
|
|
162
|
+
"RECTANGLE",
|
|
163
|
+
"ROUNDED_RECTANGLE",
|
|
164
|
+
"COMPONENT",
|
|
165
|
+
"INSTANCE",
|
|
166
|
+
"SECTION",
|
|
167
|
+
"GROUP"
|
|
168
|
+
]);
|
|
169
|
+
/**
|
|
170
|
+
* Whether `node` strokes each side by its own weight. A line or vector keeps one weight whatever
|
|
171
|
+
* the flag says, since its outline has no sides.
|
|
172
|
+
*/
|
|
173
|
+
function drawsStrokeSides(node) {
|
|
174
|
+
return node.independentStrokeWeights === true && (!node.type || SIDE_STROKE_TYPES.has(node.type));
|
|
175
|
+
}
|
|
176
|
+
/** The share of a stroke's weight that lies outside the shape it outlines. */
|
|
177
|
+
function strokeAlignShare(align) {
|
|
178
|
+
if (align === "OUTSIDE") return 1;
|
|
179
|
+
if (align === "CENTER") return .5;
|
|
180
|
+
return 0;
|
|
181
|
+
}
|
|
182
|
+
/** A layer's own side weights: top, right, bottom, left. */
|
|
183
|
+
function borderWeights(node) {
|
|
184
|
+
return [
|
|
185
|
+
node.borderTopWeight,
|
|
186
|
+
node.borderRightWeight,
|
|
187
|
+
node.borderBottomWeight,
|
|
188
|
+
node.borderLeftWeight
|
|
189
|
+
];
|
|
190
|
+
}
|
|
191
|
+
/** The weight of each side of `node` under `stroke`, its own when its sides have their own. */
|
|
192
|
+
function strokeSideWeights(node, stroke) {
|
|
193
|
+
if (!drawsStrokeSides(node)) return [
|
|
194
|
+
stroke.weight,
|
|
195
|
+
stroke.weight,
|
|
196
|
+
stroke.weight,
|
|
197
|
+
stroke.weight
|
|
198
|
+
];
|
|
199
|
+
return borderWeights(node);
|
|
200
|
+
}
|
|
201
|
+
/** A stroke's weight, or with sides of their own the widest side, for what is not drawn per side. */
|
|
202
|
+
function strokeWeightOf(node, stroke) {
|
|
203
|
+
if (!drawsStrokeSides(node)) return stroke.weight;
|
|
204
|
+
return Math.max(node.borderTopWeight ?? 0, node.borderRightWeight ?? 0, node.borderBottomWeight ?? 0, node.borderLeftWeight ?? 0);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* How far a layer's visible strokes reach past each side of its box: top, right, bottom, left.
|
|
208
|
+
* With sides of their own each side reaches by its own weight (Figma desktop, live: a 1px outside
|
|
209
|
+
* stroke with a 20px right side bounds a 100 × 100 rectangle at 121 × 102).
|
|
210
|
+
*/
|
|
211
|
+
function strokeSideReach(node) {
|
|
212
|
+
let reach = [
|
|
213
|
+
0,
|
|
214
|
+
0,
|
|
215
|
+
0,
|
|
216
|
+
0
|
|
217
|
+
];
|
|
218
|
+
for (const stroke of node.strokes) {
|
|
219
|
+
if (!stroke.visible) continue;
|
|
220
|
+
const share = strokeAlignShare(stroke.align);
|
|
221
|
+
const sides = strokeSideWeights(node, stroke);
|
|
222
|
+
reach = [
|
|
223
|
+
Math.max(reach[0], sides[0] * share),
|
|
224
|
+
Math.max(reach[1], sides[1] * share),
|
|
225
|
+
Math.max(reach[2], sides[2] * share),
|
|
226
|
+
Math.max(reach[3], sides[3] * share)
|
|
227
|
+
];
|
|
228
|
+
}
|
|
229
|
+
return reach;
|
|
230
|
+
}
|
|
231
|
+
/** How far past a path a stroke of `weight` reaches, before mitered corners: a line's stroke lies wholly to one side. */
|
|
232
|
+
function strokeExtent(weight, align, line, open) {
|
|
233
|
+
if (line || align === "OUTSIDE") return weight;
|
|
234
|
+
if (align === "CENTER" || open) return weight / 2;
|
|
235
|
+
return 0;
|
|
236
|
+
}
|
|
126
237
|
function strokeOverflow(strokes, fallbackCap, network, shape) {
|
|
127
238
|
const line = shape?.type === "LINE";
|
|
128
239
|
const enclosed = new Set(network?.regions.flatMap((region) => region.loops.flat()));
|
|
@@ -131,43 +242,23 @@ function strokeOverflow(strokes, fallbackCap, network, shape) {
|
|
|
131
242
|
let overflow = 0;
|
|
132
243
|
for (const stroke of strokes ?? []) {
|
|
133
244
|
if (!stroke.visible) continue;
|
|
134
|
-
const
|
|
135
|
-
let extra = open ? stroke.weight / 2 : 0;
|
|
136
|
-
if (line || align === "OUTSIDE") extra = stroke.weight;
|
|
137
|
-
else if (align === "CENTER") extra = stroke.weight / 2;
|
|
245
|
+
const extra = strokeExtent(strokeWeightOf(shape ?? {}, stroke), sided ? stroke.align : "CENTER", line, open);
|
|
138
246
|
const miter = network ? miterRatio(network, stroke.join ?? shape?.strokeJoin, shape?.strokeMiterLimit) : 1;
|
|
139
247
|
overflow = Math.max(overflow, extra * miter);
|
|
140
248
|
}
|
|
141
249
|
const heads = arrowCapOverflow(strokes, fallbackCap, network);
|
|
142
250
|
return Math.max(overflow, heads > 0 && line ? heads + overflow / 2 : heads);
|
|
143
251
|
}
|
|
144
|
-
/** The direction a segment leaves `vertex` in, along its handle when it has one. */
|
|
145
|
-
function leaving(network, index, vertex) {
|
|
146
|
-
const segment = network.segments[index];
|
|
147
|
-
const atStart = segment.start === vertex;
|
|
148
|
-
const handle = atStart ? segment.tangentStart : segment.tangentEnd;
|
|
149
|
-
if (handle.x !== 0 || handle.y !== 0) return handle;
|
|
150
|
-
const from = network.vertices[vertex];
|
|
151
|
-
const to = network.vertices[atStart ? segment.end : segment.start];
|
|
152
|
-
return {
|
|
153
|
-
x: to.x - from.x,
|
|
154
|
-
y: to.y - from.y
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
252
|
/**
|
|
158
253
|
* How far past half the stroke the sharpest mitered corner reaches, as a multiple: a corner of
|
|
159
254
|
* angle θ mitres out to 1/sin(θ/2), unless that passes the miter limit and it bevels instead.
|
|
160
|
-
* Corners are the vertices exactly two segments meet at, as the stroke joins them.
|
|
161
255
|
*/
|
|
162
256
|
function miterRatio(network, join, limit = 4) {
|
|
163
|
-
const meeting = /* @__PURE__ */ new Map();
|
|
164
|
-
for (const [index, segment] of network.segments.entries()) for (const vertex of /* @__PURE__ */ new Set([segment.start, segment.end])) meeting.set(vertex, [...meeting.get(vertex) ?? [], index]);
|
|
165
257
|
let ratio = 1;
|
|
166
|
-
for (const [vertex,
|
|
167
|
-
if (indices.length !== 2) continue;
|
|
258
|
+
for (const [vertex, first, second] of joinedSegmentPairs(network)) {
|
|
168
259
|
if ((network.vertices[vertex].strokeJoin ?? join ?? "MITER") !== "MITER") continue;
|
|
169
|
-
const a =
|
|
170
|
-
const b =
|
|
260
|
+
const a = leavingDirection(network, first, vertex);
|
|
261
|
+
const b = leavingDirection(network, second, vertex);
|
|
171
262
|
const lengths = Math.hypot(a.x, a.y) * Math.hypot(b.x, b.y);
|
|
172
263
|
if (lengths === 0) continue;
|
|
173
264
|
const angle = Math.acos(Math.max(-1, Math.min(1, (a.x * b.x + a.y * b.y) / lengths)));
|
|
@@ -199,10 +290,53 @@ const BLUR_SIGMA_PER_RADIUS = .43;
|
|
|
199
290
|
function blurSigma(radius) {
|
|
200
291
|
return drawnBlurRadius(radius) * BLUR_SIGMA_PER_RADIUS;
|
|
201
292
|
}
|
|
293
|
+
/** Skia's Gaussian kernels end at 3 sigma. */
|
|
294
|
+
const KERNEL_SIGMAS = 3;
|
|
295
|
+
/**
|
|
296
|
+
* How far past what it blurs a Gaussian of `sigma` leaves ink on screen: where Skia's kernel
|
|
297
|
+
* ends, and one more pixel holds its rounding.
|
|
298
|
+
*/
|
|
299
|
+
function kernelReach(sigma) {
|
|
300
|
+
return sigma > 0 ? Math.ceil(KERNEL_SIGMAS * sigma) + 1 : 0;
|
|
301
|
+
}
|
|
302
|
+
/** How far past what it blurs a Figma blur of `radius` leaves ink on screen (`kernelReach`). */
|
|
303
|
+
function blurReach(radius) {
|
|
304
|
+
return kernelReach(blurSigma(radius));
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* The distance, in sigmas, past a straight edge blurred by a Gaussian at which the edge's share
|
|
308
|
+
* drops to `share`: the inverse of the normal tail Q, by Abramowitz and Stegun 26.2.23, which is
|
|
309
|
+
* within 0.00045 of it.
|
|
310
|
+
*/
|
|
311
|
+
function gaussianTailDistance(share) {
|
|
312
|
+
if (share <= 0) return Number.POSITIVE_INFINITY;
|
|
313
|
+
if (share >= .5) return 0;
|
|
314
|
+
const s = Math.sqrt(-2 * Math.log(share));
|
|
315
|
+
return s - (2.515517 + .802853 * s + .010328 * s * s) / (1 + 1.432788 * s + .189269 * s * s + .001308 * s * s * s);
|
|
316
|
+
}
|
|
317
|
+
const COLOR_LEVELS = 255;
|
|
318
|
+
/**
|
|
319
|
+
* How far past its shape a drop shadow of `radius` and colour alpha `alpha` leaves ink on
|
|
320
|
+
* screen. Skia blurs the shape into an 8-bit mask and scales it by `alpha`, so a mask level shows
|
|
321
|
+
* once `alpha` times it rounds to a level: an opaque or half-opaque shadow shows its faintest
|
|
322
|
+
* level, out to where the kernel ends, and a fainter one ends sooner. Measured on CPU raster, the
|
|
323
|
+
* ink ends at 3.0 sigma for alpha 1 and 0.5, 2.4 for 0.25, and 2.1 for 0.1.
|
|
324
|
+
*/
|
|
325
|
+
function shadowReach(radius, alpha) {
|
|
326
|
+
const sigma = blurSigma(radius);
|
|
327
|
+
const faintestShownLevel = Math.ceil(.5 / Math.max(alpha, 1 / COLOR_LEVELS));
|
|
328
|
+
const sigmas = Math.min(KERNEL_SIGMAS, gaussianTailDistance((faintestShownLevel - 1) / COLOR_LEVELS));
|
|
329
|
+
return Math.ceil(sigmas * sigma) + 1;
|
|
330
|
+
}
|
|
202
331
|
/** The Figma blur radius that draws a Gaussian of `sigma`, the inverse of `blurSigma`. */
|
|
203
332
|
function blurRadiusForSigma(sigma) {
|
|
204
333
|
return Math.max(0, sigma) / BLUR_SIGMA_PER_RADIUS;
|
|
205
334
|
}
|
|
335
|
+
/**
|
|
336
|
+
* How far a layer's shadows and blurs reach past each side as Figma's render bounds count them:
|
|
337
|
+
* a blur by its radius, a shadow by its radius and spread, moved by its offset. Ink on screen
|
|
338
|
+
* reaches further, to `blurReach`.
|
|
339
|
+
*/
|
|
206
340
|
function effectOverflow(effects) {
|
|
207
341
|
let left = 0;
|
|
208
342
|
let right = 0;
|
|
@@ -312,6 +446,6 @@ function textDecorationOverflow(node) {
|
|
|
312
446
|
return (node.textUnderlineOffset ?? fontSize * .18) + (node.textDecorationThickness ?? Math.max(1, fontSize / 16)) + fontSize * .35;
|
|
313
447
|
}
|
|
314
448
|
//#endregion
|
|
315
|
-
export { GeometryCommand, blurRadiusForSigma, blurSigma, computeAbsoluteBounds, computeBounds, cubicPoint, degToRad, drawnBlurRadius, effectOverflow, geometryBlobBounds, intersectVisualBounds, polygonVertices, radToDeg, readGeometryBlob, rotatePoint, rotatedBBox, rotatedCorners, strokeOverflow, textDecorationOverflow, unionVisualBounds, visualBoundsToRect, widestBlurRadius };
|
|
449
|
+
export { GeometryCommand, blurRadiusForSigma, blurReach, blurSigma, borderWeights, computeAbsoluteBounds, computeBounds, cubicPoint, degToRad, drawnBlurRadius, drawsStrokeSides, effectOverflow, geometryBlobBounds, intersectVisualBounds, isWholeArc, kernelReach, polygonVertices, radToDeg, readGeometryBlob, rotatePoint, rotatedBBox, rotatedCorners, shadowReach, shadowSpreadOf, shadowSpreads, strokeAlignShare, strokeOverflow, strokeSideReach, strokeSideWeights, strokeWeightOf, sweepsWholeTurn, textDecorationOverflow, unionVisualBounds, visualBoundsToRect, widestBlurRadius };
|
|
316
450
|
|
|
317
451
|
//# sourceMappingURL=geometry.js.map
|
package/dist/geometry.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"geometry.js","names":[],"sources":["../src/geometry.ts"],"sourcesContent":["import { arrowCapOverflow } from './arrow-caps'\nimport type { Rect, Vector } from './primitives'\nimport type { Effect, SceneNode, Stroke, StrokeCap, StrokeJoin, VectorNetwork } from './types'\n\nexport { cubicPoint } from './cubic'\n\nexport function degToRad(degrees: number): number {\n return (degrees * Math.PI) / 180\n}\n\nexport function radToDeg(radians: number): number {\n return (radians * 180) / Math.PI\n}\n\nexport function rotatePoint(px: number, py: number, cx: number, cy: number, rad: number): Vector {\n const cos = Math.cos(rad)\n const sin = Math.sin(rad)\n return {\n x: cx + (px - cx) * cos - (py - cy) * sin,\n y: cy + (px - cx) * sin + (py - cy) * cos\n }\n}\n\nexport function rotatedCorners(\n cx: number,\n cy: number,\n hw: number,\n hh: number,\n rotationDeg: number\n): [Vector, Vector, Vector, Vector] {\n const rad = degToRad(rotationDeg)\n const cos = Math.cos(rad)\n const sin = Math.sin(rad)\n return [\n { x: cx + -hw * cos - -hh * sin, y: cy + -hw * sin + -hh * cos },\n { x: cx + hw * cos - -hh * sin, y: cy + hw * sin + -hh * cos },\n { x: cx + hw * cos - hh * sin, y: cy + hw * sin + hh * cos },\n { x: cx + -hw * cos - hh * sin, y: cy + -hw * sin + hh * cos }\n ]\n}\n\nexport function rotatedBBox(\n x: number,\n y: number,\n w: number,\n h: number,\n rotationDeg: number\n): { left: number; right: number; top: number; bottom: number; centerX: number; centerY: number } {\n if (rotationDeg === 0) {\n return {\n left: x,\n right: x + w,\n top: y,\n bottom: y + h,\n centerX: x + w / 2,\n centerY: y + h / 2\n }\n }\n const corners = rotatedCorners(x + w / 2, y + h / 2, w / 2, h / 2, rotationDeg)\n let left = Infinity,\n right = -Infinity,\n top = Infinity,\n bottom = -Infinity\n for (const c of corners) {\n left = Math.min(left, c.x)\n right = Math.max(right, c.x)\n top = Math.min(top, c.y)\n bottom = Math.max(bottom, c.y)\n }\n return { left, right, top, bottom, centerX: (left + right) / 2, centerY: (top + bottom) / 2 }\n}\n\nexport interface VisualBounds {\n minX: number\n minY: number\n maxX: number\n maxY: number\n}\n\ntype BoundsAccumulator = VisualBounds\n\nfunction createBoundsAccumulator(): BoundsAccumulator {\n return { minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity }\n}\n\nfunction includePoint(bounds: BoundsAccumulator, x: number, y: number): void {\n bounds.minX = Math.min(bounds.minX, x)\n bounds.minY = Math.min(bounds.minY, y)\n bounds.maxX = Math.max(bounds.maxX, x)\n bounds.maxY = Math.max(bounds.maxY, y)\n}\n\nfunction includeRect(bounds: BoundsAccumulator, rect: Rect): void {\n includePoint(bounds, rect.x, rect.y)\n includePoint(bounds, rect.x + rect.width, rect.y + rect.height)\n}\n\n/** `bounds` as a rect at its top-left corner. */\nexport function visualBoundsToRect(bounds: VisualBounds): Rect {\n return {\n x: bounds.minX,\n y: bounds.minY,\n width: bounds.maxX - bounds.minX,\n height: bounds.maxY - bounds.minY\n }\n}\n\nfunction boundsToRect(bounds: BoundsAccumulator): Rect {\n return bounds.minX === Infinity ? { x: 0, y: 0, width: 0, height: 0 } : visualBoundsToRect(bounds)\n}\n\nexport function computeBounds(items: Iterable<Rect>): Rect {\n const bounds = createBoundsAccumulator()\n for (const item of items) includeRect(bounds, item)\n return boundsToRect(bounds)\n}\n\nexport function polygonVertices(node: {\n width: number\n height: number\n pointCount: number\n type: string\n starInnerRadius: number\n}): Vector[] {\n const cx = node.width / 2\n const cy = node.height / 2\n const rx = node.width / 2\n const ry = node.height / 2\n const pointCount = Math.max(3, node.pointCount)\n const isStar = node.type === 'STAR'\n const innerRatio = isStar ? node.starInnerRadius : 1\n const totalPoints = isStar ? pointCount * 2 : pointCount\n const angleOffset = -Math.PI / 2\n\n return Array.from({ length: totalPoints }, (_, index) => {\n const angle = angleOffset + (2 * Math.PI * index) / totalPoints\n const isInner = isStar && index % 2 === 1\n const radius = isInner ? innerRatio : 1\n return {\n x: cx + rx * radius * Math.cos(angle),\n y: cy + ry * radius * Math.sin(angle)\n }\n })\n}\n\n/**\n * What shapes how far a layer's stroke reaches past its box: its type, since a line is stroked to\n * one side, and its join and miter limit, for the tips mitered path corners reach.\n */\nexport interface StrokeShape {\n type?: string\n strokeJoin?: StrokeJoin\n strokeMiterLimit?: number\n}\n\nexport function strokeOverflow(\n strokes?: Stroke[],\n fallbackCap?: StrokeCap,\n network?: VectorNetwork | null,\n shape?: StrokeShape\n): number {\n const line = shape?.type === 'LINE'\n // Open segments enclose nothing to take a side of, so their strokes draw centred; a network\n // with closed regions still strokes those on their side.\n const enclosed = new Set(network?.regions.flatMap((region) => region.loops.flat()))\n const open = network?.segments.some((_, index) => !enclosed.has(index)) ?? false\n const sided = !network || network.regions.length > 0\n let overflow = 0\n for (const stroke of strokes ?? []) {\n if (!stroke.visible) continue\n const align = sided ? stroke.align : 'CENTER'\n let extra = open ? stroke.weight / 2 : 0\n if (line || align === 'OUTSIDE') extra = stroke.weight\n else if (align === 'CENTER') extra = stroke.weight / 2\n const miter = network\n ? miterRatio(network, stroke.join ?? shape?.strokeJoin, shape?.strokeMiterLimit)\n : 1\n overflow = Math.max(overflow, extra * miter)\n }\n // A line's heads sit on its stroke's middle, half a weight off the line.\n const heads = arrowCapOverflow(strokes, fallbackCap, network)\n return Math.max(overflow, heads > 0 && line ? heads + overflow / 2 : heads)\n}\n\n/** The direction a segment leaves `vertex` in, along its handle when it has one. */\nfunction leaving(network: VectorNetwork, index: number, vertex: number): Vector {\n const segment = network.segments[index]\n const atStart = segment.start === vertex\n const handle = atStart ? segment.tangentStart : segment.tangentEnd\n if (handle.x !== 0 || handle.y !== 0) return handle\n const from = network.vertices[vertex]\n const to = network.vertices[atStart ? segment.end : segment.start]\n return { x: to.x - from.x, y: to.y - from.y }\n}\n\n/**\n * How far past half the stroke the sharpest mitered corner reaches, as a multiple: a corner of\n * angle θ mitres out to 1/sin(θ/2), unless that passes the miter limit and it bevels instead.\n * Corners are the vertices exactly two segments meet at, as the stroke joins them.\n */\nfunction miterRatio(network: VectorNetwork, join: StrokeJoin | undefined, limit = 4): number {\n const meeting = new Map<number, number[]>()\n for (const [index, segment] of network.segments.entries()) {\n for (const vertex of new Set([segment.start, segment.end])) {\n meeting.set(vertex, [...(meeting.get(vertex) ?? []), index])\n }\n }\n let ratio = 1\n for (const [vertex, indices] of meeting) {\n if (indices.length !== 2) continue\n if ((network.vertices[vertex].strokeJoin ?? join ?? 'MITER') !== 'MITER') continue\n const a = leaving(network, indices[0], vertex)\n const b = leaving(network, indices[1], vertex)\n const lengths = Math.hypot(a.x, a.y) * Math.hypot(b.x, b.y)\n if (lengths === 0) continue\n const angle = Math.acos(Math.max(-1, Math.min(1, (a.x * b.x + a.y * b.y) / lengths)))\n const miter = 1 / Math.sin(angle / 2)\n if (miter <= limit) ratio = Math.max(ratio, miter)\n }\n return ratio\n}\n\n/**\n * The blur radius Figma draws: a negative one, which only a bound variable can give, draws as no\n * blur, though the layer keeps the value.\n */\nexport function drawnBlurRadius(radius: number): number {\n return Math.max(0, radius)\n}\n\n/**\n * The widest radius an effect blurs by: its radius, or the larger end of a progressive blur,\n * which can start wider than it ends. Bounds and padding that must hold the whole blur use it.\n */\nexport function widestBlurRadius(effect: Pick<Effect, 'radius' | 'progressive'>): number {\n return Math.max(effect.radius, effect.progressive?.startRadius ?? effect.radius)\n}\n\nconst BLUR_SIGMA_PER_RADIUS = 0.43\n\n/**\n * The Gaussian sigma that draws a Figma blur radius as Figma does. Measured on Figma 126 exports,\n * layer blur, both shadows, and background blur fall off as a Gaussian of 0.43 × radius; Figma's\n * SVG export writes radius / 2, which is wider (`packages/core/tests/canvas/blur.test.ts`).\n */\nexport function blurSigma(radius: number): number {\n return drawnBlurRadius(radius) * BLUR_SIGMA_PER_RADIUS\n}\n\n/** The Figma blur radius that draws a Gaussian of `sigma`, the inverse of `blurSigma`. */\nexport function blurRadiusForSigma(sigma: number): number {\n return Math.max(0, sigma) / BLUR_SIGMA_PER_RADIUS\n}\n\nexport function effectOverflow(effects?: Effect[]) {\n let left = 0\n let right = 0\n let top = 0\n let bottom = 0\n\n for (const effect of effects ?? []) {\n if (!effect.visible) continue\n if (\n effect.type !== 'DROP_SHADOW' &&\n effect.type !== 'LAYER_BLUR' &&\n effect.type !== 'FOREGROUND_BLUR'\n ) {\n continue\n }\n const blurSpread = drawnBlurRadius(widestBlurRadius(effect)) + effect.spread\n left = Math.max(left, blurSpread + Math.max(0, -effect.offset.x))\n right = Math.max(right, blurSpread + Math.max(0, effect.offset.x))\n top = Math.max(top, blurSpread + Math.max(0, -effect.offset.y))\n bottom = Math.max(bottom, blurSpread + Math.max(0, effect.offset.y))\n }\n\n return { left, right, top, bottom }\n}\n\nexport function computeAbsoluteBounds(\n nodes: Iterable<{ id: string; width: number; height: number }>,\n getAbsolutePosition: (id: string) => Vector\n): Rect {\n const bounds = createBoundsAccumulator()\n for (const n of nodes) {\n const abs = getAbsolutePosition(n.id)\n includeRect(bounds, { x: abs.x, y: abs.y, width: n.width, height: n.height })\n }\n return boundsToRect(bounds)\n}\n\nexport function unionVisualBounds(\n a: VisualBounds | null,\n b: VisualBounds | null\n): VisualBounds | null {\n if (!a) return b\n if (!b) return a\n return {\n minX: Math.min(a.minX, b.minX),\n minY: Math.min(a.minY, b.minY),\n maxX: Math.max(a.maxX, b.maxX),\n maxY: Math.max(a.maxY, b.maxY)\n }\n}\n\nexport function intersectVisualBounds(a: VisualBounds, b: VisualBounds): VisualBounds | null {\n const minX = Math.max(a.minX, b.minX)\n const minY = Math.max(a.minY, b.minY)\n const maxX = Math.min(a.maxX, b.maxX)\n const maxY = Math.min(a.maxY, b.maxY)\n return minX < maxX && minY < maxY ? { minX, minY, maxX, maxY } : null\n}\n\n/** The path commands of a Figma geometry blob, by their byte code. */\nexport const GeometryCommand = { CLOSE: 0, MOVE: 1, LINE: 2, QUAD: 3, CUBIC: 4 } as const\nexport type GeometryCommand = (typeof GeometryCommand)[keyof typeof GeometryCommand]\n\n/**\n * Coordinate pairs per command: glyph-derived blobs are quad-heavy, so every code needs its\n * count. Unknown codes are null, so readers stop instead of desyncing into coordinate bytes.\n */\nfunction commandCoordCount(command: number): number | null {\n if (command === GeometryCommand.CLOSE) return 0\n if (command === GeometryCommand.MOVE || command === GeometryCommand.LINE) return 1\n if (command === GeometryCommand.QUAD) return 2\n if (command === GeometryCommand.CUBIC) return 3\n return null\n}\n\n/**\n * Walks a Figma geometry blob: `visit` gets each command, its coordinates as x, y pairs in one\n * array reused between calls, and the byte offset of the first coordinate. It stops at an\n * unknown command or one cut short.\n */\nexport function readGeometryBlob(\n blob: Uint8Array,\n visit: (command: GeometryCommand, coords: readonly number[], offset: number) => void\n): void {\n const view = new DataView(blob.buffer, blob.byteOffset, blob.byteLength)\n const coords: number[] = []\n let offset = 0\n while (offset < blob.length) {\n const command = blob[offset]\n const count = commandCoordCount(command)\n if (count === null || offset + 1 + count * 8 > blob.length) return\n offset += 1\n coords.length = 0\n for (let i = 0; i < count; i++) {\n coords.push(view.getFloat32(offset + i * 8, true), view.getFloat32(offset + i * 8 + 4, true))\n }\n visit(command as GeometryCommand, coords, offset)\n offset += count * 8\n }\n}\n\nexport function geometryBlobBounds(paths: Array<{ commandsBlob: Uint8Array }>): Rect | null {\n const bounds = createBoundsAccumulator()\n for (const path of paths) {\n readGeometryBlob(path.commandsBlob, (_, coords) => {\n for (let i = 0; i < coords.length; i += 2) includePoint(bounds, coords[i], coords[i + 1])\n })\n }\n\n return bounds.minX === Infinity ? null : boundsToRect(bounds)\n}\n\n/** How far a text node's underline or strikethrough reaches below its box. */\nexport function textDecorationOverflow(\n node: Pick<\n SceneNode,\n 'type' | 'fontSize' | 'textDecoration' | 'textUnderlineOffset' | 'textDecorationThickness'\n >\n): number {\n if (node.type !== 'TEXT' || node.textDecoration === 'NONE') {\n return 0\n }\n const { fontSize } = node\n const underlineOffset = node.textUnderlineOffset ?? fontSize * 0.18\n const thickness = node.textDecorationThickness ?? Math.max(1, fontSize / 16)\n return underlineOffset + thickness + fontSize * 0.35\n}\n"],"mappings":";;;AAMA,SAAgB,SAAS,SAAyB;CAChD,OAAQ,UAAU,KAAK,KAAM;AAC/B;AAEA,SAAgB,SAAS,SAAyB;CAChD,OAAQ,UAAU,MAAO,KAAK;AAChC;AAEA,SAAgB,YAAY,IAAY,IAAY,IAAY,IAAY,KAAqB;CAC/F,MAAM,MAAM,KAAK,IAAI,GAAG;CACxB,MAAM,MAAM,KAAK,IAAI,GAAG;CACxB,OAAO;EACL,GAAG,MAAM,KAAK,MAAM,OAAO,KAAK,MAAM;EACtC,GAAG,MAAM,KAAK,MAAM,OAAO,KAAK,MAAM;CACxC;AACF;AAEA,SAAgB,eACd,IACA,IACA,IACA,IACA,aACkC;CAClC,MAAM,MAAM,SAAS,WAAW;CAChC,MAAM,MAAM,KAAK,IAAI,GAAG;CACxB,MAAM,MAAM,KAAK,IAAI,GAAG;CACxB,OAAO;EACL;GAAE,GAAG,KAAK,CAAC,KAAK,MAAM,CAAC,KAAK;GAAK,G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CAAC;EAE9F,MAAM,SAA4B,QAAQ,MAAM;EAChD,UAAU,QAAQ;CACpB;AACF;AAEA,SAAgB,mBAAmB,OAAyD;CAC1F,MAAM,SAAS,wBAAwB;CACvC,KAAK,MAAM,QAAQ,OACjB,iBAAiB,KAAK,eAAe,GAAG,WAAW;EACjD,KAAK,IAAI,IAAI,GAAG,IAAI,OAAO,QAAQ,KAAK,GAAG,aAAa,QAAQ,OAAO,IAAI,OAAO,IAAI,EAAE;CAC1F,CAAC;CAGH,OAAO,OAAO,SAAS,WAAW,OAAO,aAAa,MAAM;AAC9D;;AAGA,SAAgB,uBACd,MAIQ;CACR,IAAI,KAAK,SAAS,UAAU,KAAK,mBAAmB,QAClD,OAAO;CAET,MAAM,EAAE,aAAa;CAGrB,QAFwB,KAAK,uBAAuB,WAAW,QAC7C,KAAK,2BAA2B,KAAK,IAAI,GAAG,WAAW,EAAE,KACtC,WAAW;AAClD"}
|
|
1
|
+
{"version":3,"file":"geometry.js","names":[],"sources":["../src/geometry.ts"],"sourcesContent":["import { arrowCapOverflow } from './arrow-caps'\nimport { joinedSegmentPairs, leavingDirection } from './paths/network'\nimport type { Rect, Vector } from './primitives'\nimport type {\n ArcData,\n Effect,\n SceneNode,\n Stroke,\n StrokeCap,\n StrokeJoin,\n VectorNetwork\n} from './types'\n\nexport { cubicPoint } from './cubic'\n\nexport function degToRad(degrees: number): number {\n return (degrees * Math.PI) / 180\n}\n\nexport function radToDeg(radians: number): number {\n return (radians * 180) / Math.PI\n}\n\nconst FULL_TURN = 2 * Math.PI\n\n/** Layers whose shadows Figma never spreads: a text, boolean, star, polygon or vector. */\nconst UNSPREAD_TYPES: ReadonlySet<SceneNode['type']> = new Set<SceneNode['type']>([\n 'TEXT',\n 'BOOLEAN_OPERATION',\n 'STAR',\n 'POLYGON',\n 'VECTOR'\n])\n\n/**\n * Whether a layer's shadows take their spread. Figma spreads those of boxes and ellipses, arcs\n * among them, and frames, and counts it in their render bounds; a text, boolean, star, polygon or\n * vector never spreads (Figma desktop, live).\n */\nexport function shadowSpreads(node: Pick<SceneNode, 'type'>): boolean {\n return !UNSPREAD_TYPES.has(node.type)\n}\n\n/** The spread a shadow on `node` takes: its own, or none on a layer Figma never spreads. */\nexport function shadowSpreadOf(\n node: Pick<SceneNode, 'type'>,\n effect: Pick<Effect, 'spread'>\n): number {\n return shadowSpreads(node) ? effect.spread : 0\n}\n\n/**\n * Whether a sweep from `start` to `end` is a whole turn: less than 0.001 rad short of one draws the\n * whole ellipse in Figma (desktop, live: 0.00095 short is whole, 0.001 short an arc).\n */\nexport function sweepsWholeTurn(start: number, end: number): boolean {\n return FULL_TURN - Math.abs(end - start) < 0.001\n}\n\n/** Whether an arc sweeps a whole turn, so it is drawn and edited as a whole ellipse or ring. */\nexport function isWholeArc(arc: Pick<ArcData, 'startingAngle' | 'endingAngle'>): boolean {\n return sweepsWholeTurn(arc.startingAngle, arc.endingAngle)\n}\n\nexport function rotatePoint(px: number, py: number, cx: number, cy: number, rad: number): Vector {\n const cos = Math.cos(rad)\n const sin = Math.sin(rad)\n return {\n x: cx + (px - cx) * cos - (py - cy) * sin,\n y: cy + (px - cx) * sin + (py - cy) * cos\n }\n}\n\nexport function rotatedCorners(\n cx: number,\n cy: number,\n hw: number,\n hh: number,\n rotationDeg: number\n): [Vector, Vector, Vector, Vector] {\n const rad = degToRad(rotationDeg)\n const cos = Math.cos(rad)\n const sin = Math.sin(rad)\n return [\n { x: cx + -hw * cos - -hh * sin, y: cy + -hw * sin + -hh * cos },\n { x: cx + hw * cos - -hh * sin, y: cy + hw * sin + -hh * cos },\n { x: cx + hw * cos - hh * sin, y: cy + hw * sin + hh * cos },\n { x: cx + -hw * cos - hh * sin, y: cy + -hw * sin + hh * cos }\n ]\n}\n\nexport function rotatedBBox(\n x: number,\n y: number,\n w: number,\n h: number,\n rotationDeg: number\n): { left: number; right: number; top: number; bottom: number; centerX: number; centerY: number } {\n if (rotationDeg === 0) {\n return {\n left: x,\n right: x + w,\n top: y,\n bottom: y + h,\n centerX: x + w / 2,\n centerY: y + h / 2\n }\n }\n const corners = rotatedCorners(x + w / 2, y + h / 2, w / 2, h / 2, rotationDeg)\n let left = Infinity,\n right = -Infinity,\n top = Infinity,\n bottom = -Infinity\n for (const c of corners) {\n left = Math.min(left, c.x)\n right = Math.max(right, c.x)\n top = Math.min(top, c.y)\n bottom = Math.max(bottom, c.y)\n }\n return { left, right, top, bottom, centerX: (left + right) / 2, centerY: (top + bottom) / 2 }\n}\n\nexport interface VisualBounds {\n minX: number\n minY: number\n maxX: number\n maxY: number\n}\n\ntype BoundsAccumulator = VisualBounds\n\nfunction createBoundsAccumulator(): BoundsAccumulator {\n return { minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity }\n}\n\nfunction includePoint(bounds: BoundsAccumulator, x: number, y: number): void {\n bounds.minX = Math.min(bounds.minX, x)\n bounds.minY = Math.min(bounds.minY, y)\n bounds.maxX = Math.max(bounds.maxX, x)\n bounds.maxY = Math.max(bounds.maxY, y)\n}\n\nfunction includeRect(bounds: BoundsAccumulator, rect: Rect): void {\n includePoint(bounds, rect.x, rect.y)\n includePoint(bounds, rect.x + rect.width, rect.y + rect.height)\n}\n\n/** `bounds` as a rect at its top-left corner. */\nexport function visualBoundsToRect(bounds: VisualBounds): Rect {\n return {\n x: bounds.minX,\n y: bounds.minY,\n width: bounds.maxX - bounds.minX,\n height: bounds.maxY - bounds.minY\n }\n}\n\nfunction boundsToRect(bounds: BoundsAccumulator): Rect {\n return bounds.minX === Infinity ? { x: 0, y: 0, width: 0, height: 0 } : visualBoundsToRect(bounds)\n}\n\nexport function computeBounds(items: Iterable<Rect>): Rect {\n const bounds = createBoundsAccumulator()\n for (const item of items) includeRect(bounds, item)\n return boundsToRect(bounds)\n}\n\nexport function polygonVertices(node: {\n width: number\n height: number\n pointCount: number\n type: string\n starInnerRadius: number\n}): Vector[] {\n const cx = node.width / 2\n const cy = node.height / 2\n const rx = node.width / 2\n const ry = node.height / 2\n const pointCount = Math.max(3, node.pointCount)\n const isStar = node.type === 'STAR'\n const innerRatio = isStar ? node.starInnerRadius : 1\n const totalPoints = isStar ? pointCount * 2 : pointCount\n const angleOffset = -Math.PI / 2\n\n return Array.from({ length: totalPoints }, (_, index) => {\n const angle = angleOffset + (2 * Math.PI * index) / totalPoints\n const isInner = isStar && index % 2 === 1\n const radius = isInner ? innerRatio : 1\n return {\n x: cx + rx * radius * Math.cos(angle),\n y: cy + ry * radius * Math.sin(angle)\n }\n })\n}\n\n/** A rectangle's stroke weights, side by side: top, right, bottom, left. */\nexport type SideWeights = readonly [top: number, right: number, bottom: number, left: number]\n\ntype SideStrokeNode = Pick<\n SceneNode,\n | 'independentStrokeWeights'\n | 'borderTopWeight'\n | 'borderRightWeight'\n | 'borderBottomWeight'\n | 'borderLeftWeight'\n>\n\n/** What deciding on per-side strokes reads: the side weights, and the node's type when known. */\ntype StrokeSidesInput = Partial<SideStrokeNode> & { type?: string }\n\n/** The layers the canvas strokes side by side when their sides have weights of their own. */\nconst SIDE_STROKE_TYPES: ReadonlySet<string> = new Set<SceneNode['type']>([\n 'FRAME',\n 'RECTANGLE',\n 'ROUNDED_RECTANGLE',\n 'COMPONENT',\n 'INSTANCE',\n 'SECTION',\n 'GROUP'\n])\n\n/**\n * Whether `node` strokes each side by its own weight. A line or vector keeps one weight whatever\n * the flag says, since its outline has no sides.\n */\nexport function drawsStrokeSides(node: StrokeSidesInput): boolean {\n return node.independentStrokeWeights === true && (!node.type || SIDE_STROKE_TYPES.has(node.type))\n}\n\n/** The share of a stroke's weight that lies outside the shape it outlines. */\nexport function strokeAlignShare(align: Stroke['align']): number {\n if (align === 'OUTSIDE') return 1\n if (align === 'CENTER') return 0.5\n return 0\n}\n\n/** A layer's own side weights: top, right, bottom, left. */\nexport function borderWeights(node: SideStrokeNode): SideWeights {\n return [\n node.borderTopWeight,\n node.borderRightWeight,\n node.borderBottomWeight,\n node.borderLeftWeight\n ]\n}\n\n/** The weight of each side of `node` under `stroke`, its own when its sides have their own. */\nexport function strokeSideWeights(\n node: SideStrokeNode & { type?: string },\n stroke: Pick<Stroke, 'weight'>\n): SideWeights {\n if (!drawsStrokeSides(node)) return [stroke.weight, stroke.weight, stroke.weight, stroke.weight]\n return borderWeights(node)\n}\n\n/** A stroke's weight, or with sides of their own the widest side, for what is not drawn per side. */\nexport function strokeWeightOf(node: StrokeSidesInput, stroke: Pick<Stroke, 'weight'>): number {\n if (!drawsStrokeSides(node)) return stroke.weight\n return Math.max(\n node.borderTopWeight ?? 0,\n node.borderRightWeight ?? 0,\n node.borderBottomWeight ?? 0,\n node.borderLeftWeight ?? 0\n )\n}\n\n/**\n * How far a layer's visible strokes reach past each side of its box: top, right, bottom, left.\n * With sides of their own each side reaches by its own weight (Figma desktop, live: a 1px outside\n * stroke with a 20px right side bounds a 100 × 100 rectangle at 121 × 102).\n */\nexport function strokeSideReach(\n node: SideStrokeNode & Pick<SceneNode, 'strokes'> & { type?: string }\n): SideWeights {\n let reach: SideWeights = [0, 0, 0, 0]\n for (const stroke of node.strokes) {\n if (!stroke.visible) continue\n const share = strokeAlignShare(stroke.align)\n const sides = strokeSideWeights(node, stroke)\n reach = [\n Math.max(reach[0], sides[0] * share),\n Math.max(reach[1], sides[1] * share),\n Math.max(reach[2], sides[2] * share),\n Math.max(reach[3], sides[3] * share)\n ]\n }\n return reach\n}\n\n/**\n * What shapes how far a layer's stroke reaches past its box: its type, since a line is stroked to\n * one side, and its join and miter limit, for the tips mitered path corners reach.\n */\nexport interface StrokeShape extends Partial<SideStrokeNode> {\n type?: string\n strokeJoin?: StrokeJoin\n strokeMiterLimit?: number\n}\n\n/** How far past a path a stroke of `weight` reaches, before mitered corners: a line's stroke lies wholly to one side. */\nfunction strokeExtent(\n weight: number,\n align: Stroke['align'],\n line: boolean,\n open: boolean\n): number {\n if (line || align === 'OUTSIDE') return weight\n if (align === 'CENTER' || open) return weight / 2\n return 0\n}\n\nexport function strokeOverflow(\n strokes?: Stroke[],\n fallbackCap?: StrokeCap,\n network?: VectorNetwork | null,\n shape?: StrokeShape\n): number {\n const line = shape?.type === 'LINE'\n // Open segments enclose nothing to take a side of, so their strokes draw centred; a network\n // with closed regions still strokes those on their side.\n const enclosed = new Set(network?.regions.flatMap((region) => region.loops.flat()))\n const open = network?.segments.some((_, index) => !enclosed.has(index)) ?? false\n const sided = !network || network.regions.length > 0\n let overflow = 0\n for (const stroke of strokes ?? []) {\n if (!stroke.visible) continue\n const weight = strokeWeightOf(shape ?? {}, stroke)\n const extra = strokeExtent(weight, sided ? stroke.align : 'CENTER', line, open)\n const miter = network\n ? miterRatio(network, stroke.join ?? shape?.strokeJoin, shape?.strokeMiterLimit)\n : 1\n overflow = Math.max(overflow, extra * miter)\n }\n // A line's heads sit on its stroke's middle, half a weight off the line.\n const heads = arrowCapOverflow(strokes, fallbackCap, network)\n return Math.max(overflow, heads > 0 && line ? heads + overflow / 2 : heads)\n}\n\n/**\n * How far past half the stroke the sharpest mitered corner reaches, as a multiple: a corner of\n * angle θ mitres out to 1/sin(θ/2), unless that passes the miter limit and it bevels instead.\n */\nfunction miterRatio(network: VectorNetwork, join: StrokeJoin | undefined, limit = 4): number {\n let ratio = 1\n for (const [vertex, first, second] of joinedSegmentPairs(network)) {\n if ((network.vertices[vertex].strokeJoin ?? join ?? 'MITER') !== 'MITER') continue\n const a = leavingDirection(network, first, vertex)\n const b = leavingDirection(network, second, vertex)\n const lengths = Math.hypot(a.x, a.y) * Math.hypot(b.x, b.y)\n if (lengths === 0) continue\n const angle = Math.acos(Math.max(-1, Math.min(1, (a.x * b.x + a.y * b.y) / lengths)))\n const miter = 1 / Math.sin(angle / 2)\n if (miter <= limit) ratio = Math.max(ratio, miter)\n }\n return ratio\n}\n\n/**\n * The blur radius Figma draws: a negative one, which only a bound variable can give, draws as no\n * blur, though the layer keeps the value.\n */\nexport function drawnBlurRadius(radius: number): number {\n return Math.max(0, radius)\n}\n\n/**\n * The widest radius an effect blurs by: its radius, or the larger end of a progressive blur,\n * which can start wider than it ends. Bounds and padding that must hold the whole blur use it.\n */\nexport function widestBlurRadius(effect: Pick<Effect, 'radius' | 'progressive'>): number {\n return Math.max(effect.radius, effect.progressive?.startRadius ?? effect.radius)\n}\n\nconst BLUR_SIGMA_PER_RADIUS = 0.43\n\n/**\n * The Gaussian sigma that draws a Figma blur radius as Figma does. Measured on Figma 126 exports,\n * layer blur, both shadows, and background blur fall off as a Gaussian of 0.43 × radius; Figma's\n * SVG export writes radius / 2, which is wider (`packages/core/tests/canvas/blur.test.ts`).\n */\nexport function blurSigma(radius: number): number {\n return drawnBlurRadius(radius) * BLUR_SIGMA_PER_RADIUS\n}\n\n/** Skia's Gaussian kernels end at 3 sigma. */\nconst KERNEL_SIGMAS = 3\n\n/**\n * How far past what it blurs a Gaussian of `sigma` leaves ink on screen: where Skia's kernel\n * ends, and one more pixel holds its rounding.\n */\nexport function kernelReach(sigma: number): number {\n return sigma > 0 ? Math.ceil(KERNEL_SIGMAS * sigma) + 1 : 0\n}\n\n/** How far past what it blurs a Figma blur of `radius` leaves ink on screen (`kernelReach`). */\nexport function blurReach(radius: number): number {\n return kernelReach(blurSigma(radius))\n}\n\n/**\n * The distance, in sigmas, past a straight edge blurred by a Gaussian at which the edge's share\n * drops to `share`: the inverse of the normal tail Q, by Abramowitz and Stegun 26.2.23, which is\n * within 0.00045 of it.\n */\nfunction gaussianTailDistance(share: number): number {\n if (share <= 0) {\n return Number.POSITIVE_INFINITY\n }\n if (share >= 0.5) {\n return 0\n }\n const s = Math.sqrt(-2 * Math.log(share))\n return (\n s -\n (2.515517 + 0.802853 * s + 0.010328 * s * s) /\n (1 + 1.432788 * s + 0.189269 * s * s + 0.001308 * s * s * s)\n )\n}\n\nconst COLOR_LEVELS = 255\n\n/**\n * How far past its shape a drop shadow of `radius` and colour alpha `alpha` leaves ink on\n * screen. Skia blurs the shape into an 8-bit mask and scales it by `alpha`, so a mask level shows\n * once `alpha` times it rounds to a level: an opaque or half-opaque shadow shows its faintest\n * level, out to where the kernel ends, and a fainter one ends sooner. Measured on CPU raster, the\n * ink ends at 3.0 sigma for alpha 1 and 0.5, 2.4 for 0.25, and 2.1 for 0.1.\n */\nexport function shadowReach(radius: number, alpha: number): number {\n const sigma = blurSigma(radius)\n const faintestShownLevel = Math.ceil(0.5 / Math.max(alpha, 1 / COLOR_LEVELS))\n const sigmas = Math.min(\n KERNEL_SIGMAS,\n gaussianTailDistance((faintestShownLevel - 1) / COLOR_LEVELS)\n )\n return Math.ceil(sigmas * sigma) + 1\n}\n\n/** The Figma blur radius that draws a Gaussian of `sigma`, the inverse of `blurSigma`. */\nexport function blurRadiusForSigma(sigma: number): number {\n return Math.max(0, sigma) / BLUR_SIGMA_PER_RADIUS\n}\n\n/**\n * How far a layer's shadows and blurs reach past each side as Figma's render bounds count them:\n * a blur by its radius, a shadow by its radius and spread, moved by its offset. Ink on screen\n * reaches further, to `blurReach`.\n */\nexport function effectOverflow(effects?: Effect[]) {\n let left = 0\n let right = 0\n let top = 0\n let bottom = 0\n\n for (const effect of effects ?? []) {\n if (!effect.visible) continue\n if (\n effect.type !== 'DROP_SHADOW' &&\n effect.type !== 'LAYER_BLUR' &&\n effect.type !== 'FOREGROUND_BLUR'\n ) {\n continue\n }\n const blurSpread = drawnBlurRadius(widestBlurRadius(effect)) + effect.spread\n left = Math.max(left, blurSpread + Math.max(0, -effect.offset.x))\n right = Math.max(right, blurSpread + Math.max(0, effect.offset.x))\n top = Math.max(top, blurSpread + Math.max(0, -effect.offset.y))\n bottom = Math.max(bottom, blurSpread + Math.max(0, effect.offset.y))\n }\n\n return { left, right, top, bottom }\n}\n\nexport function computeAbsoluteBounds(\n nodes: Iterable<{ id: string; width: number; height: number }>,\n getAbsolutePosition: (id: string) => Vector\n): Rect {\n const bounds = createBoundsAccumulator()\n for (const n of nodes) {\n const abs = getAbsolutePosition(n.id)\n includeRect(bounds, { x: abs.x, y: abs.y, width: n.width, height: n.height })\n }\n return boundsToRect(bounds)\n}\n\nexport function unionVisualBounds(\n a: VisualBounds | null,\n b: VisualBounds | null\n): VisualBounds | null {\n if (!a) return b\n if (!b) return a\n return {\n minX: Math.min(a.minX, b.minX),\n minY: Math.min(a.minY, b.minY),\n maxX: Math.max(a.maxX, b.maxX),\n maxY: Math.max(a.maxY, b.maxY)\n }\n}\n\nexport function intersectVisualBounds(a: VisualBounds, b: VisualBounds): VisualBounds | null {\n const minX = Math.max(a.minX, b.minX)\n const minY = Math.max(a.minY, b.minY)\n const maxX = Math.min(a.maxX, b.maxX)\n const maxY = Math.min(a.maxY, b.maxY)\n return minX < maxX && minY < maxY ? { minX, minY, maxX, maxY } : null\n}\n\n/** The path commands of a Figma geometry blob, by their byte code. */\nexport const GeometryCommand = { CLOSE: 0, MOVE: 1, LINE: 2, QUAD: 3, CUBIC: 4 } as const\nexport type GeometryCommand = (typeof GeometryCommand)[keyof typeof GeometryCommand]\n\n/**\n * Coordinate pairs per command: glyph-derived blobs are quad-heavy, so every code needs its\n * count. Unknown codes are null, so readers stop instead of desyncing into coordinate bytes.\n */\nfunction commandCoordCount(command: number): number | null {\n if (command === GeometryCommand.CLOSE) return 0\n if (command === GeometryCommand.MOVE || command === GeometryCommand.LINE) return 1\n if (command === GeometryCommand.QUAD) return 2\n if (command === GeometryCommand.CUBIC) return 3\n return null\n}\n\n/**\n * Walks a Figma geometry blob: `visit` gets each command, its coordinates as x, y pairs in one\n * array reused between calls, and the byte offset of the first coordinate. It stops at an\n * unknown command or one cut short.\n */\nexport function readGeometryBlob(\n blob: Uint8Array,\n visit: (command: GeometryCommand, coords: readonly number[], offset: number) => void\n): void {\n const view = new DataView(blob.buffer, blob.byteOffset, blob.byteLength)\n const coords: number[] = []\n let offset = 0\n while (offset < blob.length) {\n const command = blob[offset]\n const count = commandCoordCount(command)\n if (count === null || offset + 1 + count * 8 > blob.length) return\n offset += 1\n coords.length = 0\n for (let i = 0; i < count; i++) {\n coords.push(view.getFloat32(offset + i * 8, true), view.getFloat32(offset + i * 8 + 4, true))\n }\n visit(command as GeometryCommand, coords, offset)\n offset += count * 8\n }\n}\n\nexport function geometryBlobBounds(paths: Array<{ commandsBlob: Uint8Array }>): Rect | null {\n const bounds = createBoundsAccumulator()\n for (const path of paths) {\n readGeometryBlob(path.commandsBlob, (_, coords) => {\n for (let i = 0; i < coords.length; i += 2) includePoint(bounds, coords[i], coords[i + 1])\n })\n }\n\n return bounds.minX === Infinity ? null : boundsToRect(bounds)\n}\n\n/** How far a text node's underline or strikethrough reaches below its box. */\nexport function textDecorationOverflow(\n node: Pick<\n SceneNode,\n 'type' | 'fontSize' | 'textDecoration' | 'textUnderlineOffset' | 'textDecorationThickness'\n >\n): number {\n if (node.type !== 'TEXT' || node.textDecoration === 'NONE') {\n return 0\n }\n const { fontSize } = node\n const underlineOffset = node.textUnderlineOffset ?? fontSize * 0.18\n const thickness = node.textDecorationThickness ?? Math.max(1, fontSize / 16)\n return underlineOffset + thickness + fontSize * 0.35\n}\n"],"mappings":";;;;AAeA,SAAgB,SAAS,SAAyB;CAChD,OAAQ,UAAU,KAAK,KAAM;AAC/B;AAEA,SAAgB,SAAS,SAAyB;CAChD,OAAQ,UAAU,MAAO,KAAK;AAChC;AAEA,MAAM,YAAY,IAAI,KAAK;;AAG3B,MAAM,iCAAiD,IAAI,IAAuB;CAChF;CACA;CACA;CACA;CACA;AACF,CAAC;;;;;;AAOD,SAAgB,cAAc,MAAwC;CACpE,OAAO,CAAC,eAAe,IAAI,KAAK,IAAI;AACtC;;AAGA,SAAgB,eACd,MACA,QACQ;CACR,OAAO,cAAc,IAAI,IAAI,OAAO,SAAS;AAC/C;;;;;AAMA,SAAgB,gBAAgB,OAAe,KAAsB;CACnE,OAAO,YAAY,KAAK,IAAI,MAAM,KAAK,IAAI;AAC7C;;AAGA,SAAgB,WAAW,KAA8D;CACvF,OAAO,gBAAgB,IAAI,eAAe,IAAI,WAAW;AAC3D;AAEA,SAAgB,YAAY,IAAY,IAAY,IAAY,IAAY,KAAqB;CAC/F,MAAM,MAAM,KAAK,IAAI,GAAG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2
2
|
import { Fill, GradientStop, GradientTransform } from "./types.js";
|
|
3
3
|
//#region src/gradient.d.ts
|
|
4
4
|
/**
|
|
@@ -53,6 +53,13 @@ declare function linearGradientTransform(start: Vector, end: Vector, width?: num
|
|
|
53
53
|
* radii, all in the layer's unit square.
|
|
54
54
|
*/
|
|
55
55
|
declare function ellipticalGradientTransform(center: Vector, radiusX: Vector, radiusY: Vector): GradientTransform;
|
|
56
|
+
/**
|
|
57
|
+
* The transform that keeps a gradient where it was on the canvas when its layer's box becomes
|
|
58
|
+
* `box`, given in the old box's units, as Figma rewrites it when editing a path resizes the layer
|
|
59
|
+
* (Figma desktop 126: a 200 × 120 box grown to 380 × 160 and moved up 40 gives
|
|
60
|
+
* `[[1.9, 0, 0], [0, 1.333, -0.333]]` from the identity).
|
|
61
|
+
*/
|
|
62
|
+
declare function gradientTransformInBox(t: GradientTransform, box: Rect): GradientTransform;
|
|
56
63
|
//#endregion
|
|
57
|
-
export { DEFAULT_GRADIENT_TRANSFORM, GradientFillType, GradientTransformRows, IDENTITY_GRADIENT_TRANSFORM, applyGradientTransform, defaultGradientStops, ellipticalGradientTransform, gradientPoint, gradientTransformRows, invertGradientTransform, isGradientFill, isIdentityGradientTransform, linearGradientTransform, linearGradientTransformFromAxes };
|
|
64
|
+
export { DEFAULT_GRADIENT_TRANSFORM, GradientFillType, GradientTransformRows, IDENTITY_GRADIENT_TRANSFORM, applyGradientTransform, defaultGradientStops, ellipticalGradientTransform, gradientPoint, gradientTransformInBox, gradientTransformRows, invertGradientTransform, isGradientFill, isIdentityGradientTransform, linearGradientTransform, linearGradientTransformFromAxes };
|
|
58
65
|
//# sourceMappingURL=gradient.d.ts.map
|
package/dist/gradient.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gradient.d.ts","names":[],"sources":["../src/gradient.ts"],"mappings":";;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"gradient.d.ts","names":[],"sources":["../src/gradient.ts"],"mappings":";;;;;;;;;;KAYY,mBAAmB,QAC7B;iBAIc,eAAe,MAAM,eAAe,QAAQ;;cAU/C,6BAA6B;;;;;cAa7B,4BAA4B;;;;;KAa7B;;iBAGI,sBAAsB,GAAG,oBAAoB;;iBAW7C,qBAAqB,OAAO,QAAQ;iBAepC,4BAA4B,GAAG;;;;;iBAU/B,wBAAwB,GAAG,oBAAoB;;iBAkB/C,uBAAuB,GAAG,mBAAmB,OAAO,SAAS;;iBAQ7D,cAAc,GAAG,mBAAmB,OAAO,SAAS;;;;;iBAQpD,gCACd,OAAO,QACP,KAAK,QACL,QAAQ,SACP;;;;;;;iBAiBa,wBACd,OAAO,QACP,KAAK,QACL,gBACA,iBACA,iBACC;;;;;iBAYa,4BACd,QAAQ,QACR,SAAS,QACT,SAAS,SACR;;;;;;;iBAmBa,uBAAuB,GAAG,mBAAmB,KAAK,OAAO"}
|
package/dist/gradient.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import Matrix from "./matrix.js";
|
|
1
2
|
//#region src/gradient.ts
|
|
2
3
|
function isGradientFill(type) {
|
|
3
4
|
return type === "GRADIENT_LINEAR" || type === "GRADIENT_RADIAL" || type === "GRADIENT_ANGULAR" || type === "GRADIENT_DIAMOND";
|
|
@@ -138,7 +139,34 @@ function ellipticalGradientTransform(center, radiusX, radiusY) {
|
|
|
138
139
|
m12: center.y - (xAxis.y + yAxis.y) / 2
|
|
139
140
|
});
|
|
140
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* The transform that keeps a gradient where it was on the canvas when its layer's box becomes
|
|
144
|
+
* `box`, given in the old box's units, as Figma rewrites it when editing a path resizes the layer
|
|
145
|
+
* (Figma desktop 126: a 200 × 120 box grown to 380 × 160 and moved up 40 gives
|
|
146
|
+
* `[[1.9, 0, 0], [0, 1.333, -0.333]]` from the identity).
|
|
147
|
+
*/
|
|
148
|
+
function gradientTransformInBox(t, box) {
|
|
149
|
+
const [m00, m01, m02, m10, m11, m12] = Matrix.multiply(Matrix.fromAffine(t), [
|
|
150
|
+
box.width,
|
|
151
|
+
0,
|
|
152
|
+
box.x,
|
|
153
|
+
0,
|
|
154
|
+
box.height,
|
|
155
|
+
box.y,
|
|
156
|
+
0,
|
|
157
|
+
0,
|
|
158
|
+
1
|
|
159
|
+
]);
|
|
160
|
+
return {
|
|
161
|
+
m00,
|
|
162
|
+
m01,
|
|
163
|
+
m02,
|
|
164
|
+
m10,
|
|
165
|
+
m11,
|
|
166
|
+
m12
|
|
167
|
+
};
|
|
168
|
+
}
|
|
141
169
|
//#endregion
|
|
142
|
-
export { DEFAULT_GRADIENT_TRANSFORM, IDENTITY_GRADIENT_TRANSFORM, applyGradientTransform, defaultGradientStops, ellipticalGradientTransform, gradientPoint, gradientTransformRows, invertGradientTransform, isGradientFill, isIdentityGradientTransform, linearGradientTransform, linearGradientTransformFromAxes };
|
|
170
|
+
export { DEFAULT_GRADIENT_TRANSFORM, IDENTITY_GRADIENT_TRANSFORM, applyGradientTransform, defaultGradientStops, ellipticalGradientTransform, gradientPoint, gradientTransformInBox, gradientTransformRows, invertGradientTransform, isGradientFill, isIdentityGradientTransform, linearGradientTransform, linearGradientTransformFromAxes };
|
|
143
171
|
|
|
144
172
|
//# sourceMappingURL=gradient.js.map
|
package/dist/gradient.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gradient.js","names":[],"sources":["../src/gradient.ts"],"sourcesContent":["import type { Color, Vector } from './primitives'\nimport type { Fill, GradientStop, GradientTransform } from './types'\n\n/**\n * Gradient paints in Figma's convention. A paint's `gradientTransform` maps the layer's unit\n * square to gradient space, as Figma's plugin API, `.fig` files, and the clipboard store it. In\n * gradient space a linear gradient runs from (0, 0.5) to (1, 0.5); radial, angular, and diamond\n * gradients are centred on (0.5, 0.5) with radius 0.5, and an angular one starts at +x and turns\n * clockwise. Every reader and writer of gradients goes through this module.\n */\n\nexport type GradientFillType = Extract<\n Fill['type'],\n 'GRADIENT_LINEAR' | 'GRADIENT_RADIAL' | 'GRADIENT_ANGULAR' | 'GRADIENT_DIAMOND'\n>\n\nexport function isGradientFill(type: Fill['type']): type is GradientFillType {\n return (\n type === 'GRADIENT_LINEAR' ||\n type === 'GRADIENT_RADIAL' ||\n type === 'GRADIENT_ANGULAR' ||\n type === 'GRADIENT_DIAMOND'\n )\n}\n\n/** Left to right for a linear gradient; centred and filling the layer for the others. */\nexport const IDENTITY_GRADIENT_TRANSFORM: GradientTransform = {\n m00: 1,\n m01: 0,\n m02: 0,\n m10: 0,\n m11: 1,\n m12: 0\n}\n\n/**\n * Figma's transform when a fill becomes a gradient: top to bottom, and centred when switched to\n * another kind (Figma desktop 126).\n */\nexport const DEFAULT_GRADIENT_TRANSFORM: GradientTransform = {\n m00: 0,\n m01: 1,\n m02: 0,\n m10: -1,\n m11: 0,\n m12: 1\n}\n\n/**\n * A paint transform as Figma's plugin API gives and takes it, and as authors write it: two rows of\n * three, `[[m00, m01, m02], [m10, m11, m12]]`.\n */\nexport type GradientTransformRows = [[number, number, number], [number, number, number]]\n\n/** `t` as Figma's plugin API gives a paint transform: two rows of three. */\nexport function gradientTransformRows(t: GradientTransform): GradientTransformRows {\n return [\n [t.m00, t.m01, t.m02],\n [t.m10, t.m11, t.m12]\n ]\n}\n\n/** How dark a new gradient's second stop is next to the fill colour, as in Figma. */\nconst DEFAULT_GRADIENT_SHADE = 0.6\n\n/** Figma's stops for a new gradient: the fill colour, then a 60% shade of it. */\nexport function defaultGradientStops(color: Color): GradientStop[] {\n return [\n { color: { ...color }, position: 0 },\n {\n color: {\n r: color.r * DEFAULT_GRADIENT_SHADE,\n g: color.g * DEFAULT_GRADIENT_SHADE,\n b: color.b * DEFAULT_GRADIENT_SHADE,\n a: color.a\n },\n position: 1\n }\n ]\n}\n\nexport function isIdentityGradientTransform(t: GradientTransform): boolean {\n return (Object.keys(IDENTITY_GRADIENT_TRANSFORM) as (keyof GradientTransform)[]).every(\n (key) => t[key] === IDENTITY_GRADIENT_TRANSFORM[key]\n )\n}\n\n/**\n * The inverse affine map; a singular one falls back to the identity. Inverting a\n * `gradientTransform` gives the map from gradient space to the layer's unit square.\n */\nexport function invertGradientTransform(t: GradientTransform): GradientTransform {\n const det = t.m00 * t.m11 - t.m01 * t.m10\n if (Math.abs(det) < 1e-12) return IDENTITY_GRADIENT_TRANSFORM\n const m00 = t.m11 / det\n const m01 = -t.m01 / det\n const m10 = -t.m10 / det\n const m11 = t.m00 / det\n return {\n m00,\n m01,\n m02: -(m00 * t.m02 + m01 * t.m12),\n m10,\n m11,\n m12: -(m10 * t.m02 + m11 * t.m12)\n }\n}\n\n/** A point through an affine map. */\nexport function applyGradientTransform(t: GradientTransform, point: Vector): Vector {\n return {\n x: t.m00 * point.x + t.m01 * point.y + t.m02,\n y: t.m10 * point.x + t.m11 * point.y + t.m12\n }\n}\n\n/** Where a point of gradient space falls in the layer's unit square. */\nexport function gradientPoint(t: GradientTransform, point: Vector): Vector {\n return applyGradientTransform(invertGradientTransform(t), point)\n}\n\n/**\n * The transform of a linear gradient from `start` to `end` whose bands run along `across`, all in\n * the layer's unit square, such as an SVG gradient mapped through its own transform.\n */\nexport function linearGradientTransformFromAxes(\n start: Vector,\n end: Vector,\n across: Vector\n): GradientTransform {\n return invertGradientTransform({\n m00: end.x - start.x,\n m01: across.x,\n m02: start.x - across.x / 2,\n m10: end.y - start.y,\n m11: across.y,\n m12: start.y - across.y / 2\n })\n}\n\n/**\n * The transform of a linear gradient from `start` to `end`, both in the layer's unit square, in a\n * `width` × `height` layer. Its second axis is square to the line on screen and `ratio` times as\n * long; Figma keeps that ratio when a handle is dragged (measured on desktop 126), and it starts\n * as the layer's height over its width.\n */\nexport function linearGradientTransform(\n start: Vector,\n end: Vector,\n width = 1,\n height = 1,\n ratio = height / width\n): GradientTransform {\n const axis = { x: end.x - start.x, y: end.y - start.y }\n return linearGradientTransformFromAxes(start, end, {\n x: (-axis.y * height * ratio) / width,\n y: (axis.x * width * ratio) / height\n })\n}\n\n/**\n * The transform of a radial, angular, or diamond gradient from its centre and the ends of its two\n * radii, all in the layer's unit square.\n */\nexport function ellipticalGradientTransform(\n center: Vector,\n radiusX: Vector,\n radiusY: Vector\n): GradientTransform {\n const xAxis = { x: 2 * (radiusX.x - center.x), y: 2 * (radiusX.y - center.y) }\n const yAxis = { x: 2 * (radiusY.x - center.x), y: 2 * (radiusY.y - center.y) }\n return invertGradientTransform({\n m00: xAxis.x,\n m01: yAxis.x,\n m02: center.x - (xAxis.x + yAxis.x) / 2,\n m10: xAxis.y,\n m11: yAxis.y,\n m12: center.y - (xAxis.y + yAxis.y) / 2\n })\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"gradient.js","names":[],"sources":["../src/gradient.ts"],"sourcesContent":["import Matrix from './matrix'\nimport type { Color, Rect, Vector } from './primitives'\nimport type { Fill, GradientStop, GradientTransform } from './types'\n\n/**\n * Gradient paints in Figma's convention. A paint's `gradientTransform` maps the layer's unit\n * square to gradient space, as Figma's plugin API, `.fig` files, and the clipboard store it. In\n * gradient space a linear gradient runs from (0, 0.5) to (1, 0.5); radial, angular, and diamond\n * gradients are centred on (0.5, 0.5) with radius 0.5, and an angular one starts at +x and turns\n * clockwise. Every reader and writer of gradients goes through this module.\n */\n\nexport type GradientFillType = Extract<\n Fill['type'],\n 'GRADIENT_LINEAR' | 'GRADIENT_RADIAL' | 'GRADIENT_ANGULAR' | 'GRADIENT_DIAMOND'\n>\n\nexport function isGradientFill(type: Fill['type']): type is GradientFillType {\n return (\n type === 'GRADIENT_LINEAR' ||\n type === 'GRADIENT_RADIAL' ||\n type === 'GRADIENT_ANGULAR' ||\n type === 'GRADIENT_DIAMOND'\n )\n}\n\n/** Left to right for a linear gradient; centred and filling the layer for the others. */\nexport const IDENTITY_GRADIENT_TRANSFORM: GradientTransform = {\n m00: 1,\n m01: 0,\n m02: 0,\n m10: 0,\n m11: 1,\n m12: 0\n}\n\n/**\n * Figma's transform when a fill becomes a gradient: top to bottom, and centred when switched to\n * another kind (Figma desktop 126).\n */\nexport const DEFAULT_GRADIENT_TRANSFORM: GradientTransform = {\n m00: 0,\n m01: 1,\n m02: 0,\n m10: -1,\n m11: 0,\n m12: 1\n}\n\n/**\n * A paint transform as Figma's plugin API gives and takes it, and as authors write it: two rows of\n * three, `[[m00, m01, m02], [m10, m11, m12]]`.\n */\nexport type GradientTransformRows = [[number, number, number], [number, number, number]]\n\n/** `t` as Figma's plugin API gives a paint transform: two rows of three. */\nexport function gradientTransformRows(t: GradientTransform): GradientTransformRows {\n return [\n [t.m00, t.m01, t.m02],\n [t.m10, t.m11, t.m12]\n ]\n}\n\n/** How dark a new gradient's second stop is next to the fill colour, as in Figma. */\nconst DEFAULT_GRADIENT_SHADE = 0.6\n\n/** Figma's stops for a new gradient: the fill colour, then a 60% shade of it. */\nexport function defaultGradientStops(color: Color): GradientStop[] {\n return [\n { color: { ...color }, position: 0 },\n {\n color: {\n r: color.r * DEFAULT_GRADIENT_SHADE,\n g: color.g * DEFAULT_GRADIENT_SHADE,\n b: color.b * DEFAULT_GRADIENT_SHADE,\n a: color.a\n },\n position: 1\n }\n ]\n}\n\nexport function isIdentityGradientTransform(t: GradientTransform): boolean {\n return (Object.keys(IDENTITY_GRADIENT_TRANSFORM) as (keyof GradientTransform)[]).every(\n (key) => t[key] === IDENTITY_GRADIENT_TRANSFORM[key]\n )\n}\n\n/**\n * The inverse affine map; a singular one falls back to the identity. Inverting a\n * `gradientTransform` gives the map from gradient space to the layer's unit square.\n */\nexport function invertGradientTransform(t: GradientTransform): GradientTransform {\n const det = t.m00 * t.m11 - t.m01 * t.m10\n if (Math.abs(det) < 1e-12) return IDENTITY_GRADIENT_TRANSFORM\n const m00 = t.m11 / det\n const m01 = -t.m01 / det\n const m10 = -t.m10 / det\n const m11 = t.m00 / det\n return {\n m00,\n m01,\n m02: -(m00 * t.m02 + m01 * t.m12),\n m10,\n m11,\n m12: -(m10 * t.m02 + m11 * t.m12)\n }\n}\n\n/** A point through an affine map. */\nexport function applyGradientTransform(t: GradientTransform, point: Vector): Vector {\n return {\n x: t.m00 * point.x + t.m01 * point.y + t.m02,\n y: t.m10 * point.x + t.m11 * point.y + t.m12\n }\n}\n\n/** Where a point of gradient space falls in the layer's unit square. */\nexport function gradientPoint(t: GradientTransform, point: Vector): Vector {\n return applyGradientTransform(invertGradientTransform(t), point)\n}\n\n/**\n * The transform of a linear gradient from `start` to `end` whose bands run along `across`, all in\n * the layer's unit square, such as an SVG gradient mapped through its own transform.\n */\nexport function linearGradientTransformFromAxes(\n start: Vector,\n end: Vector,\n across: Vector\n): GradientTransform {\n return invertGradientTransform({\n m00: end.x - start.x,\n m01: across.x,\n m02: start.x - across.x / 2,\n m10: end.y - start.y,\n m11: across.y,\n m12: start.y - across.y / 2\n })\n}\n\n/**\n * The transform of a linear gradient from `start` to `end`, both in the layer's unit square, in a\n * `width` × `height` layer. Its second axis is square to the line on screen and `ratio` times as\n * long; Figma keeps that ratio when a handle is dragged (measured on desktop 126), and it starts\n * as the layer's height over its width.\n */\nexport function linearGradientTransform(\n start: Vector,\n end: Vector,\n width = 1,\n height = 1,\n ratio = height / width\n): GradientTransform {\n const axis = { x: end.x - start.x, y: end.y - start.y }\n return linearGradientTransformFromAxes(start, end, {\n x: (-axis.y * height * ratio) / width,\n y: (axis.x * width * ratio) / height\n })\n}\n\n/**\n * The transform of a radial, angular, or diamond gradient from its centre and the ends of its two\n * radii, all in the layer's unit square.\n */\nexport function ellipticalGradientTransform(\n center: Vector,\n radiusX: Vector,\n radiusY: Vector\n): GradientTransform {\n const xAxis = { x: 2 * (radiusX.x - center.x), y: 2 * (radiusX.y - center.y) }\n const yAxis = { x: 2 * (radiusY.x - center.x), y: 2 * (radiusY.y - center.y) }\n return invertGradientTransform({\n m00: xAxis.x,\n m01: yAxis.x,\n m02: center.x - (xAxis.x + yAxis.x) / 2,\n m10: xAxis.y,\n m11: yAxis.y,\n m12: center.y - (xAxis.y + yAxis.y) / 2\n })\n}\n\n/**\n * The transform that keeps a gradient where it was on the canvas when its layer's box becomes\n * `box`, given in the old box's units, as Figma rewrites it when editing a path resizes the layer\n * (Figma desktop 126: a 200 × 120 box grown to 380 × 160 and moved up 40 gives\n * `[[1.9, 0, 0], [0, 1.333, -0.333]]` from the identity).\n */\nexport function gradientTransformInBox(t: GradientTransform, box: Rect): GradientTransform {\n const [m00, m01, m02, m10, m11, m12] = Matrix.multiply(Matrix.fromAffine(t), [\n box.width,\n 0,\n box.x,\n 0,\n box.height,\n box.y,\n 0,\n 0,\n 1\n ])\n return { m00, m01, m02, m10, m11, m12 }\n}\n"],"mappings":";;AAiBA,SAAgB,eAAe,MAA8C;CAC3E,OACE,SAAS,qBACT,SAAS,qBACT,SAAS,sBACT,SAAS;AAEb;;AAGA,MAAa,8BAAiD;CAC5D,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;AACP;;;;;AAMA,MAAa,6BAAgD;CAC3D,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;AACP;;AASA,SAAgB,sBAAsB,GAA6C;CACjF,OAAO,CACL;EAAC,EAAE;EAAK,EAAE;EAAK,EAAE;CAAG,GACpB;EAAC,EAAE;EAAK,EAAE;EAAK,EAAE;CAAG,CACtB;AACF;;AAGA,MAAM,yBAAyB;;AAG/B,SAAgB,qBAAqB,OAA8B;CACjE,OAAO,CACL;EAAE,OAAO,EAAE,GAAG,MAAM;EAAG,UAAU;CAAE,GACnC;EACE,OAAO;GACL,GAAG,MAAM,IAAI;GACb,GAAG,MAAM,IAAI;GACb,GAAG,MAAM,IAAI;GACb,GAAG,MAAM;EACX;EACA,UAAU;CACZ,CACF;AACF;AAEA,SAAgB,4BAA4B,GAA+B;CACzE,OAAQ,OAAO,KAAK,2BAA2B,CAAC,CAAiC,OAC9E,QAAQ,EAAE,SAAS,4BAA4B,IAClD;AACF;;;;;AAMA,SAAgB,wBAAwB,GAAyC;CAC/E,MAAM,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,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