Mercurial > mplayer.hg
annotate libass/ass_fontconfig.c @ 19064:1ca92a0b7703
Cosmetics.
author | eugeni |
---|---|
date | Thu, 13 Jul 2006 18:52:16 +0000 |
parents | 5525af2ee4c7 |
children | f0d49f38ce97 |
rev | line source |
---|---|
18937 | 1 #include "config.h" |
2 | |
3 #include <stdlib.h> | |
4 #include <stdio.h> | |
5 #include <assert.h> | |
6 #include <string.h> | |
7 #include <sys/types.h> | |
8 #include <sys/stat.h> | |
9 | |
10 #include "mp_msg.h" | |
11 #include "ass_fontconfig.h" | |
12 | |
13 #ifdef HAVE_FONTCONFIG | |
14 #include <fontconfig/fontconfig.h> | |
15 #endif | |
16 | |
17 struct fc_instance_s { | |
18 #ifdef HAVE_FONTCONFIG | |
19 FcConfig* config; | |
20 #endif | |
21 char* family_default; | |
22 char* path_default; | |
23 int index_default; | |
24 }; | |
25 | |
26 extern int no_more_font_messages; | |
27 | |
28 #ifdef HAVE_FONTCONFIG | |
29 /** | |
30 * \brief Low-level font selection. | |
31 * \param priv private data | |
32 * \param family font family | |
33 * \param bold font weight value | |
34 * \param italic font slant value | |
35 * \param index out: font index inside a file | |
36 * \return font file path | |
37 */ | |
38 static char* _select_font(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index) | |
39 { | |
40 FcBool rc; | |
41 FcResult result; | |
42 FcPattern *pat, *rpat; | |
43 int val_i; | |
44 FcChar8* val_s; | |
19001 | 45 FcBool val_b; |
18937 | 46 |
47 *index = 0; | |
48 | |
19063
5525af2ee4c7
Use FcPatternAdd-Type instead of FcNameParse. The latter, as it turns out, requires escaping of some characters ('-', maybe more).
eugeni
parents:
19001
diff
changeset
|
49 pat = FcPatternCreate(); |
18937 | 50 if (!pat) |
51 return 0; | |
52 | |
19063
5525af2ee4c7
Use FcPatternAdd-Type instead of FcNameParse. The latter, as it turns out, requires escaping of some characters ('-', maybe more).
eugeni
parents:
19001
diff
changeset
|
53 FcPatternAddString(pat, FC_FAMILY, (const FcChar8*)family); |
5525af2ee4c7
Use FcPatternAdd-Type instead of FcNameParse. The latter, as it turns out, requires escaping of some characters ('-', maybe more).
eugeni
parents:
19001
diff
changeset
|
54 FcPatternAddBool(pat, FC_OUTLINE, FcTrue); |
5525af2ee4c7
Use FcPatternAdd-Type instead of FcNameParse. The latter, as it turns out, requires escaping of some characters ('-', maybe more).
eugeni
parents:
19001
diff
changeset
|
55 FcPatternAddInteger(pat, FC_SLANT, italic); |
5525af2ee4c7
Use FcPatternAdd-Type instead of FcNameParse. The latter, as it turns out, requires escaping of some characters ('-', maybe more).
eugeni
parents:
19001
diff
changeset
|
56 FcPatternAddInteger(pat, FC_WEIGHT, bold); |
5525af2ee4c7
Use FcPatternAdd-Type instead of FcNameParse. The latter, as it turns out, requires escaping of some characters ('-', maybe more).
eugeni
parents:
19001
diff
changeset
|
57 |
18937 | 58 FcDefaultSubstitute(pat); |
59 | |
60 rc = FcConfigSubstitute(priv->config, pat, FcMatchPattern); | |
61 if (!rc) | |
62 return 0; | |
63 | |
64 rpat = FcFontMatch(priv->config, pat, &result); | |
65 if (!rpat) | |
66 return 0; | |
67 | |
19001 | 68 result = FcPatternGetBool(rpat, FC_OUTLINE, 0, &val_b); |
69 if (result != FcResultMatch) | |
70 return 0; | |
19064 | 71 if (val_b != FcTrue) |
19001 | 72 return 0; |
73 | |
18937 | 74 result = FcPatternGetInteger(rpat, FC_INDEX, 0, &val_i); |
75 if (result != FcResultMatch) | |
76 return 0; | |
77 *index = val_i; | |
78 | |
79 result = FcPatternGetString(rpat, FC_FAMILY, 0, &val_s); | |
80 if (result != FcResultMatch) | |
81 return 0; | |
82 | |
83 if (strcasecmp((const char*)val_s, family) != 0) | |
84 mp_msg(MSGT_GLOBAL, MSGL_WARN, "fontconfig: selected font family is not the requested one: '%s' != '%s'\n", | |
85 (const char*)val_s, family); | |
86 | |
87 result = FcPatternGetString(rpat, FC_FILE, 0, &val_s); | |
88 if (result != FcResultMatch) | |
89 return 0; | |
90 | |
91 return strdup((const char*)val_s); | |
92 } | |
93 | |
94 /** | |
95 * \brief Find a font. Use default family or path if necessary. | |
96 * \param priv_ private data | |
97 * \param family font family | |
98 * \param bold font weight value | |
99 * \param italic font slant value | |
100 * \param index out: font index inside a file | |
101 * \return font file path | |
102 */ | |
103 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index) | |
104 { | |
105 char* res = 0; | |
106 if (family && *family) | |
107 res = _select_font(priv, family, bold, italic, index); | |
108 if (!res && priv->family_default) { | |
109 res = _select_font(priv, priv->family_default, bold, italic, index); | |
110 if (res && !no_more_font_messages) | |
111 mp_msg(MSGT_GLOBAL, MSGL_WARN, "fontconfig_select: using default font family: (%s, %d, %d) -> %s, %d\n", | |
112 family, bold, italic, res, *index); | |
113 } | |
114 if (!res && priv->path_default) { | |
115 res = priv->path_default; | |
116 *index = priv->index_default; | |
117 if (!no_more_font_messages) | |
118 mp_msg(MSGT_GLOBAL, MSGL_WARN, "fontconfig_select: using default font: (%s, %d, %d) -> %s, %d\n", | |
119 family, bold, italic, res, *index); | |
120 } | |
121 if (!res) { | |
122 res = _select_font(priv, "Arial", bold, italic, index); | |
123 if (res && !no_more_font_messages) | |
124 mp_msg(MSGT_GLOBAL, MSGL_WARN, "fontconfig_select: using 'Arial' font family: (%s, %d, %d) -> %s, %d\n", | |
125 family, bold, italic, res, *index); | |
126 } | |
127 if (res) | |
128 mp_msg(MSGT_GLOBAL, MSGL_V, "fontconfig_select: (%s, %d, %d) -> %s, %d\n", | |
129 family, bold, italic, res, *index); | |
130 return res; | |
131 } | |
132 | |
133 /** | |
134 * \brief Init fontconfig. | |
135 * \param dir additional directoryu for fonts | |
136 * \param family default font family | |
137 * \param path default font path | |
138 * \return pointer to fontconfig private data | |
139 */ | |
140 fc_instance_t* fontconfig_init(const char* dir, const char* family, const char* path) | |
141 { | |
142 int rc; | |
143 struct stat st; | |
144 fc_instance_t* priv = calloc(1, sizeof(fc_instance_t)); | |
145 | |
146 rc = FcInit(); | |
147 assert(rc); | |
148 | |
149 priv->config = FcConfigGetCurrent(); | |
150 if (!priv->config) { | |
151 mp_msg(MSGT_GLOBAL, MSGL_FATAL, "FcInitLoadConfigAndFonts failed\n"); | |
152 return 0; | |
153 } | |
154 | |
155 rc = FcConfigAppFontAddDir(priv->config, (const FcChar8*)dir); | |
156 if (!rc) { | |
157 mp_msg(MSGT_GLOBAL, MSGL_WARN, "FcConfigAppFontAddDir failed\n"); | |
158 } | |
159 | |
160 priv->family_default = family ? strdup(family) : 0; | |
161 priv->index_default = 0; | |
162 | |
163 rc = stat(path, &st); | |
164 if (!rc && S_ISREG(st.st_mode)) | |
165 priv->path_default = path ? strdup(path) : 0; | |
166 else | |
167 priv->path_default = 0; | |
168 | |
169 return priv; | |
170 } | |
171 | |
172 #else | |
173 | |
174 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index) | |
175 { | |
176 *index = priv->index_default; | |
177 return priv->path_default; | |
178 } | |
179 | |
180 fc_instance_t* fontconfig_init(const char* dir, const char* family, const char* path) | |
181 { | |
182 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Fontconfig disabled, only default font will be used\n"); | |
183 | |
184 fc_instance_t* priv = calloc(1, sizeof(fc_instance_t)); | |
185 | |
186 priv->path_default = strdup(path); | |
187 priv->index_default = 0; | |
188 return priv; | |
189 } | |
190 | |
191 #endif | |
192 | |
193 void fontconfig_done(fc_instance_t* priv) | |
194 { | |
195 // don't call FcFini() here, library can still be used by some code | |
196 if (priv && priv->path_default) free(priv->path_default); | |
197 if (priv && priv->family_default) free(priv->family_default); | |
198 if (priv) free(priv); | |
199 } | |
200 | |
201 |