File: cfontconfig.cpp

package info (click to toggle)
pcmanx-gtk2 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,856 kB
  • sloc: ansic: 346,484; sh: 11,213; cpp: 10,433; makefile: 281
file content (125 lines) | stat: -rw-r--r-- 3,596 bytes parent folder | download | duplicates (4)
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
124
125
/* -*- coding: utf-8; indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*- */
/* vim:set fileencodings=utf-8 tabstop=4 expandtab shiftwidth=4 softtabstop=4: */
/**
 * Copyright (c) 2011 PCManX Development Group <pcmanx@googlegroups.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
#ifdef __GNUG__
  #pragma implementation "cfontconfig.h"
#endif

#include <X11/Xft/Xft.h>
#include <fontconfig/fontconfig.h>
#include <gdk/gdkx.h>

#include "cfontconfig.h"

struct CFontPack {
    gchar*   name;
    XftFont* font;
    bool     check;
};

CFontConfig::CFontConfig(void)
{
    FcInit();

    FcPattern* pattern = FcPatternCreate();
    FcConfigSubstitute(0, pattern, FcMatchPattern);
    FcDefaultSubstitute(pattern);

    FcFontSet* font_set = FcFontSetCreate();
    FcResult result;
    FcFontSet* patterns = FcFontSort(0, pattern, FcTrue, 0, &result);

    for (int i = 0; i < patterns->nfont; i++) {

        FcPattern* font_pattern = FcFontRenderPrepare(NULL, pattern, patterns->fonts[i]);

        if (font_pattern) {
            FcFontSetAdd(font_set, font_pattern);
        }
    }

    FcFontSetSortDestroy(patterns);
    FcPatternDestroy(pattern);

    for (int i = 0; i < font_set->nfont; i++) {

        FcPattern* font = FcPatternFilter(font_set->fonts[i], NULL);
        FcChar8* family = NULL;

        if (FcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch) {

            CFontPack* pack = new CFontPack();
            pack->name = g_strdup((gchar*)family);
            pack->font = NULL;
            pack->check = false;
            fonts.push_back(pack);
        }

        FcPatternDestroy(font);
    }

    FcFontSetDestroy(font_set);
}

CFontConfig::~CFontConfig(void)
{
    Display *display = gdk_x11_get_default_xdisplay();

    for (vector<CFontPack*>::iterator it = fonts.begin(); it != fonts.end(); it++) {

        XftFontClose(display, (*it)->font);
        g_free((*it)->name);
        delete *it;
    }

    FcFini();
}

CFontConfig* CFontConfig::Instance(void)
{
    static CFontConfig* instance = new CFontConfig();
    return instance;
}

XftFont* CFontConfig::SearchFontFor(FcChar32 ucs4)
{
    Display *display = gdk_x11_get_default_xdisplay();
    gint screen = gdk_x11_get_default_screen();

    for (vector<CFontPack*>::iterator it = fonts.begin(); it != fonts.end(); it++) {

        if ((*it)->check == false) {

            (*it)->font = XftFontOpen(display, screen,
                    FC_FAMILY, FcTypeString, (*it)->name,
                    FC_SIZE, FcTypeDouble, (double) 16,
                    FC_WEIGHT, FcTypeInteger, FC_WEIGHT_MEDIUM,
                    FC_ANTIALIAS, FcTypeBool, FcTrue,
                    XFT_CORE, FcTypeBool, FcFalse,
                    NULL);
            (*it)->check = true;
        }

        if (XftCharExists(display, (*it)->font, ucs4) == FcTrue) {
            return (*it)->font;
        }
    }
    return NULL;
}