File: fontselect.c

package info (click to toggle)
s3d 0.2.2-10
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,544 kB
  • ctags: 5,243
  • sloc: ansic: 20,861; python: 488; perl: 98; makefile: 39; sh: 29
file content (108 lines) | stat: -rw-r--r-- 3,615 bytes parent folder | download | duplicates (3)
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
/*
 * fontselect.c
 *
 * Copyright (C) 2004-2011  Simon Wunderlich <sw@simonwunderlich.de>
 *
 * This file is part of the s3d API, the API of s3d (the 3d network display server).
 * See http://s3d.berlios.de/ for more updates.
 *
 * The s3d API is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * The s3d API 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with the s3d API; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */


/*  here we select our truetype font. I'd recommend the fontconfig way,  */
/*  as it always gives the best matching font. a xft version and a win32 */
/*  version would be nice too, to be implemented :) */
#include "s3d.h"
#include "s3dlib.h"
#include <dirent.h>   /*  dirent */
#include <X11/Xlib.h>  /*  Display type, XOpenDisplay(), XCloseDIsplay etc. */
#ifdef WITH_FONTCONFIG
#include "ft2build.h"
#include FT_FREETYPE_H
#include <fontconfig/fontconfig.h>
#endif
#include <stdlib.h>    /*  malloc(), free() */
#include <string.h>    /*  strlen(), strncasecmp(), strncmp() */


#ifdef WITH_FONTCONFIG
char *s3d_findfont(const char *mask)
{
	FcPattern *pattern = NULL, *match = NULL;
	FcChar8 *file = NULL;
	FcResult result;

	pattern = FcNameParse((FcChar8 *)mask);
	FcConfigSubstitute(NULL, pattern, FcMatchPattern);
	FcDefaultSubstitute(pattern);
	s3dprintf(LOW, "Looking for font %s", mask);

	if (!(match = FcFontMatch(NULL, pattern, &result)))
		return NULL;
	if (FcPatternGetString(match, FC_FILE, 0, &file) != FcResultMatch)
		return NULL;
	return (char *)file;
}
#else
/*  this uses the xserver to get a font-path and scan it for ttf-fonts. */
/*  if it matches, give it out ... it's not nice, right, and might not */
/*  work on your place. */
char *s3d_findfont(const char *mask)
{
	char **flist = NULL;
	int fnum = 0;
	char *disp = NULL;
	int n;
	char *fname;
	char *good = NULL;
	struct dirent **namelist;
	Display *dpy;

	dpy = XOpenDisplay(disp);  /*  Open display and check for success */
	if (dpy == NULL)
		errds(VHIGH, "s3d_findfont()", "unable to open display %s", XDisplayName(disp));
	else {
		if (!(flist = XGetFontPath(dpy, &fnum))) {
			errds(VHIGH, "s3d_findfont():XGetFontPath()", "unable to get font path.");
		} else
			while (fnum--) {
				/*  now scan the directories  */
				n =  scandir(flist[fnum], &namelist, 0, alphasort);
				while (n-- > 0) {
					fname = namelist[n]->d_name;
					if (strlen(fname) > (strlen(mask) + 3)) { /*  there should be enough space for the .ttf ending */
						/*  check for the first n characters */
						if (0 == strncasecmp(fname, mask, strlen(mask))) {
							/*  name matches! now check for the end... */
							if (0 == strncasecmp(fname + (strlen(fname) - 3), "ttf", 3)) { /*  check if it has a ttf-ending */
								if (good == NULL)
									good = malloc(256);
								strncpy(good, flist[fnum], 255);
								good[256] = 0;        /* just in case */
								strncat(good, fname, 255 - strlen(good));
								if ((strlen(mask) + 4) == strlen(fname)) {
									return good;
								}
							}
						}
					}
				}
			}
		XCloseDisplay(dpy);
	}
	return good;
}
#endif