LCOV - code coverage report
Current view: top level - src - cairo.c (source / functions) Coverage Total Hit
Test: PHP Cairo Extension Coverage Lines: 95.8 % 96 92
Test Date: 2025-09-10 21:28:33 Functions: 100.0 % 12 12

            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              :   +----------------------------------------------------------------------+
      12              : */
      13              : 
      14              : #ifdef HAVE_CONFIG_H
      15              : #    include "config.h"
      16              : #endif
      17              : 
      18              : #include <php.h>
      19              : #include <zend_exceptions.h>
      20              : #include <ext/standard/info.h>
      21              : #include <cairo.h>
      22              : 
      23              : #include "php_cairo.h"
      24              : #include "php_cairo_internal.h"
      25              : #include "cairo_arginfo.h"
      26              : 
      27              : /* ----------------------------------------------------------------
      28              :     Cairo Namespace
      29              : ------------------------------------------------------------------ */
      30              : 
      31              : #if defined(CAIRO_HAS_FT_FONT) && defined(HAVE_FREETYPE)
      32            1 : const char* php_cairo_get_ft_error(int error) {
      33              : 
      34            1 :     php_cairo_ft_error *current_error = php_cairo_ft_errors;
      35              : 
      36            2 :     while (current_error->err_msg != NULL) {
      37            2 :         if (current_error->err_code == error) {
      38            1 :             return current_error->err_msg;
      39              :         }
      40            1 :         current_error++;
      41              :     }
      42            0 :     return NULL;
      43              : }
      44              : #endif
      45              : 
      46           74 : zval php_enum_from_cairo_c_enum(
      47              :     zend_class_entry *enum_ce,
      48              :     long c_enum_value
      49              : ) {
      50              :     zval php_enum;
      51              :     zval backing_value;
      52              :     zval retval;
      53              : 
      54           74 :     ZVAL_LONG(&backing_value, c_enum_value);
      55              : 
      56              :     zend_call_method_with_1_params(NULL, enum_ce, NULL, "from", &retval, &backing_value);
      57              : 
      58           74 :     if (Z_TYPE(retval) == IS_OBJECT) {
      59           74 :         ZVAL_COPY(&php_enum, &retval);
      60              :     } else {
      61            0 :         ZVAL_NULL(&php_enum);
      62            0 :         zend_throw_exception_ex(ce_cairo_exception, 0,
      63              :             "Failed to create %s enum from value: %ld",
      64            0 :             ZSTR_VAL(enum_ce->name),
      65              :             c_enum_value
      66              :         );
      67              :     }
      68              : 
      69           74 :     zval_ptr_dtor(&retval);
      70           74 :     return php_enum;
      71              : }
      72              : 
      73              : 
      74              : /* {{{ proto int \Cairo\version(void)
      75              :        Returns an integer version number of the cairo library being used */
      76            5 : PHP_FUNCTION(Cairo_version)
      77              : {
      78            5 :     ZEND_PARSE_PARAMETERS_NONE();
      79              : 
      80            3 :     RETURN_LONG(cairo_version());
      81              : }
      82              : /* }}} */
      83              : 
      84              : /* {{{ proto string \Cairo\version_string(void)
      85              :        Returns a string version of the cairo library being used */
      86            4 : PHP_FUNCTION(Cairo_version_string)
      87              : {
      88            4 :     ZEND_PARSE_PARAMETERS_NONE();
      89              : 
      90            4 :     RETURN_STRING(cairo_version_string());
      91              : }
      92              : /* }}} */
      93              : 
      94              : /* {{{ proto int \Cairo::version(void)
      95              :        Returns an integer version number of the cairo library being used */
      96            2 : PHP_METHOD(Cairo_Cairo, version)
      97              : {
      98            2 :     ZEND_PARSE_PARAMETERS_NONE();
      99              : 
     100            1 :     RETURN_LONG(cairo_version());
     101              : }
     102              : /* }}} */
     103              : 
     104              : /* {{{ proto string \Cairo::versionString(void)
     105              :        Returns a string version of the cairo library being used */
     106            2 : PHP_METHOD(Cairo_Cairo, versionString)
     107              : {
     108            2 :     ZEND_PARSE_PARAMETERS_NONE();
     109              : 
     110            2 :     RETURN_STRING(cairo_version_string());
     111              : }
     112              : /* }}} */
     113              : 
     114              : /* {{{ proto array \Cairo::availableSurfaces(void)
     115              :        Returns an array of available Cairo backend surfaces */
     116           17 : PHP_METHOD(Cairo_Cairo, availableSurfaces)
     117              : {
     118           17 :     ZEND_PARSE_PARAMETERS_NONE();
     119              : 
     120           16 :     array_init(return_value);
     121           16 :     add_next_index_string(return_value, "IMAGE");
     122              : 
     123              : #ifdef CAIRO_HAS_PNG_FUNCTIONS
     124           16 :     add_next_index_string(return_value, "PNG");
     125              : #endif
     126              : 
     127              : #ifdef CAIRO_HAS_JPEG_FUNCTIONS
     128           16 :     add_next_index_string(return_value, "JPEG");
     129              : #endif
     130              : 
     131              : #ifdef CAIRO_HAS_PDF_SURFACE
     132           16 :     add_next_index_string(return_value, "PDF");
     133              : #endif
     134              : 
     135              : #ifdef CAIRO_HAS_PS_SURFACE
     136           16 :     add_next_index_string(return_value, "PS");
     137              : #endif
     138              : 
     139              : #ifdef CAIRO_HAS_SVG_SURFACE
     140           16 :     add_next_index_string(return_value, "SVG");
     141              : #endif
     142              : 
     143              : #ifdef CAIRO_HAS_XLIB_SURFACE
     144           16 :     add_next_index_string(return_value, "XLIB");
     145              : #endif
     146              : 
     147              : #ifdef CAIRO_HAS_RECORDING_SURFACE
     148           16 :     add_next_index_string(return_value, "RECORDING");
     149              : #endif
     150              : 
     151              : #ifdef CAIRO_HAS_QUARTZ_SURFACE
     152              :     add_next_index_string(return_value, "QUARTZ");
     153              : #endif
     154              : 
     155              : #ifdef CAIRO_HAS_WIN32_SURFACE
     156              :     add_next_index_string(return_value, "WIN32");
     157              : #endif
     158              : }
     159              : /* }}} */
     160              : 
     161              : /* {{{ proto array \Cairo::availableFonts(void)
     162              :        Returns an array of available Cairo font backends */
     163            2 : PHP_METHOD(Cairo_Cairo, availableFonts)
     164              : {
     165            2 :     ZEND_PARSE_PARAMETERS_NONE();
     166              : 
     167            1 :     array_init(return_value);
     168              : 
     169              : #if defined(CAIRO_HAS_FT_FONT) && defined(HAVE_FREETYPE)
     170            1 :     add_next_index_string(return_value, "FREETYPE");
     171              : #endif
     172              : #ifdef CAIRO_HAS_QUARTZ_FONT
     173              :     add_next_index_string(return_value, "QUARTZ");
     174              : #endif
     175              : #ifdef CAIRO_HAS_WIN32_FONT
     176              :     add_next_index_string(return_value, "WIN32");
     177              : #endif
     178              : #ifdef CAIRO_HAS_USER_FONT
     179            1 :     add_next_index_string(return_value, "USER");
     180              : #endif
     181              : }
     182              : /* }}} */
     183              : 
     184              : 
     185              : /* {{{ PHP_MINIT_FUNCTION */
     186          424 : PHP_MINIT_FUNCTION(cairo)
     187              : {
     188              :     zend_class_entry ce;
     189              :     zend_class_entry *cairo_ce_cairo;
     190              : 
     191          424 :     cairo_ce_cairo = register_class_Cairo_Cairo();
     192              : 
     193              :     /* Namespaced version constants */
     194          424 :     register_cairo_symbols(module_number);
     195              : 
     196          424 :     PHP_MINIT(cairo_pattern)(INIT_FUNC_ARGS_PASSTHRU);
     197          424 :     PHP_MINIT(cairo_rectangle)(INIT_FUNC_ARGS_PASSTHRU);
     198          424 :     PHP_MINIT(cairo_matrix)(INIT_FUNC_ARGS_PASSTHRU);
     199          424 :     PHP_MINIT(cairo_exception)(INIT_FUNC_ARGS_PASSTHRU);
     200          424 :     PHP_MINIT(cairo_region)(INIT_FUNC_ARGS_PASSTHRU);
     201          424 :     PHP_MINIT(cairo_font_face)(INIT_FUNC_ARGS_PASSTHRU);
     202          424 :     PHP_MINIT(cairo_font)(INIT_FUNC_ARGS_PASSTHRU);
     203          424 :     PHP_MINIT(cairo_font_options)(INIT_FUNC_ARGS_PASSTHRU);
     204          424 :     PHP_MINIT(cairo_scaled_font)(INIT_FUNC_ARGS_PASSTHRU);
     205              : #if defined(CAIRO_HAS_FT_FONT) && defined(HAVE_FREETYPE)
     206          424 :     PHP_MINIT(cairo_ft_font)(INIT_FUNC_ARGS_PASSTHRU);
     207              : #endif
     208              : #if defined(CAIRO_HAS_QUARTZ_FONT)
     209              :     PHP_MINIT(cairo_quartz_font)(INIT_FUNC_ARGS_PASSTHRU);
     210              : #endif
     211              : #if defined(CAIRO_HAS_WIN32_FONT) && defined(HAVE_WIN32_FONT)
     212              :     PHP_MINIT(cairo_win32_font)(INIT_FUNC_ARGS_PASSTHRU);
     213              : #endif
     214          424 :     PHP_MINIT(cairo_surface)(INIT_FUNC_ARGS_PASSTHRU);
     215          424 :     PHP_MINIT(cairo_image_surface)(INIT_FUNC_ARGS_PASSTHRU);
     216          424 :     PHP_MINIT(cairo_sub_surface)(INIT_FUNC_ARGS_PASSTHRU);
     217          424 :     PHP_MINIT(cairo_recording_surface)(INIT_FUNC_ARGS_PASSTHRU);
     218          424 :     PHP_MINIT(cairo_pdf_surface)(INIT_FUNC_ARGS_PASSTHRU);
     219          424 :     PHP_MINIT(cairo_svg_surface)(INIT_FUNC_ARGS_PASSTHRU);
     220          424 :     PHP_MINIT(cairo_ps_surface)(INIT_FUNC_ARGS_PASSTHRU);
     221          424 :     PHP_MINIT(cairo_path)(INIT_FUNC_ARGS_PASSTHRU);
     222          424 :     PHP_MINIT(cairo_context)(INIT_FUNC_ARGS_PASSTHRU);
     223          424 :     PHP_MINIT(cairo_glyph)(INIT_FUNC_ARGS_PASSTHRU);
     224          424 :     PHP_MINIT(cairo_text_cluster)(INIT_FUNC_ARGS_PASSTHRU);
     225              : 
     226          424 :     return SUCCESS;
     227              : }
     228              : /* }}} */
     229              : 
     230              : /* {{{ PHP_MSHUTDOWN_FUNCTION */
     231          424 : PHP_MSHUTDOWN_FUNCTION(cairo)
     232              : {
     233              : #if defined(ZEND_DEBUG) && ZEND_DEBUG == 1
     234              :     cairo_debug_reset_static_data();
     235              : #endif
     236              : 
     237          424 :     return SUCCESS;
     238              : }
     239              : 
     240              : /* {{{ PHP_MINFO_FUNCTION */
     241            1 : PHP_MINFO_FUNCTION(cairo)
     242              : {
     243            1 :     php_info_print_table_start();
     244            1 :     php_info_print_table_header(2, "Cairo Graphics Library Bindings", "enabled");
     245            1 :     php_info_print_table_header(1,
     246              : #ifdef COMPILE_DL_CAIRO
     247              :         "compiled as dynamic module"
     248              : #else
     249              :         "compiled as static module"
     250              : #endif
     251              :     );
     252            1 :     php_info_print_table_row(2, "Cairo Library Version", CAIRO_VERSION_STRING);
     253            1 :     php_info_print_table_row(2, "Extension Version", PHP_CAIRO_VERSION);
     254            1 :     php_info_print_table_row(1, "Surface Backends Available");
     255            1 :     php_info_print_table_row(2, "Image Surface", "enabled");
     256            1 :     php_info_print_table_row(2, "PNG Support",
     257              : #ifdef CAIRO_HAS_PNG_FUNCTIONS
     258              :         "enabled"
     259              : #else
     260              :         "disabled"
     261              : #endif
     262              :     );
     263            1 :     php_info_print_table_row(2, "JPEG Support",
     264              : #ifdef CAIRO_HAS_JPEG_FUNCTIONS
     265              :         "enabled"
     266              : #else
     267              :         "disabled"
     268              : #endif
     269              :         );
     270            1 :     php_info_print_table_row(2, "PDF Surface",
     271              : #ifdef CAIRO_HAS_PDF_SURFACE
     272              :         "enabled"
     273              : #else
     274              :         "disabled"
     275              : #endif
     276              :     );
     277            1 :     php_info_print_table_row(2, "PS Surface",
     278              : #ifdef CAIRO_HAS_PS_SURFACE
     279              :         "enabled"
     280              : #else
     281              :         "disabled"
     282              : #endif
     283              :     );
     284            1 :     php_info_print_table_row(2, "Xlib (X11, X.org) Surface",
     285              : #ifdef CAIRO_HAS_XLIB_SURFACE
     286              :         "enabled"
     287              : #else
     288              :         "disabled"
     289              : #endif
     290              :     );
     291            1 :     php_info_print_table_row(2, "Quartz (MacOSX) Surface",
     292              : #ifdef CAIRO_HAS_QUARTZ_SURFACE
     293              :         "enabled"
     294              : #else
     295              :         "disabled"
     296              : #endif
     297              :     );
     298            1 :     php_info_print_table_row(2, "SVG Surface",
     299              : #ifdef CAIRO_HAS_SVG_SURFACE
     300              :         "enabled"
     301              : #else
     302              :         "disabled"
     303              : #endif
     304              :     );
     305            1 :     php_info_print_table_row(2, "Win32 Surface",
     306              : #ifdef CAIRO_HAS_WIN32_SURFACE
     307              :         "enabled"
     308              : #else
     309              :         "disabled"
     310              : #endif
     311              :     );
     312            1 :     php_info_print_table_row(2, "Recording Surface",
     313              : #ifdef CAIRO_HAS_RECORDING_SURFACE
     314              :         "enabled"
     315              : #else
     316              :         "disabled"
     317              : #endif
     318              :     );
     319            1 :     php_info_print_table_row(1, "Font Backends Available");
     320            1 :     php_info_print_table_row(2, "Freetype Fonts",
     321              : #if defined(CAIRO_HAS_FT_FONT) && defined(HAVE_FREETYPE)
     322              :         "enabled"
     323              : #else
     324              :         "disabled"
     325              : #endif
     326              :     );
     327            1 :     php_info_print_table_row(2, "Quartz Fonts",
     328              : #ifdef CAIRO_HAS_QUARTZ_FONT
     329              :         "enabled"
     330              : #else
     331              :         "disabled"
     332              : #endif
     333              :     );
     334            1 :     php_info_print_table_row(2, "Win32 Fonts",
     335              : #ifdef CAIRO_HAS_WIN32_FONT
     336              :         "enabled"
     337              : #else
     338              :         "disabled"
     339              : #endif
     340              :     );
     341            1 :     php_info_print_table_row(2, "User Fonts",
     342              : #ifdef CAIRO_HAS_USER_FONT
     343              :         "enabled"
     344              : #else
     345              :         "disabled"
     346              : #endif
     347              :     );
     348            1 :     php_info_print_table_end();
     349            1 : }
     350              : /* }}} */
     351              : 
     352              : /* {{{ cairo_module_entry */
     353              : zend_module_entry cairo_module_entry = {
     354              :     STANDARD_MODULE_HEADER_EX,
     355              :     NULL,
     356              :     NULL,
     357              :     "cairo",
     358              :     ext_functions,
     359              :     PHP_MINIT(cairo),
     360              :     PHP_MSHUTDOWN(cairo),
     361              :     NULL,
     362              :     NULL,
     363              :     PHP_MINFO(cairo),
     364              :     PHP_CAIRO_VERSION,
     365              :     STANDARD_MODULE_PROPERTIES
     366              : };
     367              : /* }}} */
     368              : 
     369              : #ifdef COMPILE_DL_CAIRO
     370          424 :     ZEND_GET_MODULE(cairo)
     371              : #endif
        

Generated by: LCOV version 2.0-1