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 // SPDX-FileCopyrightText: 2020 Philip Chimento <philip.chimento@gmail.com>
5 
6 #include <config.h>
7 
8 #include <cairo-features.h>  // for CAIRO_HAS_PDF_SURFACE
9 #include <cairo.h>
10 
11 #include <js/TypeDecls.h>
12 
13 #include "gjs/jsapi-util.h"
14 
15 #if CAIRO_HAS_PDF_SURFACE
16 #    include <cairo-pdf.h>
17 
18 #    include <js/PropertyDescriptor.h>  // for JSPROP_READONLY
19 #    include <js/PropertySpec.h>
20 #    include <js/RootingAPI.h>
21 #    include <jsapi.h>  // for JS_NewObjectWithGivenProto
22 #    include <jspubtd.h>  // for JSProtoKey
23 
24 #    include "gjs/jsapi-util-args.h"
25 #    include "modules/cairo-private.h"
26 
27 namespace JS {
28 class CallArgs;
29 }
30 
new_proto(JSContext * cx,JSProtoKey)31 JSObject* CairoPDFSurface::new_proto(JSContext* cx, JSProtoKey) {
32     JS::RootedObject parent_proto(cx, CairoSurface::prototype(cx));
33     return JS_NewObjectWithGivenProto(cx, nullptr, parent_proto);
34 }
35 
constructor_impl(JSContext * context,const JS::CallArgs & argv)36 cairo_surface_t* CairoPDFSurface::constructor_impl(JSContext* context,
37                                                    const JS::CallArgs& argv) {
38     GjsAutoChar filename;
39     double width, height;
40     cairo_surface_t *surface;
41     if (!gjs_parse_call_args(context, "PDFSurface", argv, "Fff",
42                              "filename", &filename,
43                              "width", &width,
44                              "height", &height))
45         return nullptr;
46 
47     surface = cairo_pdf_surface_create(filename, width, height);
48 
49     if (!gjs_cairo_check_status(context, cairo_surface_status(surface),
50                                 "surface"))
51         return nullptr;
52 
53     return surface;
54 }
55 
56 // clang-format off
57 JSPropertySpec gjs_cairo_pdf_surface_proto_props[] = {
58     JS_STRING_SYM_PS(toStringTag, "PDFSurface", JSPROP_READONLY),
59     JS_PS_END};
60 // clang-format on
61 #else
from_c_ptr(JSContext * context,cairo_surface_t * surface)62 JSObject* CairoPDFSurface::from_c_ptr(JSContext* context,
63                                       cairo_surface_t* surface) {
64     gjs_throw(context,
65         "could not create PDF surface, recompile cairo and gjs with "
66         "PDF support.");
67     return nullptr;
68 }
69 #endif /* CAIRO_HAS_PDF_SURFACE */
70