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* CairoImageSurface::new_proto(JSContext* cx, JSProtoKey) {
23 : 2 : JS::RootedObject parent_proto(cx, CairoSurface::prototype(cx));
24 : 2 : return JS_NewObjectWithGivenProto(cx, nullptr, parent_proto);
25 : 2 : }
26 : :
27 : 45 : cairo_surface_t* CairoImageSurface::constructor_impl(JSContext* context,
28 : : const JS::CallArgs& argv) {
29 : : int format, width, height;
30 : : cairo_surface_t *surface;
31 : :
32 : : // create_for_data optional parameter
33 [ - + ]: 45 : if (!gjs_parse_call_args(context, "ImageSurface", argv, "iii",
34 : : "format", &format,
35 : : "width", &width,
36 : : "height", &height))
37 : 0 : return nullptr;
38 : :
39 : 45 : surface = cairo_image_surface_create((cairo_format_t) format, width, height);
40 : :
41 [ - + ]: 45 : if (!gjs_cairo_check_status(context, cairo_surface_status(surface), "surface"))
42 : 0 : return nullptr;
43 : :
44 : 45 : return surface;
45 : : }
46 : :
47 : : // clang-format off
48 : : const JSPropertySpec CairoImageSurface::proto_props[] = {
49 : : JS_STRING_SYM_PS(toStringTag, "ImageSurface", JSPROP_READONLY),
50 : : JS_PS_END};
51 : : // clang-format on
52 : :
53 : : GJS_JSAPI_RETURN_CONVENTION
54 : : static bool
55 : 0 : createFromPNG_func(JSContext *context,
56 : : unsigned argc,
57 : : JS::Value *vp)
58 : : {
59 : 0 : JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
60 : 0 : GjsAutoChar filename;
61 : : cairo_surface_t *surface;
62 : :
63 [ # # ]: 0 : if (!gjs_parse_call_args(context, "createFromPNG", argv, "F",
64 : : "filename", &filename))
65 : 0 : return false;
66 : :
67 : 0 : surface = cairo_image_surface_create_from_png(filename);
68 : :
69 [ # # ]: 0 : if (!gjs_cairo_check_status(context, cairo_surface_status(surface), "surface"))
70 : 0 : return false;
71 : :
72 : 0 : JSObject* surface_wrapper = CairoImageSurface::from_c_ptr(context, surface);
73 [ # # ]: 0 : if (!surface_wrapper)
74 : 0 : return false;
75 : :
76 : 0 : cairo_surface_destroy(surface);
77 : :
78 : 0 : argv.rval().setObject(*surface_wrapper);
79 : 0 : return true;
80 : 0 : }
81 : :
82 : : GJS_JSAPI_RETURN_CONVENTION
83 : : static bool
84 : 4 : getFormat_func(JSContext *context,
85 : : unsigned argc,
86 : : JS::Value *vp)
87 : : {
88 [ - + ]: 4 : GJS_GET_THIS(context, argc, vp, rec, obj);
89 : : cairo_format_t format;
90 : :
91 [ - + ]: 4 : if (argc > 1) {
92 : 0 : gjs_throw(context, "ImageSurface.getFormat() takes no arguments");
93 : 0 : return false;
94 : : }
95 : :
96 : 4 : cairo_surface_t* surface = CairoSurface::for_js(context, obj);
97 [ - + ]: 4 : if (!surface)
98 : 0 : return false;
99 : :
100 : 4 : format = cairo_image_surface_get_format(surface);
101 : :
102 [ - + ]: 4 : if (!gjs_cairo_check_status(context, cairo_surface_status(surface), "surface"))
103 : 0 : return false;
104 : :
105 : 4 : rec.rval().setInt32(format);
106 : 4 : return true;
107 : 4 : }
108 : :
109 : : GJS_JSAPI_RETURN_CONVENTION
110 : : static bool
111 : 4 : getWidth_func(JSContext *context,
112 : : unsigned argc,
113 : : JS::Value *vp)
114 : : {
115 [ - + ]: 4 : GJS_GET_THIS(context, argc, vp, rec, obj);
116 : : int width;
117 : :
118 [ - + ]: 4 : if (argc > 1) {
119 : 0 : gjs_throw(context, "ImageSurface.getWidth() takes no arguments");
120 : 0 : return false;
121 : : }
122 : :
123 : 4 : cairo_surface_t* surface = CairoSurface::for_js(context, obj);
124 [ - + ]: 4 : if (!surface)
125 : 0 : return false;
126 : :
127 : 4 : width = cairo_image_surface_get_width(surface);
128 : :
129 [ - + ]: 4 : if (!gjs_cairo_check_status(context, cairo_surface_status(surface), "surface"))
130 : 0 : return false;
131 : :
132 : 4 : rec.rval().setInt32(width);
133 : 4 : return true;
134 : 4 : }
135 : :
136 : : GJS_JSAPI_RETURN_CONVENTION
137 : : static bool
138 : 4 : getHeight_func(JSContext *context,
139 : : unsigned argc,
140 : : JS::Value *vp)
141 : : {
142 [ - + ]: 4 : GJS_GET_THIS(context, argc, vp, rec, obj);
143 : : int height;
144 : :
145 [ - + ]: 4 : if (argc > 1) {
146 : 0 : gjs_throw(context, "ImageSurface.getHeight() takes no arguments");
147 : 0 : return false;
148 : : }
149 : :
150 : 4 : cairo_surface_t* surface = CairoSurface::for_js(context, obj);
151 [ - + ]: 4 : if (!surface)
152 : 0 : return false;
153 : :
154 : 4 : height = cairo_image_surface_get_height(surface);
155 : :
156 [ - + ]: 4 : if (!gjs_cairo_check_status(context, cairo_surface_status(surface), "surface"))
157 : 0 : return false;
158 : :
159 : 4 : rec.rval().setInt32(height);
160 : 4 : return true;
161 : 4 : }
162 : :
163 : : GJS_JSAPI_RETURN_CONVENTION
164 : : static bool
165 : 0 : getStride_func(JSContext *context,
166 : : unsigned argc,
167 : : JS::Value *vp)
168 : : {
169 [ # # ]: 0 : GJS_GET_THIS(context, argc, vp, rec, obj);
170 : : int stride;
171 : :
172 [ # # ]: 0 : if (argc > 1) {
173 : 0 : gjs_throw(context, "ImageSurface.getStride() takes no arguments");
174 : 0 : return false;
175 : : }
176 : :
177 : 0 : cairo_surface_t* surface = CairoSurface::for_js(context, obj);
178 [ # # ]: 0 : if (!surface)
179 : 0 : return false;
180 : :
181 : 0 : stride = cairo_image_surface_get_stride(surface);
182 : :
183 [ # # ]: 0 : if (!gjs_cairo_check_status(context, cairo_surface_status(surface), "surface"))
184 : 0 : return false;
185 : :
186 : 0 : rec.rval().setInt32(stride);
187 : 0 : return true;
188 : 0 : }
189 : :
190 : : const JSFunctionSpec CairoImageSurface::proto_funcs[] = {
191 : : JS_FN("createFromPNG", createFromPNG_func, 0, 0),
192 : : // getData
193 : : JS_FN("getFormat", getFormat_func, 0, 0),
194 : : JS_FN("getWidth", getWidth_func, 0, 0),
195 : : JS_FN("getHeight", getHeight_func, 0, 0),
196 : : JS_FN("getStride", getStride_func, 0, 0), JS_FS_END};
197 : :
198 : : const JSFunctionSpec CairoImageSurface::static_funcs[] = {
199 : : JS_FN("createFromPNG", createFromPNG_func, 1, GJS_MODULE_PROP_FLAGS),
200 : : JS_FS_END};
|