@gtkx/cairo 1.2.2

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 (86) hide show
  1. package/dist/context.d.ts +229 -0
  2. package/dist/context.d.ts.map +1 -0
  3. package/dist/context.js +646 -0
  4. package/dist/context.js.map +1 -0
  5. package/dist/device.d.ts +21 -0
  6. package/dist/device.d.ts.map +1 -0
  7. package/dist/device.js +39 -0
  8. package/dist/device.js.map +1 -0
  9. package/dist/enums.d.ts +526 -0
  10. package/dist/enums.d.ts.map +1 -0
  11. package/dist/enums.js +482 -0
  12. package/dist/enums.js.map +1 -0
  13. package/dist/font-face.d.ts +55 -0
  14. package/dist/font-face.d.ts.map +1 -0
  15. package/dist/font-face.js +103 -0
  16. package/dist/font-face.js.map +1 -0
  17. package/dist/font-options.d.ts +43 -0
  18. package/dist/font-options.d.ts.map +1 -0
  19. package/dist/font-options.js +95 -0
  20. package/dist/font-options.js.map +1 -0
  21. package/dist/index.d.ts +29 -0
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/index.js +27 -0
  24. package/dist/index.js.map +1 -0
  25. package/dist/lib.d.ts +36 -0
  26. package/dist/lib.d.ts.map +1 -0
  27. package/dist/lib.js +120 -0
  28. package/dist/lib.js.map +1 -0
  29. package/dist/matrix.d.ts +40 -0
  30. package/dist/matrix.d.ts.map +1 -0
  31. package/dist/matrix.js +93 -0
  32. package/dist/matrix.js.map +1 -0
  33. package/dist/path.d.ts +14 -0
  34. package/dist/path.d.ts.map +1 -0
  35. package/dist/path.js +67 -0
  36. package/dist/path.js.map +1 -0
  37. package/dist/pattern.d.ts +95 -0
  38. package/dist/pattern.d.ts.map +1 -0
  39. package/dist/pattern.js +250 -0
  40. package/dist/pattern.js.map +1 -0
  41. package/dist/region.d.ts +59 -0
  42. package/dist/region.d.ts.map +1 -0
  43. package/dist/region.js +154 -0
  44. package/dist/region.js.map +1 -0
  45. package/dist/scaled-font.d.ts +43 -0
  46. package/dist/scaled-font.d.ts.map +1 -0
  47. package/dist/scaled-font.js +156 -0
  48. package/dist/scaled-font.js.map +1 -0
  49. package/dist/structs.d.ts +120 -0
  50. package/dist/structs.d.ts.map +1 -0
  51. package/dist/structs.js +185 -0
  52. package/dist/structs.js.map +1 -0
  53. package/dist/surface.d.ts +98 -0
  54. package/dist/surface.d.ts.map +1 -0
  55. package/dist/surface.js +274 -0
  56. package/dist/surface.js.map +1 -0
  57. package/dist/text.d.ts +10 -0
  58. package/dist/text.d.ts.map +1 -0
  59. package/dist/text.js +45 -0
  60. package/dist/text.js.map +1 -0
  61. package/dist/types.d.ts +245 -0
  62. package/dist/types.d.ts.map +1 -0
  63. package/dist/types.js +2 -0
  64. package/dist/types.js.map +1 -0
  65. package/dist/version.d.ts +9 -0
  66. package/dist/version.d.ts.map +1 -0
  67. package/dist/version.js +13 -0
  68. package/dist/version.js.map +1 -0
  69. package/package.json +64 -0
  70. package/src/context.ts +899 -0
  71. package/src/device.ts +50 -0
  72. package/src/enums.ts +571 -0
  73. package/src/font-face.ts +157 -0
  74. package/src/font-options.ts +142 -0
  75. package/src/index.ts +28 -0
  76. package/src/lib.ts +186 -0
  77. package/src/matrix.ts +135 -0
  78. package/src/path.ts +87 -0
  79. package/src/pattern.ts +423 -0
  80. package/src/region.ts +216 -0
  81. package/src/scaled-font.ts +268 -0
  82. package/src/structs.ts +306 -0
  83. package/src/surface.ts +411 -0
  84. package/src/text.ts +56 -0
  85. package/src/types.ts +290 -0
  86. package/src/version.ts +16 -0
