-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
266 additions
and
1,467 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
|
||
set -e | ||
|
||
run-s build test | ||
git add . | ||
npm run build | ||
npm t | ||
git add -u |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
* | ||
!LICENSE | ||
!dist/ | ||
!src/ | ||
!package.json | ||
!CHANGELOG.md | ||
!README.md | ||
!tsconfig.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package-lock=true | ||
save-exact=true | ||
save-dev=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
"use strict"; | ||
// WARNING: This file is autogenerated, edit lib/index.template.ts | ||
import svgPath from "svgpath"; | ||
// Precision for consider cubic polynom as quadratic one | ||
const CBEZIER_MINMAX_EPSILON = 0.00000001; | ||
// https://github.com/kpym/SVGPathy/blob/acd1a50c626b36d81969f6e98e8602e128ba4302/lib/box.js#L89 | ||
function minmaxQ(A) { | ||
const min = Math.min(A[0], A[2]), max = Math.max(A[0], A[2]); | ||
if (A[1] >= A[0] ? A[2] >= A[1] : A[2] <= A[1]) { | ||
// if no extremum in ]0,1[ | ||
return [min, max]; | ||
} | ||
// check if the extremum E is min or max | ||
const E = (A[0] * A[2] - A[1] * A[1]) / (A[0] - 2 * A[1] + A[2]); | ||
return E < min ? [E, max] : [min, E]; | ||
} | ||
// https://github.com/kpym/SVGPathy/blob/acd1a50c626b36d81969f6e98e8602e128ba4302/lib/box.js#L127 | ||
function minmaxC(A) { | ||
const K = A[0] - 3 * A[1] + 3 * A[2] - A[3]; | ||
// if the polynomial is (almost) quadratic and not cubic | ||
if (Math.abs(K) < CBEZIER_MINMAX_EPSILON) { | ||
if (A[0] === A[3] && A[0] === A[1]) { | ||
// no curve, point targeting same location | ||
return [A[0], A[3]]; | ||
} | ||
return minmaxQ([ | ||
A[0], | ||
-0.5 * A[0] + 1.5 * A[1], | ||
A[0] - 3 * A[1] + 3 * A[2], | ||
]); | ||
} | ||
// the reduced discriminant of the derivative | ||
const T = -A[0] * A[2] + | ||
A[0] * A[3] - | ||
A[1] * A[2] - | ||
A[1] * A[3] + | ||
A[1] * A[1] + | ||
A[2] * A[2]; | ||
// if the polynomial is monotone in [0,1] | ||
if (T <= 0) { | ||
return [Math.min(A[0], A[3]), Math.max(A[0], A[3])]; | ||
} | ||
const S = Math.sqrt(T); | ||
// potential extrema | ||
let min = Math.min(A[0], A[3]), max = Math.max(A[0], A[3]); | ||
const L = A[0] - 2 * A[1] + A[2]; | ||
// check local extrema | ||
for (let R = (L + S) / K, i = 1; i <= 2; R = (L - S) / K, i++) { | ||
if (R > 0 && R < 1) { | ||
// if the extrema is for R in [0,1] | ||
const Q = A[0] * (1 - R) * (1 - R) * (1 - R) + | ||
A[1] * 3 * (1 - R) * (1 - R) * R + | ||
A[2] * 3 * (1 - R) * R * R + | ||
A[3] * R * R * R; | ||
if (Q < min) { | ||
min = Q; | ||
} | ||
if (Q > max) { | ||
max = Q; | ||
} | ||
} | ||
} | ||
return [min, max]; | ||
} | ||
/** | ||
* Compute bounding boxes of SVG paths. | ||
* @param {String} d SVG path for which their bounding box will be computed. | ||
* @returns {BBox} | ||
*/ | ||
export function svgPathBbox(d) { | ||
const min = [Infinity, Infinity], max = [-Infinity, -Infinity]; | ||
svgPath(d) | ||
.abs() | ||
.unarc() | ||
.unshort() | ||
.iterate((seg, _, x, y) => { | ||
switch (seg[0]) { | ||
// The next cases are ordered based on simple-icons data | ||
case "M": { | ||
if (min[0] > seg[1]) { | ||
min[0] = seg[1]; | ||
} | ||
if (min[1] > seg[2]) { | ||
min[1] = seg[2]; | ||
} | ||
if (max[0] < seg[1]) { | ||
max[0] = seg[1]; | ||
} | ||
if (max[1] < seg[2]) { | ||
max[1] = seg[2]; | ||
} | ||
break; | ||
} | ||
case "H": { | ||
if (min[0] > seg[1]) { | ||
min[0] = seg[1]; | ||
} | ||
if (max[0] < seg[1]) { | ||
max[0] = seg[1]; | ||
} | ||
break; | ||
} | ||
case "V": { | ||
if (min[1] > seg[1]) { | ||
min[1] = seg[1]; | ||
} | ||
if (max[1] < seg[1]) { | ||
max[1] = seg[1]; | ||
} | ||
break; | ||
} | ||
case "L": { | ||
if (min[0] > seg[1]) { | ||
min[0] = seg[1]; | ||
} | ||
if (min[1] > seg[2]) { | ||
min[1] = seg[2]; | ||
} | ||
if (max[0] < seg[1]) { | ||
max[0] = seg[1]; | ||
} | ||
if (max[1] < seg[2]) { | ||
max[1] = seg[2]; | ||
} | ||
break; | ||
} | ||
case "C": { | ||
const cxMinMax = minmaxC([x, seg[1], seg[3], seg[5]]); | ||
if (min[0] > cxMinMax[0]) { | ||
min[0] = cxMinMax[0]; | ||
} | ||
if (max[0] < cxMinMax[1]) { | ||
max[0] = cxMinMax[1]; | ||
} | ||
const cyMinMax = minmaxC([y, seg[2], seg[4], seg[6]]); | ||
if (min[1] > cyMinMax[0]) { | ||
min[1] = cyMinMax[0]; | ||
} | ||
if (max[1] < cyMinMax[1]) { | ||
max[1] = cyMinMax[1]; | ||
} | ||
break; | ||
} | ||
case "Q": { | ||
const qxMinMax = minmaxQ([x, seg[1], seg[3]]); | ||
if (min[0] > qxMinMax[0]) { | ||
min[0] = qxMinMax[0]; | ||
} | ||
if (max[0] < qxMinMax[1]) { | ||
max[0] = qxMinMax[1]; | ||
} | ||
const qyMinMax = minmaxQ([y, seg[2], seg[4]]); | ||
if (min[1] > qyMinMax[0]) { | ||
min[1] = qyMinMax[0]; | ||
} | ||
if (max[1] < qyMinMax[1]) { | ||
max[1] = qyMinMax[1]; | ||
} | ||
break; | ||
} | ||
} | ||
}, true); | ||
return [min[0], min[1], max[0], max[1]]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
const util = require("util") | ||
const svgPathBbox = require("../dist/index.js"); | ||
const { svgPathBbox } = require("../dist/index.js"); | ||
|
||
const bbox = svgPathBbox("M0 0H3V6Z"); | ||
process.stdout.write(`${util.inspect(bbox)}\n`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { inspect } from "node:util"; | ||
import svgPathBbox from "../dist/index.mjs"; | ||
import { svgPathBbox } from "../dist/es2015/index.mjs"; | ||
|
||
const bbox = svgPathBbox("M0 0H3V6Z"); | ||
process.stdout.write(`${inspect(bbox)}\n`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.