Branch data Line data Source code
1 : : /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 : : // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
3 : : // SPDX-FileCopyrightText: 2010 litl, LLC.
4 : :
5 : : #include <config.h>
6 : :
7 : : #include <cairo.h>
8 : : #include <glib.h>
9 : :
10 : : #include <js/CallArgs.h>
11 : : #include <js/Class.h>
12 : : #include <js/Object.h> // for GetClass
13 : : #include <js/PropertyDescriptor.h> // for JSPROP_READONLY
14 : : #include <js/PropertySpec.h>
15 : : #include <js/RootingAPI.h>
16 : : #include <js/TypeDecls.h>
17 : :
18 : : #include "gjs/jsapi-class.h"
19 : : #include "gjs/jsapi-util.h"
20 : : #include "gjs/macros.h"
21 : : #include "modules/cairo-private.h"
22 : :
23 : : /* Properties */
24 : :
25 : : // clang-format off
26 : : const JSPropertySpec CairoPattern::proto_props[] = {
27 : : JS_STRING_SYM_PS(toStringTag, "Pattern", JSPROP_READONLY),
28 : : JS_PS_END};
29 : : // clang-format on
30 : :
31 : : /* Methods */
32 : :
33 : : GJS_JSAPI_RETURN_CONVENTION
34 : 0 : bool CairoPattern::getType_func(JSContext* context, unsigned argc,
35 : : JS::Value* vp) {
36 [ # # ]: 0 : GJS_GET_THIS(context, argc, vp, rec, obj);
37 : : cairo_pattern_type_t type;
38 : :
39 [ # # ]: 0 : if (argc > 1) {
40 : 0 : gjs_throw(context, "Pattern.getType() takes no arguments");
41 : 0 : return false;
42 : : }
43 : :
44 : 0 : cairo_pattern_t* pattern = CairoPattern::for_js(context, obj);
45 [ # # ]: 0 : if (!pattern)
46 : 0 : return false;
47 : :
48 : 0 : type = cairo_pattern_get_type(pattern);
49 : :
50 [ # # ]: 0 : if (!gjs_cairo_check_status(context, cairo_pattern_status(pattern), "pattern"))
51 : 0 : return false;
52 : :
53 : 0 : rec.rval().setInt32(type);
54 : 0 : return true;
55 : 0 : }
56 : :
57 : : const JSFunctionSpec CairoPattern::proto_funcs[] = {
58 : : // getMatrix
59 : : JS_FN("getType", getType_func, 0, 0),
60 : : // setMatrix
61 : : JS_FS_END};
62 : :
63 : : /* Public API */
64 : :
65 : : /**
66 : : * CairoPattern::finalize_impl:
67 : : * @pattern: pointer to free
68 : : *
69 : : * Destroys the resources associated with a pattern wrapper.
70 : : *
71 : : * This is mainly used for subclasses.
72 : : */
73 : 15 : void CairoPattern::finalize_impl(JS::GCContext*, cairo_pattern_t* pattern) {
74 [ - + ]: 15 : if (!pattern)
75 : 0 : return;
76 : 15 : cairo_pattern_destroy(pattern);
77 : : }
78 : :
79 : : /**
80 : : * gjs_cairo_pattern_from_pattern:
81 : : * @context: the context
82 : : * @pattern: cairo_pattern to attach to the object
83 : : *
84 : : * Constructs a pattern wrapper given cairo pattern.
85 : : * A reference to @pattern will be taken.
86 : : *
87 : : */
88 : : JSObject *
89 : 7 : gjs_cairo_pattern_from_pattern(JSContext *context,
90 : : cairo_pattern_t *pattern)
91 : : {
92 : 7 : g_return_val_if_fail(context, nullptr);
93 : 7 : g_return_val_if_fail(pattern, nullptr);
94 : :
95 [ + + + + : 7 : switch (cairo_pattern_get_type(pattern)) {
- ]
96 : 3 : case CAIRO_PATTERN_TYPE_SOLID:
97 : 3 : return CairoSolidPattern::from_c_ptr(context, pattern);
98 : 2 : case CAIRO_PATTERN_TYPE_SURFACE:
99 : 2 : return CairoSurfacePattern::from_c_ptr(context, pattern);
100 : 1 : case CAIRO_PATTERN_TYPE_LINEAR:
101 : 1 : return CairoLinearGradient::from_c_ptr(context, pattern);
102 : 1 : case CAIRO_PATTERN_TYPE_RADIAL:
103 : 1 : return CairoRadialGradient::from_c_ptr(context, pattern);
104 : 0 : case CAIRO_PATTERN_TYPE_MESH:
105 : : case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
106 : : default:
107 : 0 : gjs_throw(context,
108 : : "failed to create pattern, unsupported pattern type %d",
109 : 0 : cairo_pattern_get_type(pattern));
110 : 0 : return nullptr;
111 : : }
112 : : }
113 : :
114 : : /**
115 : : * CairoPattern::for_js:
116 : : * @cx: the context
117 : : * @pattern_wrapper: pattern wrapper
118 : : *
119 : : * Returns: the pattern attached to the wrapper.
120 : : */
121 : 9 : cairo_pattern_t* CairoPattern::for_js(JSContext* cx,
122 : : JS::HandleObject pattern_wrapper) {
123 : 9 : g_return_val_if_fail(cx, nullptr);
124 : 9 : g_return_val_if_fail(pattern_wrapper, nullptr);
125 : :
126 : 9 : JS::RootedObject proto(cx, CairoPattern::prototype(cx));
127 : :
128 : 9 : bool is_pattern_subclass = false;
129 [ - + ]: 9 : if (!gjs_object_in_prototype_chain(cx, proto, pattern_wrapper,
130 : : &is_pattern_subclass))
131 : 0 : return nullptr;
132 [ + + ]: 9 : if (!is_pattern_subclass) {
133 : 2 : gjs_throw(cx, "Expected Cairo.Pattern but got %s",
134 : 2 : JS::GetClass(pattern_wrapper)->name);
135 : 2 : return nullptr;
136 : : }
137 : :
138 : 7 : return JS::GetMaybePtrFromReservedSlot<cairo_pattern_t>(
139 : 7 : pattern_wrapper, CairoPattern::POINTER);
140 : 9 : }
|