LCOV - code coverage report
Current view: top level - modules - cairo-image-surface.cpp (source / functions) Coverage Total Hit
Test: gjs-1.89.1 Code Coverage Lines: 50.6 % 85 43
Test Date: 2026-07-06 00:54:07 Functions: 75.0 % 8 6
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 35.7 % 42 15

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

Generated by: LCOV version 2.0-1