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 : :
9 : : #include <js/CallArgs.h>
10 : : #include <js/PropertyDescriptor.h> // for JSPROP_READONLY
11 : : #include <js/PropertySpec.h>
12 : : #include <js/RootingAPI.h>
13 : : #include <js/TypeDecls.h>
14 : : #include <jsapi.h> // for JS_NewObjectWithGivenProto
15 : : #include <jspubtd.h> // for JSProtoKey
16 : :
17 : : #include "gjs/jsapi-util-args.h"
18 : : #include "gjs/jsapi-util.h"
19 : : #include "gjs/macros.h"
20 : : #include "modules/cairo-private.h"
21 : :
22 : 2 : JSObject* CairoSurfacePattern::new_proto(JSContext* cx, JSProtoKey) {
23 : 2 : JS::RootedObject parent_proto(cx, CairoPattern::prototype(cx));
24 : 2 : return JS_NewObjectWithGivenProto(cx, nullptr, parent_proto);
25 : 2 : }
26 : :
27 : 2 : cairo_pattern_t* CairoSurfacePattern::constructor_impl(
28 : : JSContext* context, const JS::CallArgs& argv) {
29 : : cairo_pattern_t *pattern;
30 : 2 : JS::RootedObject surface_wrapper(context);
31 [ - + ]: 2 : if (!gjs_parse_call_args(context, "SurfacePattern", argv, "o",
32 : : "surface", &surface_wrapper))
33 : 0 : return nullptr;
34 : :
35 : 2 : cairo_surface_t* surface = CairoSurface::for_js(context, surface_wrapper);
36 [ - + ]: 2 : if (!surface)
37 : 0 : return nullptr;
38 : :
39 : 2 : pattern = cairo_pattern_create_for_surface(surface);
40 : :
41 [ - + ]: 2 : if (!gjs_cairo_check_status(context, cairo_pattern_status(pattern), "pattern"))
42 : 0 : return nullptr;
43 : :
44 : 2 : return pattern;
45 : 2 : }
46 : :
47 : : const JSPropertySpec CairoSurfacePattern::proto_props[] = {
48 : : JS_STRING_SYM_PS(toStringTag, "SurfacePattern", JSPROP_READONLY),
49 : : JS_PS_END};
50 : :
51 : : GJS_JSAPI_RETURN_CONVENTION
52 : : static bool
53 : 0 : setExtend_func(JSContext *context,
54 : : unsigned argc,
55 : : JS::Value *vp)
56 : : {
57 [ # # ]: 0 : GJS_GET_THIS(context, argc, vp, argv, obj);
58 : : cairo_extend_t extend;
59 : :
60 [ # # ]: 0 : if (!gjs_parse_call_args(context, "setExtend", argv, "i",
61 : : "extend", &extend))
62 : 0 : return false;
63 : :
64 : 0 : cairo_pattern_t* pattern = CairoPattern::for_js(context, obj);
65 [ # # ]: 0 : if (!pattern)
66 : 0 : return false;
67 : :
68 : 0 : cairo_pattern_set_extend(pattern, extend);
69 : :
70 [ # # ]: 0 : if (!gjs_cairo_check_status(context, cairo_pattern_status(pattern), "pattern"))
71 : 0 : return false;
72 : :
73 : 0 : argv.rval().setUndefined();
74 : 0 : return true;
75 : 0 : }
76 : :
77 : : GJS_JSAPI_RETURN_CONVENTION
78 : : static bool
79 : 0 : getExtend_func(JSContext *context,
80 : : unsigned argc,
81 : : JS::Value *vp)
82 : : {
83 [ # # ]: 0 : GJS_GET_THIS(context, argc, vp, rec, obj);
84 : : cairo_extend_t extend;
85 : :
86 [ # # ]: 0 : if (argc > 0) {
87 : 0 : gjs_throw(context, "SurfacePattern.getExtend() requires no arguments");
88 : 0 : return false;
89 : : }
90 : :
91 : 0 : cairo_pattern_t* pattern = CairoPattern::for_js(context, obj);
92 [ # # ]: 0 : if (!pattern)
93 : 0 : return false;
94 : :
95 : 0 : extend = cairo_pattern_get_extend(pattern);
96 : :
97 [ # # ]: 0 : if (!gjs_cairo_check_status(context, cairo_pattern_status(pattern), "pattern"))
98 : 0 : return false;
99 : :
100 : 0 : rec.rval().setInt32(extend);
101 : :
102 : 0 : return true;
103 : 0 : }
104 : :
105 : : GJS_JSAPI_RETURN_CONVENTION
106 : : static bool
107 : 0 : setFilter_func(JSContext *context,
108 : : unsigned argc,
109 : : JS::Value *vp)
110 : : {
111 [ # # ]: 0 : GJS_GET_THIS(context, argc, vp, argv, obj);
112 : : cairo_filter_t filter;
113 : :
114 [ # # ]: 0 : if (!gjs_parse_call_args(context, "setFilter", argv, "i",
115 : : "filter", &filter))
116 : 0 : return false;
117 : :
118 : 0 : cairo_pattern_t* pattern = CairoPattern::for_js(context, obj);
119 [ # # ]: 0 : if (!pattern)
120 : 0 : return false;
121 : :
122 : 0 : cairo_pattern_set_filter(pattern, filter);
123 : :
124 [ # # ]: 0 : if (!gjs_cairo_check_status(context, cairo_pattern_status(pattern), "pattern"))
125 : 0 : return false;
126 : :
127 : 0 : argv.rval().setUndefined();
128 : 0 : return true;
129 : 0 : }
130 : :
131 : : GJS_JSAPI_RETURN_CONVENTION
132 : : static bool
133 : 0 : getFilter_func(JSContext *context,
134 : : unsigned argc,
135 : : JS::Value *vp)
136 : : {
137 [ # # ]: 0 : GJS_GET_THIS(context, argc, vp, rec, obj);
138 : : cairo_filter_t filter;
139 : :
140 [ # # ]: 0 : if (argc > 0) {
141 : 0 : gjs_throw(context, "SurfacePattern.getFilter() requires no arguments");
142 : 0 : return false;
143 : : }
144 : :
145 : 0 : cairo_pattern_t* pattern = CairoPattern::for_js(context, obj);
146 [ # # ]: 0 : if (!pattern)
147 : 0 : return false;
148 : :
149 : 0 : filter = cairo_pattern_get_filter(pattern);
150 : :
151 [ # # ]: 0 : if (!gjs_cairo_check_status(context, cairo_pattern_status(pattern), "pattern"))
152 : 0 : return false;
153 : :
154 : 0 : rec.rval().setInt32(filter);
155 : :
156 : 0 : return true;
157 : 0 : }
158 : :
159 : : // clang-format off
160 : : const JSFunctionSpec CairoSurfacePattern::proto_funcs[] = {
161 : : JS_FN("setExtend", setExtend_func, 0, 0),
162 : : JS_FN("getExtend", getExtend_func, 0, 0),
163 : : JS_FN("setFilter", setFilter_func, 0, 0),
164 : : JS_FN("getFilter", getFilter_func, 0, 0),
165 : : JS_FS_END};
166 : : // clang-format on
|