@format.dev/utils 0.3.0 → 0.4.1

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/README.md CHANGED
@@ -16,6 +16,16 @@ The DOM policy lists (`src/dom.ts`):
16
16
  - `FORBID_TAGS` is the render-time removal list: `STUDIO_FORBID_TAGS` plus the form elements.
17
17
  - `UNSUPPORTED_TAGS` lists the tags the Format Studio preview keeps visible but the rendered PDF drops.
18
18
 
19
+ The page zoom (`src/page-zoom.ts`), imported from `@format.dev/utils/page-zoom`:
20
+
21
+ - `DOCUMENT_RESOLUTION` is the resolution Format lays out every document at, ten layout pixels to each CSS pixel.
22
+ - `previewPageZoom` returns the zoom a preview runs the engine at to reach that resolution, so a document is measured the same way on a display of any pixel density.
23
+ - `renderedPageZoom` reads the zoom a page carries.
24
+
25
+ ## Entry points
26
+
27
+ The root entry, `@format.dev/utils`, runs anywhere and type-checks without the DOM library. Node projects import it, and one DOM type in its exports would fail their build. Code that needs a browser has an entry point of its own, as the page zoom does, and the root doesn't re-export it.
28
+
19
29
  ## Module format
20
30
 
21
31
  The package ships ES modules only. Loading it with `require()` needs Node 22.13 or later, which is the floor `@format.dev/zip` declares.
package/dist/mime.d.ts CHANGED
@@ -19,11 +19,7 @@ declare const EXTENSION_TO_MIME_MAP: {
19
19
  readonly svg: "image/svg+xml";
20
20
  readonly webp: "image/webp";
21
21
  readonly bmp: "image/bmp";
22
- readonly tiff: "image/tiff";
23
22
  readonly avif: "image/avif";
24
- readonly tif: "image/tiff";
25
- readonly heic: "image/heic";
26
- readonly heif: "image/heif";
27
23
  };
28
24
  declare const EXCLUDED_EXTENSIONS: readonly ["js", "mjs", "wasm", "webmanifest"];
29
25
  type FormatAssetsMimeExtension = keyof typeof EXTENSION_TO_MIME_MAP;
@@ -32,7 +28,7 @@ type UserAssetsMimeExtension = Exclude<FormatAssetsMimeExtension, ExcludedExtens
32
28
  declare const MIME_TYPE: Readonly<Record<FormatAssetsMimeExtension, string>>;
33
29
  type MimeType = (typeof MIME_TYPE)[FormatAssetsMimeExtension];
34
30
  declare const USER_MIME_TYPE: Readonly<Record<UserAssetsMimeExtension, string>>;
35
- declare const FORMAT_MIME_TYPE: Readonly<Record<"js" | "mjs" | "wasm" | "webmanifest" | "html" | "css" | "json" | "ttf" | "otf" | "woff" | "woff2" | "eot" | "png" | "jpg" | "jpeg" | "gif" | "svg" | "webp" | "bmp" | "tiff" | "avif" | "tif" | "heic" | "heif", string>>;
31
+ declare const FORMAT_MIME_TYPE: Readonly<Record<"html" | "js" | "mjs" | "wasm" | "webmanifest" | "css" | "json" | "ttf" | "otf" | "woff" | "woff2" | "eot" | "png" | "jpg" | "jpeg" | "gif" | "svg" | "webp" | "bmp" | "avif", string>>;
36
32
  declare const EXT_TO_MIME: Readonly<Record<string, string>>;
37
33
  declare const MIME_TO_EXT: Readonly<Record<string, string>>;
