File: cairo-image-surface.cpp

package info (click to toggle)
gjs 1.87.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 7,128 kB
  • sloc: cpp: 38,390; javascript: 31,939; ansic: 15,994; sh: 1,743; python: 791; xml: 137; makefile: 40
file content (173 lines) | stat: -rw-r--r-- 5,159 bytes parent folder | download
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
/* -*- 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 <js/CallArgs.h>
#include <js/PropertyDescriptor.h>  // for JSPROP_READONLY
#include <js/PropertySpec.h>
#include <js/RootingAPI.h>
#include <js/TypeDecls.h>
#include <jsapi.h>  // for JS_NewObjectWithGivenProto
#include <jspubtd.h>  // for JSProtoKey

#include "gjs/auto.h"
#include "gjs/jsapi-util-args.h"
#include "gjs/jsapi-util.h"
#include "gjs/macros.h"
#include "modules/cairo-private.h"

JSObject* CairoImageSurface::new_proto(JSContext* cx, JSProtoKey) {
    JS::RootedObject parent_proto(cx, CairoSurface::prototype(cx));
    return JS_NewObjectWithGivenProto(cx, nullptr, parent_proto);
}

cairo_surface_t* CairoImageSurface::constructor_impl(JSContext* cx,
                                                     const JS::CallArgs& args) {
    int format, width, height;
    // create_for_data optional parameter
    if (!gjs_parse_call_args(cx, "ImageSurface", args, "iii", "format", &format,
                             "width", &width, "height", &height))
        return nullptr;

    cairo_surface_t* surface =
        cairo_image_surface_create((cairo_format_t)format, width, height);

    if (!gjs_cairo_check_status(cx, cairo_surface_status(surface), "surface"))
        return nullptr;

    return surface;
}

// clang-format off
const JSPropertySpec CairoImageSurface::proto_props[] = {
    JS_STRING_SYM_PS(toStringTag, "ImageSurface", JSPROP_READONLY),
    JS_PS_END};
// clang-format on

GJS_JSAPI_RETURN_CONVENTION
static bool createFromPNG_func(JSContext* cx, unsigned argc, JS::Value* vp) {
    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
    Gjs::AutoChar filename;

    if (!gjs_parse_call_args(cx, "createFromPNG", args, "F", "filename",
                             &filename))
        return false;

    cairo_surface_t* surface = cairo_image_surface_create_from_png(filename);

    if (!gjs_cairo_check_status(cx, cairo_surface_status(surface), "surface"))
        return false;

    JSObject* surface_wrapper = CairoImageSurface::from_c_ptr(cx, surface);
    if (!surface_wrapper)
        return false;

    cairo_surface_destroy(surface);

    args.rval().setObject(*surface_wrapper);
    return true;
}

GJS_JSAPI_RETURN_CONVENTION
static bool getFormat_func(JSContext* cx, unsigned argc, JS::Value* vp) {
    GJS_GET_THIS(cx, argc, vp, rec, obj);

    if (argc > 1) {
        gjs_throw(cx, "ImageSurface.getFormat() takes no arguments");
        return false;
    }

    cairo_surface_t* surface = CairoSurface::for_js(cx, obj);
    if (!surface)
        return false;

    cairo_format_t format = cairo_image_surface_get_format(surface);

    if (!gjs_cairo_check_status(cx, cairo_surface_status(surface), "surface"))
        return false;

    rec.rval().setInt32(format);
    return true;
}

GJS_JSAPI_RETURN_CONVENTION
static bool getWidth_func(JSContext* cx, unsigned argc, JS::Value* vp) {
    GJS_GET_THIS(cx, argc, vp, rec, obj);

    if (argc > 1) {
        gjs_throw(cx, "ImageSurface.getWidth() takes no arguments");
        return false;
    }

    cairo_surface_t* surface = CairoSurface::for_js(cx, obj);
    if (!surface)
        return false;

    int width = cairo_image_surface_get_width(surface);

    if (!gjs_cairo_check_status(cx, cairo_surface_status(surface), "surface"))
        return false;

    rec.rval().setInt32(width);
    return true;
}

GJS_JSAPI_RETURN_CONVENTION
static bool getHeight_func(JSContext* cx, unsigned argc, JS::Value* vp) {
    GJS_GET_THIS(cx, argc, vp, rec, obj);

    if (argc > 1) {
        gjs_throw(cx, "ImageSurface.getHeight() takes no arguments");
        return false;
    }

    cairo_surface_t* surface = CairoSurface::for_js(cx, obj);
    if (!surface)
        return false;

    int height = cairo_image_surface_get_height(surface);

    if (!gjs_cairo_check_status(cx, cairo_surface_status(surface), "surface"))
        return false;

    rec.rval().setInt32(height);
    return true;
}

GJS_JSAPI_RETURN_CONVENTION
static bool getStride_func(JSContext* cx, unsigned argc, JS::Value* vp) {
    GJS_GET_THIS(cx, argc, vp, rec, obj);

    if (argc > 1) {
        gjs_throw(cx, "ImageSurface.getStride() takes no arguments");
        return false;
    }

    cairo_surface_t* surface = CairoSurface::for_js(cx, obj);
    if (!surface)
        return false;

    int stride = cairo_image_surface_get_stride(surface);

    if (!gjs_cairo_check_status(cx, cairo_surface_status(surface), "surface"))
        return false;

    rec.rval().setInt32(stride);
    return true;
}

const JSFunctionSpec CairoImageSurface::proto_funcs[] = {
    JS_FN("createFromPNG", createFromPNG_func, 0, 0),
    // getData
    JS_FN("getFormat", getFormat_func, 0, 0),
    JS_FN("getWidth", getWidth_func, 0, 0),
    JS_FN("getHeight", getHeight_func, 0, 0),
    JS_FN("getStride", getStride_func, 0, 0), JS_FS_END};

const JSFunctionSpec CairoImageSurface::static_funcs[] = {
    JS_FN("createFromPNG", createFromPNG_func, 1, GJS_MODULE_PROP_FLAGS),
    JS_FS_END};