LCOV - code coverage report
Current view: top level - src - font_map.c (source / functions) Coverage Total Hit
Test: PHP Pango Extension Coverage Lines: 100.0 % 41 41
Test Date: 2025-10-26 00:52:08 Functions: 100.0 % 7 7

            Line data    Source code
       1              : /*
       2              :   +----------------------------------------------------------------------+
       3              :   | For PHP Version 8.2+                                                 |
       4              :   +----------------------------------------------------------------------+
       5              :   | Copyright (c) The PHP Group                                          |
       6              :   +----------------------------------------------------------------------+
       7              :   | This source file is subject to version 3.01 of the PHP license,      |
       8              :   | that is bundled with this package in the file LICENSE, and is        |
       9              :   | available through the world-wide-web at the following url:           |
      10              :   | http://www.php.net/license/3_01.txt                                  |
      11              :   | If you did not receive a copy of the PHP license and are unable to   |
      12              :   | obtain it through the world-wide-web, please send a note to          |
      13              :   | license@php.net so we can mail you a copy immediately.               |
      14              :   +----------------------------------------------------------------------+
      15              :   | Author: Marcel Bolten <github@marcelbolten.de>                       |
      16              :   +----------------------------------------------------------------------+
      17              : */
      18              : 
      19              : #ifdef HAVE_CONFIG_H
      20              : #include "config.h"
      21              : #endif
      22              : 
      23              : #include "php.h"
      24              : #include "php_pango.h"
      25              : #include "font_map_arginfo.h"
      26              : 
      27              : #include <string.h>
      28              : #include "zend_exceptions.h"
      29              : 
      30              : zend_class_entry *pango_ce_pango_font_map;
      31              : 
      32              : static zend_object_handlers pango_font_map_object_handlers;
      33              : 
      34           84 : pango_font_map_object *pango_font_map_fetch_object(zend_object *object)
      35              : {
      36           84 :     return (pango_font_map_object *) ((char*)(object) - XtOffsetOf(pango_font_map_object, std));
      37              : }
      38              : 
      39          258 : PHP_PANGO_API zend_class_entry* php_pango_get_font_map_ce()
      40              : {
      41          258 :     return pango_ce_pango_font_map;
      42              : }
      43              : 
      44           15 : PHP_PANGO_API PangoFontMap* pango_font_map_object_get_font_map(zval *zv)
      45              : {
      46           15 :     pango_font_map_object *obj = Z_PANGO_FONT_MAP_P(zv);
      47           15 :     return obj->font_map;
      48              : }
      49              : 
      50              : #if PANGO_VERSION >= PANGO_VERSION_ENCODE(1, 56, 0)
      51              : /* {{{ Loads a font file with one or more fonts into the FontMap. */
      52              : PHP_METHOD(Pango_FontMap, addFontFile)
      53              : {
      54              :     char *filename;
      55              :     size_t filename_len;
      56              :     PangoFontMap* font_map;
      57              :     GError* error = NULL;
      58              :     bool result;
      59              : 
      60              :     ZEND_PARSE_PARAMETERS_START(1, 1)
      61              :         Z_PARAM_STRING(filename, filename_len)
      62              :     ZEND_PARSE_PARAMETERS_END();
      63              : 
      64              :     font_map = pango_font_map_object_get_font_map(getThis());
      65              : 
      66              :     result = pango_font_map_add_font_file(font_map, filename, &error);
      67              :     if (error) {
      68              :         zend_throw_exception_ex(
      69              :             pango_ce_pango_exception,
      70              :             0,
      71              :             "Error adding font file '%s': %s",
      72              :             filename,
      73              :             error->message
      74              :         );
      75              :         g_error_free(error);
      76              :         RETURN_THROWS();
      77              :     }
      78              : 
      79              :     RETURN_BOOL(result);
      80              : }
      81              : /* }}} */
      82              : #endif
      83              : 
      84              : /* {{{ Creates a context connected to font map. */
      85            2 : PHP_METHOD(Pango_FontMap, createContext)
      86              : {
      87              :     PangoFontMap* font_map;
      88              :     zval context_zv;
      89              :     pango_context_object *context_object;
      90              : 
      91            2 :     ZEND_PARSE_PARAMETERS_NONE();
      92              : 
      93            2 :     font_map = pango_font_map_object_get_font_map(getThis());
      94              : 
      95            1 :     object_init_ex(&context_zv, php_pango_get_context_ce());
      96            1 :     context_object = Z_PANGO_CONTEXT_P(&context_zv);
      97            1 :     context_object->context = pango_font_map_create_context(font_map);
      98              : 
      99            2 :     RETURN_ZVAL(&context_zv, 0, 0);
     100              : }
     101              : /* }}} */
     102              : 
     103              : #if PANGO_VERSION >= PANGO_VERSION_ENCODE(1, 46, 0)
     104              : /* {{{ Gets a font family by name. */
     105            8 : PHP_METHOD(Pango_FontMap, getFamily)
     106              : {
     107              :     char *name;
     108              :     size_t name_len;
     109              :     PangoFontMap* font_map;
     110              :     PangoFontFamily *font_family;
     111              :     pango_font_family_object *font_family_object;
     112              : 
     113            8 :     ZEND_PARSE_PARAMETERS_START(1, 1)
     114           12 :         Z_PARAM_STRING(name, name_len)
     115            9 :     ZEND_PARSE_PARAMETERS_END();
     116              : 
     117           10 :     font_map = pango_font_map_object_get_font_map(getThis());
     118              : 
     119            5 :     font_family = pango_font_map_get_family(font_map, name);
     120            5 :     if (!font_family) {
     121            1 :         zend_throw_exception_ex(
     122              :             pango_ce_pango_exception,
     123              :             0,
     124              :             "Font family '%s' not found.",
     125              :             name
     126              :         );
     127            1 :         RETURN_THROWS();
     128              :     }
     129              : 
     130            4 :     object_init_ex(return_value, php_pango_get_font_family_ce());
     131            4 :     font_family_object = Z_PANGO_FONT_FAMILY_P(return_value);
     132            4 :     font_family_object->font_family = g_object_ref(font_family);
     133              : }
     134              : /* }}} */
     135              : #endif
     136              : 
     137              : /* {{{ List all families for a font map. */
     138           10 : PHP_METHOD(Pango_FontMap, listFamilies)
     139              : {
     140              :     PangoFontMap* font_map;
     141              :     PangoFontFamily** families;
     142              :     int num_families;
     143              :     zval family_zv;
     144              :     pango_font_family_object *font_family_object;
     145              : 
     146           10 :     ZEND_PARSE_PARAMETERS_NONE();
     147              : 
     148           18 :     font_map = pango_font_map_object_get_font_map(getThis());
     149              : 
     150            9 :     pango_font_map_list_families(font_map, &families, &num_families);
     151              : 
     152            9 :     array_init(return_value);
     153          261 :     for (int i = 0; i < num_families; i++) {
     154          252 :         object_init_ex(&family_zv, php_pango_get_font_family_ce());
     155          252 :         font_family_object = Z_PANGO_FONT_FAMILY_P(&family_zv);
     156          252 :         font_family_object->font_family = g_object_ref(families[i]);
     157              :         add_next_index_zval(return_value, &family_zv);
     158              :     }
     159              : 
     160            9 :     g_free(families);
     161              : }
     162              : /* }}} */
     163              : 
     164              : 
     165              : /* ----------------------------------------------------------------
     166              :     \Pango\FontMap Object management
     167              : ------------------------------------------------------------------*/
     168              : 
     169              : /* {{{ */
     170              : // static void pango_font_map_free_obj(zend_object *zobj)
     171              : // {
     172              : //     pango_font_map_object *intern = pango_font_map_fetch_object(zobj);
     173              : 
     174              : //     if (!intern) {
     175              : //         return;
     176              : //     }
     177              : 
     178              : //     if (intern->font_map && !intern->is_default) {
     179              : //         g_object_unref(intern->font_map);
     180              : //     }
     181              : 
     182              : //     zend_object_std_dtor(&intern->std);
     183              : // }
     184              : /* }}} */
     185              : 
     186              : /* {{{ */
     187              : // static zend_object* pango_font_map_obj_ctor(zend_class_entry *ce, pango_font_map_object **intern)
     188              : // {
     189              : //     pango_font_map_object *object = ecalloc(1, sizeof(pango_font_map_object) + zend_object_properties_size(ce));
     190              : 
     191              : //     zend_object_std_init(&object->std, ce);
     192              : 
     193              : //     object->std.handlers = &pango_font_map_object_handlers;
     194              : //     *intern = object;
     195              : 
     196              : //     return &object->std;
     197              : // }
     198              : /* }}} */
     199              : 
     200              : /* {{{ */
     201              : // static zend_object* pango_font_map_create_object(zend_class_entry *ce)
     202              : // {
     203              : //     pango_font_map_object *intern = NULL;
     204              : //     zend_object *return_value = pango_font_map_obj_ctor(ce, &intern);
     205              : 
     206              : //     object_properties_init(&intern->std, ce);
     207              : //     return return_value;
     208              : // }
     209              : /* }}} */
     210              : 
     211              : /* {{{ PHP_MINIT_FUNCTION */
     212          194 : PHP_MINIT_FUNCTION(pango_font_map)
     213              : {
     214          194 :     memcpy(
     215              :         &pango_font_map_object_handlers,
     216              :         zend_get_std_object_handlers(),
     217              :         sizeof(zend_object_handlers)
     218              :     );
     219              : 
     220          194 :     pango_font_map_object_handlers.offset = XtOffsetOf(pango_font_map_object, std);
     221              :     // pango_font_map_object_handlers.free_obj = pango_font_map_free_obj;
     222              : 
     223          194 :     pango_ce_pango_font_map = register_class_Pango_FontMap();
     224              :     // pango_ce_pango_font_map->create_object = pango_font_map_create_object;
     225              : 
     226          194 :     return SUCCESS;
     227              : }
     228              : /* }}} */
        

Generated by: LCOV version 2.0-1