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/macros.h"
19 : : #include "modules/cairo-private.h"
20 : :
21 : 2 : JSObject* CairoSolidPattern::new_proto(JSContext* cx, JSProtoKey) {
22 : 2 : JS::RootedObject parent_proto(cx, CairoPattern::prototype(cx));
23 : 2 : return JS_NewObjectWithGivenProto(cx, nullptr, parent_proto);
24 : 2 : }
25 : :
26 : : // clang-format off
27 : : const JSPropertySpec CairoSolidPattern::proto_props[] = {
28 : : JS_STRING_SYM_PS(toStringTag, "SolidPattern", JSPROP_READONLY),
29 : : JS_PS_END};
30 : : // clang-format on
31 : :
32 : : GJS_JSAPI_RETURN_CONVENTION
33 : : static bool
34 : 3 : createRGB_func(JSContext *context,
35 : : unsigned argc,
36 : : JS::Value *vp)
37 : : {
38 : 3 : JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
39 : : double red, green, blue;
40 : : cairo_pattern_t *pattern;
41 : :
42 [ - + ]: 3 : if (!gjs_parse_call_args(context, "createRGB", argv, "fff",
43 : : "red", &red,
44 : : "green", &green,
45 : : "blue", &blue))
46 : 0 : return false;
47 : :
48 : 3 : pattern = cairo_pattern_create_rgb(red, green, blue);
49 [ - + ]: 3 : if (!gjs_cairo_check_status(context, cairo_pattern_status(pattern), "pattern"))
50 : 0 : return false;
51 : :
52 : 3 : JSObject* pattern_wrapper = CairoSolidPattern::from_c_ptr(context, pattern);
53 [ - + ]: 3 : if (!pattern_wrapper)
54 : 0 : return false;
55 : 3 : cairo_pattern_destroy(pattern);
56 : :
57 : 3 : argv.rval().setObjectOrNull(pattern_wrapper);
58 : :
59 : 3 : return true;
60 : : }
61 : :
62 : : GJS_JSAPI_RETURN_CONVENTION
63 : : static bool
64 : 1 : createRGBA_func(JSContext *context,
65 : : unsigned argc,
66 : : JS::Value *vp)
67 : : {
68 : 1 : JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
69 : : double red, green, blue, alpha;
70 : : cairo_pattern_t *pattern;
71 : :
72 [ - + ]: 1 : if (!gjs_parse_call_args(context, "createRGBA", argv, "ffff",
73 : : "red", &red,
74 : : "green", &green,
75 : : "blue", &blue,
76 : : "alpha", &alpha))
77 : 0 : return false;
78 : :
79 : 1 : pattern = cairo_pattern_create_rgba(red, green, blue, alpha);
80 [ - + ]: 1 : if (!gjs_cairo_check_status(context, cairo_pattern_status(pattern), "pattern"))
81 : 0 : return false;
82 : :
83 : 1 : JSObject* pattern_wrapper = CairoSolidPattern::from_c_ptr(context, pattern);
84 [ - + ]: 1 : if (!pattern_wrapper)
85 : 0 : return false;
86 : 1 : cairo_pattern_destroy(pattern);
87 : :
88 : 1 : argv.rval().setObjectOrNull(pattern_wrapper);
89 : :
90 : 1 : return true;
91 : : }
92 : :
93 : : // clang-format off
94 : : const JSFunctionSpec CairoSolidPattern::static_funcs[] = {
95 : : JS_FN("createRGB", createRGB_func, 0, 0),
96 : : JS_FN("createRGBA", createRGBA_func, 0, 0),
97 : : JS_FS_END};
98 : : // clang-format on
|