@gtkx/cairo 2.0.0-beta.9 → 2.0.0

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 (56) hide show
  1. package/README.md +8 -168
  2. package/dist/context.d.ts +27 -6
  3. package/dist/context.d.ts.map +1 -1
  4. package/dist/context.js +46 -17
  5. package/dist/context.js.map +1 -1
  6. package/dist/device.d.ts.map +1 -1
  7. package/dist/enums.d.ts +10 -1
  8. package/dist/enums.d.ts.map +1 -1
  9. package/dist/enums.js +9 -0
  10. package/dist/enums.js.map +1 -1
  11. package/dist/font-face.d.ts +5 -3
  12. package/dist/font-face.d.ts.map +1 -1
  13. package/dist/font-face.js +10 -5
  14. package/dist/font-face.js.map +1 -1
  15. package/dist/font-options.d.ts +2 -2
  16. package/dist/font-options.d.ts.map +1 -1
  17. package/dist/font-options.js +1 -1
  18. package/dist/font-options.js.map +1 -1
  19. package/dist/lib.d.ts +2 -1
  20. package/dist/lib.d.ts.map +1 -1
  21. package/dist/lib.js +10 -37
  22. package/dist/lib.js.map +1 -1
  23. package/dist/matrix.d.ts.map +1 -1
  24. package/dist/path.d.ts +4 -3
  25. package/dist/path.d.ts.map +1 -1
  26. package/dist/path.js +17 -26
  27. package/dist/path.js.map +1 -1
  28. package/dist/pattern.d.ts +13 -7
  29. package/dist/pattern.d.ts.map +1 -1
  30. package/dist/pattern.js +36 -20
  31. package/dist/pattern.js.map +1 -1
  32. package/dist/region.d.ts.map +1 -1
  33. package/dist/scaled-font.d.ts.map +1 -1
  34. package/dist/scaled-font.js +3 -1
  35. package/dist/scaled-font.js.map +1 -1
  36. package/dist/status.d.ts.map +1 -1
  37. package/dist/structs.d.ts.map +1 -1
  38. package/dist/structs.js +32 -36
  39. package/dist/structs.js.map +1 -1
  40. package/dist/surface.d.ts +3 -3
  41. package/dist/surface.d.ts.map +1 -1
  42. package/dist/surface.js +27 -21
  43. package/dist/surface.js.map +1 -1
  44. package/dist/text.d.ts.map +1 -1
  45. package/dist/version.d.ts.map +1 -1
  46. package/package.json +6 -17
  47. package/src/context.ts +52 -19
  48. package/src/enums.ts +10 -1
  49. package/src/font-face.ts +13 -11
  50. package/src/font-options.ts +5 -9
  51. package/src/lib.ts +11 -36
  52. package/src/path.ts +21 -31
  53. package/src/pattern.ts +41 -28
  54. package/src/scaled-font.ts +4 -6
  55. package/src/structs.ts +32 -48
  56. package/src/surface.ts +40 -37
package/src/structs.ts CHANGED
@@ -3,17 +3,13 @@ import {
3
3
  type ExternalObject,
4
4
  getHandle,
5
5
  type Handle,
6
- read,
7
6
  registerWrapperClass,
8
7
  resolveType,
9
8
  setHandle,
9
+ type StridedField,
10
10
  t,
11
- toNative,
12
- write,
13
11
  } from "@gtkx/runtime";
14
12
 
15
- type FieldDescriptor = typeof t.float64 | typeof t.int32 | typeof t.biguint64;
16
-
17
13
  /** Initial field values for a `Rectangle`; a field left out or set to null keeps zero. */
