LCOV - code coverage report
Current view: top level - src - svg_surface.c (source / functions) Coverage Total Hit
Test: PHP Cairo Extension Coverage Lines: 92.0 % 75 69
Test Date: 2025-09-10 21:28:33 Functions: 100.0 % 7 7

            Line data    Source code
       1              : /*
       2              :   +----------------------------------------------------------------------+
       3              :   | For PHP Version 8                                                    |
       4              :   +----------------------------------------------------------------------+
       5              :   | Copyright (c) 2015 Elizabeth M Smith                                 |
       6              :   +----------------------------------------------------------------------+
       7              :   | http://www.opensource.org/licenses/mit-license.php  MIT License      |
       8              :   | Also available in LICENSE                                            |
       9              :   +----------------------------------------------------------------------+
      10              :   | Authors: Elizabeth M Smith <auroraeosrose@gmail.com>                 |
      11              :   |          Swen Zanon <swen.zanon@geoglis.de>                          |
      12              :   +----------------------------------------------------------------------+
      13              : */
      14              : 
      15              : #ifdef HAVE_CONFIG_H
      16              : #include "config.h"
      17              : #endif
      18              : 
      19              : #include <cairo.h>
      20              : #include <php.h>
      21              : #include <zend_exceptions.h>
      22              : 
      23              : #include "php_cairo.h"
      24              : #include "php_cairo_internal.h"
      25              : 
      26              : #if CAIRO_HAS_SVG_SURFACE
      27              : #include <cairo-svg.h>
      28              : #include "svg_surface_arginfo.h"
      29              : 
      30              : zend_class_entry *ce_cairo_svgsurface;
      31              : zend_class_entry *ce_cairo_svgversion;
      32              : zend_class_entry *ce_cairo_svgunit;
      33              : 
      34              : /* ----------------------------------------------------------------
      35              :     \Cairo\Surface\Svg Class API
      36              : ------------------------------------------------------------------*/
      37              : 
      38              : /* {{{ proto void __construct(string|resource file, float width, float height)
      39              :        Creates a SVG surface of the specified size in points to be written to filename. */
      40           13 : PHP_METHOD(Cairo_Surface_Svg, __construct)
      41              : {
      42           13 :     zval *stream_zval = NULL;
      43              :     stream_closure *closure;
      44           13 :     php_stream *stream = NULL;
      45              :     double width, height;
      46           13 :     bool owned_stream = false;
      47              :     cairo_surface_object *surface_object;
      48              : 
      49           13 :     ZEND_PARSE_PARAMETERS_START(3, 3)
      50            9 :         Z_PARAM_ZVAL(stream_zval)
      51           18 :         Z_PARAM_DOUBLE(width)
      52           16 :         Z_PARAM_DOUBLE(height)
      53           13 :     ZEND_PARSE_PARAMETERS_END();
      54              : 
      55           14 :     surface_object = Z_CAIRO_SURFACE_P(getThis());
      56            7 :     if (!surface_object) {
      57            0 :             RETURN_NULL();
      58              :         }
      59              : 
      60              :     /* special case - a NULL file is like an "in memory" svg */
      61           14 :     if (Z_TYPE_P(stream_zval) == IS_NULL) {
      62            4 :         surface_object->surface = cairo_svg_surface_create(NULL, width, height);
      63              :     }
      64              :     /* Otherwise it can be a filename or a PHP stream */
      65              :     else {
      66            6 :         if (Z_TYPE_P(stream_zval) == IS_STRING) {
      67            1 :             stream = php_stream_open_wrapper(Z_STRVAL_P(stream_zval), "w+b", REPORT_ERRORS, NULL);
      68            1 :             owned_stream = 1;
      69            4 :         } else if (Z_TYPE_P(stream_zval) == IS_RESOURCE) {
      70            1 :             php_stream_from_zval(stream, stream_zval);
      71              :         } else {
      72            1 :             zend_throw_exception(ce_cairo_exception, "Cairo\\Surface\\Svg::__construct() expects parameter 1 to be null, a string, or a stream resource", 0);
      73            1 :             RETURN_THROWS();
      74              :         }
      75              : 
      76              :         /* Pack stream into struct */
      77            2 :         closure = ecalloc(1, sizeof(stream_closure));
      78            2 :         closure->stream = stream;
      79            2 :         closure->owned_stream = owned_stream;
      80              : 
      81            2 :         surface_object->closure = closure;
      82            2 :         surface_object->surface = cairo_svg_surface_create_for_stream(php_cairo_write_func, (void *)closure, width, height);
      83              :     }
      84              : 
      85            6 :     if (php_cairo_throw_exception(cairo_surface_status(surface_object->surface))) {
      86            0 :         RETURN_THROWS();
      87              :     }
      88              : }
      89              : /* }}} */
      90              : 
      91              : /* {{{ proto void \Cairo\Surface\Svg::restrictToVersion(\Cairo\Surface\Svg\Version version)
      92              :        Restricts the generated SVG file to version. This should be called before any drawing takes place on the surface */
      93            4 : PHP_METHOD(Cairo_Surface_Svg, restrictToVersion)
      94              : {
      95              :     cairo_surface_object *surface_object;
      96              :     zval *version;
      97              : 
      98            4 :     ZEND_PARSE_PARAMETERS_START(1, 1)
      99            4 :         Z_PARAM_OBJECT_OF_CLASS(version, ce_cairo_svgversion)
     100            4 :     ZEND_PARSE_PARAMETERS_END();
     101              : 
     102            2 :     surface_object = cairo_surface_object_get(getThis());
     103            1 :     if (!surface_object) {
     104            0 :         RETURN_THROWS();
     105              :     }
     106              : 
     107            1 :     cairo_svg_surface_restrict_to_version(
     108              :         surface_object->surface,
     109            2 :         Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(version)))
     110              :     );
     111              : 
     112            1 :     if (php_cairo_throw_exception(cairo_surface_status(surface_object->surface))) {
     113            0 :         RETURN_THROWS();
     114              :     }
     115              : }
     116              : /* }}} */
     117              : 
     118              : /* {{{ proto string \Cairo\Surface\Svg::versionToString(\Cairo\Surface\Svg\Version version)
     119              :        Get the string representation of the given version id. This function will return NULL if version isn't valid. */
     120            4 : PHP_METHOD(Cairo_Surface_Svg, versionToString)
     121              : {
     122              :     zval *version;
     123              : 
     124            4 :     ZEND_PARSE_PARAMETERS_START(1, 1)
     125            4 :         Z_PARAM_OBJECT_OF_CLASS(version, ce_cairo_svgversion)
     126            4 :     ZEND_PARSE_PARAMETERS_END();
     127              : 
     128            3 :     RETURN_STRING(cairo_svg_version_to_string(Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(version)))));
     129              : }
     130              : /* }}} */
     131              : 
     132              : /* {{{ proto array \Cairo\Surface\Svg::getVersions()
     133              :        Used to retrieve the list of supported versions */
     134            2 : PHP_METHOD(Cairo_Surface_Svg, getVersions)
     135              : {
     136            2 :     const cairo_svg_version_t *versions = 0;
     137            2 :     int version_count = 0, i = 0;
     138              :     zval svg_version_case;
     139              : 
     140            2 :     ZEND_PARSE_PARAMETERS_NONE();
     141              : 
     142            1 :     cairo_svg_get_versions(&versions, &version_count);
     143            1 :     array_init(return_value);
     144              : 
     145            3 :     for (i = 0; i < version_count; i++) {
     146            2 :         svg_version_case = php_enum_from_cairo_c_enum(ce_cairo_svgversion, versions[i]);
     147              :         add_next_index_zval(return_value, &svg_version_case);
     148              :     }
     149              : }
     150              : /* }}} */
     151              : 
     152              : 
     153              : #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 16, 0)
     154              : 
     155              : /* {{{ proto array \Cairo\Surface\Svg::setDocumentUnit(\Cairo\Surface\Svg\Unit unit)
     156              :        ... */
     157            4 : PHP_METHOD(Cairo_Surface_Svg, setDocumentUnit)
     158              : {
     159              :     cairo_surface_object *surface_object;
     160              :     zval *unit;
     161              : 
     162            4 :     ZEND_PARSE_PARAMETERS_START(1, 1)
     163            6 :         Z_PARAM_OBJECT_OF_CLASS(unit, ce_cairo_svgunit)
     164            4 :     ZEND_PARSE_PARAMETERS_END();
     165              : 
     166            2 :     surface_object = cairo_surface_object_get(getThis());
     167            1 :     if (!surface_object) {
     168            0 :         RETURN_THROWS();
     169              :     }
     170              : 
     171            1 :     cairo_svg_surface_set_document_unit(
     172              :         surface_object->surface,
     173            2 :         Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(unit)))
     174              :     );
     175              : }
     176              : /* }}} */
     177              : 
     178              : /* {{{ proto \Cairo\Surface\SVG\Unit \Cairo\Surface\Svg::getDocumentUnit()
     179              :        ... */
     180            3 : PHP_METHOD(Cairo_Surface_Svg, getDocumentUnit)
     181              : {
     182              :     cairo_surface_object *surface_object;
     183              :     zval svg_unit_case;
     184              : 
     185            5 :     ZEND_PARSE_PARAMETERS_NONE();
     186              : 
     187            4 :     surface_object = cairo_surface_object_get(getThis());
     188            2 :     if (!surface_object) {
     189            0 :         RETURN_THROWS();
     190              :     }
     191              : 
     192            2 :     svg_unit_case = php_enum_from_cairo_c_enum(
     193              :         ce_cairo_svgunit,
     194            2 :         cairo_svg_surface_get_document_unit(surface_object->surface)
     195              :     );
     196              : 
     197            2 :     if (Z_TYPE(svg_unit_case) == IS_OBJECT) {
     198            4 :         RETURN_ZVAL(&svg_unit_case, 1, 1);
     199              :     }
     200              : }
     201              : /* }}} */
     202              : #endif
     203              : 
     204              : /* ----------------------------------------------------------------
     205              :     \Cairo\Surface\Svg Definition and registration
     206              : ------------------------------------------------------------------*/
     207              : 
     208              : /* {{{ PHP_MINIT_FUNCTION */
     209          424 : PHP_MINIT_FUNCTION(cairo_svg_surface)
     210              : {
     211              :     /* Svg-Surface */
     212          424 :     ce_cairo_svgsurface = register_class_Cairo_Surface_Svg(ce_cairo_surface);
     213              : 
     214              :     /* Svg-Version */
     215          424 :     ce_cairo_svgversion = register_class_Cairo_Surface_Svg_Version();
     216              : 
     217              :     /* Svg-Unit */
     218          424 :     ce_cairo_svgunit = register_class_Cairo_Surface_Svg_Unit();
     219              : 
     220          424 :     return SUCCESS;
     221              : }
     222              : 
     223              : #endif
        

Generated by: LCOV version 2.0-1