@nexterm/canvas 1.1.1 → 1.2.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/index.d.ts +140 -35
- package/index.js +10 -0
- package/node-canvas.d.ts +35 -7
- package/node-canvas.js +74 -24
- package/package.json +13 -13
package/index.d.ts
CHANGED
|
@@ -42,22 +42,23 @@ export function setTextRenderer(renderer: TextRenderer): void
|
|
|
42
42
|
export function getTextRenderer(): TextRenderer
|
|
43
43
|
|
|
44
44
|
interface CanvasRenderingContext2D
|
|
45
|
-
extends
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
45
|
+
extends
|
|
46
|
+
CanvasCompositing,
|
|
47
|
+
CanvasDrawPath,
|
|
48
|
+
CanvasFillStrokeStyles,
|
|
49
|
+
CanvasFilters,
|
|
50
|
+
CanvasImageData,
|
|
51
|
+
CanvasImageSmoothing,
|
|
52
|
+
CanvasPath,
|
|
53
|
+
CanvasPathDrawingStyles,
|
|
54
|
+
CanvasRect,
|
|
55
|
+
CanvasSettings,
|
|
56
|
+
CanvasShadowStyles,
|
|
57
|
+
CanvasState,
|
|
58
|
+
CanvasText,
|
|
59
|
+
CanvasTextDrawingStyles,
|
|
60
|
+
CanvasTransform,
|
|
61
|
+
CanvasPDFAnnotations {}
|
|
61
62
|
|
|
62
63
|
interface CanvasState {
|
|
63
64
|
isContextLost(): boolean
|
|
@@ -289,13 +290,7 @@ type CanvasFontStretch =
|
|
|
289
290
|
| 'ultra-condensed'
|
|
290
291
|
| 'ultra-expanded'
|
|
291
292
|
type CanvasFontVariantCaps =
|
|
292
|
-
| 'all-petite-caps'
|
|
293
|
-
| 'all-small-caps'
|
|
294
|
-
| 'normal'
|
|
295
|
-
| 'petite-caps'
|
|
296
|
-
| 'small-caps'
|
|
297
|
-
| 'titling-caps'
|
|
298
|
-
| 'unicase'
|
|
293
|
+
'all-petite-caps' | 'all-small-caps' | 'normal' | 'petite-caps' | 'small-caps' | 'titling-caps' | 'unicase'
|
|
299
294
|
type CanvasTextAlign = 'center' | 'end' | 'left' | 'right' | 'start'
|
|
300
295
|
type CanvasTextBaseline = 'alphabetic' | 'bottom' | 'hanging' | 'ideographic' | 'middle' | 'top'
|
|
301
296
|
type CanvasTextRendering = 'auto' | 'geometricPrecision' | 'optimizeLegibility' | 'optimizeSpeed'
|
|
@@ -311,7 +306,6 @@ interface CanvasTextDrawingStyles {
|
|
|
311
306
|
textBaseline: CanvasTextBaseline
|
|
312
307
|
textRendering: CanvasTextRendering
|
|
313
308
|
wordSpacing: string
|
|
314
|
-
fontVariationSettings: string
|
|
315
309
|
lang: string
|
|
316
310
|
}
|
|
317
311
|
|
|
@@ -533,7 +527,7 @@ type OmitNeverOfMatrix = OmitMatrixMethod[keyof OmitMatrixMethod]
|
|
|
533
527
|
|
|
534
528
|
export const DOMMatrix: {
|
|
535
529
|
prototype: DOMMatrix
|
|
536
|
-
new(init?: string | number[]): DOMMatrix
|
|
530
|
+
new (init?: string | number[]): DOMMatrix
|
|
537
531
|
fromFloat32Array(array32: Float32Array): DOMMatrix
|
|
538
532
|
fromFloat64Array(array64: Float64Array): DOMMatrix
|
|
539
533
|
fromMatrix(other?: DOMMatrixInit): DOMMatrix
|
|
@@ -560,7 +554,7 @@ export interface DOMRect extends DOMRectReadOnly {
|
|
|
560
554
|
|
|
561
555
|
export const DOMRect: {
|
|
562
556
|
prototype: DOMRect
|
|
563
|
-
new(x?: number, y?: number, width?: number, height?: number): DOMRect
|
|
557
|
+
new (x?: number, y?: number, width?: number, height?: number): DOMRect
|
|
564
558
|
fromRect(other?: DOMRectInit): DOMRect
|
|
565
559
|
}
|
|
566
560
|
|
|
@@ -582,7 +576,7 @@ export interface DOMPoint extends DOMPointReadOnly {
|
|
|
582
576
|
|
|
583
577
|
export const DOMPoint: {
|
|
584
578
|
prototype: DOMPoint
|
|
585
|
-
new(x?: number, y?: number, z?: number, w?: number): DOMPoint
|
|
579
|
+
new (x?: number, y?: number, z?: number, w?: number): DOMPoint
|
|
586
580
|
fromPoint(other?: DOMPointInit): DOMPoint
|
|
587
581
|
}
|
|
588
582
|
|
|
@@ -744,8 +738,27 @@ export interface SKRSContext2D extends CanvasRenderingContext2D {
|
|
|
744
738
|
getContextAttributes(): Required<CanvasRenderingContext2DSettings>
|
|
745
739
|
getTransform(): DOMMatrix
|
|
746
740
|
|
|
741
|
+
/**
|
|
742
|
+
* Non-standard (`node-canvas` extension): controls raster antialiasing for
|
|
743
|
+
* subsequent drawing operations. Default `"default"`.
|
|
744
|
+
*
|
|
745
|
+
* - `"none"`: disables antialiasing on raster shapes, clips, text, and bitmap
|
|
746
|
+
* image borders (vector SVG/PDF output and SVG `Image` content are unaffected).
|
|
747
|
+
* - `"default" | "gray" | "subpixel"`: keeps antialiasing enabled.
|
|
748
|
+
*
|
|
749
|
+
* Invalid values are silently ignored.
|
|
750
|
+
*/
|
|
751
|
+
antialias: 'default' | 'none' | 'gray' | 'subpixel'
|
|
752
|
+
|
|
747
753
|
letterSpacing: string
|
|
748
754
|
wordSpacing: string
|
|
755
|
+
/**
|
|
756
|
+
* Non-standard: Chrome's CanvasRenderingContext2D has no such attribute.
|
|
757
|
+
* Behaves like the CSS `font-variation-settings` property in Chrome.
|
|
758
|
+
* Invalid values are ignored. Default `"normal"`.
|
|
759
|
+
* @experimental
|
|
760
|
+
*/
|
|
761
|
+
fontVariationSettings: string
|
|
749
762
|
/**
|
|
750
763
|
* Non-standard: Chrome's CanvasRenderingContext2D has no such attribute.
|
|
751
764
|
* Behaves like the CSS `font-feature-settings` property in Chrome.
|
|
@@ -874,6 +887,33 @@ export interface JpegEncodeOptions extends ImageMetadata {
|
|
|
874
887
|
quality?: number
|
|
875
888
|
}
|
|
876
889
|
|
|
890
|
+
/**
|
|
891
|
+
* libpng row filter flags for {@link PngEncodeOptions.filters}, combinable with `|`.
|
|
892
|
+
* Use `None` (`0x08`) to disable row filtering; default is `All` (`0xF8`).
|
|
893
|
+
*/
|
|
894
|
+
export enum PngFilter {
|
|
895
|
+
None = 0x08,
|
|
896
|
+
Sub = 0x10,
|
|
897
|
+
Up = 0x20,
|
|
898
|
+
Avg = 0x40,
|
|
899
|
+
Paeth = 0x80,
|
|
900
|
+
All = 0xf8,
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
export interface PngEncodeOptions extends ImageMetadata {
|
|
904
|
+
/**
|
|
905
|
+
* zlib compression level, an integer from 0 (uncompressed, fastest) to 9 (smallest).
|
|
906
|
+
* Out-of-range, fractional and non-number values use the default (6).
|
|
907
|
+
*/
|
|
908
|
+
compressionLevel?: number
|
|
909
|
+
/**
|
|
910
|
+
* libpng row filter bitmask (see {@link PngFilter}): a non-zero OR of
|
|
911
|
+
* `0x08` (none), `0x10` (sub), `0x20` (up), `0x40` (average), `0x80` (Paeth).
|
|
912
|
+
* Any other value (including `0`) uses the default (`0xF8`, all filters).
|
|
913
|
+
*/
|
|
914
|
+
filters?: number
|
|
915
|
+
}
|
|
916
|
+
|
|
877
917
|
/** GIF encoding configuration for single-frame encoding */
|
|
878
918
|
export interface GifConfig {
|
|
879
919
|
/**
|
|
@@ -1059,18 +1099,18 @@ export class Canvas {
|
|
|
1059
1099
|
getContext(contextType: '2d', contextAttributes?: ContextAttributes): SKRSContext2D
|
|
1060
1100
|
encodeSync(format: 'webp' | 'jpeg', quality?: number): Buffer
|
|
1061
1101
|
encodeSync(format: 'jpeg', options: JpegEncodeOptions): Buffer
|
|
1062
|
-
encodeSync(format: 'png', options?:
|
|
1102
|
+
encodeSync(format: 'png', options?: PngEncodeOptions): Buffer
|
|
1063
1103
|
encodeSync(format: 'avif', cfg?: AvifConfig | number): Buffer
|
|
1064
1104
|
encodeSync(format: 'gif', quality?: number): Buffer
|
|
1065
1105
|
encode(format: 'webp' | 'jpeg', quality?: number): Promise<Buffer>
|
|
1066
1106
|
encode(format: 'jpeg', options: JpegEncodeOptions): Promise<Buffer>
|
|
1067
|
-
encode(format: 'png', options?:
|
|
1107
|
+
encode(format: 'png', options?: PngEncodeOptions): Promise<Buffer>
|
|
1068
1108
|
encode(format: 'avif', cfg?: AvifConfig | number): Promise<Buffer>
|
|
1069
1109
|
encode(format: 'gif', quality?: number): Promise<Buffer>
|
|
1070
|
-
/** Writes no image metadata; use `encode` and stream the Buffer for
|
|
1110
|
+
/** Writes no image metadata and uses default PNG options; use `encode` and stream the Buffer for those. */
|
|
1071
1111
|
encodeStream(format: 'webp' | 'jpeg', quality?: number): ReadableStream<Buffer>
|
|
1072
1112
|
encodeStream(format: 'png'): ReadableStream<Buffer>
|
|
1073
|
-
toBuffer(mime: 'image/png', options?:
|
|
1113
|
+
toBuffer(mime: 'image/png', options?: PngEncodeOptions): Buffer
|
|
1074
1114
|
toBuffer(mime: 'image/jpeg' | 'image/webp', quality?: number): Buffer
|
|
1075
1115
|
/**
|
|
1076
1116
|
* JPEG with metadata, at quality 92: toBuffer does not read a quality from an
|
|
@@ -1132,12 +1172,77 @@ export interface FontVariationAxis {
|
|
|
1132
1172
|
hidden: boolean
|
|
1133
1173
|
}
|
|
1134
1174
|
|
|
1175
|
+
/**
|
|
1176
|
+
* A font's `@font-face` descriptors, as `new FontFace(name, data, descriptors)`
|
|
1177
|
+
* takes them. An invalid value fails the registration (FontFace's status
|
|
1178
|
+
* 'error'); unknown keys are ignored. Values are kept in quarter steps, as
|
|
1179
|
+
* Chrome keeps them (300.7 is 300.5).
|
|
1180
|
+
*/
|
|
1181
|
+
export interface FontFaceDescriptors {
|
|
1182
|
+
/**
|
|
1183
|
+
* A FontFace weight, as a CSS string or a number (converted with
|
|
1184
|
+
* `String()` first): `normal` (default), `bold`, `auto`, a number from 1
|
|
1185
|
+
* to 1000, a `calc()` number, or a range `<a> <b>` (a reversed one is
|
|
1186
|
+
* swapped). A `calc()` weight outside 1-1000 makes every descriptor
|
|
1187
|
+
* `auto`, as Chrome does. `bolder`, `lighter` and any other value are
|
|
1188
|
+
* invalid.
|
|
1189
|
+
*/
|
|
1190
|
+
weight?: number | 'normal' | 'bold' | 'auto' | `${number}` | `${number} ${number}` | (string & {})
|
|
1191
|
+
/**
|
|
1192
|
+
* `normal` (default), `italic`, `oblique`, `oblique <angle>`,
|
|
1193
|
+
* `oblique <angle> <angle>` (a range; a reversed one is swapped) or
|
|
1194
|
+
* `auto`, in any ASCII case. An angle is a CSS angle such as `10deg`,
|
|
1195
|
+
* `0.2rad` or `calc(5deg)`; a written number from -90 to 90. A value that
|
|
1196
|
+
* is not a string is converted with `String()` first (`null` is 'null',
|
|
1197
|
+
* invalid).
|
|
1198
|
+
*/
|
|
1199
|
+
style?: 'normal' | 'italic' | 'oblique' | 'auto' | `oblique ${string}`
|
|
1200
|
+
/**
|
|
1201
|
+
* `normal` (default), `auto`, one of the eight stretch keywords
|
|
1202
|
+
* (`ultra-condensed`, `extra-condensed`, `condensed`, `semi-condensed`,
|
|
1203
|
+
* `semi-expanded`, `expanded`, `extra-expanded`, `ultra-expanded`), a
|
|
1204
|
+
* non-negative CSS percentage or `calc()` percentage, or a two-value
|
|
1205
|
+
* percentage range `<a> <b>` (a reversed one is swapped), in any ASCII
|
|
1206
|
+
* case. `normal` and `auto` keep the font's own `wdth` axis unclamped;
|
|
1207
|
+
* `100%` and the other keywords/percentages clamp it. A value that is not
|
|
1208
|
+
* a string is converted with `String()` first (`null` is 'null',
|
|
1209
|
+
* invalid).
|
|
1210
|
+
*/
|
|
1211
|
+
stretch?: CanvasFontStretch | 'auto' | `${number}%` | `${number}% ${number}%` | (string & {})
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1135
1214
|
interface IGlobalFonts {
|
|
1136
1215
|
readonly families: { family: string; styles: { weight: number; width: string; style: string }[] }[]
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1216
|
+
/**
|
|
1217
|
+
* The FontKey, or null if it failed (also for invalid `descriptors`, which
|
|
1218
|
+
* registers nothing; a `descriptors` that is not an object, or a Symbol
|
|
1219
|
+
* value, throws a TypeError). `descriptors`: the font's descriptors under
|
|
1220
|
+
* this name; a text font list uses them where this name matches the font.
|
|
1221
|
+
* Without them (or with every one `auto`), `auto`: the face is picked as
|
|
1222
|
+
* `normal` (weight 400, upright, width 100%), and the font's own weight,
|
|
1223
|
+
* style and width axis ranges are kept for variations (no clamping).
|
|
1224
|
+
* Faces of one family are chosen by their descriptors, as with
|
|
1225
|
+
* `@font-face`. Without descriptors every face is `auto` and the newest
|
|
1226
|
+
* face wins; give `{ weight, style, stretch }` for each file of a family,
|
|
1227
|
+
* or use `loadFontsFromDir` (installed fonts match by the file's own style).
|
|
1228
|
+
* A font registered under a generic keyword (`serif`, `sans-serif`, ...):
|
|
1229
|
+
* for that keyword written unquoted, each face is matched by the file's
|
|
1230
|
+
* own style and descriptors are ignored (bold is made from weight 600, as
|
|
1231
|
+
* for other registered fonts). Only the quoted name (`"serif"`) follows
|
|
1232
|
+
* the descriptor and newest-face rule.
|
|
1233
|
+
*/
|
|
1234
|
+
register(font: Buffer, nameAlias?: string, descriptors?: FontFaceDescriptors): FontKey | null
|
|
1235
|
+
/**
|
|
1236
|
+
* Absolute path. The FontKey, or null if it failed. `descriptors`: as
|
|
1237
|
+
* `register`'s; a file `loadFontsFromDir` loaded is an installed font and
|
|
1238
|
+
* takes none, so a call with them registers the file again. Faces of one
|
|
1239
|
+
* family are chosen as for `register`: without descriptors the newest
|
|
1240
|
+
* face wins. Exceptions: an unquoted generic keyword name, and a file
|
|
1241
|
+
* `loadFontsFromDir` already loaded, given again with no descriptors and
|
|
1242
|
+
* no other alias, which stays an installed font and keeps matching by the
|
|
1243
|
+
* file's own style.
|
|
1244
|
+
*/
|
|
1245
|
+
registerFromPath(path: string, nameAlias?: string, descriptors?: FontFaceDescriptors): FontKey | null
|
|
1141
1246
|
has(name: string): boolean
|
|
1142
1247
|
loadFontsFromDir(path: string): number
|
|
1143
1248
|
/**
|
package/index.js
CHANGED
|
@@ -86,6 +86,15 @@ const SvgExportFlag = {
|
|
|
86
86
|
RelativePathEncoding: 0x04,
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
+
const PngFilter = {
|
|
90
|
+
None: 0x08,
|
|
91
|
+
Sub: 0x10,
|
|
92
|
+
Up: 0x20,
|
|
93
|
+
Avg: 0x40,
|
|
94
|
+
Paeth: 0x80,
|
|
95
|
+
All: 0xf8,
|
|
96
|
+
}
|
|
97
|
+
|
|
89
98
|
if (!('families' in GlobalFonts)) {
|
|
90
99
|
Object.defineProperty(GlobalFonts, 'families', {
|
|
91
100
|
get: function () {
|
|
@@ -732,6 +741,7 @@ module.exports = {
|
|
|
732
741
|
StrokeCap,
|
|
733
742
|
StrokeJoin,
|
|
734
743
|
SvgExportFlag,
|
|
744
|
+
PngFilter,
|
|
735
745
|
GlobalFonts: GlobalFonts,
|
|
736
746
|
convertSVGTextToPath,
|
|
737
747
|
DOMPoint,
|
package/node-canvas.d.ts
CHANGED
|
@@ -37,9 +37,12 @@ import {
|
|
|
37
37
|
// ---------------------------------------------------------------------------
|
|
38
38
|
|
|
39
39
|
export interface PngConfig {
|
|
40
|
-
/** ZLIB compression level (0-9).
|
|
40
|
+
/** ZLIB compression level (0-9). Default 6. Out-of-range, fractional and non-number values use the default. */
|
|
41
41
|
compressionLevel?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
|
|
42
|
-
/**
|
|
42
|
+
/**
|
|
43
|
+
* libpng row filter bitmask: a non-zero OR of `0x08` (none), `0x10` (sub), `0x20` (up),
|
|
44
|
+
* `0x40` (average), `0x80` (Paeth). Any other value (including `0`) uses the default (`0xF8`, all filters).
|
|
45
|
+
*/
|
|
43
46
|
filters?: number
|
|
44
47
|
/** Palette for indexed PNGs. Not supported by Skia backend. */
|
|
45
48
|
palette?: Uint8ClampedArray
|
|
@@ -165,18 +168,43 @@ export function loadImage(
|
|
|
165
168
|
* Register a font file for use in canvas text rendering.
|
|
166
169
|
* Compatible with node-canvas's `registerFont()`.
|
|
167
170
|
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
+
* A non-empty `family` is the face's only name, as `new FontFace(family, ...)`
|
|
172
|
+
* in Chrome: the family embedded in the file is not listed (Behavior change
|
|
173
|
+
* from 1.2.0, where it was). `family: ''` (or the embedded name) registers
|
|
174
|
+
* the face under its embedded name.
|
|
175
|
+
*
|
|
176
|
+
* `weight` and `style` are the face's `@font-face` descriptors: the faces of
|
|
177
|
+
* one family are picked by them with CSS font matching. Register one call per
|
|
178
|
+
* face file, e.g. `{ family: 'Lato', weight: 'bold', style: 'italic' }`.
|
|
179
|
+
* A field that is not given, or that the `@font-face` grammar rejects, is
|
|
180
|
+
* `auto`, and the other field is kept; a value the grammar accepts
|
|
181
|
+
* (`calc(700)` too) works exactly as in `GlobalFonts.registerFromPath`. An
|
|
182
|
+
* explicit weight or style (`'normal'` included) pins a variable font to it;
|
|
183
|
+
* use a range such as `'100 900'` to keep the axis. It throws only when the
|
|
184
|
+
* file cannot be registered.
|
|
171
185
|
*
|
|
172
186
|
* @param path Absolute path to the font file (.ttf, .otf, etc.)
|
|
173
187
|
* @param fontFace Font face properties matching CSS @font-face descriptors
|
|
174
188
|
*/
|
|
175
|
-
export function registerFont(path: string, fontFace: { family: string; weight?: string; style?: string }): void
|
|
189
|
+
export function registerFont(path: string, fontFace: { family: string; weight?: string | number; style?: string }): void
|
|
176
190
|
|
|
177
191
|
/**
|
|
178
192
|
* Deregister all previously registered fonts.
|
|
179
|
-
* Compatible with node-canvas's `deregisterAllFonts()
|
|
193
|
+
* Compatible with node-canvas's `deregisterAllFonts()`: it removes the
|
|
194
|
+
* registrations that `registerFont` calls made through the same loaded native
|
|
195
|
+
* binding (one per main thread or worker; a worker's stay when it exits, as in
|
|
196
|
+
* node-canvas, and only its own `deregisterAllFonts` removes them), each
|
|
197
|
+
* whole, as `GlobalFonts.remove(key)` removes one, with the `setAlias` entries
|
|
198
|
+
* made from them. `GlobalFonts` registrations and `loadFontsFromDir` fonts are
|
|
199
|
+
* never touched. A `registerFont` registration is its own: a file registered
|
|
200
|
+
* both with `registerFont` and with `GlobalFonts` has a face from each while
|
|
201
|
+
* both are live, and a `setAlias` made while a `registerFont` face of the
|
|
202
|
+
* family was the newest goes with it.
|
|
203
|
+
*
|
|
204
|
+
* A later worker never reuses an exited worker's registrations, so each
|
|
205
|
+
* worker that calls `registerFont` adds its faces for the life of the process
|
|
206
|
+
* (only `GlobalFonts.removeAll()` removes them then). A pool that recycles
|
|
207
|
+
* workers should call `deregisterAllFonts()` in each worker before it exits.
|
|
180
208
|
*/
|
|
181
209
|
export function deregisterAllFonts(): void
|
|
182
210
|
|
package/node-canvas.js
CHANGED
|
@@ -20,7 +20,8 @@ const {
|
|
|
20
20
|
} = require('./index.js')
|
|
21
21
|
|
|
22
22
|
// CanvasRenderingContext2D is not re-exported by index.js, grab from native bindings
|
|
23
|
-
const
|
|
23
|
+
const binding = require('./js-binding')
|
|
24
|
+
const { CanvasRenderingContext2D } = binding
|
|
24
25
|
|
|
25
26
|
// node-canvas defaults JPEG quality to 0.75 across all paths.
|
|
26
27
|
// @napi-rs/canvas native default is 0.92 (matching Blink/Chrome).
|
|
@@ -110,17 +111,33 @@ function _extractQuality(mime, configOrQuality) {
|
|
|
110
111
|
}
|
|
111
112
|
|
|
112
113
|
/**
|
|
113
|
-
* The native PNG options for a node-canvas PngConfig:
|
|
114
|
-
*
|
|
115
|
-
*
|
|
114
|
+
* The native PNG options for a node-canvas PngConfig: `resolution` (pHYs chunk),
|
|
115
|
+
* `compressionLevel` (zlib 0-9) and `filters` (libpng filter bitmask); undefined
|
|
116
|
+
* when none of them is a number, so the native call stays a plain PNG encode.
|
|
116
117
|
*
|
|
117
118
|
* @param {object|undefined} config
|
|
118
|
-
* @returns {{ resolution
|
|
119
|
+
* @returns {{ resolution?: number, compressionLevel?: number, filters?: number }|undefined}
|
|
119
120
|
*/
|
|
120
121
|
function _pngOptions(config) {
|
|
121
122
|
if (config === null || typeof config !== 'object') return undefined
|
|
122
123
|
const resolution = config.resolution
|
|
123
|
-
|
|
124
|
+
const compressionLevel = config.compressionLevel
|
|
125
|
+
const filters = config.filters
|
|
126
|
+
const options = {}
|
|
127
|
+
let hasOption = false
|
|
128
|
+
if (typeof resolution === 'number') {
|
|
129
|
+
options.resolution = resolution
|
|
130
|
+
hasOption = true
|
|
131
|
+
}
|
|
132
|
+
if (typeof compressionLevel === 'number') {
|
|
133
|
+
options.compressionLevel = compressionLevel
|
|
134
|
+
hasOption = true
|
|
135
|
+
}
|
|
136
|
+
if (typeof filters === 'number') {
|
|
137
|
+
options.filters = filters
|
|
138
|
+
hasOption = true
|
|
139
|
+
}
|
|
140
|
+
return hasOption ? options : undefined
|
|
124
141
|
}
|
|
125
142
|
|
|
126
143
|
// ---------------------------------------------------------------------------
|
|
@@ -257,31 +274,55 @@ function _addCompatMethods(canvas) {
|
|
|
257
274
|
// Public API: registerFont / deregisterAllFonts
|
|
258
275
|
// ---------------------------------------------------------------------------
|
|
259
276
|
|
|
260
|
-
// Track FontKeys from registerFont() so deregisterAllFonts() can remove
|
|
261
|
-
// only user-registered fonts (matching node-canvas behavior which leaves
|
|
262
|
-
// system fonts untouched).
|
|
263
|
-
const _registeredFontKeys = []
|
|
264
|
-
|
|
265
277
|
/**
|
|
266
278
|
* Register a font file with the specified font face properties.
|
|
267
279
|
* Compatible with node-canvas's registerFont(path, { family, weight?, style? }).
|
|
268
280
|
*
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
*
|
|
281
|
+
* The file is registered as a registration of its own, made as
|
|
282
|
+
* GlobalFonts.registerFromPath makes a path it never saw, with `weight` and
|
|
283
|
+
* `style` as the face's @font-face descriptors, so the faces of one family
|
|
284
|
+
* are picked by them with CSS font matching. Register one call per face
|
|
285
|
+
* file. A non-empty `family` is the face's only name, as
|
|
286
|
+
* `new FontFace(family, ...)` in Chrome: the family embedded in the file is
|
|
287
|
+
* not listed (Behavior change from 1.2.0, where it was). `family: ''` (or
|
|
288
|
+
* the embedded name) registers it under its embedded name. Only the
|
|
289
|
+
* registerFont calls of this N-API env (the main thread or one worker) share
|
|
290
|
+
* it: the same declaration again reuses it and makes it the newest. A
|
|
291
|
+
* GlobalFonts call or loadFontsFromDir never reuses it, so a file registered
|
|
292
|
+
* both ways has a face from each.
|
|
293
|
+
*
|
|
294
|
+
* - A field that is not given is `auto`: a call with only `family` registers
|
|
295
|
+
* the file as GlobalFonts.registerFromPath(path, family) does, except that
|
|
296
|
+
* the embedded family is not listed, and a variable font keeps its axes.
|
|
297
|
+
* - An explicit weight or style, `'normal'` included, pins a variable font to
|
|
298
|
+
* it, as with @font-face. Use a range such as `'100 900'` to keep the axis.
|
|
299
|
+
* - Each of `weight` and `style` is read on its own: a value the @font-face
|
|
300
|
+
* grammar rejects (`'bolder'`, `''`) counts as not given, as node-canvas
|
|
301
|
+
* ignores unknown values instead of throwing, and the other field is kept.
|
|
302
|
+
* A value the grammar accepts (`calc(700)` too) works exactly as in
|
|
303
|
+
* GlobalFonts.registerFromPath. It throws only when the file cannot be
|
|
304
|
+
* registered.
|
|
272
305
|
*
|
|
273
306
|
* @param {string} path Absolute path to the font file (.ttf, .otf, etc.)
|
|
274
|
-
* @param {{ family: string, weight?: string, style?: string }} fontFace
|
|
307
|
+
* @param {{ family: string, weight?: string | number, style?: string }} fontFace
|
|
275
308
|
*/
|
|
276
309
|
function registerFont(path, fontFace) {
|
|
277
|
-
|
|
310
|
+
const family = fontFace ? fontFace.family : undefined
|
|
311
|
+
if (typeof family !== 'string') {
|
|
278
312
|
throw new TypeError('registerFont requires a fontFace with a "family" property')
|
|
279
313
|
}
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
314
|
+
// node-canvas reads `weight` as a string or a number and `style` as a
|
|
315
|
+
// string; any other value counts as not given. The native call reads each
|
|
316
|
+
// value with the @font-face grammar (src/global_fonts.rs
|
|
317
|
+
// register_font_face): one it rejects counts as not given too.
|
|
318
|
+
const w = fontFace.weight
|
|
319
|
+
const s = fontFace.style
|
|
320
|
+
const weight = typeof w === 'string' || typeof w === 'number' ? String(w) : 'auto'
|
|
321
|
+
const style = typeof s === 'string' ? s : 'auto'
|
|
322
|
+
// Read from the binding at each call, not once at load: the tests stub it.
|
|
323
|
+
if (!binding._registerFontFace(path, family, weight, style)) {
|
|
324
|
+
throw new Error(`Failed to register font from "${path}" with family "${family}"`)
|
|
283
325
|
}
|
|
284
|
-
_registeredFontKeys.push(key)
|
|
285
326
|
}
|
|
286
327
|
|
|
287
328
|
/**
|
|
@@ -290,10 +331,19 @@ function registerFont(path, fontFace) {
|
|
|
290
331
|
* user-registered fonts and leaves system fonts untouched.
|
|
291
332
|
*/
|
|
292
333
|
function deregisterAllFonts() {
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
334
|
+
// Removes the registerFont registrations of this N-API env whole (one per
|
|
335
|
+
// loaded native binding: the main thread or a worker; copies of this
|
|
336
|
+
// module that reuse the cached binding share it), as GlobalFonts.remove(key)
|
|
337
|
+
// removes a registration, the setAlias entries made from them included. A
|
|
338
|
+
// worker's stay when it exits, as in node-canvas (then only
|
|
339
|
+
// GlobalFonts.removeAll removes them; no later worker reuses them, so a
|
|
340
|
+
// pool that recycles workers should call this in each worker before it
|
|
341
|
+
// exits, or each worker adds its faces, as each adds its fonts in
|
|
342
|
+
// node-canvas). GlobalFonts
|
|
343
|
+
// registrations and loadFontsFromDir fonts are never touched, as
|
|
344
|
+
// node-canvas leaves other fonts untouched.
|
|
345
|
+
// Read at each call, as in registerFont.
|
|
346
|
+
binding._releaseFontFaces()
|
|
297
347
|
}
|
|
298
348
|
|
|
299
349
|
// ---------------------------------------------------------------------------
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexterm/canvas",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "@napi-rs/canvas 1.
|
|
3
|
+
"version": "1.2.1",
|
|
4
|
+
"description": "@napi-rs/canvas 1.2.1 with musl binaries",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -43,16 +43,16 @@
|
|
|
43
43
|
}
|
|
44
44
|
},
|
|
45
45
|
"optionalDependencies": {
|
|
46
|
-
"@napi-rs/canvas-linux-x64-gnu": "1.
|
|
47
|
-
"@napi-rs/canvas-darwin-x64": "1.
|
|
48
|
-
"@napi-rs/canvas-win32-x64-msvc": "1.
|
|
49
|
-
"@napi-rs/canvas-win32-arm64-msvc": "1.
|
|
50
|
-
"@napi-rs/canvas-linux-arm-gnueabihf": "1.
|
|
51
|
-
"@napi-rs/canvas-linux-x64-musl": "npm:@nexterm/canvas-linux-x64-musl@1.
|
|
52
|
-
"@napi-rs/canvas-linux-arm64-gnu": "1.
|
|
53
|
-
"@napi-rs/canvas-linux-arm64-musl": "npm:@nexterm/canvas-linux-arm64-musl@1.
|
|
54
|
-
"@napi-rs/canvas-darwin-arm64": "1.
|
|
55
|
-
"@napi-rs/canvas-android-arm64": "1.
|
|
56
|
-
"@napi-rs/canvas-linux-riscv64-gnu": "1.
|
|
46
|
+
"@napi-rs/canvas-linux-x64-gnu": "1.2.1",
|
|
47
|
+
"@napi-rs/canvas-darwin-x64": "1.2.1",
|
|
48
|
+
"@napi-rs/canvas-win32-x64-msvc": "1.2.1",
|
|
49
|
+
"@napi-rs/canvas-win32-arm64-msvc": "1.2.1",
|
|
50
|
+
"@napi-rs/canvas-linux-arm-gnueabihf": "1.2.1",
|
|
51
|
+
"@napi-rs/canvas-linux-x64-musl": "npm:@nexterm/canvas-linux-x64-musl@1.2.1",
|
|
52
|
+
"@napi-rs/canvas-linux-arm64-gnu": "1.2.1",
|
|
53
|
+
"@napi-rs/canvas-linux-arm64-musl": "npm:@nexterm/canvas-linux-arm64-musl@1.2.1",
|
|
54
|
+
"@napi-rs/canvas-darwin-arm64": "1.2.1",
|
|
55
|
+
"@napi-rs/canvas-android-arm64": "1.2.1",
|
|
56
|
+
"@napi-rs/canvas-linux-riscv64-gnu": "1.2.1"
|
|
57
57
|
}
|
|
58
58
|
}
|