18
14
  type RectangleConstructorProps = {
19
15
  /** Horizontal position of the left edge. */
@@ -65,25 +61,13 @@ const RECTANGLE_SIZE = 32;
65
61
  const RECTANGLE_INT_SIZE = 16;
66
62
  const GLYPH_SIZE = 24;
67
63
  const TEXT_CLUSTER_SIZE = 8;
68
- const DOUBLE = t.float64;
69
- const INT = t.int32;
70
- const ULONG = t.biguint64;
71
-
72
- const readField = (instance: object, descriptor: FieldDescriptor, offset: number): number =>
73
- read(getHandle(instance), descriptor, offset) as number;
74
-
75
- const writeField = (instance: object, descriptor: FieldDescriptor, offset: number, value: unknown): void => {
76
- write(getHandle(instance), descriptor, offset, toNative(descriptor, value));
77
- };
64
+ const DOUBLE = t.fieldAt(t.float64);
65
+ const INT = t.fieldAt(t.int32);
66
+ const ULONG = t.fieldAt(t.biguint64);
78
67
 
79
- const writeInitial = (
80
- handle: ExternalObject<Handle>,
81
- descriptor: FieldDescriptor,
82
- offset: number,
83
- value: unknown,
84
- ): void => {
68
+ const writeInitial = (handle: ExternalObject<Handle>, field: StridedField, offset: number, value: unknown): void => {
85
69
  if (value != null) {
86
- write(handle, descriptor, offset, toNative(descriptor, value));
70
+ field.write(handle, offset, value);
87
71
  }
88
72
  };
89
73
 
@@ -108,42 +92,42 @@ class Rectangle {
108
92
 
109
93
  /** Horizontal position of the left edge. */
110
94
  get x(): number {
111
- return readField(this, DOUBLE, 0);
95
+ return DOUBLE.read(getHandle(this), 0) as number;
112
96
  }
113
97
 
114
98
  /** Horizontal position of the left edge. */
115
99
  set x(value: number) {
116
- writeField(this, DOUBLE, 0, value);
100
+ DOUBLE.write(getHandle(this), 0, value);
117
101
  }
118
102
 
119
103
  /** Vertical position of the top edge. */
120
104
  get y(): number {
121
- return readField(this, DOUBLE, 8);
105
+ return DOUBLE.read(getHandle(this), 8) as number;
122
106
  }
123
107
 
124
108
  /** Vertical position of the top edge. */
125
109
  set y(value: number) {
126
- writeField(this, DOUBLE, 8, value);
110
+ DOUBLE.write(getHandle(this), 8, value);
127
111
  }
128
112
 
129
113
  /** Width of the rectangle. */
130
114
  get width(): number {
131
- return readField(this, DOUBLE, 16);
115
+ return DOUBLE.read(getHandle(this), 16) as number;
132
116
  }
133
117
 
134
118
  /** Width of the rectangle. */
135
119
  set width(value: number) {
136
- writeField(this, DOUBLE, 16, value);
120
+ DOUBLE.write(getHandle(this), 16, value);
137
121
  }
138
122
 
139
123
  /** Height of the rectangle. */
140
124
  get height(): number {
141
- return readField(this, DOUBLE, 24);
125
+ return DOUBLE.read(getHandle(this), 24) as number;
142
126
  }
143
127
 
144
128
  /** Height of the rectangle. */
145
129
  set height(value: number) {
146
- writeField(this, DOUBLE, 24, value);
130
+ DOUBLE.write(getHandle(this), 24, value);
147
131
  }
148
132
  }
149
133
 
@@ -168,42 +152,42 @@ class RectangleInt {
168
152
 
169
153
  /** Horizontal position of the left edge. */
170
154
  get x(): number {
171
- return readField(this, INT, 0);
155
+ return INT.read(getHandle(this), 0) as number;
172
156
  }
173
157
 
174
158
  /** Horizontal position of the left edge. */
175
159
  set x(value: number) {
176
- writeField(this, INT, 0, value);
160
+ INT.write(getHandle(this), 0, value);
177
161
  }
178
162
 
179
163
  /** Vertical position of the top edge. */
180
164
  get y(): number {
181
- return readField(this, INT, 4);
165
+ return INT.read(getHandle(this), 4) as number;
182
166
  }
183
167
 
184
168
  /** Vertical position of the top edge. */
185
169
  set y(value: number) {
186
- writeField(this, INT, 4, value);
170
+ INT.write(getHandle(this), 4, value);
187
171
  }
188
172
 
189
173
  /** Width of the rectangle. */
190
174
  get width(): number {
191
- return readField(this, INT, 8);
175
+ return INT.read(getHandle(this), 8) as number;
192
176
  }
193
177
 
194
178
  /** Width of the rectangle. */
195
179
  set width(value: number) {
196
- writeField(this, INT, 8, value);
180
+ INT.write(getHandle(this), 8, value);
197
181
  }
198
182
 
199
183
  /** Height of the rectangle. */
200
184
  get height(): number {
201
- return readField(this, INT, 12);
185
+ return INT.read(getHandle(this), 12) as number;
202
186
  }
203
187
 
204
188
  /** Height of the rectangle. */
205
189
  set height(value: number) {
206
- writeField(this, INT, 12, value);
190
+ INT.write(getHandle(this), 12, value);
207
191
  }
208
192
  }
209
193
 
@@ -227,32 +211,32 @@ class Glyph {
227
211
 
228
212
  /** Index of the glyph in the font. */
229
213
  get index(): bigint {
230
- return read(getHandle(this), ULONG, 0) as bigint;
214
+ return ULONG.read(getHandle(this), 0) as bigint;
231
215
  }
232
216
 
233
217
  /** Index of the glyph in the font. */
234
218
  set index(value: bigint) {
235
- writeField(this, ULONG, 0, value);
219
+ ULONG.write(getHandle(this), 0, value);
236
220
  }
237
221
 
238
222
  /** Horizontal position of the glyph origin. */
239
223
  get x(): number {
240
- return readField(this, DOUBLE, 8);
224
+ return DOUBLE.read(getHandle(this), 8) as number;
241
225
  }
242
226
 
243
227
  /** Horizontal position of the glyph origin. */
244
228
  set x(value: number) {
245
- writeField(this, DOUBLE, 8, value);
229
+ DOUBLE.write(getHandle(this), 8, value);
246
230
  }
247
231
 
248
232
  /** Vertical position of the glyph origin. */
249
233
  get y(): number {
250
- return readField(this, DOUBLE, 16);
234
+ return DOUBLE.read(getHandle(this), 16) as number;
251
235
  }
252
236
 
253
237
  /** Vertical position of the glyph origin. */
254
238
  set y(value: number) {
255
- writeField(this, DOUBLE, 16, value);
239
+ DOUBLE.write(getHandle(this), 16, value);
256
240
  }
257
241
  }
258
242
 
@@ -275,22 +259,22 @@ class TextCluster {
275
259
 
276
260
  /** Number of UTF-8 bytes the cluster covers. */
277
261
  get numBytes(): number {
278
- return readField(this, INT, 0);
262
+ return INT.read(getHandle(this), 0) as number;
279
263
  }
280
264
 
281
265
  /** Number of UTF-8 bytes the cluster covers. */
282
266
  set numBytes(value: number) {
283
- writeField(this, INT, 0, value);
267
+ INT.write(getHandle(this), 0, value);
284
268
  }
285
269
 
286
270
  /** Number of glyphs the cluster covers. */
287
271
  get numGlyphs(): number {
288
- return readField(this, INT, 4);
272
+ return INT.read(getHandle(this), 4) as number;
289
273
  }
290
274
 
291
275
  /** Number of glyphs the cluster covers. */
292
276
  set numGlyphs(value: number) {
293
- writeField(this, INT, 4, value);
277
+ INT.write(getHandle(this), 4, value);
294
278
  }
295
279
  }
296
280
 
package/src/surface.ts CHANGED
@@ -11,7 +11,9 @@ import {
11
11
  type WrapperClassResolver,
12
12
  write,
13
13
  } from "@gtkx/runtime";
14
+ import { reportExternalMemory } from "@gtkx/runtime/internal";
14
15
  import type { DeviceOffset, DeviceScale, FallbackResolution, InkExtents, RectangleData } from "./types.js";
16
+ import { Device } from "./device.js";
15
17
  import { type Content, type Format, type Status, SurfaceType } from "./enums.js";
16
18
  import { FontOptions } from "./font-options.js";
17
19
  import {
@@ -29,7 +31,6 @@ import { checkSurface } from "./status.js";
29
31
  const SURFACE_TYPE = cairoGType("cairo_gobject_surface_get_type");
30
32
  const RECTANGLE_SIZE = 32;
31
33
  const DOUBLE = t.fieldAt(t.float64);
32
- const UINT8 = t.fieldAt(t.uint8);
33
34
  const DEVICE_PAIR_ARGS = [SURFACE_T, t.float64, t.float64];
34
35
  const DEVICE_PAIR_OUT_ARGS = [SURFACE_T, t.ref(t.float64), t.ref(t.float64)];
35
36
  const SIMILAR_ARGS = [SURFACE_T, t.int32, t.int32, t.int32];
@@ -42,7 +43,7 @@ const cairoSurfaceCreateForRectangle = bindCairo(
42
43
  SURFACE_FULL_T,
43
44
  );
44
45
 
45
- const cairoSurfaceWriteToPng = bindCairo("cairo_surface_write_to_png", [SURFACE_T, t.string("full")], t.int32);
46
+ const cairoSurfaceWriteToPng = bindCairo("cairo_surface_write_to_png", [SURFACE_T, t.string("borrowed")], t.int32);
46
47
  const cairoSurfaceStatus = bindCairo("cairo_surface_status", [SURFACE_T], t.int32);
47
48
  const cairoSurfaceFinish = bindCairo("cairo_surface_finish", [SURFACE_T], t.void);
48
49
  const cairoSurfaceFlush = bindCairo("cairo_surface_flush", [SURFACE_T], t.void);
@@ -77,7 +78,7 @@ const cairoSurfaceHasShowTextGlyphs = bindCairo("cairo_surface_has_show_text_gly
77
78
 
78
79
  const cairoSurfaceSupportsMimeType = bindCairo(
79
80
  "cairo_surface_supports_mime_type",
80
- [SURFACE_T, t.string("full")],
81
+ [SURFACE_T, t.string("borrowed")],
81
82
  t.boolean,
82
83
  );
83
84
 
@@ -85,7 +86,7 @@ const cairoImageSurfaceCreate = bindCairo("cairo_image_surface_create", [t.int32
85
86
 
86
87
  const cairoImageSurfaceCreateFromPng = bindCairo(
87
88
  "cairo_image_surface_create_from_png",
88
- [t.string("full")],
89
+ [t.string("borrowed")],
89
90
  SURFACE_FULL_T,
90
91
  );
91
92
 
@@ -95,12 +96,6 @@ const cairoImageSurfaceGetFormat = bindCairo("cairo_image_surface_get_format", [
95
96
  const cairoImageSurfaceGetStride = bindCairo("cairo_image_surface_get_stride", [SURFACE_T], t.int32);
96
97
  const cairoRecordingSurfaceCreate = bindCairo("cairo_recording_surface_create", [t.int32, RECTANGLE_T], SURFACE_FULL_T);
97
98
 
98
- const cairoRecordingSurfaceCreateUnbounded = bindCairo(
99
- "cairo_recording_surface_create",
100
- [t.int32, t.uint64],
101
- SURFACE_FULL_T,
102
- );
103
-
104
99
  const cairoRecordingSurfaceInkExtents = bindCairo(
105
100
  "cairo_recording_surface_ink_extents",
106
101
  [SURFACE_T, t.ref(t.float64), t.ref(t.float64), t.ref(t.float64), t.ref(t.float64)],
@@ -115,6 +110,17 @@ const cairoRecordingSurfaceGetExtents = bindCairo(
115
110
 
116
111
  const wrapSurface = (handle: unknown): Surface => wrapHandle(handle as ExternalObject<Handle>, Surface);
117
112
 
113
+ const ownSurface = (handle: ExternalObject<Handle>): ExternalObject<Handle> => {
114
+ const surface = checkSurface(handle);
115
+
116
+ if (cairoSurfaceGetType(surface) === SurfaceType.IMAGE) {
117
+ const bytes = (cairoImageSurfaceGetStride(surface) as number) * (cairoImageSurfaceGetHeight(surface) as number);
118
+ reportExternalMemory(surface, bytes);
119
+ }
120
+
121
+ return surface;
122
+ };
123
+
118
124
  const readPair = <T>(boundFn: BoundFunction, self: object, build: (first: number, second: number) => T): T => {
119
125
  const first = { value: 0 };
120
126
  const second = { value: 0 };
@@ -140,10 +146,11 @@ const readRectangle = (buffer: ExternalObject<Handle>): RectangleData => ({
140
146
  height: DOUBLE.read(buffer, 24) as number,
141
147
  });
142
148
 
143
- const createRecordingSurface = (content: Content, extents?: RectangleData): ExternalObject<Handle> =>
144
- (extents === undefined
145
- ? cairoRecordingSurfaceCreateUnbounded(content, 0)
146
- : cairoRecordingSurfaceCreate(content, allocRectangle(extents))) as ExternalObject<Handle>;
149
+ const createRecordingSurface = (content: Content, extents?: RectangleData): ExternalObject<Handle> => {
150
+ const rectangle = extents === undefined ? null : allocRectangle(extents);
151
+
152
+ return cairoRecordingSurfaceCreate(content, rectangle) as ExternalObject<Handle>;
153
+ };
147
154
 
148
155
  const surfaceClassFor: WrapperClassResolver = (handle) => {
149
156
  const type = cairoSurfaceGetType(handle) as SurfaceType;
@@ -174,21 +181,24 @@ abstract class Surface {
174
181
  static createSimilar(other: Surface, content: Content, width: number, height: number): Surface {
175
182
  const handle = cairoSurfaceCreateSimilar(getHandle(other), content, width, height) as ExternalObject<Handle>;
176
183
 
177
- return wrapSurface(checkSurface(handle));
184
+ return wrapSurface(ownSurface(handle));
178
185
  }
179
186
 
180
187
  /** Creates an image surface of the given `format` and size as compatible as possible with `other`. */
181
188
  static createSimilarImage(other: Surface, format: Format, width: number, height: number): ImageSurface {
182
- const handle =
183
- cairoSurfaceCreateSimilarImage(getHandle(other), format, width, height) as ExternalObject<Handle>;
189
+ const handle = cairoSurfaceCreateSimilarImage(
190
+ getHandle(other),
191
+ format,
192
+ width,
193
+ height,
194
+ ) as ExternalObject<Handle>;
184
195
 
185
- return wrapHandle(checkSurface(handle), ImageSurface);
196
+ return wrapHandle(ownSurface(handle), ImageSurface);
186
197
  }
187
198
 
188
199
  /** Creates a surface that draws onto the given rectangle of `target`. */
189
200
  static createForRectangle(target: Surface, x: number, y: number, width: number, height: number): Surface {
190
- const handle =
191
- cairoSurfaceCreateForRectangle(getHandle(target), x, y, width, height) as ExternalObject<Handle>;
201
+ const handle = cairoSurfaceCreateForRectangle(getHandle(target), x, y, width, height) as ExternalObject<Handle>;
192
202
 
193
203
  return wrapSurface(checkSurface(handle));
194
204
  }
@@ -216,9 +226,11 @@ abstract class Surface {
216
226
  cairoSurfaceFlush(getHandle(this));
217
227
  }
218
228
 
219
- /** Returns the raw handle of the device behind the surface, or null when it has none. */
220
- getDevice(): ExternalObject<Handle> | null {
221
- return cairoSurfaceGetDevice(getHandle(this)) as ExternalObject<Handle> | null;
229
+ /** Returns the managed device behind the surface, or null when it has none. */
230
+ getDevice(): Device | null {
231
+ const handle = cairoSurfaceGetDevice(getHandle(this)) as ExternalObject<Handle> | null;
232
+
233
+ return handle === null ? null : wrapHandle(handle, Device);
222
234
  }
223
235
 
224
236
  /** Returns the font options the surface's backend prefers for rendering text. */
@@ -312,21 +324,18 @@ abstract class Surface {
312
324
  class ImageSurface extends Surface {
313
325
  /** Creates an image surface of the given pixel `format` and size, the same as `new ImageSurface(...)`. */
314
326
  static create(format: Format, width: number, height: number): ImageSurface {
315
- return wrapHandle(
316
- checkSurface(cairoImageSurfaceCreate(format, width, height) as ExternalObject<Handle>),
317
- this,
318
- );
327
+ return wrapHandle(ownSurface(cairoImageSurfaceCreate(format, width, height) as ExternalObject<Handle>), this);
319
328
  }
320
329
 
321
330
  /** Loads a PNG file into a new image surface; throws when the file is missing or invalid. */
322
331
  static createFromPng(filename: string): ImageSurface {
323
- return wrapHandle(checkSurface(cairoImageSurfaceCreateFromPng(filename) as ExternalObject<Handle>), this);
332
+ return wrapHandle(ownSurface(cairoImageSurfaceCreateFromPng(filename) as ExternalObject<Handle>), this);
324
333
  }
325
334
 
326
335
  /** Creates an image surface of the given pixel `format` and size. */
327
336
  constructor(format: Format, width: number, height: number) {
328
337
  super();
329
- setHandle(this, checkSurface(cairoImageSurfaceCreate(format, width, height) as ExternalObject<Handle>));
338
+ setHandle(this, ownSurface(cairoImageSurfaceCreate(format, width, height) as ExternalObject<Handle>));
330
339
  }
331
340
 
332
341
  /** Returns the width of the surface in pixels. */
@@ -361,16 +370,10 @@ class ImageSurface extends Surface {
361
370
  const getImageData = bindCairo(
362
371
  "cairo_image_surface_get_data",
363
372
  [SURFACE_T],
364
- t.struct("borrowed", { size: totalBytes }),
373
+ t.fixedArray(t.uint8, totalBytes, "borrowed", { isBytes: true }),
365
374
  );
366
375
 
367
- const data = getImageData(getHandle(this)) as ExternalObject<Handle> | null;
368
-
369
- if (data === null) {
370
- return new Uint8Array(0);
371
- }
372
-
373
- return Uint8Array.from({ length: totalBytes }, (_, index) => UINT8.read(data, index) as number);
376
+ return getImageData(getHandle(this)) as Uint8Array;
374
377
  }
375
378
  }
376
379