@@ -0,0 +1,157 @@
1
+ import {
2
+ type ExternalObject,
3
+ getHandle,
4
+ type Handle,
5
+ registerWrapperClass,
6
+ registerWrapperClassResolver,
7
+ t,
8
+ wrapHandle,
9
+ type WrapperClassResolver,
10
+ } from "@gtkx/runtime";
11
+ import { type FontSlant, FontType, type FontWeight, type Status } from "./enums.js";
12
+ import { bindCairo, cairoGType, FC_PATTERN_T, FONT_FACE_FULL_T, FONT_FACE_T, FT_FACE_T } from "./lib.js";
13
+
14
+ /** One of the `FtSynthesize` styles a FreeType font face synthesizes. */
15
+ type FtSynthesize = (typeof FtSynthesize)[keyof typeof FtSynthesize];
16
+
17
+ const FONT_FACE_TYPE = cairoGType("cairo_gobject_font_face_get_type");
18
+ const cairoFontFaceStatus = bindCairo("cairo_font_face_status", [FONT_FACE_T], t.int32);
19
+ const cairoFontFaceGetType = bindCairo("cairo_font_face_get_type", [FONT_FACE_T], t.int32);
20
+ const cairoFontFaceGetReferenceCount = bindCairo("cairo_font_face_get_reference_count", [FONT_FACE_T], t.int32);
21
+ const cairoToyFontFaceGetSlant = bindCairo("cairo_toy_font_face_get_slant", [FONT_FACE_T], t.int32);
22
+ const cairoToyFontFaceGetWeight = bindCairo("cairo_toy_font_face_get_weight", [FONT_FACE_T], t.int32);
23
+ const cairoFtFontFaceGetSynthesize = bindCairo("cairo_ft_font_face_get_synthesize", [FONT_FACE_T], t.int32);
24
+ const cairoFtFontFaceSetSynthesize = bindCairo("cairo_ft_font_face_set_synthesize", [FONT_FACE_T, t.int32], t.void);
25
+ const cairoToyFontFaceGetFamily = bindCairo("cairo_toy_font_face_get_family", [FONT_FACE_T], t.string("borrowed"));
26
+
27
+ const cairoFtFontFaceCreateForPattern = bindCairo(
28
+ "cairo_ft_font_face_create_for_pattern",
29
+ [FC_PATTERN_T],
30
+ FONT_FACE_FULL_T,
31
+ );
32
+
33
+ const cairoToyFontFaceCreate = bindCairo(
34
+ "cairo_toy_font_face_create",
35
+ [t.string("full"), t.int32, t.int32],
36
+ FONT_FACE_FULL_T,
37
+ );
38
+
39
+ const cairoFtFontFaceCreateForFtFace = bindCairo(
40
+ "cairo_ft_font_face_create_for_ft_face",
41
+ [FT_FACE_T, t.int32],
42
+ FONT_FACE_FULL_T,
43
+ );
44
+
45
+ const cairoFtFontFaceUnsetSynthesize = bindCairo(
46
+ "cairo_ft_font_face_unset_synthesize",
47
+ [FONT_FACE_T, t.int32],
48
+ t.void,
49
+ );
50
+
51
+ /**
52
+ * Styles a FreeType font face synthesizes when the font itself lacks them.
53
+ * @enum
54
+ */
55
+ const FtSynthesize = {
56
+ /** Embolden the glyphs. */
57
+ BOLD: 1,
58
+ /** Slant the glyphs. */
59
+ OBLIQUE: 2,
60
+ } as const;
61
+
62
+ const fontFaceClassFor: WrapperClassResolver = (handle) => {
63
+ const type = cairoFontFaceGetType(handle) as FontType;
64
+
65
+ if (type === FontType.TOY) {
66
+ return ToyFontFace;
67
+ }
68
+
69
+ if (type === FontType.FT) {
70
+ return FtFontFace;
71
+ }
72
+
73
+ return FontFace;
74
+ };
75
+
76
+ /**
77
+ * A cairo font face (`cairo_font_face_t`): a typeface independent of size and transformation. Font faces come
78
+ * from the `create*` statics or from a context, and wrap as the concrete class their type reports
79
+ * (`instanceof ToyFontFace` after `selectFontFace`).
80
+ */
81
+ abstract class FontFace {
82
+ static {
83
+ registerWrapperClass(this, FONT_FACE_TYPE);
84
+ registerWrapperClassResolver(this, fontFaceClassFor);
85
+ }
86
+
87
+ /** Creates a toy font face from a family name, slant and weight. */
88
+ static create(family: string, slant: FontSlant, weight: FontWeight): ToyFontFace {
89
+ return wrapHandle(cairoToyFontFaceCreate(family, slant, weight) as ExternalObject<Handle>, ToyFontFace);
90
+ }
91
+
92
+ /** Creates a font face for a FreeType `FT_Face` handle, with the given `FT_LOAD_*` flags. */
93
+ static createForFtFace(face: ExternalObject<Handle>, loadFlags: number): FtFontFace {
94
+ return wrapHandle(cairoFtFontFaceCreateForFtFace(face, loadFlags) as ExternalObject<Handle>, FtFontFace);
95
+ }
96
+
97
+ /** Creates a font face for a fontconfig `FcPattern` handle. */
98
+ static createForPattern(pattern: ExternalObject<Handle>): FtFontFace {
99
+ return wrapHandle(cairoFtFontFaceCreateForPattern(pattern) as ExternalObject<Handle>, FtFontFace);
100
+ }
101
+
102
+ /** GType of `CairoFontFace`, the boxed type this class is registered under. */
103
+ declare __type__: bigint;
104
+
105
+ /** Returns the error status of the font face, `Status.SUCCESS` when it is usable. */
106
+ status(): Status {
107
+ return cairoFontFaceStatus(getHandle(this)) as Status;
108
+ }
109
+
110
+ /** Returns the backend the font face belongs to. */
111
+ getType(): FontType {
112
+ return cairoFontFaceGetType(getHandle(this)) as FontType;
113
+ }
114
+
115
+ /** Returns the reference count of the font face. */
116
+ getReferenceCount(): number {
117
+ return cairoFontFaceGetReferenceCount(getHandle(this)) as number;
118
+ }
119
+ }
120
+
121
+ /** A toy font face, created with `FontFace.create` or a context's `selectFontFace`. */
122
+ class ToyFontFace extends FontFace {
123
+ /** Returns the family name the font face was created with. */
124
+ getFamily(): string {
125
+ return cairoToyFontFaceGetFamily(getHandle(this)) as string;
126
+ }
127
+
128
+ /** Returns the slant the font face was created with. */
129
+ getSlant(): FontSlant {
130
+ return cairoToyFontFaceGetSlant(getHandle(this)) as FontSlant;
131
+ }
132
+
133
+ /** Returns the weight the font face was created with. */
134
+ getWeight(): FontWeight {
135
+ return cairoToyFontFaceGetWeight(getHandle(this)) as FontWeight;
136
+ }
137
+ }
138
+
139
+ /** A FreeType font face, created with `FontFace.createForFtFace` or `FontFace.createForPattern`. */
140
+ class FtFontFace extends FontFace {
141
+ /** Returns the styles currently synthesized for the font face. */
142
+ getSynthesize(): FtSynthesize {
143
+ return cairoFtFontFaceGetSynthesize(getHandle(this)) as FtSynthesize;
144
+ }
145
+
146
+ /** Enables synthesizing of the given styles. */
147
+ setSynthesize(synthFlags: FtSynthesize): void {
148
+ cairoFtFontFaceSetSynthesize(getHandle(this), synthFlags);
149
+ }
150
+
151
+ /** Disables synthesizing of the given styles. */
152
+ unsetSynthesize(synthFlags: FtSynthesize): void {
153
+ cairoFtFontFaceUnsetSynthesize(getHandle(this), synthFlags);
154
+ }
155
+ }
156
+
157
+ export { FontFace, FtFontFace, FtSynthesize, ToyFontFace };
@@ -0,0 +1,142 @@
1
+ import { type ExternalObject, getHandle, type Handle, registerWrapperClass, setHandle, t } from "@gtkx/runtime";
2
+ import type { Antialias, HintMetrics, HintStyle, Status, SubpixelOrder } from "./enums.js";
3
+ import { bindCairo, cairoGType, FONT_OPTIONS_FULL_T, FONT_OPTIONS_T } from "./lib.js";
4
+
5
+ const FONT_OPTIONS_TYPE = cairoGType("cairo_gobject_font_options_get_type");
6
+ const cairoFontOptionsCreate = bindCairo("cairo_font_options_create", [], FONT_OPTIONS_FULL_T);
7
+ const cairoFontOptionsCopy = bindCairo("cairo_font_options_copy", [FONT_OPTIONS_T], FONT_OPTIONS_FULL_T);
8
+ const cairoFontOptionsSetHintStyle = bindCairo("cairo_font_options_set_hint_style", [FONT_OPTIONS_T, t.int32], t.void);
9
+ const cairoFontOptionsGetHintStyle = bindCairo("cairo_font_options_get_hint_style", [FONT_OPTIONS_T], t.int32);
10
+ const cairoFontOptionsSetAntialias = bindCairo("cairo_font_options_set_antialias", [FONT_OPTIONS_T, t.int32], t.void);
11
+ const cairoFontOptionsGetAntialias = bindCairo("cairo_font_options_get_antialias", [FONT_OPTIONS_T], t.int32);
12
+ const cairoFontOptionsGetHintMetrics = bindCairo("cairo_font_options_get_hint_metrics", [FONT_OPTIONS_T], t.int32);
13
+ const cairoFontOptionsEqual = bindCairo("cairo_font_options_equal", [FONT_OPTIONS_T, FONT_OPTIONS_T], t.boolean);
14
+ const cairoFontOptionsMerge = bindCairo("cairo_font_options_merge", [FONT_OPTIONS_T, FONT_OPTIONS_T], t.void);
15
+ const cairoFontOptionsStatus = bindCairo("cairo_font_options_status", [FONT_OPTIONS_T], t.int32);
16
+ const cairoFontOptionsHash = bindCairo("cairo_font_options_hash", [FONT_OPTIONS_T], t.uint64);
17
+
18
+ const cairoFontOptionsGetVariations = bindCairo(
19
+ "cairo_font_options_get_variations",
20
+ [FONT_OPTIONS_T],
21
+ t.string("borrowed"),
22
+ );
23
+
24
+ const cairoFontOptionsSetVariations = bindCairo(
25
+ "cairo_font_options_set_variations",
26
+ [FONT_OPTIONS_T, t.string("full")],
27
+ t.void,
28
+ );
29
+
30
+ const cairoFontOptionsSetHintMetrics = bindCairo(
31
+ "cairo_font_options_set_hint_metrics",
32
+ [FONT_OPTIONS_T, t.int32],
33
+ t.void,
34
+ );
35
+
36
+ const cairoFontOptionsSetSubpixelOrder = bindCairo(
37
+ "cairo_font_options_set_subpixel_order",
38
+ [FONT_OPTIONS_T, t.int32],
39
+ t.void,
40
+ );
41
+
42
+ const cairoFontOptionsGetSubpixelOrder = bindCairo(
43
+ "cairo_font_options_get_subpixel_order",
44
+ [FONT_OPTIONS_T],
45
+ t.int32,
46
+ );
47
+
48
+ /**
49
+ * Cairo font options (`cairo_font_options_t`): the rendering settings applied to text, such as antialiasing,
50
+ * hinting and font variations.
51
+ */
52
+ class FontOptions {
53
+ static {
54
+ registerWrapperClass(this, FONT_OPTIONS_TYPE);
55
+ }
56
+
57
+ /** Creates a set of font options with all settings at their defaults, the same as `new FontOptions()`. */
58
+ static create(): FontOptions {
59
+ return new FontOptions();
60
+ }
61
+
62
+ /** GType of `CairoFontOptions`, the boxed type this class is registered under. */
63
+ declare __type__: bigint;
64
+
65
+ /** Creates a set of font options: a copy of `other` when given, otherwise all settings at their defaults. */
66
+ constructor(other?: FontOptions) {
67
+ const handle = other === undefined ? cairoFontOptionsCreate() : cairoFontOptionsCopy(getHandle(other));
68
+ setHandle(this, handle as ExternalObject<Handle>);
69
+ }
70
+
71
+ /** Sets how outlines are fitted to the pixel grid. */
72
+ setHintStyle(hintStyle: HintStyle): void {
73
+ cairoFontOptionsSetHintStyle(getHandle(this), hintStyle);
74
+ }
75
+
76
+ /** Returns how outlines are fitted to the pixel grid. */
77
+ getHintStyle(): HintStyle {
78
+ return cairoFontOptionsGetHintStyle(getHandle(this)) as HintStyle;
79
+ }
80
+
81
+ /** Sets the antialiasing mode used for text. */
82
+ setAntialias(antialias: Antialias): void {
83
+ cairoFontOptionsSetAntialias(getHandle(this), antialias);
84
+ }
85
+
86
+ /** Returns the antialiasing mode used for text. */
87
+ getAntialias(): Antialias {
88
+ return cairoFontOptionsGetAntialias(getHandle(this)) as Antialias;
89
+ }
90
+
91
+ /** Sets whether font metrics are quantized to integer pixels. */
92
+ setHintMetrics(hintMetrics: HintMetrics): void {
93
+ cairoFontOptionsSetHintMetrics(getHandle(this), hintMetrics);
94
+ }
95
+
96
+ /** Returns whether font metrics are quantized to integer pixels. */
97
+ getHintMetrics(): HintMetrics {
98
+ return cairoFontOptionsGetHintMetrics(getHandle(this)) as HintMetrics;
99
+ }
100
+
101
+ /** Sets the order of subpixels on the display, used by subpixel antialiasing. */
102
+ setSubpixelOrder(subpixelOrder: SubpixelOrder): void {
103
+ cairoFontOptionsSetSubpixelOrder(getHandle(this), subpixelOrder);
104
+ }
105
+
106
+ /** Returns the order of subpixels on the display. */
107
+ getSubpixelOrder(): SubpixelOrder {
108
+ return cairoFontOptionsGetSubpixelOrder(getHandle(this)) as SubpixelOrder;
109
+ }
110
+
111
+ /** Returns whether `other` holds the same settings. */
112
+ equal(other: FontOptions): boolean {
113
+ return cairoFontOptionsEqual(getHandle(this), getHandle(other)) as boolean;
114
+ }
115
+
116
+ /** Copies every non-default setting of `other` over this one. */
117
+ merge(other: FontOptions): void {
118
+ cairoFontOptionsMerge(getHandle(this), getHandle(other));
119
+ }
120
+
121
+ /** Returns the error status of the font options, `Status.SUCCESS` when they are usable. */
122
+ status(): Status {
123
+ return cairoFontOptionsStatus(getHandle(this)) as Status;
124
+ }
125
+
126
+ /** Returns a hash of the settings, equal for options that compare `equal`. */
127
+ hash(): number {
128
+ return cairoFontOptionsHash(getHandle(this)) as number;
129
+ }
130
+
131
+ /** Sets the OpenType font variations to apply, as a comma-separated `axis=value` string. */
132
+ setVariations(variations: string): void {
133
+ cairoFontOptionsSetVariations(getHandle(this), variations);
134
+ }
135
+
136
+ /** Returns the OpenType font variations to apply. */
137
+ getVariations(): string {
138
+ return cairoFontOptionsGetVariations(getHandle(this)) as string;
139
+ }
140
+ }
141
+
142
+ export { FontOptions };
package/src/index.ts ADDED
@@ -0,0 +1,28 @@
1
+ /** @public */
2
+ export { Context } from "./context.js";
3
+ /** @public */
4
+ export { Device } from "./device.js";
5
+ /** @public */
6
+ export * from "./enums.js";
7
+ /** @public */
8
+ export { FontFace, FtFontFace, FtSynthesize, ToyFontFace } from "./font-face.js";
9
+ /** @public */
10
+ export { FontOptions } from "./font-options.js";
11
+ /** @public */
12
+ export { Matrix } from "./matrix.js";
13
+ /** @public */
14
+ export { Path } from "./path.js";
15
+ /** @public */
16
+ export { LinearPattern, MeshPattern, Pattern, RadialPattern } from "./pattern.js";
17
+ /** @public */
18
+ export { Region } from "./region.js";
19
+ /** @public */
20
+ export { ScaledFont } from "./scaled-font.js";
21
+ /** @public */
22
+ export * from "./structs.js";
23
+ /** @public */
24
+ export { ImageSurface, RecordingSurface, Surface } from "./surface.js";
25
+ /** @public */
26
+ export type * from "./types.js";
27
+ /** @public */
28
+ export { statusToString, version, versionString } from "./version.js";
package/src/lib.ts ADDED
@@ -0,0 +1,186 @@
1
+ import { resolveType, t } from "@gtkx/runtime";
2
+
3
+ type Descriptor = Parameters<typeof t.bind>[3];
4
+ type BoundFunction = ReturnType<typeof t.bind>;
5
+ type BoxedDescriptor = ReturnType<typeof t.boxed>;
6
+
7
+ const CAIRO_LIBRARY = "libcairo.so.2";
8
+ const CAIRO_GOBJECT_LIBRARY = "libcairo-gobject.so.2";
9
+
10
+ const CONTEXT_T: BoxedDescriptor = t.boxed("CairoContext", {
11
+ ownership: "borrowed",
12
+ sharedLibrary: CAIRO_GOBJECT_LIBRARY,
13
+ getTypeFnName: "cairo_gobject_context_get_type",
14
+ });
15
+
16
+ const CONTEXT_FULL_T: BoxedDescriptor = t.boxed("CairoContext", {
17
+ ownership: "full",
18
+ sharedLibrary: CAIRO_GOBJECT_LIBRARY,
19
+ getTypeFnName: "cairo_gobject_context_get_type",
20
+ });
21
+
22
+ const SURFACE_T: BoxedDescriptor = t.boxed("CairoSurface", {
23
+ ownership: "borrowed",
24
+ sharedLibrary: CAIRO_GOBJECT_LIBRARY,
25
+ getTypeFnName: "cairo_gobject_surface_get_type",
26
+ });
27
+
28
+ const SURFACE_FULL_T: BoxedDescriptor = t.boxed("CairoSurface", {
29
+ ownership: "full",
30
+ sharedLibrary: CAIRO_GOBJECT_LIBRARY,
31
+ getTypeFnName: "cairo_gobject_surface_get_type",
32
+ });
33
+
34
+ const DEVICE_T: BoxedDescriptor = t.boxed("CairoDevice", {
35
+ ownership: "borrowed",
36
+ sharedLibrary: CAIRO_GOBJECT_LIBRARY,
37
+ getTypeFnName: "cairo_gobject_device_get_type",
38
+ });
39
+
40
+ const PATTERN_T: BoxedDescriptor = t.boxed("CairoPattern", {
41
+ ownership: "borrowed",
42
+ sharedLibrary: CAIRO_GOBJECT_LIBRARY,
43
+ getTypeFnName: "cairo_gobject_pattern_get_type",
44
+ });
45
+
46
+ const PATTERN_FULL_T: BoxedDescriptor = t.boxed("CairoPattern", {
47
+ ownership: "full",
48
+ sharedLibrary: CAIRO_GOBJECT_LIBRARY,
49
+ getTypeFnName: "cairo_gobject_pattern_get_type",
50
+ });
51
+
52
+ const REGION_T: BoxedDescriptor = t.boxed("CairoRegion", {
53
+ ownership: "borrowed",
54
+ sharedLibrary: CAIRO_GOBJECT_LIBRARY,
55
+ getTypeFnName: "cairo_gobject_region_get_type",
56
+ });
57
+
58
+ const REGION_FULL_T: BoxedDescriptor = t.boxed("CairoRegion", {
59
+ ownership: "full",
60
+ sharedLibrary: CAIRO_GOBJECT_LIBRARY,
61
+ getTypeFnName: "cairo_gobject_region_get_type",
62
+ });
63
+
64
+ const FONT_OPTIONS_T: BoxedDescriptor = t.boxed("CairoFontOptions", {
65
+ ownership: "borrowed",
66
+ sharedLibrary: CAIRO_GOBJECT_LIBRARY,
67
+ getTypeFnName: "cairo_gobject_font_options_get_type",
68
+ });
69
+
70
+ const FONT_OPTIONS_FULL_T: BoxedDescriptor = t.boxed("CairoFontOptions", {
71
+ ownership: "full",
72
+ sharedLibrary: CAIRO_GOBJECT_LIBRARY,
73
+ getTypeFnName: "cairo_gobject_font_options_get_type",
74
+ });
75
+
76
+ const FONT_FACE_T: BoxedDescriptor = t.boxed("CairoFontFace", {
77
+ ownership: "borrowed",
78
+ sharedLibrary: CAIRO_GOBJECT_LIBRARY,
79
+ getTypeFnName: "cairo_gobject_font_face_get_type",
80
+ });
81
+
82
+ const FONT_FACE_FULL_T: BoxedDescriptor = t.boxed("CairoFontFace", {
83
+ ownership: "full",
84
+ sharedLibrary: CAIRO_GOBJECT_LIBRARY,
85
+ getTypeFnName: "cairo_gobject_font_face_get_type",
86
+ });
87
+
88
+ const SCALED_FONT_T: BoxedDescriptor = t.boxed("CairoScaledFont", {
89
+ ownership: "borrowed",
90
+ sharedLibrary: CAIRO_GOBJECT_LIBRARY,
91
+ getTypeFnName: "cairo_gobject_scaled_font_get_type",
92
+ });
93
+
94
+ const SCALED_FONT_FULL_T: BoxedDescriptor = t.boxed("CairoScaledFont", {
95
+ ownership: "full",
96
+ sharedLibrary: CAIRO_GOBJECT_LIBRARY,
97
+ getTypeFnName: "cairo_gobject_scaled_font_get_type",
98
+ });
99
+
100
+ const FC_PATTERN_T: BoxedDescriptor = t.boxed("FcPattern", { ownership: "borrowed", sharedLibrary: CAIRO_LIBRARY });
101
+ const FT_FACE_T: BoxedDescriptor = t.boxed("FT_Face", { ownership: "borrowed", sharedLibrary: CAIRO_LIBRARY });
102
+ const MATRIX_T: BoxedDescriptor = t.boxed("cairo_matrix_t", { ownership: "borrowed", sharedLibrary: CAIRO_LIBRARY });
103
+
104
+ const RECTANGLE_T: BoxedDescriptor = t.boxed("cairo_rectangle_t", {
105
+ ownership: "borrowed",
106
+ sharedLibrary: CAIRO_LIBRARY,
107
+ });
108
+
109
+ const RECTANGLE_INT_T: BoxedDescriptor = t.boxed("cairo_rectangle_int_t", {
110
+ ownership: "borrowed",
111
+ sharedLibrary: CAIRO_LIBRARY,
112
+ });
113
+
114
+ const RECTANGLE_INT_ARRAY_T: BoxedDescriptor = t.boxed("cairo_rectangle_int_t[]", {
115
+ ownership: "borrowed",
116
+ sharedLibrary: CAIRO_LIBRARY,
117
+ });
118
+
119
+ const RECTANGLE_LIST_T: BoxedDescriptor = t.boxed("cairo_rectangle_list_t", {
120
+ ownership: "borrowed",
121
+ sharedLibrary: CAIRO_LIBRARY,
122
+ });
123
+
124
+ const GLYPH_T: BoxedDescriptor = t.boxed("cairo_glyph_t", { ownership: "borrowed", sharedLibrary: CAIRO_LIBRARY });
125
+
126
+ const TEXT_CLUSTER_T: BoxedDescriptor = t.boxed("cairo_text_cluster_t", {
127
+ ownership: "borrowed",
128
+ sharedLibrary: CAIRO_LIBRARY,
129
+ });
130
+
131
+ const TEXT_EXTENTS_T: BoxedDescriptor = t.boxed("cairo_text_extents_t", {
132
+ ownership: "borrowed",
133
+ sharedLibrary: CAIRO_LIBRARY,
134
+ });
135
+
136
+ const FONT_EXTENTS_T: BoxedDescriptor = t.boxed("cairo_font_extents_t", {
137
+ ownership: "borrowed",
138
+ sharedLibrary: CAIRO_LIBRARY,
139
+ });
140
+
141
+ const PATH_T: BoxedDescriptor = t.boxed("cairo_path_t", {
142
+ ownership: "full",
143
+ sharedLibrary: CAIRO_LIBRARY,
144
+ freeFnName: "cairo_path_destroy",
145
+ });
146
+
147
+ const DOUBLE_BUFFER_T: BoxedDescriptor = t.boxed("double[]", { ownership: "borrowed", sharedLibrary: CAIRO_LIBRARY });
148
+
149
+ const bindCairo = (symbol: string, args: Descriptor[], returns: Descriptor): BoundFunction =>
150
+ t.bind(CAIRO_LIBRARY, symbol, args, returns);
151
+
152
+ const cairoGType = (typeSymbol: string): bigint => resolveType(CAIRO_GOBJECT_LIBRARY, typeSymbol);
153
+
154
+ export {
155
+ bindCairo,
156
+ type BoundFunction,
157
+ cairoGType,
158
+ CONTEXT_FULL_T,
159
+ CONTEXT_T,
160
+ DEVICE_T,
161
+ DOUBLE_BUFFER_T,
162
+ FC_PATTERN_T,
163
+ FONT_EXTENTS_T,
164
+ FONT_FACE_FULL_T,
165
+ FONT_FACE_T,
166
+ FONT_OPTIONS_FULL_T,
167
+ FONT_OPTIONS_T,
168
+ FT_FACE_T,
169
+ GLYPH_T,
170
+ MATRIX_T,
171
+ PATH_T,
172
+ PATTERN_FULL_T,
173
+ PATTERN_T,
174
+ RECTANGLE_INT_ARRAY_T,
175
+ RECTANGLE_INT_T,
176
+ RECTANGLE_LIST_T,
177
+ RECTANGLE_T,
178
+ REGION_FULL_T,
179
+ REGION_T,
180
+ SCALED_FONT_FULL_T,
181
+ SCALED_FONT_T,
182
+ SURFACE_FULL_T,
183
+ SURFACE_T,
184
+ TEXT_CLUSTER_T,
185
+ TEXT_EXTENTS_T,
186
+ };
package/src/matrix.ts ADDED
@@ -0,0 +1,135 @@
1
+ import { alloc, type ExternalObject, getHandle, type Handle, setHandle, t, wrapHandle } from "@gtkx/runtime";
2
+ import type { Status } from "./enums.js";
3
+ import type { Distance, Point } from "./types.js";
4
+ import { bindCairo, MATRIX_T } from "./lib.js";
5
+
6
+ type MatrixAllocation = { handle: ExternalObject<Handle>; matrix: Matrix };
7
+
8
+ const MATRIX_SIZE = 48;
9
+
10
+ const cairoMatrixInit = bindCairo(
11
+ "cairo_matrix_init",
12
+ [MATRIX_T, t.float64, t.float64, t.float64, t.float64, t.float64, t.float64],
13
+ t.void,
14
+ );
15
+
16
+ const cairoMatrixInitIdentity = bindCairo("cairo_matrix_init_identity", [MATRIX_T], t.void);
17
+ const cairoMatrixInitTranslate = bindCairo("cairo_matrix_init_translate", [MATRIX_T, t.float64, t.float64], t.void);
18
+ const cairoMatrixInitScale = bindCairo("cairo_matrix_init_scale", [MATRIX_T, t.float64, t.float64], t.void);
19
+ const cairoMatrixInitRotate = bindCairo("cairo_matrix_init_rotate", [MATRIX_T, t.float64], t.void);
20
+ const cairoMatrixMultiply = bindCairo("cairo_matrix_multiply", [MATRIX_T, MATRIX_T, MATRIX_T], t.void);
21
+ const cairoMatrixTranslate = bindCairo("cairo_matrix_translate", [MATRIX_T, t.float64, t.float64], t.void);
22
+ const cairoMatrixScale = bindCairo("cairo_matrix_scale", [MATRIX_T, t.float64, t.float64], t.void);
23
+ const cairoMatrixRotate = bindCairo("cairo_matrix_rotate", [MATRIX_T, t.float64], t.void);
24
+ const cairoMatrixInvert = bindCairo("cairo_matrix_invert", [MATRIX_T], t.int32);
25
+
26
+ const cairoMatrixTransformPoint = bindCairo(
27
+ "cairo_matrix_transform_point",
28
+ [MATRIX_T, t.ref(t.float64), t.ref(t.float64)],
29
+ t.void,
30
+ );
31
+
32
+ const cairoMatrixTransformDistance = bindCairo(
33
+ "cairo_matrix_transform_distance",
34
+ [MATRIX_T, t.ref(t.float64), t.ref(t.float64)],
35
+ t.void,
36
+ );
37
+
38
+ const allocMatrix = (): MatrixAllocation => {
39
+ const handle = alloc(MATRIX_SIZE);
40
+
41
+ return { handle, matrix: wrapHandle(handle, Matrix) };
42
+ };
43
+
44
+ /**
45
+ * An affine transformation (`cairo_matrix_t`) mapping user space to device space:
46
+ * `x' = xx * x + xy * y + x0` and `y' = yx * x + yy * y + y0`.
47
+ */
48
+ class Matrix {
49
+ /** Returns the identity transformation. */
50
+ static initIdentity(): Matrix {
51
+ const { handle, matrix } = allocMatrix();
52
+ cairoMatrixInitIdentity(handle);
53
+
54
+ return matrix;
55
+ }
56
+
57
+ /** Returns a transformation translating by `tx` and `ty`. */
58
+ static initTranslate(tx: number, ty: number): Matrix {
59
+ const { handle, matrix } = allocMatrix();
60
+ cairoMatrixInitTranslate(handle, tx, ty);
61
+
62
+ return matrix;
63
+ }
64
+
65
+ /** Returns a transformation scaling by `sx` and `sy`. */
66
+ static initScale(sx: number, sy: number): Matrix {
67
+ const { handle, matrix } = allocMatrix();
68
+ cairoMatrixInitScale(handle, sx, sy);
69
+
70
+ return matrix;
71
+ }
72
+
73
+ /** Returns a transformation rotating by `radians`, positive angles turning from the X axis toward the Y axis. */
74
+ static initRotate(radians: number): Matrix {
75
+ const { handle, matrix } = allocMatrix();
76
+ cairoMatrixInitRotate(handle, radians);
77
+
78
+ return matrix;
79
+ }
80
+
81
+ /** Returns the product of `a` and `b`: the transformation that applies `a` first, then `b`. */
82
+ static multiply(a: Matrix, b: Matrix): Matrix {
83
+ const { handle, matrix } = allocMatrix();
84
+ cairoMatrixMultiply(handle, getHandle(a), getHandle(b));
85
+
86
+ return matrix;
87
+ }
88
+
89
+ /** Creates a matrix from its six affine components. */
90
+ constructor(xx: number, yx: number, xy: number, yy: number, x0: number, y0: number) {
91
+ const handle = alloc(MATRIX_SIZE);
92
+ setHandle(this, handle);
93
+ cairoMatrixInit(handle, xx, yx, xy, yy, x0, y0);
94
+ }
95
+
96
+ /** Applies a translation by `tx` and `ty` before the existing transformation. */
97
+ translate(tx: number, ty: number): void {
98
+ cairoMatrixTranslate(getHandle(this), tx, ty);
99
+ }
100
+
101
+ /** Applies a scaling by `sx` and `sy` before the existing transformation. */
102
+ scale(sx: number, sy: number): void {
103
+ cairoMatrixScale(getHandle(this), sx, sy);
104
+ }
105
+
106
+ /** Applies a rotation by `radians` before the existing transformation. */
107
+ rotate(radians: number): void {
108
+ cairoMatrixRotate(getHandle(this), radians);
109
+ }
110
+
111
+ /** Replaces the matrix with its inverse, returning `Status.INVALID_MATRIX` when it has none. */
112
+ invert(): Status {
113
+ return cairoMatrixInvert(getHandle(this)) as Status;
114
+ }
115
+
116
+ /** Transforms the point `(x, y)`, translation included. */
117
+ transformPoint(x: number, y: number): Point {
118
+ const xRef = { value: x };
119
+ const yRef = { value: y };
120
+ cairoMatrixTransformPoint(getHandle(this), xRef, yRef);
121
+
122
+ return { x: xRef.value, y: yRef.value };
123
+ }
124
+
125
+ /** Transforms the distance vector `(dx, dy)`, ignoring translation. */
126
+ transformDistance(dx: number, dy: number): Distance {
127
+ const dxRef = { value: dx };
128
+ const dyRef = { value: dy };
129
+ cairoMatrixTransformDistance(getHandle(this), dxRef, dyRef);
130
+
131
+ return { dx: dxRef.value, dy: dyRef.value };
132
+ }
133
+ }
134
+
135
+ export { allocMatrix, Matrix };