38
34
  declare function getPermittedContentType(ext: string): string | undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"mime.d.ts","names":[],"sources":["../src/mime.ts"],"sourcesContent":[],"mappings":";cAAa;EAAA,SAAA,IAAA,EAAA,WA4BH;EAEG,SAAA,EAAA,EAAA,iBAAmE;EAEpE,SAAA,GAAA,EAAA,iBAAyB;EAEzB,SAAA,IAAA,EAAA,kBAA6B;EAE7B,SAAA,WAAA,EAAA,2BAAuB;EAAW,SAAA,GAAA,EAAA,UAAA;EAA2B,SAAA,IAAA,EAAA,kBAAA;EAAnC,SAAA,GAAA,EAAA,UAAA;EAAO,SAAA,GAAA,EAAA,UAAA;EAEhC,SAAA,IAAsF,EAAA,WAAA;EAA3D,SAAA,KAAA,EAAA,YAAA;EAAP,SAAA,GAAA,EAAA,+BAAA;EAAT,SAAA,GAAA,EAAA,WAAA;EAAQ,SAAA,GAAA,EAAA,WAAA;EAEpB,SAAA,IAAQ,EAAA,YAAW;EAElB,SAAA,GAAA,EAAA,WAEyC;EAFT,SAAA,GAAA,EAAA,eAAA;EAAP,SAAA,IAAA,EAAA,YAAA;EAAT,SAAA,GAAA,EAAA,WAAA;EAAQ,SAAA,IAAA,EAAA,YAAA;EAIxB,SAAA,IAAA,EAAA,YAA4B;EAE5B,SAAA,GAAA,EAEZ,YAFkC;EAItB,SAAA,IAAA,EAEwB,YAFF;EAInB,SAAA,IAAA,EAAA,YAAuB;AAKvC,CAAA;AAIgB,cAnCH,mBAmC6C,EAAA,SAAA,CAAA,IAAA,EAAA,KAAuB,EAAA,MAAA,EAAA,aAAA,CAAA;KAjCrE,yBAAA,gBAAyC;KAEzC,kBAAA,WAA6B;KAE7B,uBAAA,GAA0B,QAAQ,2BAA2B;cAE5D,WAAW,SAAS,OAAO;KAE5B,QAAA,WAAmB,WAAW;cAE7B,gBAAgB,SAAS,OAAO;cAIhC,kBAAgB,SAAA;cAEhB,aAAa,SAAS;cAItB,aAAa,SAAS;iBAInB,uBAAA;iBAKA,sBAAA,sBAA4C;iBAI5C,oBAAA,sBAA0C"}
1
+ {"version":3,"file":"mime.d.ts","names":[],"sources":["../src/mime.ts"],"sourcesContent":[],"mappings":";cAAa;EAAA,SAAA,IAAA,EAAA,WAyBH;EAEG,SAAA,EAAA,EAAA,iBAAmE;EAEpE,SAAA,GAAA,EAAA,iBAAyB;EAEzB,SAAA,IAAA,EAAA,kBAA6B;EAE7B,SAAA,WAAA,EAAA,2BAAuB;EAAW,SAAA,GAAA,EAAA,UAAA;EAA2B,SAAA,IAAA,EAAA,kBAAA;EAAnC,SAAA,GAAA,EAAA,UAAA;EAAO,SAAA,GAAA,EAAA,UAAA;EAEhC,SAAA,IAAsF,EAAA,WAAA;EAA3D,SAAA,KAAA,EAAA,YAAA;EAAP,SAAA,GAAA,EAAA,+BAAA;EAAT,SAAA,GAAA,EAAA,WAAA;EAAQ,SAAA,GAAA,EAAA,WAAA;EAEpB,SAAA,IAAQ,EAAA,YAAW;EAElB,SAAA,GAAA,EAAA,WAEyC;EAFT,SAAA,GAAA,EAAA,eAAA;EAAP,SAAA,IAAA,EAAA,YAAA;EAAT,SAAA,GAAA,EAAA,WAAA;EAAQ,SAAA,IAAA,EAAA,YAAA;AAIrC,CAAA;AAEa,cAlBA,mBAkBa,EAAA,SAAQ,CAAA,IAAA,EAAA,KAAA,EAAA,MAAA,EAAA,aAAA,CAAA;AAIrB,KApBD,yBAAA,GAoBc,MAAA,OApB2B,qBAoBnB;AAIlB,KAtBJ,kBAAA,GAsB2B,CAAA,OAtBE,mBAsBF,CAAA,CAAA,MAAA,CAAA;AAKvB,KAzBJ,uBAAA,GAA0B,OAyBsB,CAzBd,yBAyBuC,EAzBZ,kBAyBY,CAAA;AAIrE,cA3BH,SA2BuB,EA3BZ,QA2BkC,CA3BzB,MA2ByB,CA3BlB,yBA2ByC,EAAA,MAAA,CAAA,CAAA;KAzBrE,QAAA,WAAmB,WAAW;cAE7B,gBAAgB,SAAS,OAAO;cAIhC,kBAAgB,SAAA;cAEhB,aAAa,SAAS;cAItB,aAAa,SAAS;iBAInB,uBAAA;iBAKA,sBAAA,sBAA4C;iBAI5C,oBAAA,sBAA0C"}
package/dist/mime.js CHANGED
@@ -19,11 +19,7 @@ const EXTENSION_TO_MIME_MAP = {
19
19
  svg: "image/svg+xml",
20
20
  webp: "image/webp",
21
21
  bmp: "image/bmp",
22
- tiff: "image/tiff",
23
- avif: "image/avif",
24
- tif: "image/tiff",
25
- heic: "image/heic",
26
- heif: "image/heif"
22
+ avif: "image/avif"
27
23
  };
