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