File: font.c

package info (click to toggle)
deepin-terminal 3.2.1.1%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,764 kB
  • sloc: ansic: 128; exp: 25; sh: 18; makefile: 15
file content (210 lines) | stat: -rw-r--r-- 6,391 bytes parent folder | download | duplicates (2)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* -*- Mode: C; indent-tabs-mode: nil; tab-width: 4 -*-
 * -*- coding: utf-8 -*-
 *
 * Copyright (C) 2011 ~ 2018 Deepin, Inc.
 *               2011 ~ 2018 Wang Yong
 *
 * Author:     Wang Yong <wangyong@deepin.com>
 * Maintainer: Wang Yong <wangyong@deepin.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 3 of the License, or
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include <fontconfig/fontconfig.h>
#include <fontconfig/fcfreetype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <glib.h>

static int string_rematch(const char* string,const char* pattern) {
    int ret = 0;
    regex_t regex;
    regcomp(&regex, pattern, REG_ICASE);
    if (regexec(&regex, string, 0, NULL, 0) == REG_NOERROR)
        ret = 1;
    regfree(&regex);
    return ret;
}

gchar** list_mono_or_dot_fonts(gint* num, int* result_length) {
    FcInit();

    FcPattern *pat = FcPatternCreate();
    if (!pat) {
        fprintf(stderr, "Create FcPattern Failed\n");
        return NULL;
    }

    FcObjectSet *os = FcObjectSetBuild(
        FC_FAMILY,
        FC_FAMILYLANG,
        FC_FULLNAME,
        FC_FULLNAMELANG,
        FC_STYLE,
        FC_FILE,
        FC_LANG,
        FC_SPACING,
        FC_CHARSET,
        NULL);
    if (!os) {
        fprintf(stderr, "Build FcObjectSet Failed\n");
        FcPatternDestroy(pat);
        return NULL;
    }

    FcFontSet *fs = FcFontList(0, pat, os);
    FcObjectSetDestroy(os);
    FcPatternDestroy(pat);
    if (!fs) {
        fprintf(stderr, "List Font Failed\n");
        return NULL;
    }

    /* Read family name of mono font. */
    gchar** fonts = NULL;
    int j;
    int count = 0;
    for (j = 0; j < fs->nfont; j++) {
        /* printf("family: %s\n familylang: %s\n fullname: %s\n fullnamelang: %s\n style: %s\n file: %s\n lang: %s\n spacing: %s\n charset: %s\n", */
        /*       FcPatternFormat(fs->fonts[j], (FcChar8*)"%{family}"), */
        /*       FcPatternFormat(fs->fonts[j], (FcChar8*)"%{familylang}"), */
        /*       FcPatternFormat(fs->fonts[j], (FcChar8*)"%{fullname}"), */
        /*       FcPatternFormat(fs->fonts[j], (FcChar8*)"%{fullnamelang}"), */
        /*       FcPatternFormat(fs->fonts[j], (FcChar8*)"%{style}"), */
        /*       FcPatternFormat(fs->fonts[j], (FcChar8*)"%{file}"), */
        /*       FcPatternFormat(fs->fonts[j], (FcChar8*)"%{lang}"), */
        /*       FcPatternFormat(fs->fonts[j], (FcChar8*)"%{spacing}"), */
        /*       FcPatternFormat(fs->fonts[j], (FcChar8*)"%{charset}") */
        /*       ); */

    char *font_family = (char*) FcPatternFormat(fs->fonts[j], (FcChar8*)"%{family}");
    char *comma = NULL;

    // split with ',' and using last one
    while ((comma = strchr(font_family, ',')) != NULL)
        font_family = comma + 1;

        /* spacing 100 is mono font, spacing 110 is dot font */
        if (strcmp((char*) FcPatternFormat(fs->fonts[j], (FcChar8*)"%{spacing}"), "100") == 0
            || strcmp((char*) FcPatternFormat(fs->fonts[j], (FcChar8*)"%{spacing}"), "110") == 0
            || string_rematch(font_family, "mono")
            || strcmp(font_family, "YaHei Consolas Hybrid") == 0
            || strcmp(font_family, "Monaco") == 0
            ) {
            /* Realloc was realloc(fonts, 0), and you have to take space for <char *> */
            fonts = realloc(fonts, (count + 1) * sizeof(gchar*));
            if (fonts == NULL) {
                fprintf(stderr, "Alloc memory at append %d font info failed\n", count + 1);
                return NULL;
            }

            /* Filter charset font */
            char *charset = (char*)FcPatternFormat(fs->fonts[j], (FcChar8*)"%{charset}");
            if (charset == NULL || strlen(charset) == 0) {
                free(charset);
                continue;
            }
            free(charset);

            /* Got font name */
            gchar* font = g_strdup(font_family);

            /* Need space for store font */
            fonts[count] = malloc((strlen(font) + 1) * sizeof(gchar));
            if (fonts[count] == NULL) {
                fprintf(stderr, "Malloc %d failed\n", count + 1);
                return NULL;
            }

            strcpy(fonts[count], font);

            free(font);

            count++;
        }
    }

    /* Remove duplicate font family. */
    int i, k;
    for (i = 0; i < count; i++) {
        for (j = i + 1; j < count;) {
            if (strcmp(fonts[j], fonts[i]) == 0) {
                for (k = j; k < count; k++) {
                    fonts[k] = fonts[k + 1];
                }
                count--;
            } else
              j++;
        }
    }
    *num = count;
    *result_length = count;

    FcFontSetDestroy(fs);

    return fonts;
}

gchar* font_match(gchar* family) {
     FcPattern* pat = FcNameParse((FcChar8*)family);
     if (!pat) {
         return NULL;
     }

     FcConfigSubstitute(NULL, pat, FcMatchPattern);
     FcDefaultSubstitute(pat);

     FcResult result;
     FcPattern* match = FcFontMatch(NULL, pat, &result);
     FcPatternDestroy(pat);
     if (!match) {
         return NULL;
     }

     FcFontSet* fs = FcFontSetCreate();
     if (!fs) {
         FcPatternDestroy(match);
         return NULL;
     }

     FcFontSetAdd(fs, match);
     FcPattern* font = FcPatternFilter(fs->fonts[0], NULL);
     FcChar8* ret = FcPatternFormat(font, (const FcChar8*)"%{family}");

     FcPatternDestroy(font);
     FcFontSetDestroy(fs);
     FcPatternDestroy(match);

     if (!ret) {
         return NULL;
     }

     return (gchar*)ret;
}

/* void main(int argc, char *argv[]) {
    gint num = 0;
    int font_num = 0;
    gchar** fonts = list_mono_or_dot_fonts(&num, &font_num);

    int i;
    for (i = 0; i < font_num; i++) {
        printf("%s\n", fonts[i]);
    }

    printf("\n");
    printf("%s", font_match("Mono"));
} */