@napi-rs/canvas 1.1.0 → 1.1.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.
Files changed (3) hide show
  1. package/README.md +33 -4
  2. package/index.d.ts +53 -3
  3. package/package.json +14 -14
package/README.md CHANGED
@@ -35,6 +35,10 @@ All Apple M chips on **macOS**.
35
35
 
36
36
  Since Skia relies on the [glibc](https://www.gnu.org/software/libc/) 2.18 API, you need to have at least glibc version >= 2.18 on your system.
37
37
 
38
+ Note: the armv7 (`linux-arm-gnueabihf`) packages from 0.1.66 to 1.1.0 need
39
+ a newer glibc (0.1.66 needs 2.28, 1.1.0 needs 2.30). Later versions need
40
+ glibc 2.18, like the other targets.
41
+
38
42
  ## AWS Lambda usage
39
43
 
40
44
  To use this library on Lambda you will need to use a Lambda layer.
@@ -121,26 +125,43 @@ WebP, AVIF and GIF output ignore these options. `toDataURL`, `toDataURLAsync`, `
121
125
 
122
126
  ## Text rendering
123
127
 
124
- By default text looks the same on every OS. Every font is drawn with FreeType, with Skia's default text gamma, no LCD (subpixel) text and one font family table (the one Linux uses). The same font files give the same pixels and the same `measureText` results on macOS, Linux and Windows.
128
+ By default text is drawn the same way on every OS. Every font is drawn with FreeType, with Skia's default text gamma, no LCD (subpixel) text and one font family table (the one Linux uses).
129
+
130
+ Text gives the same pixels and the same `measureText` results on macOS, Linux and Windows when all of these are true:
131
+
132
+ 1. `ctx.font` names only fonts you registered. The canvas default `10px sans-serif`, the other generic families and names like `Arial` use installed fonts, unless you registered a font under that name.
133
+ 2. Your registered fonts have every character you draw. A character they lack is taken from an installed font.
134
+ 3. You set `ctx.lang`, for example to `'en'`. The default `'inherit'` follows the host language.
135
+ 4. You use the default `'portable'` renderer, not `setTextRenderer('system')`.
136
+ 5. The `FREETYPE_PROPERTIES` env var is not set.
125
137
 
126
- To get the same output everywhere, register your own font files under names no OS installs, and draw only with those names:
138
+ x64 and arm64 builds can still differ by 1 color level on some edge pixels.
127
139
 
128
140
  ```js
129
141
  const { GlobalFonts } = require('@napi-rs/canvas')
130
142
 
143
+ // A name no OS installs
131
144
  GlobalFonts.registerFromPath('./fonts/Lato-Regular.ttf', 'MyLato')
132
145
  ctx.font = '20px MyLato'
146
+ ctx.lang = 'en'
147
+ ```
148
+
149
+ A name registered over an installed family (for example `Helvetica`) works like a CSS `@font-face` in Chrome: only font lists that name it use it, for every style (bold and italic are made from the faces you registered when you registered no bold or italic face). The generic families, such as `sans-serif`, keep the installed family. To change a generic family, register the font under the keyword itself:
150
+
151
+ ```js
152
+ GlobalFonts.registerFromPath('./fonts/Lato-Regular.ttf', 'sans-serif')
133
153
  ```
134
154
 
135
155
  These still depend on the OS and the machine:
136
156
 
137
157
  - Installed fonts. Names like `Arial` or `Helvetica`, and the generic families (`sans-serif`, `serif`, `monospace`, `system-ui`, the canvas default `10px sans-serif`) resolve to whatever is installed.
138
- - Characters your registered fonts lack. They are taken from the installed default fonts when those have them, else they draw as boxes. There is no search through all installed fonts.
158
+ - Characters your registered fonts lack. They are taken from an installed font (the ones `loadSystemFonts` and `loadFontsFromDir` load, or the OS font folder) that has them, else they draw as boxes. A font you `register` or `registerFromPath` is never used for a character another font lacks, as in Chrome: to give a host without fonts a fallback font, load it with `loadFontsFromDir`.
139
159
  - The `ex`, `ch`, `cap` and line-height units inside SVG images use an installed default font.
140
160
  - Lottie (Skottie) text uses installed fonts.
141
161
  - PDF font embedding depends on the build target.
142
162
  - x64 and arm64 builds can differ by 1 color level on some edge pixels.
143
163
  - A `FREETYPE_PROPERTIES` env var changes FreeType's hinting.
164
+ - The host language. The default `ctx.lang` (`'inherit'`) follows the OS UI language (the macOS language list, the Windows user locale, or `LANGUAGE` / `LC_ALL` / `LC_MESSAGES` / `LANG` elsewhere), as Chrome does. It changes `locl` glyphs, case mapping (`small-caps` under `tr`) and CJK punctuation spacing. Set `ctx.lang` (for example `'en'`) to pin it. The installed font picked for a Han character still follows the host language unless `ctx.lang` names a CJK language (for example `'ja'` or `'zh-Hans'`). On macOS with `setTextRenderer('system')`, the generic families also follow a `ja`, `ko`, `zh-CN`, `zh-TW`, `ar`, `he` or `fa` UI language whatever `ctx.lang` is, as in Chrome (`serif` is Hiragino Mincho ProN under a Japanese UI).
144
165
 
145
166
  Set `DISABLE_SYSTEM_FONTS_LOAD=1` to keep installed fonts out of `GlobalFonts.families`. By default they can still be found by name, on every OS.
146
167
 
@@ -157,7 +178,15 @@ getTextRenderer() // 'system'
157
178
 
158
179
  It applies to the whole process. Call it once at startup, before you create canvases. `setTextRenderer('portable')` switches back. Any other value throws a `TypeError`. A canvas created before the switch keeps its old text gamma until you set its width or height. A `ctx.font` value set before the switch keeps its parsed form (sizes in `ex`, `ch` and other font-relative units, and on macOS the `BlinkMacSystemFont` to `system-ui` rewrite), so set `ctx.font` again after the switch.
159
180
 
160
- > **Behavior change** ([#1402](https://github.com/Brooooooklyn/canvas/issues/1402)): text on macOS and Linux is drawn the way Windows draws it, so pixels and `measureText` results differ from 1.0.12 and 1.0.13. On macOS, the installed fonts are FreeType faces again (as in 1.0.11), read from `/System/Library/Fonts`, `/Library/Fonts` and `~/Library/Fonts`; CoreText's per-character fallback is off, and `sans-serif`, `serif` and `monospace` map to Arial, Times New Roman and the Linux monospace list. On macOS, `DISABLE_SYSTEM_FONTS_LOAD` no longer hides installed fonts from name lookup, as on Linux and Windows and before 1.0.12; it only keeps them out of `GlobalFonts.families`. Call `setTextRenderer('system')` to get the 1.0.12 / 1.0.13 behavior back.
181
+ > **Behavior change** ([#1402](https://github.com/Brooooooklyn/canvas/issues/1402)): text on macOS and Linux is drawn the way Windows draws it, so pixels and `measureText` results differ from 1.0.12 and 1.0.13. On macOS, the installed fonts are FreeType faces again (as in 1.0.11), read from `/System/Library/Fonts`, `/Library/Fonts` and `~/Library/Fonts`; CoreText's per-character fallback is off, and `sans-serif`, `serif` and `monospace` map to Arial, Times New Roman and the Linux monospace list. On macOS, `DISABLE_SYSTEM_FONTS_LOAD` no longer hides installed fonts from name lookup, as on Linux and Windows and before 1.0.12; it only keeps them out of `GlobalFonts.families`. Call `setTextRenderer('system')` to draw with each OS's own font renderer and installed fonts again, as 1.0.12 and 1.0.13 did. Both renderers now shape text the way Chrome does, so the output is not the same as in 1.0.12 and 1.0.13.
182
+
183
+ > **Behavior change**: a character no font of the font list has is taken from an installed font, as Chrome's font cache takes it from the platform's fonts. This applies to the portable renderer on every OS and to the system renderer off macOS (on macOS, CoreText still picks first). The font is chosen the way Chrome on Windows chooses it: a bold or italic list first tries its first family at the normal style, then the font Windows names for the character's script (on macOS, the font Chrome's CoreText fallback names for it), then the first installed family, by name, that has the character. Before, such a character drew as a box (the first font's `.notdef`). Private use characters still draw as boxes.
184
+
185
+ > **Behavior change**: SVG text (SVG images, the SVG images nested in them, and `convertSVGTextToPath`) takes a character its first font lacks from the next family of its `font-family` list, then from the system's fonts, as canvas text does. Chrome does the same. Before, the whole text used the first font, and a character it lacks drew as a box. Text with no such character draws the same as before. `convertSVGTextToPath` now reads the document the way an SVG image does (`<style>` sheets, `var()`, `<use>` and the other CSS rules), so its output matches what the SVG image draws. A document that is not well-formed UTF-8 XML is read as before, with only the first family of each `font-family` list.
186
+
187
+ > **Behavior change**: the default `ctx.lang`, `'inherit'`, follows the host language, as Chrome does. Before, it meant no language. Text whose font has language-specific glyphs or spacing can draw and measure differently on hosts with different UI languages; set `ctx.lang` to pin it.
188
+
189
+ > **Behavior change**: SVG text whose family lacks the bold or italic face it asks for draws a synthetic bold or italic, with the rules canvas text uses: a registered font is bolded at `font-weight` 600 and up and slanted for `italic` and `oblique` unless it has that face (or a `wght` axis above 400, or a `slnt` axis); an installed font follows its own faces. Chrome does the same. The advances do not change, so the text does not move; the glyphs are heavier or slanted, and `convertSVGTextToPath` outlines grow to match. Before, SVG text drew the regular face for every style.
161
190
 
162
191
  ## Emoji text
163
192
 
package/index.d.ts CHANGED
@@ -9,9 +9,17 @@ export type TextRenderer = 'portable' | 'system'
9
9
  * Selects how text is drawn.
10
10
  *
11
11
  * `'portable'` (the default) draws text the same way on every OS: FreeType,
12
- * Skia's default text gamma, no LCD text and one font family table. The same
13
- * registered fonts give the same pixels and TextMetrics on macOS, Linux and
14
- * Windows.
12
+ * Skia's default text gamma, no LCD text and one font family table. Text gives
13
+ * the same pixels and TextMetrics on macOS, Linux and Windows when:
14
+ * - `ctx.font` names only fonts you registered (the default `10px sans-serif`
15
+ * and names like `Arial` use installed fonts, unless you registered a font
16
+ * under that name);
17
+ * - your registered fonts have every character you draw (a missing character
18
+ * comes from an installed font);
19
+ * - you set `ctx.lang` (for example `'en'`), because the default `'inherit'`
20
+ * follows the host language;
21
+ * - `FREETYPE_PROPERTIES` is not set.
22
+ * x64 and arm64 builds can still differ by 1 color level on some edge pixels.
15
23
  *
16
24
  * `'system'` draws text with each OS's own font renderer, as Chrome does:
17
25
  * CoreText on macOS (Fontations for some color and variable fonts) with
@@ -738,6 +746,48 @@ export interface SKRSContext2D extends CanvasRenderingContext2D {
738
746
 
739
747
  letterSpacing: string
740
748
  wordSpacing: string
749
+ /**
750
+ * Non-standard: Chrome's CanvasRenderingContext2D has no such attribute.
751
+ * Behaves like the CSS `font-feature-settings` property in Chrome.
752
+ * Invalid values are ignored. Default `"normal"`.
753
+ * @experimental
754
+ */
755
+ fontFeatureSettings: string
756
+ /**
757
+ * Non-standard: Chrome's CanvasRenderingContext2D has no such attribute.
758
+ * Behaves like the CSS `font-variant-ligatures` property in Chrome.
759
+ * Invalid values are ignored. Default `"normal"`.
760
+ * @experimental
761
+ */
762
+ fontVariantLigatures: string
763
+ /**
764
+ * Non-standard: Chrome's CanvasRenderingContext2D has no such attribute.
765
+ * Behaves like the CSS `font-variant-numeric` property in Chrome.
766
+ * Invalid values are ignored. Default `"normal"`.
767
+ * @experimental
768
+ */
769
+ fontVariantNumeric: string
770
+ /**
771
+ * Non-standard: Chrome's CanvasRenderingContext2D has no such attribute.
772
+ * Behaves like the CSS `font-variant-position` property in Chrome.
773
+ * Invalid values are ignored. Default `"normal"`.
774
+ * @experimental
775
+ */
776
+ fontVariantPosition: 'normal' | 'sub' | 'super'
777
+ /**
778
+ * Non-standard: Chrome's CanvasRenderingContext2D has no such attribute.
779
+ * Behaves like the CSS `font-size-adjust` property in Chrome.
780
+ * Invalid values are ignored. Default `"none"`.
781
+ * @experimental
782
+ */
783
+ fontSizeAdjust: string
784
+ /**
785
+ * Non-standard: Chrome's CanvasRenderingContext2D has no such attribute.
786
+ * Behaves like the CSS `font-optical-sizing` property in Chrome.
787
+ * Invalid values are ignored. Default `"auto"`.
788
+ * @experimental
789
+ */
790
+ fontOpticalSizing: 'auto' | 'none'
741
791
  }
742
792
 
743
793
  export type ColorSpace = 'srgb' | 'display-p3'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@napi-rs/canvas",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Canvas for Node.js with skia backend",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -103,7 +103,7 @@
103
103
  "npm-run-all2": "^9.0.0",
104
104
  "oxlint": "^1.34.0",
105
105
  "oxlint-tsgolint": "^7.0.0",
106
- "pdfjs-dist": "6.4.299",
106
+ "pdfjs-dist": "6.5.438",
107
107
  "pinst": "^3.0.0",
108
108
  "png.js": "^0.2.1",
109
109
  "prettier": "^3.7.4",
@@ -162,16 +162,16 @@
162
162
  "@napi-rs/canvas": "workspace:"
163
163
  },
164
164
  "optionalDependencies": {
165
- "@napi-rs/canvas-linux-x64-gnu": "1.1.0",
166
- "@napi-rs/canvas-darwin-x64": "1.1.0",
167
- "@napi-rs/canvas-win32-x64-msvc": "1.1.0",
168
- "@napi-rs/canvas-win32-arm64-msvc": "1.1.0",
169
- "@napi-rs/canvas-linux-arm-gnueabihf": "1.1.0",
170
- "@napi-rs/canvas-linux-x64-musl": "1.1.0",
171
- "@napi-rs/canvas-linux-arm64-gnu": "1.1.0",
172
- "@napi-rs/canvas-linux-arm64-musl": "1.1.0",
173
- "@napi-rs/canvas-darwin-arm64": "1.1.0",
174
- "@napi-rs/canvas-android-arm64": "1.1.0",
175
- "@napi-rs/canvas-linux-riscv64-gnu": "1.1.0"
165
+ "@napi-rs/canvas-linux-x64-gnu": "1.1.1",
166
+ "@napi-rs/canvas-darwin-x64": "1.1.1",
167
+ "@napi-rs/canvas-win32-x64-msvc": "1.1.1",
168
+ "@napi-rs/canvas-win32-arm64-msvc": "1.1.1",
169
+ "@napi-rs/canvas-linux-arm-gnueabihf": "1.1.1",
170
+ "@napi-rs/canvas-linux-x64-musl": "1.1.1",
171
+ "@napi-rs/canvas-linux-arm64-gnu": "1.1.1",
172
+ "@napi-rs/canvas-linux-arm64-musl": "1.1.1",
173
+ "@napi-rs/canvas-darwin-arm64": "1.1.1",
174
+ "@napi-rs/canvas-android-arm64": "1.1.1",
175
+ "@napi-rs/canvas-linux-riscv64-gnu": "1.1.1"
176
176
  }
177
- }
177
+ }