@newtonedev/components 0.1.9 → 0.1.10
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/fonts/measureFont.d.ts +2 -1
- package/dist/fonts/measureFont.d.ts.map +1 -1
- package/dist/index.cjs +11 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +11 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/fonts/measureFont.ts +15 -2
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Measure the average character width ratio for a font using the Canvas API.
|
|
3
3
|
*
|
|
4
|
-
* Waits for the font to load via `document.fonts.load()` before measuring
|
|
4
|
+
* Waits for the font to load via `document.fonts.load()` before measuring,
|
|
5
|
+
* with a 3-second timeout to prevent hangs when fonts fail to load silently.
|
|
5
6
|
* Falls back to 0.55 if the browser context is unavailable, the font fails
|
|
6
7
|
* to load, or canvas is not supported.
|
|
7
8
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"measureFont.d.ts","sourceRoot":"","sources":["../../src/fonts/measureFont.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"measureFont.d.ts","sourceRoot":"","sources":["../../src/fonts/measureFont.ts"],"names":[],"mappings":"AAeA;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,mBAAmB,CACvC,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,EAChB,QAAQ,SAAK,GACZ,OAAO,CAAC,MAAM,CAAC,CAiBjB"}
|
package/dist/index.cjs
CHANGED
|
@@ -840,10 +840,20 @@ function Icon({
|
|
|
840
840
|
|
|
841
841
|
// src/fonts/measureFont.ts
|
|
842
842
|
var REF_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
|
|
843
|
+
function withTimeout(promise, ms, fallback) {
|
|
844
|
+
return Promise.race([
|
|
845
|
+
promise,
|
|
846
|
+
new Promise((resolve) => setTimeout(() => resolve(fallback), ms))
|
|
847
|
+
]);
|
|
848
|
+
}
|
|
843
849
|
async function measureAvgCharWidth(fontFamily, fontWeight, fallback, fontSize = 16) {
|
|
844
850
|
if (typeof document === "undefined") return 0.55;
|
|
845
851
|
try {
|
|
846
|
-
await
|
|
852
|
+
await withTimeout(
|
|
853
|
+
document.fonts.load(`${fontWeight} ${fontSize}px "${fontFamily}"`),
|
|
854
|
+
3e3,
|
|
855
|
+
[]
|
|
856
|
+
);
|
|
847
857
|
const canvas = document.createElement("canvas");
|
|
848
858
|
const ctx = canvas.getContext("2d");
|
|
849
859
|
if (!ctx) return 0.55;
|