@gtkx/cairo 1.3.0 → 1.4.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.
- package/README.md +5 -4
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +11 -13
- package/dist/context.js.map +1 -1
- package/dist/font-face.d.ts +2 -7
- package/dist/font-face.d.ts.map +1 -1
- package/dist/font-face.js +3 -13
- package/dist/font-face.js.map +1 -1
- package/dist/lib.d.ts +1 -3
- package/dist/lib.d.ts.map +1 -1
- package/dist/lib.js +3 -4
- package/dist/lib.js.map +1 -1
- package/dist/path.d.ts.map +1 -1
- package/dist/path.js +8 -5
- package/dist/path.js.map +1 -1
- package/dist/region.d.ts.map +1 -1
- package/dist/region.js +6 -5
- package/dist/region.js.map +1 -1
- package/dist/scaled-font.d.ts +3 -5
- package/dist/scaled-font.d.ts.map +1 -1
- package/dist/scaled-font.js +3 -62
- package/dist/scaled-font.js.map +1 -1
- package/dist/structs.d.ts +1 -1
- package/dist/structs.d.ts.map +1 -1
- package/dist/structs.js +1 -1
- package/dist/structs.js.map +1 -1
- package/dist/surface.d.ts +0 -5
- package/dist/surface.d.ts.map +1 -1
- package/dist/surface.js +9 -17
- package/dist/surface.js.map +1 -1
- package/dist/text.d.ts.map +1 -1
- package/dist/text.js +20 -17
- package/dist/text.js.map +1 -1
- package/package.json +4 -4
- package/src/context.ts +10 -14
- package/src/font-face.ts +3 -25
- package/src/lib.ts +2 -5
- package/src/path.ts +8 -5
- package/src/region.ts +5 -5
- package/src/scaled-font.ts +3 -92
- package/src/structs.ts +1 -1
- package/src/surface.ts +7 -20
- package/src/text.ts +20 -17
package/src/scaled-font.ts
CHANGED
|
@@ -2,14 +2,13 @@ import {
|
|
|
2
2
|
type ExternalObject,
|
|
3
3
|
getHandle,
|
|
4
4
|
type Handle,
|
|
5
|
-
read,
|
|
6
5
|
registerWrapperClass,
|
|
7
6
|
setHandle,
|
|
8
7
|
t,
|
|
9
8
|
wrapHandle,
|
|
10
9
|
} from "@gtkx/runtime";
|
|
11
|
-
import type { FontType, Status
|
|
12
|
-
import type { CairoGlyph,
|
|
10
|
+
import type { FontType, Status } from "./enums.js";
|
|
11
|
+
import type { CairoGlyph, FontExtents, TextExtents } from "./types.js";
|
|
13
12
|
import { FontFace } from "./font-face.js";
|
|
14
13
|
import { FontOptions } from "./font-options.js";
|
|
15
14
|
import {
|
|
@@ -23,23 +22,18 @@ import {
|
|
|
23
22
|
MATRIX_T,
|
|
24
23
|
SCALED_FONT_FULL_T,
|
|
25
24
|
SCALED_FONT_T,
|
|
26
|
-
TEXT_CLUSTER_T,
|
|
27
25
|
TEXT_EXTENTS_T,
|
|
28
26
|
} from "./lib.js";
|
|
29
27
|
import { allocMatrix, Matrix } from "./matrix.js";
|
|
30
28
|
import { allocFontExtents, allocGlyphBuffer, allocTextExtents, readFontExtents, readTextExtents } from "./text.js";
|
|
31
29
|
|
|
32
30
|
const SCALED_FONT_TYPE = cairoGType("cairo_gobject_scaled_font_get_type");
|
|
33
|
-
const GLYPH_SIZE = 24;
|
|
34
|
-
const CLUSTER_SIZE = 8;
|
|
35
31
|
const cairoScaledFontStatus = bindCairo("cairo_scaled_font_status", [SCALED_FONT_T], t.int32);
|
|
36
32
|
const cairoScaledFontExtents = bindCairo("cairo_scaled_font_extents", [SCALED_FONT_T, FONT_EXTENTS_T], t.void);
|
|
37
33
|
const cairoScaledFontGetFontFace = bindCairo("cairo_scaled_font_get_font_face", [SCALED_FONT_T], FONT_FACE_T);
|
|
38
34
|
const cairoScaledFontGetFontMatrix = bindCairo("cairo_scaled_font_get_font_matrix", [SCALED_FONT_T, MATRIX_T], t.void);
|
|
39
35
|
const cairoScaledFontGetCtm = bindCairo("cairo_scaled_font_get_ctm", [SCALED_FONT_T, MATRIX_T], t.void);
|
|
40
36
|
const cairoScaledFontGetType = bindCairo("cairo_scaled_font_get_type", [SCALED_FONT_T], t.int32);
|
|
41
|
-
const cairoGlyphFree = bindCairo("cairo_glyph_free", [GLYPH_T], t.void);
|
|
42
|
-
const cairoTextClusterFree = bindCairo("cairo_text_cluster_free", [TEXT_CLUSTER_T], t.void);
|
|
43
37
|
|
|
44
38
|
const cairoScaledFontCreate = bindCairo(
|
|
45
39
|
"cairo_scaled_font_create",
|
|
@@ -77,23 +71,6 @@ const cairoScaledFontGetReferenceCount = bindCairo(
|
|
|
77
71
|
t.int32,
|
|
78
72
|
);
|
|
79
73
|
|
|
80
|
-
const cairoScaledFontTextToGlyphs = bindCairo(
|
|
81
|
-
"cairo_scaled_font_text_to_glyphs",
|
|
82
|
-
[
|
|
83
|
-
SCALED_FONT_T,
|
|
84
|
-
t.float64,
|
|
85
|
-
t.float64,
|
|
86
|
-
t.string("full"),
|
|
87
|
-
t.int32,
|
|
88
|
-
t.ref(t.struct("borrowed")),
|
|
89
|
-
t.ref(t.int32),
|
|
90
|
-
t.ref(t.struct("borrowed")),
|
|
91
|
-
t.ref(t.int32),
|
|
92
|
-
t.ref(t.int32),
|
|
93
|
-
],
|
|
94
|
-
t.int32,
|
|
95
|
-
);
|
|
96
|
-
|
|
97
74
|
const readMatrixVia = (self: ScaledFont, boundFn: BoundFunction): Matrix => {
|
|
98
75
|
const { handle, matrix } = allocMatrix();
|
|
99
76
|
boundFn(getHandle(self), handle);
|
|
@@ -101,40 +78,9 @@ const readMatrixVia = (self: ScaledFont, boundFn: BoundFunction): Matrix => {
|
|
|
101
78
|
return matrix;
|
|
102
79
|
};
|
|
103
80
|
|
|
104
|
-
const readGlyphs = (buffer: ExternalObject<Handle> | null, count: number): CairoGlyph[] => {
|
|
105
|
-
if (buffer === null) {
|
|
106
|
-
return [];
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return Array.from({ length: count }, (_, index) => {
|
|
110
|
-
const offset = index * GLYPH_SIZE;
|
|
111
|
-
|
|
112
|
-
return {
|
|
113
|
-
index: read(buffer, t.uint64, offset) as number,
|
|
114
|
-
x: read(buffer, t.float64, offset + 8) as number,
|
|
115
|
-
y: read(buffer, t.float64, offset + 16) as number,
|
|
116
|
-
};
|
|
117
|
-
});
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
const readClusters = (buffer: ExternalObject<Handle> | null, count: number): CairoTextCluster[] => {
|
|
121
|
-
if (buffer === null) {
|
|
122
|
-
return [];
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return Array.from({ length: count }, (_, index) => {
|
|
126
|
-
const offset = index * CLUSTER_SIZE;
|
|
127
|
-
|
|
128
|
-
return {
|
|
129
|
-
numBytes: read(buffer, t.int32, offset) as number,
|
|
130
|
-
numGlyphs: read(buffer, t.int32, offset + 4) as number,
|
|
131
|
-
};
|
|
132
|
-
});
|
|
133
|
-
};
|
|
134
|
-
|
|
135
81
|
/**
|
|
136
82
|
* A cairo scaled font (`cairo_scaled_font_t`): a font face frozen at a given font matrix, transformation and
|
|
137
|
-
* set of font options, ready to measure and
|
|
83
|
+
* set of font options, ready to measure text and positioned glyphs.
|
|
138
84
|
*/
|
|
139
85
|
class ScaledFont {
|
|
140
86
|
static {
|
|
@@ -228,41 +174,6 @@ class ScaledFont {
|
|
|
228
174
|
getReferenceCount(): number {
|
|
229
175
|
return cairoScaledFontGetReferenceCount(getHandle(this)) as number;
|
|
230
176
|
}
|
|
231
|
-
|
|
232
|
-
/** Shapes `text` starting at `(x, y)` into glyphs, clusters and the flags describing their order. */
|
|
233
|
-
textToGlyphs(x: number, y: number, text: string): [CairoGlyph[], CairoTextCluster[], TextClusterFlags] {
|
|
234
|
-
const glyphsRef: { value: ExternalObject<Handle> | null } = { value: null };
|
|
235
|
-
const numGlyphsRef = { value: 0 };
|
|
236
|
-
const clustersRef: { value: ExternalObject<Handle> | null } = { value: null };
|
|
237
|
-
const numClustersRef = { value: 0 };
|
|
238
|
-
const clusterFlagsRef = { value: 0 };
|
|
239
|
-
|
|
240
|
-
cairoScaledFontTextToGlyphs(
|
|
241
|
-
getHandle(this),
|
|
242
|
-
x,
|
|
243
|
-
y,
|
|
244
|
-
text,
|
|
245
|
-
-1,
|
|
246
|
-
glyphsRef,
|
|
247
|
-
numGlyphsRef,
|
|
248
|
-
clustersRef,
|
|
249
|
-
numClustersRef,
|
|
250
|
-
clusterFlagsRef,
|
|
251
|
-
);
|
|
252
|
-
|
|
253
|
-
const glyphs = readGlyphs(glyphsRef.value, numGlyphsRef.value);
|
|
254
|
-
const clusters = readClusters(clustersRef.value, numClustersRef.value);
|
|
255
|
-
|
|
256
|
-
if (glyphsRef.value !== null) {
|
|
257
|
-
cairoGlyphFree(glyphsRef.value);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
if (clustersRef.value !== null) {
|
|
261
|
-
cairoTextClusterFree(clustersRef.value);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
return [glyphs, clusters, clusterFlagsRef.value as TextClusterFlags];
|
|
265
|
-
}
|
|
266
177
|
}
|
|
267
178
|
|
|
268
179
|
export { ScaledFont };
|
package/src/structs.ts
CHANGED
|
@@ -147,7 +147,7 @@ class Rectangle {
|
|
|
147
147
|
}
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
-
/** A rectangle with integer edges, as regions
|
|
150
|
+
/** A rectangle with integer edges, as Cairo regions use. */
|
|
151
151
|
class RectangleInt {
|
|
152
152
|
static {
|
|
153
153
|
registerWrapperClass(this, RECTANGLE_INT_TYPE);
|
package/src/surface.ts
CHANGED
|
@@ -3,7 +3,6 @@ import {
|
|
|
3
3
|
type ExternalObject,
|
|
4
4
|
getHandle,
|
|
5
5
|
type Handle,
|
|
6
|
-
read,
|
|
7
6
|
registerWrapperClass,
|
|
8
7
|
registerWrapperClassResolver,
|
|
9
8
|
setHandle,
|
|
@@ -12,7 +11,6 @@ import {
|
|
|
12
11
|
type WrapperClassResolver,
|
|
13
12
|
write,
|
|
14
13
|
} from "@gtkx/runtime";
|
|
15
|
-
import type { RectangleInt } from "./structs.js";
|
|
16
14
|
import type { DeviceOffset, DeviceScale, FallbackResolution, InkExtents, RectangleData } from "./types.js";
|
|
17
15
|
import { type Content, type Format, type Status, SurfaceType } from "./enums.js";
|
|
18
16
|
import { FontOptions } from "./font-options.js";
|
|
@@ -22,7 +20,6 @@ import {
|
|
|
22
20
|
cairoGType,
|
|
23
21
|
DEVICE_T,
|
|
24
22
|
FONT_OPTIONS_T,
|
|
25
|
-
RECTANGLE_INT_T,
|
|
26
23
|
RECTANGLE_T,
|
|
27
24
|
SURFACE_FULL_T,
|
|
28
25
|
SURFACE_T,
|
|
@@ -31,6 +28,8 @@ import { checkSurface } from "./status.js";
|
|
|
31
28
|
|
|
32
29
|
const SURFACE_TYPE = cairoGType("cairo_gobject_surface_get_type");
|
|
33
30
|
const RECTANGLE_SIZE = 32;
|
|
31
|
+
const DOUBLE = t.fieldAt(t.float64);
|
|
32
|
+
const UINT8 = t.fieldAt(t.uint8);
|
|
34
33
|
const DEVICE_PAIR_ARGS = [SURFACE_T, t.float64, t.float64];
|
|
35
34
|
const DEVICE_PAIR_OUT_ARGS = [SURFACE_T, t.ref(t.float64), t.ref(t.float64)];
|
|
36
35
|
const SIMILAR_ARGS = [SURFACE_T, t.int32, t.int32, t.int32];
|
|
@@ -82,8 +81,6 @@ const cairoSurfaceSupportsMimeType = bindCairo(
|
|
|
82
81
|
t.boolean,
|
|
83
82
|
);
|
|
84
83
|
|
|
85
|
-
const cairoSurfaceMapToImage = bindCairo("cairo_surface_map_to_image", [SURFACE_T, RECTANGLE_INT_T], SURFACE_T);
|
|
86
|
-
const cairoSurfaceUnmapImage = bindCairo("cairo_surface_unmap_image", [SURFACE_T, SURFACE_T], t.void);
|
|
87
84
|
const cairoImageSurfaceCreate = bindCairo("cairo_image_surface_create", [t.int32, t.int32, t.int32], SURFACE_FULL_T);
|
|
88
85
|
|
|
89
86
|
const cairoImageSurfaceCreateFromPng = bindCairo(
|
|
@@ -137,10 +134,10 @@ const allocRectangle = (rect: RectangleData): ExternalObject<Handle> => {
|
|
|
137
134
|
};
|
|
138
135
|
|
|
139
136
|
const readRectangle = (buffer: ExternalObject<Handle>): RectangleData => ({
|
|
140
|
-
x: read(buffer,
|
|
141
|
-
y: read(buffer,
|
|
142
|
-
width: read(buffer,
|
|
143
|
-
height: read(buffer,
|
|
137
|
+
x: DOUBLE.read(buffer, 0) as number,
|
|
138
|
+
y: DOUBLE.read(buffer, 8) as number,
|
|
139
|
+
width: DOUBLE.read(buffer, 16) as number,
|
|
140
|
+
height: DOUBLE.read(buffer, 24) as number,
|
|
144
141
|
});
|
|
145
142
|
|
|
146
143
|
const createRecordingSurface = (content: Content, extents?: RectangleData): ExternalObject<Handle> =>
|
|
@@ -309,16 +306,6 @@ abstract class Surface {
|
|
|
309
306
|
supportsMimeType(mimeType: string): boolean {
|
|
310
307
|
return cairoSurfaceSupportsMimeType(getHandle(this), mimeType) as boolean;
|
|
311
308
|
}
|
|
312
|
-
|
|
313
|
-
/** Returns an image surface giving direct access to the pixels of `extents`; release it with `unmapImage`. */
|
|
314
|
-
mapToImage(extents: RectangleInt): Surface {
|
|
315
|
-
return wrapSurface(cairoSurfaceMapToImage(getHandle(this), getHandle(extents)));
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
/** Uploads the pixels of `image`, obtained from `mapToImage`, back to the surface and releases it. */
|
|
319
|
-
unmapImage(image: Surface): void {
|
|
320
|
-
cairoSurfaceUnmapImage(getHandle(this), getHandle(image));
|
|
321
|
-
}
|
|
322
309
|
}
|
|
323
310
|
|
|
324
311
|
/** A surface backed by an in-memory pixel buffer, the usual target for offscreen drawing. */
|
|
@@ -383,7 +370,7 @@ class ImageSurface extends Surface {
|
|
|
383
370
|
return new Uint8Array(0);
|
|
384
371
|
}
|
|
385
372
|
|
|
386
|
-
return Uint8Array.from({ length: totalBytes }, (_, index) => read(data,
|
|
373
|
+
return Uint8Array.from({ length: totalBytes }, (_, index) => UINT8.read(data, index) as number);
|
|
387
374
|
}
|
|
388
375
|
}
|
|
389
376
|
|
package/src/text.ts
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
|
-
import { alloc, type ExternalObject, type Handle,
|
|
1
|
+
import { alloc, type ExternalObject, type Handle, t } from "@gtkx/runtime";
|
|
2
2
|
import type { CairoGlyph, CairoTextCluster, FontExtents, TextExtents } from "./types.js";
|
|
3
3
|
|
|
4
4
|
const GLYPH_SIZE = 24;
|
|
5
5
|
const CLUSTER_SIZE = 8;
|
|
6
6
|
const TEXT_EXTENTS_SIZE = 48;
|
|
7
7
|
const FONT_EXTENTS_SIZE = 40;
|
|
8
|
+
const DOUBLE = t.fieldAt(t.float64);
|
|
9
|
+
const UINT64 = t.fieldAt(t.uint64);
|
|
10
|
+
const INT = t.fieldAt(t.int32);
|
|
8
11
|
|
|
9
12
|
const allocGlyphBuffer = (glyphs: CairoGlyph[]): ExternalObject<Handle> => {
|
|
10
13
|
const buffer = alloc(glyphs.length * GLYPH_SIZE);
|
|
11
14
|
let offset = 0;
|
|
12
15
|
|
|
13
16
|
for (const glyph of glyphs) {
|
|
14
|
-
write(buffer,
|
|
15
|
-
write(buffer,
|
|
16
|
-
write(buffer,
|
|
17
|
+
UINT64.write(buffer, offset, glyph.index);
|
|
18
|
+
DOUBLE.write(buffer, offset + 8, glyph.x);
|
|
19
|
+
DOUBLE.write(buffer, offset + 16, glyph.y);
|
|
17
20
|
offset += GLYPH_SIZE;
|
|
18
21
|
}
|
|
19
22
|
|
|
@@ -25,8 +28,8 @@ const allocClusterBuffer = (clusters: CairoTextCluster[]): ExternalObject<Handle
|
|
|
25
28
|
let offset = 0;
|
|
26
29
|
|
|
27
30
|
for (const cluster of clusters) {
|
|
28
|
-
write(buffer,
|
|
29
|
-
write(buffer,
|
|
31
|
+
INT.write(buffer, offset, cluster.numBytes);
|
|
32
|
+
INT.write(buffer, offset + 4, cluster.numGlyphs);
|
|
30
33
|
offset += CLUSTER_SIZE;
|
|
31
34
|
}
|
|
32
35
|
|
|
@@ -37,20 +40,20 @@ const allocTextExtents = (): ExternalObject<Handle> => alloc(TEXT_EXTENTS_SIZE);
|
|
|
37
40
|
const allocFontExtents = (): ExternalObject<Handle> => alloc(FONT_EXTENTS_SIZE);
|
|
38
41
|
|
|
39
42
|
const readTextExtents = (handle: ExternalObject<Handle>): TextExtents => ({
|
|
40
|
-
xBearing: read(handle,
|
|
41
|
-
yBearing: read(handle,
|
|
42
|
-
width: read(handle,
|
|
43
|
-
height: read(handle,
|
|
44
|
-
xAdvance: read(handle,
|
|
45
|
-
yAdvance: read(handle,
|
|
43
|
+
xBearing: DOUBLE.read(handle, 0) as number,
|
|
44
|
+
yBearing: DOUBLE.read(handle, 8) as number,
|
|
45
|
+
width: DOUBLE.read(handle, 16) as number,
|
|
46
|
+
height: DOUBLE.read(handle, 24) as number,
|
|
47
|
+
xAdvance: DOUBLE.read(handle, 32) as number,
|
|
48
|
+
yAdvance: DOUBLE.read(handle, 40) as number,
|
|
46
49
|
});
|
|
47
50
|
|
|
48
51
|
const readFontExtents = (handle: ExternalObject<Handle>): FontExtents => ({
|
|
49
|
-
ascent: read(handle,
|
|
50
|
-
descent: read(handle,
|
|
51
|
-
height: read(handle,
|
|
52
|
-
maxXAdvance: read(handle,
|
|
53
|
-
maxYAdvance: read(handle,
|
|
52
|
+
ascent: DOUBLE.read(handle, 0) as number,
|
|
53
|
+
descent: DOUBLE.read(handle, 8) as number,
|
|
54
|
+
height: DOUBLE.read(handle, 16) as number,
|
|
55
|
+
maxXAdvance: DOUBLE.read(handle, 24) as number,
|
|
56
|
+
maxYAdvance: DOUBLE.read(handle, 32) as number,
|
|
54
57
|
});
|
|
55
58
|
|
|
56
59
|
export { allocClusterBuffer, allocFontExtents, allocGlyphBuffer, allocTextExtents, readFontExtents, readTextExtents };
|