28
24
  const EXCLUDED_EXTENSIONS = [
29
25
  "js",
package/dist/mime.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"mime.js","names":["MIME_TYPE: Readonly<Record<FormatAssetsMimeExtension, string>>","USER_MIME_TYPE: Readonly<Record<UserAssetsMimeExtension, string>>","EXT_TO_MIME: Readonly<Record<string, string>>","MIME_TO_EXT: Readonly<Record<string, string>>"],"sources":["../src/mime.ts"],"sourcesContent":["export const EXTENSION_TO_MIME_MAP = {\n\t// format-level\n\thtml: 'text/html',\n\tjs: 'text/javascript',\n\tmjs: 'text/javascript',\n\twasm: 'application/wasm',\n\twebmanifest: 'application/manifest+json',\n\n\t// user-level\n\tcss: 'text/css',\n\tjson: 'application/json',\n\tttf: 'font/ttf',\n\totf: 'font/otf',\n\twoff: 'font/woff',\n\twoff2: 'font/woff2',\n\teot: 'application/vnd.ms-fontobject',\n\tpng: 'image/png',\n\tjpg: 'image/jpg',\n\tjpeg: 'image/jpeg',\n\tgif: 'image/gif',\n\tsvg: 'image/svg+xml',\n\twebp: 'image/webp',\n\tbmp: 'image/bmp',\n\ttiff: 'image/tiff',\n\tavif: 'image/avif',\n\ttif: 'image/tiff',\n\theic: 'image/heic',\n\theif: 'image/heif'\n} as const\n\nexport const EXCLUDED_EXTENSIONS = ['js', 'mjs', 'wasm', 'webmanifest'] as const\n\nexport type FormatAssetsMimeExtension = keyof typeof EXTENSION_TO_MIME_MAP\n\nexport type ExcludedExtensions = (typeof EXCLUDED_EXTENSIONS)[number]\n\nexport type UserAssetsMimeExtension = Exclude<FormatAssetsMimeExtension, ExcludedExtensions>\n\nexport const MIME_TYPE: Readonly<Record<FormatAssetsMimeExtension, string>> = EXTENSION_TO_MIME_MAP\n\nexport type MimeType = (typeof MIME_TYPE)[FormatAssetsMimeExtension]\n\nexport const USER_MIME_TYPE: Readonly<Record<UserAssetsMimeExtension, string>> = Object.fromEntries(\n\tObject.entries(EXTENSION_TO_MIME_MAP).filter(([ext]) => !EXCLUDED_EXTENSIONS.includes(ext as ExcludedExtensions))\n) as Readonly<Record<UserAssetsMimeExtension, string>>\n\nexport const FORMAT_MIME_TYPE = MIME_TYPE\n\nexport const EXT_TO_MIME: Readonly<Record<string, string>> = Object.fromEntries(\n\tObject.entries(EXTENSION_TO_MIME_MAP).map(([ext, mime]) => [`.${ext}`, mime])\n)\n\nexport const MIME_TO_EXT: Readonly<Record<string, string>> = Object.fromEntries(\n\tObject.entries(EXTENSION_TO_MIME_MAP).map(([ext, mime]) => [mime, `.${ext}`])\n) as Readonly<Record<string, string>>\n\nexport function getPermittedContentType(ext: string): string | undefined {\n\tconst normalized = ext.startsWith('.') ? ext : `.${ext}`\n\treturn EXT_TO_MIME[normalized]\n}\n\nexport function isFormatAssetExtension(ext: string): ext is FormatAssetsMimeExtension {\n\treturn ext in FORMAT_MIME_TYPE\n}\n\nexport function isUserAssetExtension(ext: string): ext is UserAssetsMimeExtension {\n\treturn ext in USER_MIME_TYPE\n}\n"],"mappings":";AAAA,MAAa,wBAAwB;CAEpC,MAAM;CACN,IAAI;CACJ,KAAK;CACL,MAAM;CACN,aAAa;CAGb,KAAK;CACL,MAAM;CACN,KAAK;CACL,KAAK;CACL,MAAM;CACN,OAAO;CACP,KAAK;CACL,KAAK;CACL,KAAK;CACL,MAAM;CACN,KAAK;CACL,KAAK;CACL,MAAM;CACN,KAAK;CACL,MAAM;CACN,MAAM;CACN,KAAK;CACL,MAAM;CACN,MAAM;CACN;AAED,MAAa,sBAAsB;CAAC;CAAM;CAAO;CAAQ;CAAc;AAQvE,MAAaA,YAAiE;AAI9E,MAAaC,iBAAoE,OAAO,YACvF,OAAO,QAAQ,sBAAsB,CAAC,QAAQ,CAAC,SAAS,CAAC,oBAAoB,SAAS,IAA0B,CAAC,CACjH;AAED,MAAa,mBAAmB;AAEhC,MAAaC,cAAgD,OAAO,YACnE,OAAO,QAAQ,sBAAsB,CAAC,KAAK,CAAC,KAAK,UAAU,CAAC,IAAI,OAAO,KAAK,CAAC,CAC7E;AAED,MAAaC,cAAgD,OAAO,YACnE,OAAO,QAAQ,sBAAsB,CAAC,KAAK,CAAC,KAAK,UAAU,CAAC,MAAM,IAAI,MAAM,CAAC,CAC7E;AAED,SAAgB,wBAAwB,KAAiC;AAExE,QAAO,YADY,IAAI,WAAW,IAAI,GAAG,MAAM,IAAI;;AAIpD,SAAgB,uBAAuB,KAA+C;AACrF,QAAO,OAAO;;AAGf,SAAgB,qBAAqB,KAA6C;AACjF,QAAO,OAAO"}
1
+ {"version":3,"file":"mime.js","names":["MIME_TYPE: Readonly<Record<FormatAssetsMimeExtension, string>>","USER_MIME_TYPE: Readonly<Record<UserAssetsMimeExtension, string>>","EXT_TO_MIME: Readonly<Record<string, string>>","MIME_TO_EXT: Readonly<Record<string, string>>"],"sources":["../src/mime.ts"],"sourcesContent":["export const EXTENSION_TO_MIME_MAP = {\n\t// format-level\n\thtml: 'text/html',\n\tjs: 'text/javascript',\n\tmjs: 'text/javascript',\n\twasm: 'application/wasm',\n\twebmanifest: 'application/manifest+json',\n\n\t// user-level\n\tcss: 'text/css',\n\tjson: 'application/json',\n\tttf: 'font/ttf',\n\totf: 'font/otf',\n\twoff: 'font/woff',\n\twoff2: 'font/woff2',\n\teot: 'application/vnd.ms-fontobject',\n\t// Renders cannot decode TIFF or HEIC, so neither format is accepted.\n\tpng: 'image/png',\n\tjpg: 'image/jpg',\n\tjpeg: 'image/jpeg',\n\tgif: 'image/gif',\n\tsvg: 'image/svg+xml',\n\twebp: 'image/webp',\n\tbmp: 'image/bmp',\n\tavif: 'image/avif'\n} as const\n\nexport const EXCLUDED_EXTENSIONS = ['js', 'mjs', 'wasm', 'webmanifest'] as const\n\nexport type FormatAssetsMimeExtension = keyof typeof EXTENSION_TO_MIME_MAP\n\nexport type ExcludedExtensions = (typeof EXCLUDED_EXTENSIONS)[number]\n\nexport type UserAssetsMimeExtension = Exclude<FormatAssetsMimeExtension, ExcludedExtensions>\n\nexport const MIME_TYPE: Readonly<Record<FormatAssetsMimeExtension, string>> = EXTENSION_TO_MIME_MAP\n\nexport type MimeType = (typeof MIME_TYPE)[FormatAssetsMimeExtension]\n\nexport const USER_MIME_TYPE: Readonly<Record<UserAssetsMimeExtension, string>> = Object.fromEntries(\n\tObject.entries(EXTENSION_TO_MIME_MAP).filter(([ext]) => !EXCLUDED_EXTENSIONS.includes(ext as ExcludedExtensions))\n) as Readonly<Record<UserAssetsMimeExtension, string>>\n\nexport const FORMAT_MIME_TYPE = MIME_TYPE\n\nexport const EXT_TO_MIME: Readonly<Record<string, string>> = Object.fromEntries(\n\tObject.entries(EXTENSION_TO_MIME_MAP).map(([ext, mime]) => [`.${ext}`, mime])\n)\n\nexport const MIME_TO_EXT: Readonly<Record<string, string>> = Object.fromEntries(\n\tObject.entries(EXTENSION_TO_MIME_MAP).map(([ext, mime]) => [mime, `.${ext}`])\n) as Readonly<Record<string, string>>\n\nexport function getPermittedContentType(ext: string): string | undefined {\n\tconst normalized = ext.startsWith('.') ? ext : `.${ext}`\n\treturn EXT_TO_MIME[normalized]\n}\n\nexport function isFormatAssetExtension(ext: string): ext is FormatAssetsMimeExtension {\n\treturn ext in FORMAT_MIME_TYPE\n}\n\nexport function isUserAssetExtension(ext: string): ext is UserAssetsMimeExtension {\n\treturn ext in USER_MIME_TYPE\n}\n"],"mappings":";AAAA,MAAa,wBAAwB;CAEpC,MAAM;CACN,IAAI;CACJ,KAAK;CACL,MAAM;CACN,aAAa;CAGb,KAAK;CACL,MAAM;CACN,KAAK;CACL,KAAK;CACL,MAAM;CACN,OAAO;CACP,KAAK;CAEL,KAAK;CACL,KAAK;CACL,MAAM;CACN,KAAK;CACL,KAAK;CACL,MAAM;CACN,KAAK;CACL,MAAM;CACN;AAED,MAAa,sBAAsB;CAAC;CAAM;CAAO;CAAQ;CAAc;AAQvE,MAAaA,YAAiE;AAI9E,MAAaC,iBAAoE,OAAO,YACvF,OAAO,QAAQ,sBAAsB,CAAC,QAAQ,CAAC,SAAS,CAAC,oBAAoB,SAAS,IAA0B,CAAC,CACjH;AAED,MAAa,mBAAmB;AAEhC,MAAaC,cAAgD,OAAO,YACnE,OAAO,QAAQ,sBAAsB,CAAC,KAAK,CAAC,KAAK,UAAU,CAAC,IAAI,OAAO,KAAK,CAAC,CAC7E;AAED,MAAaC,cAAgD,OAAO,YACnE,OAAO,QAAQ,sBAAsB,CAAC,KAAK,CAAC,KAAK,UAAU,CAAC,MAAM,IAAI,MAAM,CAAC,CAC7E;AAED,SAAgB,wBAAwB,KAAiC;AAExE,QAAO,YADY,IAAI,WAAW,IAAI,GAAG,MAAM,IAAI;;AAIpD,SAAgB,uBAAuB,KAA+C;AACrF,QAAO,OAAO;;AAGf,SAAgB,qBAAqB,KAA6C;AACjF,QAAO,OAAO"}
@@ -0,0 +1,34 @@
1
+ //#region src/page-zoom.d.ts
2
+ /**
3
+ * Format lays out every document at one resolution, ten layout pixels to each CSS
4
+ * pixel. Border widths, font metrics and font sizes are rounded to a layout pixel, so
5
+ * one resolution means a document is measured the same way on any display, at any
6
+ * browser zoom, and in its PDF.
7
+ */
8
+ declare const DOCUMENT_RESOLUTION = 10;
9
+ /**
10
+ * The `pageZoom` a preview runs the engine at: Format's resolution divided by the
11
+ * device pixel ratio. The browser rounds border widths, font metrics and font sizes
12
+ * in device pixels, at the CSS zoom times the device pixel ratio. At this zoom that
13
+ * product is the resolution on any display, so a document wraps its lines and breaks
14
+ * its pages the same way on all of them. A preview shows its pages scaled by the
15
+ * inverse, `devicePixelRatio / DOCUMENT_RESOLUTION`.
16
+ *
17
+ * The browser keeps zooms as 32-bit floats and multiplies them in float, so the
18
+ * plain quotient can miss. At a ratio of 2.11, 10 / 2.11 stores as 4.7393365, whose
19
+ * product with 2.11 is 9.999999. Font sizes are floored to 0.01 device px, so a 16px
20
+ * font then lands at 159.99 instead of 160.00, and lines wrap differently. The zoom
21
+ * returned is the float nearest the quotient whose float product with the ratio is
22
+ * exactly the resolution, or the first float above it when none is exact.
23
+ */
24
+ declare function previewPageZoom(devicePixelRatio?: number): number;
25
+ /**
26
+ * Returns the page zoom `page` carries, or 1 when there is no page. A preview gives its pages
27
+ * the zoom of the new device pixel ratio when the ratio changes. Pages that a run is
28
+ * still building keep the zoom the run started with until it finishes, so code that
29
+ * places or scales pages reads the zoom they have.
30
+ */
31
+ declare function renderedPageZoom(page: Element | null): number;
32
+ //#endregion
33
+ export { DOCUMENT_RESOLUTION, previewPageZoom, renderedPageZoom };
34
+ //# sourceMappingURL=page-zoom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"page-zoom.d.ts","names":[],"sources":["../src/page-zoom.ts"],"sourcesContent":[],"mappings":";;AAMA;AAiBA;AAkCA;;;cAnDa,mBAAA;;;;;;;;;;;;;;;;iBAiBG,eAAA;;;;;;;iBAkCA,gBAAA,OAAuB"}
@@ -0,0 +1,64 @@
1
+ //#region src/page-zoom.ts
2
+ /**
3
+ * Format lays out every document at one resolution, ten layout pixels to each CSS
4
+ * pixel. Border widths, font metrics and font sizes are rounded to a layout pixel, so
5
+ * one resolution means a document is measured the same way on any display, at any
6
+ * browser zoom, and in its PDF.
7
+ */
8
+ const DOCUMENT_RESOLUTION = 10;
9
+ /**
10
+ * The `pageZoom` a preview runs the engine at: Format's resolution divided by the
11
+ * device pixel ratio. The browser rounds border widths, font metrics and font sizes
12
+ * in device pixels, at the CSS zoom times the device pixel ratio. At this zoom that
13
+ * product is the resolution on any display, so a document wraps its lines and breaks
14
+ * its pages the same way on all of them. A preview shows its pages scaled by the
15
+ * inverse, `devicePixelRatio / DOCUMENT_RESOLUTION`.
16
+ *
17
+ * The browser keeps zooms as 32-bit floats and multiplies them in float, so the
18
+ * plain quotient can miss. At a ratio of 2.11, 10 / 2.11 stores as 4.7393365, whose
19
+ * product with 2.11 is 9.999999. Font sizes are floored to 0.01 device px, so a 16px
20
+ * font then lands at 159.99 instead of 160.00, and lines wrap differently. The zoom
21
+ * returned is the float nearest the quotient whose float product with the ratio is
22
+ * exactly the resolution, or the first float above it when none is exact.
23
+ */
24
+ function previewPageZoom(devicePixelRatio = window.devicePixelRatio || 1) {
25
+ const ratio = Math.fround(devicePixelRatio);
26
+ const product = (zoom$1) => Math.fround(zoom$1 * ratio);
27
+ const quotient = Math.fround(DOCUMENT_RESOLUTION / ratio);
28
+ const exact = [
29
+ 0,
30
+ -1,
31
+ 1,
32
+ -2,
33
+ 2,
34
+ -3,
35
+ 3,
36
+ -4,
37
+ 4
38
+ ].map((step) => float32Step(quotient, step)).find((zoom$1) => product(zoom$1) === DOCUMENT_RESOLUTION);
39
+ if (exact !== void 0) return exact;
40
+ let zoom = quotient;
41
+ while (product(zoom) < DOCUMENT_RESOLUTION) zoom = float32Step(zoom, 1);
42
+ return zoom;
43
+ }
44
+ /** The 32-bit float `steps` representable values away from the float `value`. */
45
+ function float32Step(value, steps) {
46
+ const floats = new Float32Array([value]);
47
+ const bits = new Uint32Array(floats.buffer);
48
+ bits[0] += steps;
49
+ return floats[0];
50
+ }
51
+ /**
52
+ * Returns the page zoom `page` carries, or 1 when there is no page. A preview gives its pages
53
+ * the zoom of the new device pixel ratio when the ratio changes. Pages that a run is
54
+ * still building keep the zoom the run started with until it finishes, so code that
55
+ * places or scales pages reads the zoom they have.
56
+ */
57
+ function renderedPageZoom(page) {
58
+ const zoom = page?.currentCSSZoom;
59
+ return typeof zoom === "number" && zoom > 0 ? zoom : 1;
60
+ }
61
+
62
+ //#endregion
63
+ export { DOCUMENT_RESOLUTION, previewPageZoom, renderedPageZoom };
64
+ //# sourceMappingURL=page-zoom.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"page-zoom.js","names":["zoom"],"sources":["../src/page-zoom.ts"],"sourcesContent":["/**\n * Format lays out every document at one resolution, ten layout pixels to each CSS\n * pixel. Border widths, font metrics and font sizes are rounded to a layout pixel, so\n * one resolution means a document is measured the same way on any display, at any\n * browser zoom, and in its PDF.\n */\nexport const DOCUMENT_RESOLUTION = 10\n\n/**\n * The `pageZoom` a preview runs the engine at: Format's resolution divided by the\n * device pixel ratio. The browser rounds border widths, font metrics and font sizes\n * in device pixels, at the CSS zoom times the device pixel ratio. At this zoom that\n * product is the resolution on any display, so a document wraps its lines and breaks\n * its pages the same way on all of them. A preview shows its pages scaled by the\n * inverse, `devicePixelRatio / DOCUMENT_RESOLUTION`.\n *\n * The browser keeps zooms as 32-bit floats and multiplies them in float, so the\n * plain quotient can miss. At a ratio of 2.11, 10 / 2.11 stores as 4.7393365, whose\n * product with 2.11 is 9.999999. Font sizes are floored to 0.01 device px, so a 16px\n * font then lands at 159.99 instead of 160.00, and lines wrap differently. The zoom\n * returned is the float nearest the quotient whose float product with the ratio is\n * exactly the resolution, or the first float above it when none is exact.\n */\nexport function previewPageZoom(devicePixelRatio = window.devicePixelRatio || 1): number {\n\tconst ratio = Math.fround(devicePixelRatio)\n\tconst product = (zoom: number) => Math.fround(zoom * ratio)\n\tconst quotient = Math.fround(DOCUMENT_RESOLUTION / ratio)\n\tconst candidates = [0, -1, 1, -2, 2, -3, 3, -4, 4].map(step => float32Step(quotient, step))\n\tconst exact = candidates.find(zoom => product(zoom) === DOCUMENT_RESOLUTION)\n\n\tif (exact !== undefined) {\n\t\treturn exact\n\t}\n\n\tlet zoom = quotient\n\n\twhile (product(zoom) < DOCUMENT_RESOLUTION) {\n\t\tzoom = float32Step(zoom, 1)\n\t}\n\n\treturn zoom\n}\n\n/** The 32-bit float `steps` representable values away from the float `value`. */\nfunction float32Step(value: number, steps: number): number {\n\tconst floats = new Float32Array([value])\n\tconst bits = new Uint32Array(floats.buffer)\n\tbits[0] += steps\n\treturn floats[0]\n}\n\n/**\n * Returns the page zoom `page` carries, or 1 when there is no page. A preview gives its pages\n * the zoom of the new device pixel ratio when the ratio changes. Pages that a run is\n * still building keep the zoom the run started with until it finishes, so code that\n * places or scales pages reads the zoom they have.\n */\nexport function renderedPageZoom(page: Element | null): number {\n\tconst zoom = (page as HTMLElement | null)?.currentCSSZoom\n\treturn typeof zoom === 'number' && zoom > 0 ? zoom : 1\n}\n"],"mappings":";;;;;;;AAMA,MAAa,sBAAsB;;;;;;;;;;;;;;;;AAiBnC,SAAgB,gBAAgB,mBAAmB,OAAO,oBAAoB,GAAW;CACxF,MAAM,QAAQ,KAAK,OAAO,iBAAiB;CAC3C,MAAM,WAAW,WAAiB,KAAK,OAAOA,SAAO,MAAM;CAC3D,MAAM,WAAW,KAAK,OAAO,sBAAsB,MAAM;CAEzD,MAAM,QADa;EAAC;EAAG;EAAI;EAAG;EAAI;EAAG;EAAI;EAAG;EAAI;EAAE,CAAC,KAAI,SAAQ,YAAY,UAAU,KAAK,CAAC,CAClE,MAAK,WAAQ,QAAQA,OAAK,KAAK,oBAAoB;AAE5E,KAAI,UAAU,OACb,QAAO;CAGR,IAAI,OAAO;AAEX,QAAO,QAAQ,KAAK,GAAG,oBACtB,QAAO,YAAY,MAAM,EAAE;AAG5B,QAAO;;;AAIR,SAAS,YAAY,OAAe,OAAuB;CAC1D,MAAM,SAAS,IAAI,aAAa,CAAC,MAAM,CAAC;CACxC,MAAM,OAAO,IAAI,YAAY,OAAO,OAAO;AAC3C,MAAK,MAAM;AACX,QAAO,OAAO;;;;;;;;AASf,SAAgB,iBAAiB,MAA8B;CAC9D,MAAM,OAAQ,MAA6B;AAC3C,QAAO,OAAO,SAAS,YAAY,OAAO,IAAI,OAAO"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@format.dev/utils",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "license": "MIT",
5
5
  "description": "Shared utilities across the Format platform.",
6
6
  "author": "the format.dev developers",
@@ -15,6 +15,10 @@
15
15
  ".": {
16
16
  "types": "./dist/index.d.ts",
17
17
  "default": "./dist/index.js"
18
+ },
19
+ "./page-zoom": {
20
+ "types": "./dist/page-zoom.d.ts",
21
+ "default": "./dist/page-zoom.js"
18
22
  }
19
23
  },
20
24
  "files": [