File: cairoCFerBind_createFont.c

package info (click to toggle)
pyferret 7.6.5-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 138,136 kB
  • sloc: fortran: 240,609; ansic: 25,235; python: 24,026; sh: 1,618; makefile: 1,123; pascal: 569; csh: 307; awk: 18
file content (123 lines) | stat: -rw-r--r-- 4,169 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* Python.h should always be first */
#include <Python.h>
#include <string.h>
#include "grdel.h"
#include "cferbind.h"
#include "cairoCFerBind.h"
#include "FerMem.h"

/* Instantiate the global value */
const char *CCFBFontId = "CCFBFontId";

/*
 * Create a font object for this "Window".
 * The fontsize is in points (1/72")
 *
 * Returns a font object if successful.   If an error occurs,
 * grdelerrmsg is assigned an appropriate error message and NULL
 * is returned.
 */
grdelType cairoCFerBind_createFont(CFerBind *self, const char *familyname, int namelen,
                        double fontsize, int italic, int bold, int underlined)
{
    CairoCFerBindData *instdata;
    double adjfontsize;
    char *family;
    CCFBFont *fontobj;
#ifndef USEPANGOCAIRO
    cairo_font_slant_t slant;
    cairo_font_weight_t weight;
#endif

    /* Sanity check */
    if ( (self->enginename != CairoCFerBindName) &&
         (self->enginename != PyQtCairoCFerBindName) ) {
        strcpy(grdelerrmsg, "cairoCFerBind_createFont: unexpected error, "
                            "self is not a valid CFerBind struct");
        return NULL;
    }
    if ( fontsize <= 0.0 ) {
        strcpy(grdelerrmsg, "cairoCFerBind_createFont: invalid font size given");
        return NULL;
    }
    instdata = (CairoCFerBindData *) self->instancedata;
    /* adjust the font size for Cairo text drawing */
    if ( instdata->imageformat == CCFBIF_PNG ) {
        adjfontsize = fontsize * 96.0 / 72.0;
    }
    else {
        adjfontsize = fontsize * 96.0 / instdata->pixelsperinch;
    }

    if ( familyname == NULL ) {
        strcpy(grdelerrmsg, "cairoCFerBind_createFont: familyname is not given");
        return NULL;
    }
    if ( namelen < 0 ) {
        strcpy(grdelerrmsg, "cairoCFerBind_createFont: invalid familyname length given");
        return NULL;
    }

    fontobj = (CCFBFont *) FerMem_Malloc(sizeof(CCFBFont), __FILE__, __LINE__);
    if ( fontobj == NULL ) {
        strcpy(grdelerrmsg, "cairoCFerBind_createFont: "
                            "out of memory for a CCFBFont structure");
        return NULL;
    }

    family = (char *) FerMem_Malloc(namelen+1, __FILE__, __LINE__);
    if ( family == NULL ) {
        strcpy(grdelerrmsg, "cairoCFerBind_createFont: "
                            "out of memory for a copy of the familyname string");
        FerMem_Free(fontobj, __FILE__, __LINE__);
        return NULL;
    }
    strncpy(family, familyname, namelen);
    family[namelen] = '\0';

#ifdef USEPANGOCAIRO

    fontobj->fontdesc = pango_font_description_new();
    pango_font_description_set_family(fontobj->fontdesc, family);
    if ( italic == 0 )
        pango_font_description_set_style(fontobj->fontdesc, PANGO_STYLE_NORMAL);
    else
        pango_font_description_set_style(fontobj->fontdesc, PANGO_STYLE_ITALIC);
    if ( bold == 0 )
        pango_font_description_set_weight(fontobj->fontdesc, PANGO_WEIGHT_NORMAL);
    else
        pango_font_description_set_weight(fontobj->fontdesc, PANGO_WEIGHT_BOLD);
    pango_font_description_set_variant(fontobj->fontdesc, PANGO_VARIANT_NORMAL);
    pango_font_description_set_stretch(fontobj->fontdesc, PANGO_STRETCH_NORMAL);
    pango_font_description_set_size(fontobj->fontdesc, (int) (adjfontsize * PANGO_SCALE + 0.5));

#else

    if ( italic != 0 )
	    slant = CAIRO_FONT_SLANT_ITALIC;
    else
       slant = CAIRO_FONT_SLANT_NORMAL;
    if ( bold != 0 )
       weight = CAIRO_FONT_WEIGHT_BOLD;
    else
       weight = CAIRO_FONT_WEIGHT_NORMAL;
    fontobj->fontface = cairo_toy_font_face_create(family, slant, weight);
    if ( cairo_font_face_status(fontobj->fontface) != CAIRO_STATUS_SUCCESS ) {
        strcpy(grdelerrmsg, "cairoCFerBind_createFont: "
                            "unable to find a font face for the given font");
        cairo_font_face_destroy(fontobj->fontface);
        FerMem_Free(family, __FILE__, __LINE__);
        FerMem_Free(fontobj, __FILE__, __LINE__);
        return NULL;
    }
    fontobj->fontsize = adjfontsize;

#endif

    FerMem_Free(family, __FILE__, __LINE__);

    fontobj->underline = underlined;
    fontobj->id = CCFBFontId;
    return fontobj;
}