1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2010 litl, LLC.
#include <config.h>
#include <cairo.h>
#include <girepository/girepository.h>
#include <glib.h>
#include <js/Array.h>
#include <js/CallArgs.h>
#include <js/Class.h>
#include <js/Object.h> // for GetClass
#include <js/PropertyDescriptor.h> // for JSPROP_READONLY
#include <js/PropertySpec.h>
#include <js/RootingAPI.h>
#include <js/TypeDecls.h>
#include <js/Value.h>
#include <js/ValueArray.h>
#include "gi/arg-inl.h"
#include "gi/arg.h"
#include "gi/cwrapper.h"
#include "gi/foreign.h"
#include "gjs/auto.h"
#include "gjs/enum-utils.h"
#include "gjs/jsapi-class.h"
#include "gjs/jsapi-util-args.h"
#include "gjs/jsapi-util.h"
#include "gjs/macros.h"
#include "modules/cairo-private.h"
/* Properties */
// clang-format off
const JSPropertySpec CairoSurface::proto_props[] = {
JS_STRING_SYM_PS(toStringTag, "Surface", JSPROP_READONLY),
JS_PS_END};
// clang-format on
/* Methods */
GJS_JSAPI_RETURN_CONVENTION
static bool
writeToPNG_func(JSContext *context,
unsigned argc,
JS::Value *vp)
{
GJS_GET_THIS(context, argc, vp, argv, obj);
Gjs::AutoChar filename;
if (!gjs_parse_call_args(context, "writeToPNG", argv, "F",
"filename", &filename))
return false;
cairo_surface_t* surface = CairoSurface::for_js(context, obj);
if (!surface)
return false;
cairo_surface_write_to_png(surface, filename);
if (!gjs_cairo_check_status(context, cairo_surface_status(surface),
"surface"))
return false;
argv.rval().setUndefined();
return true;
}
GJS_JSAPI_RETURN_CONVENTION
bool flush_func(JSContext* cx,
unsigned argc,
JS::Value* vp) {
GJS_GET_THIS(cx, argc, vp, argv, obj);
if (argc > 1) {
gjs_throw(cx, "Surface.flush() takes no arguments");
return false;
}
cairo_surface_t* surface = CairoSurface::for_js(cx, obj);
if (!surface)
return false;
cairo_surface_flush(surface);
if (!gjs_cairo_check_status(cx, cairo_surface_status(surface), "surface"))
return false;
argv.rval().setUndefined();
return true;
}
GJS_JSAPI_RETURN_CONVENTION
bool finish_func(JSContext* cx,
unsigned argc,
JS::Value* vp) {
GJS_GET_THIS(cx, argc, vp, argv, obj);
if (argc > 1) {
gjs_throw(cx, "Surface.finish() takes no arguments");
return false;
}
cairo_surface_t* surface = CairoSurface::for_js(cx, obj);
if (!surface)
return false;
cairo_surface_finish(surface);
if (!gjs_cairo_check_status(cx, cairo_surface_status(surface), "surface"))
return false;
argv.rval().setUndefined();
return true;
}
GJS_JSAPI_RETURN_CONVENTION
bool CairoSurface::getType_func(JSContext* context, unsigned argc,
JS::Value* vp) {
GJS_GET_THIS(context, argc, vp, rec, obj);
cairo_surface_type_t type;
if (argc > 1) {
gjs_throw(context, "Surface.getType() takes no arguments");
return false;
}
cairo_surface_t* surface = CairoSurface::for_js(context, obj);
if (!surface)
return false;
type = cairo_surface_get_type(surface);
if (!gjs_cairo_check_status(context, cairo_surface_status(surface),
"surface"))
return false;
rec.rval().setInt32(type);
return true;
}
GJS_JSAPI_RETURN_CONVENTION
static bool setDeviceOffset_func(JSContext* cx, unsigned argc, JS::Value* vp) {
GJS_GET_THIS(cx, argc, vp, args, obj);
double x_offset = 0.0, y_offset = 0.0;
if (!gjs_parse_call_args(cx, "setDeviceOffset", args, "ff", "x_offset",
&x_offset, "y_offset", &y_offset))
return false;
cairo_surface_t* surface = CairoSurface::for_js(cx, obj);
if (!surface)
return false;
cairo_surface_set_device_offset(surface, x_offset, y_offset);
if (!gjs_cairo_check_status(cx, cairo_surface_status(surface), "surface"))
return false;
args.rval().setUndefined();
return true;
}
GJS_JSAPI_RETURN_CONVENTION
static bool getDeviceOffset_func(JSContext* cx, unsigned argc, JS::Value* vp) {
GJS_GET_THIS(cx, argc, vp, args, obj);
if (argc > 0) {
gjs_throw(cx, "Surface.getDeviceOffset() takes no arguments");
return false;
}
cairo_surface_t* surface = CairoSurface::for_js(cx, obj);
if (!surface)
return false;
double x_offset, y_offset;
cairo_surface_get_device_offset(surface, &x_offset, &y_offset);
// cannot error
JS::RootedValueArray<2> elements(cx);
elements[0].setNumber(JS::CanonicalizeNaN(x_offset));
elements[1].setNumber(JS::CanonicalizeNaN(y_offset));
JS::RootedObject retval(cx, JS::NewArrayObject(cx, elements));
if (!retval)
return false;
args.rval().setObject(*retval);
return true;
}
GJS_JSAPI_RETURN_CONVENTION
static bool setDeviceScale_func(JSContext* cx, unsigned argc, JS::Value* vp) {
GJS_GET_THIS(cx, argc, vp, args, obj);
double x_scale = 1.0, y_scale = 1.0;
if (!gjs_parse_call_args(cx, "setDeviceScale", args, "ff", "x_scale",
&x_scale, "y_scale", &y_scale))
return false;
cairo_surface_t* surface = CairoSurface::for_js(cx, obj);
if (!surface)
return false;
cairo_surface_set_device_scale(surface, x_scale, y_scale);
if (!gjs_cairo_check_status(cx, cairo_surface_status(surface),
"surface"))
return false;
args.rval().setUndefined();
return true;
}
GJS_JSAPI_RETURN_CONVENTION
static bool getDeviceScale_func(JSContext* cx, unsigned argc, JS::Value* vp) {
GJS_GET_THIS(cx, argc, vp, args, obj);
if (argc > 0) {
gjs_throw(cx, "Surface.getDeviceScale() takes no arguments");
return false;
}
cairo_surface_t* surface = CairoSurface::for_js(cx, obj);
if (!surface)
return false;
double x_scale, y_scale;
cairo_surface_get_device_scale(surface, &x_scale, &y_scale);
// cannot error
JS::RootedValueArray<2> elements(cx);
elements[0].setNumber(JS::CanonicalizeNaN(x_scale));
elements[1].setNumber(JS::CanonicalizeNaN(y_scale));
JS::RootedObject retval(cx, JS::NewArrayObject(cx, elements));
if (!retval)
return false;
args.rval().setObject(*retval);
return true;
}
const JSFunctionSpec CairoSurface::proto_funcs[] = {
JS_FN("flush", flush_func, 0, 0),
JS_FN("finish", finish_func, 0, 0),
// getContent
// getFontOptions
JS_FN("getType", getType_func, 0, 0),
// markDirty
// markDirtyRectangle
JS_FN("setDeviceOffset", setDeviceOffset_func, 2, 0),
JS_FN("getDeviceOffset", getDeviceOffset_func, 0, 0),
JS_FN("setDeviceScale", setDeviceScale_func, 2, 0),
JS_FN("getDeviceScale", getDeviceScale_func, 0, 0),
// setFallbackResolution
// getFallbackResolution
// copyPage
// showPage
// hasShowTextGlyphs
JS_FN("writeToPNG", writeToPNG_func, 0, 0), JS_FS_END};
/* Public API */
/**
* CairoSurface::finalize_impl:
* @surface: the pointer to finalize
*
* Destroys the resources associated with a surface wrapper.
*
* This is mainly used for subclasses.
*/
void CairoSurface::finalize_impl(JS::GCContext*, cairo_surface_t* surface) {
if (!surface)
return;
cairo_surface_destroy(surface);
}
/**
* CairoSurface::from_c_ptr:
* @context: the context
* @surface: cairo_surface to attach to the object
*
* Constructs a surface wrapper given cairo surface.
* A reference to @surface will be taken.
*
*/
JSObject* CairoSurface::from_c_ptr(JSContext* context,
cairo_surface_t* surface) {
g_return_val_if_fail(context, nullptr);
g_return_val_if_fail(surface, nullptr);
cairo_surface_type_t type = cairo_surface_get_type(surface);
if (type == CAIRO_SURFACE_TYPE_IMAGE)
return CairoImageSurface::from_c_ptr(context, surface);
if (type == CAIRO_SURFACE_TYPE_PDF)
return CairoPDFSurface::from_c_ptr(context, surface);
if (type == CAIRO_SURFACE_TYPE_PS)
return CairoPSSurface::from_c_ptr(context, surface);
if (type == CAIRO_SURFACE_TYPE_SVG)
return CairoSVGSurface::from_c_ptr(context, surface);
return CairoSurface::CWrapper::from_c_ptr(context, surface);
}
/**
* CairoSurface::for_js:
* @cx: the context
* @surface_wrapper: surface wrapper
*
* Overrides NativeObject::for_js().
*
* Returns: the surface attached to the wrapper.
*/
cairo_surface_t* CairoSurface::for_js(JSContext* cx,
JS::HandleObject surface_wrapper) {
g_return_val_if_fail(cx, nullptr);
g_return_val_if_fail(surface_wrapper, nullptr);
JS::RootedObject proto(cx, CairoSurface::prototype(cx));
bool is_surface_subclass = false;
if (!gjs_object_in_prototype_chain(cx, proto, surface_wrapper,
&is_surface_subclass))
return nullptr;
if (!is_surface_subclass) {
gjs_throw(cx, "Expected Cairo.Surface but got %s",
JS::GetClass(surface_wrapper)->name);
return nullptr;
}
return JS::GetMaybePtrFromReservedSlot<cairo_surface_t>(
surface_wrapper, CairoSurface::POINTER);
}
GJS_JSAPI_RETURN_CONVENTION static bool surface_to_gi_argument(
JSContext* context, JS::Value value, const char* arg_name,
GjsArgumentType argument_type, GITransfer transfer, GjsArgumentFlags flags,
GIArgument* arg) {
if (value.isNull()) {
if (!(flags & GjsArgumentFlags::MAY_BE_NULL)) {
Gjs::AutoChar display_name{
gjs_argument_display_name(arg_name, argument_type)};
gjs_throw(context, "%s may not be null", display_name.get());
return false;
}
gjs_arg_unset(arg);
return true;
}
if (!value.isObject()) {
Gjs::AutoChar display_name{
gjs_argument_display_name(arg_name, argument_type)};
gjs_throw(context, "%s is not a Cairo.Surface", display_name.get());
return false;
}
JS::RootedObject surface_wrapper(context, &value.toObject());
cairo_surface_t* s = CairoSurface::for_js(context, surface_wrapper);
if (!s)
return false;
if (transfer == GI_TRANSFER_EVERYTHING)
cairo_surface_reference(s);
gjs_arg_set(arg, s);
return true;
}
GJS_JSAPI_RETURN_CONVENTION
static bool surface_from_gi_argument(JSContext* cx,
JS::MutableHandleValue value_p,
GIArgument* arg) {
JSObject* obj =
CairoSurface::from_c_ptr(cx, gjs_arg_get<cairo_surface_t*>(arg));
if (!obj)
return false;
value_p.setObject(*obj);
return true;
}
static bool surface_release_argument(JSContext*, GITransfer transfer,
GIArgument* arg) {
if (transfer != GI_TRANSFER_NOTHING)
cairo_surface_destroy(gjs_arg_get<cairo_surface_t*>(arg));
return true;
}
void gjs_cairo_surface_init(void) {
static GjsForeignInfo foreign_info = {surface_to_gi_argument,
surface_from_gi_argument,
surface_release_argument};
gjs_struct_foreign_register("cairo", "Surface", &foreign_info);
}
|