Mercurial > mplayer.hg
annotate libass/ass_fontconfig.c @ 22290:24abe3182dfe
Fix nonsense function type
author | uau |
---|---|
date | Thu, 22 Feb 2007 05:39:17 +0000 |
parents | 0d8005d2fe5c |
children | 2f3f27a014ef |
rev | line source |
---|---|
20008
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*- |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
2 // vim:ts=8:sw=8:noet:ai: |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
3 /* |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
4 Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com> |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
5 |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
6 This program is free software; you can redistribute it and/or modify |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
7 it under the terms of the GNU General Public License as published by |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
8 the Free Software Foundation; either version 2 of the License, or |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
9 (at your option) any later version. |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
10 |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
11 This program is distributed in the hope that it will be useful, |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
14 GNU General Public License for more details. |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
15 |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
16 You should have received a copy of the GNU General Public License |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
17 along with this program; if not, write to the Free Software |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
19 */ |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
20 |
18937 | 21 #include "config.h" |
22 | |
23 #include <stdlib.h> | |
24 #include <stdio.h> | |
25 #include <assert.h> | |
26 #include <string.h> | |
27 #include <sys/types.h> | |
28 #include <sys/stat.h> | |
29 | |
21026
d138463e820b
Collect all includes of mplayer headers in libass in a single file (mputils.h).
eugeni
parents:
20629
diff
changeset
|
30 #include "mputils.h" |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
31 #include "ass.h" |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
32 #include "ass_library.h" |
18937 | 33 #include "ass_fontconfig.h" |
34 | |
35 #ifdef HAVE_FONTCONFIG | |
36 #include <fontconfig/fontconfig.h> | |
21460 | 37 #include <fontconfig/fcfreetype.h> |
18937 | 38 #endif |
39 | |
40 struct fc_instance_s { | |
41 #ifdef HAVE_FONTCONFIG | |
42 FcConfig* config; | |
43 #endif | |
44 char* family_default; | |
45 char* path_default; | |
46 int index_default; | |
47 }; | |
48 | |
49 #ifdef HAVE_FONTCONFIG | |
50 /** | |
51 * \brief Low-level font selection. | |
52 * \param priv private data | |
53 * \param family font family | |
54 * \param bold font weight value | |
55 * \param italic font slant value | |
56 * \param index out: font index inside a file | |
21630 | 57 * \param charset: contains the characters that should be present in the font, can be NULL |
18937 | 58 * \return font file path |
59 */ | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
60 static char* _select_font(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index, |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
61 FcCharSet* charset) |
18937 | 62 { |
63 FcBool rc; | |
64 FcResult result; | |
65 FcPattern *pat, *rpat; | |
66 int val_i; | |
67 FcChar8* val_s; | |
19001 | 68 FcBool val_b; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
69 FcCharSet* val_cs; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
70 FcFontSet* fset; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
71 int curf, bestf, bestdiff = 0; |
18937 | 72 |
73 *index = 0; | |
74 | |
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
|
75 pat = FcPatternCreate(); |
18937 | 76 if (!pat) |
77 return 0; | |
78 | |
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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 |
18937 | 84 FcDefaultSubstitute(pat); |
85 | |
86 rc = FcConfigSubstitute(priv->config, pat, FcMatchPattern); | |
87 if (!rc) | |
88 return 0; | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
89 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
90 fset = FcFontSort(priv->config, pat, FcTrue, NULL, &result); |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
91 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
92 bestf = -1; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
93 if (charset) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
94 bestdiff = FcCharSetCount(charset) + 1; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
95 for (curf = 0; curf < fset->nfont; ++curf) { |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
96 rpat = fset->fonts[curf]; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
97 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
98 result = FcPatternGetBool(rpat, FC_OUTLINE, 0, &val_b); |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
99 if (result != FcResultMatch) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
100 continue; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
101 if (val_b != FcTrue) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
102 continue; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
103 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
104 if (charset) { |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
105 int diff; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
106 result = FcPatternGetCharSet(rpat, FC_CHARSET, 0, &val_cs); |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
107 if (result != FcResultMatch) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
108 continue; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
109 diff = FcCharSetSubtractCount(charset, val_cs); |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
110 if (diff < bestdiff) { |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
111 bestdiff = diff; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
112 bestf = curf; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
113 } |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
114 if (diff == 0) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
115 break; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
116 } else { |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
117 bestf = curf; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
118 break; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
119 } |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
120 } |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
121 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
122 if (bestf < 0) |
18937 | 123 return 0; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
124 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
125 rpat = fset->fonts[bestf]; |
19001 | 126 |
18937 | 127 result = FcPatternGetInteger(rpat, FC_INDEX, 0, &val_i); |
128 if (result != FcResultMatch) | |
129 return 0; | |
130 *index = val_i; | |
131 | |
132 result = FcPatternGetString(rpat, FC_FAMILY, 0, &val_s); | |
133 if (result != FcResultMatch) | |
134 return 0; | |
135 | |
136 if (strcasecmp((const char*)val_s, family) != 0) | |
21066 | 137 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_SelectedFontFamilyIsNotTheRequestedOne, |
18937 | 138 (const char*)val_s, family); |
139 | |
140 result = FcPatternGetString(rpat, FC_FILE, 0, &val_s); | |
141 if (result != FcResultMatch) | |
142 return 0; | |
143 | |
144 return strdup((const char*)val_s); | |
145 } | |
146 | |
147 /** | |
148 * \brief Find a font. Use default family or path if necessary. | |
149 * \param priv_ private data | |
150 * \param family font family | |
151 * \param bold font weight value | |
152 * \param italic font slant value | |
153 * \param index out: font index inside a file | |
21630 | 154 * \param charset: contains the characters that should be present in the font, can be NULL |
18937 | 155 * \return font file path |
156 */ | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
157 char* fontconfig_select_with_charset(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index, |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
158 FcCharSet* charset) |
18937 | 159 { |
160 char* res = 0; | |
161 if (family && *family) | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
162 res = _select_font(priv, family, bold, italic, index, charset); |
18937 | 163 if (!res && priv->family_default) { |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
164 res = _select_font(priv, priv->family_default, bold, italic, index, charset); |
21279 | 165 if (res) |
21066 | 166 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFontFamily, |
18937 | 167 family, bold, italic, res, *index); |
168 } | |
169 if (!res && priv->path_default) { | |
170 res = priv->path_default; | |
171 *index = priv->index_default; | |
21279 | 172 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFont, |
173 family, bold, italic, res, *index); | |
18937 | 174 } |
175 if (!res) { | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
176 res = _select_font(priv, "Arial", bold, italic, index, charset); |
21279 | 177 if (res) |
21066 | 178 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingArialFontFamily, |
18937 | 179 family, bold, italic, res, *index); |
180 } | |
181 if (res) | |
20629
e8885ec63928
Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents:
20105
diff
changeset
|
182 mp_msg(MSGT_ASS, MSGL_V, "fontconfig_select: (%s, %d, %d) -> %s, %d\n", |
18937 | 183 family, bold, italic, res, *index); |
184 return res; | |
185 } | |
186 | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
187 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
188 { |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
189 return fontconfig_select_with_charset(priv, family, bold, italic, index, 0); |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
190 } |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
191 |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
192 static char* validate_fname(char* name) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
193 { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
194 char* fname; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
195 char* p; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
196 char* q; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
197 unsigned code; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
198 int sz = strlen(name); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
199 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
200 q = fname = malloc(sz + 1); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
201 p = name; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
202 while (*p) { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
203 code = utf8_get_char(&p); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
204 if (code == 0) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
205 break; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
206 if ( (code > 0x7F) || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
207 (code == '\\') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
208 (code == '/') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
209 (code == ':') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
210 (code == '*') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
211 (code == '?') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
212 (code == '<') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
213 (code == '>') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
214 (code == '|') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
215 (code == 0)) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
216 { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
217 *q++ = '_'; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
218 } else { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
219 *q++ = code; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
220 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
221 if (p - name > sz) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
222 break; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
223 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
224 *q = 0; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
225 return fname; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
226 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
227 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
228 /** |
21630 | 229 * \brief Process memory font. |
230 * \param priv private data | |
231 * \param library library object | |
232 * \param ftlibrary freetype library object | |
233 * \param idx index of the processed font in library->fontdata | |
234 * With FontConfig >= 2.4.2, builds a font pattern in memory via FT_New_Memory_Face/FcFreeTypeQueryFace. | |
235 * With older FontConfig versions, save the font to ~/.mplayer/fonts. | |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
236 */ |
21460 | 237 static void process_fontdata(fc_instance_t* priv, ass_library_t* library, FT_Library ftlibrary, int idx) |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
238 { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
239 char buf[1000]; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
240 FILE* fp = 0; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
241 int rc; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
242 struct stat st; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
243 char* fname; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
244 const char* name = library->fontdata[idx].name; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
245 const char* data = library->fontdata[idx].data; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
246 int data_size = library->fontdata[idx].size; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
247 const char* fonts_dir = library->fonts_dir; |
21460 | 248 FT_Face face; |
249 FcPattern* pattern; | |
250 FcFontSet* fset; | |
251 FcBool res; | |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
252 |
21460 | 253 #if (FC_VERSION < 20402) |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
254 if (!fonts_dir) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
255 return; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
256 rc = stat(fonts_dir, &st); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
257 if (rc) { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
258 int res; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
259 #ifndef __MINGW32__ |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
260 res = mkdir(fonts_dir, 0700); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
261 #else |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
262 res = mkdir(fonts_dir); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
263 #endif |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
264 if (res) { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
265 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FailedToCreateDirectory, fonts_dir); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
266 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
267 } else if (!S_ISDIR(st.st_mode)) { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
268 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NotADirectory, fonts_dir); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
269 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
270 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
271 fname = validate_fname((char*)name); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
272 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
273 snprintf(buf, 1000, "%s/%s", fonts_dir, fname); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
274 free(fname); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
275 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
276 fp = fopen(buf, "wb"); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
277 if (!fp) return; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
278 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
279 fwrite(data, data_size, 1, fp); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
280 fclose(fp); |
21460 | 281 |
282 #else // (FC_VERSION >= 20402) | |
283 | |
21615
62989854d340
Avoid "pointer targets differ in signedness" warnings.
eugeni
parents:
21460
diff
changeset
|
284 rc = FT_New_Memory_Face(ftlibrary, (unsigned char*)data, data_size, 0, &face); |
21460 | 285 if (rc) { |
286 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont, name); | |
287 return; | |
288 } | |
289 | |
21615
62989854d340
Avoid "pointer targets differ in signedness" warnings.
eugeni
parents:
21460
diff
changeset
|
290 pattern = FcFreeTypeQueryFace(face, (unsigned char*)name, 0, FcConfigGetBlanks(priv->config)); |
21460 | 291 if (!pattern) { |
292 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcFreeTypeQueryFace"); | |
293 FT_Done_Face(face); | |
294 return; | |
295 } | |
296 | |
297 fset = FcConfigGetFonts(priv->config, FcSetSystem); // somehow it failes when asked for FcSetApplication | |
298 if (!fset) { | |
299 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcConfigGetFonts"); | |
300 FT_Done_Face(face); | |
301 return; | |
302 } | |
303 | |
304 res = FcFontSetAdd(fset, pattern); | |
305 if (!res) { | |
306 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcFontSetAdd"); | |
307 FT_Done_Face(face); | |
308 return; | |
309 } | |
310 | |
311 FT_Done_Face(face); | |
312 #endif | |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
313 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
314 |
18937 | 315 /** |
316 * \brief Init fontconfig. | |
21630 | 317 * \param library libass library object |
318 * \param ftlibrary freetype library object | |
18937 | 319 * \param family default font family |
320 * \param path default font path | |
321 * \return pointer to fontconfig private data | |
322 */ | |
21460 | 323 fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path) |
18937 | 324 { |
325 int rc; | |
326 struct stat st; | |
327 fc_instance_t* priv = calloc(1, sizeof(fc_instance_t)); | |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
328 const char* dir = library->fonts_dir; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
329 int i; |
18937 | 330 |
331 rc = FcInit(); | |
332 assert(rc); | |
333 | |
334 priv->config = FcConfigGetCurrent(); | |
335 if (!priv->config) { | |
21066 | 336 mp_msg(MSGT_ASS, MSGL_FATAL, MSGTR_LIBASS_FcInitLoadConfigAndFontsFailed); |
18937 | 337 return 0; |
338 } | |
339 | |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
340 for (i = 0; i < library->num_fontdata; ++i) |
21460 | 341 process_fontdata(priv, library, ftlibrary, i); |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
342 |
19340 | 343 if (FcDirCacheValid((const FcChar8 *)dir) == FcFalse) |
344 { | |
21066 | 345 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_UpdatingFontCache); |
20105
bcb586a0800c
Avoid crash with fontconfig 2.3.9x (as shipped with SuSE 10.1, FcDirScan is broken)
reimar
parents:
20008
diff
changeset
|
346 if (FcGetVersion() >= 20390 && FcGetVersion() < 20400) |
20629
e8885ec63928
Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents:
20105
diff
changeset
|
347 mp_msg(MSGT_ASS, MSGL_WARN, |
21066 | 348 MSGTR_LIBASS_BetaVersionsOfFontconfigAreNotSupported); |
19901
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
349 // FontConfig >= 2.4.0 updates cache automatically in FcConfigAppFontAddDir() |
20105
bcb586a0800c
Avoid crash with fontconfig 2.3.9x (as shipped with SuSE 10.1, FcDirScan is broken)
reimar
parents:
20008
diff
changeset
|
350 if (FcGetVersion() < 20390) { |
19902 | 351 FcFontSet* fcs; |
352 FcStrSet* fss; | |
353 fcs = FcFontSetCreate(); | |
354 fss = FcStrSetCreate(); | |
355 rc = FcStrSetAdd(fss, (const FcChar8*)dir); | |
356 if (!rc) { | |
21066 | 357 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcStrSetAddFailed); |
19902 | 358 goto ErrorFontCache; |
359 } | |
19340 | 360 |
19902 | 361 rc = FcDirScan(fcs, fss, NULL, FcConfigGetBlanks(priv->config), (const FcChar8 *)dir, FcFalse); |
362 if (!rc) { | |
21066 | 363 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirScanFailed); |
19902 | 364 goto ErrorFontCache; |
365 } | |
19340 | 366 |
19902 | 367 rc = FcDirSave(fcs, fss, (const FcChar8 *)dir); |
368 if (!rc) { | |
21066 | 369 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirSave); |
19902 | 370 goto ErrorFontCache; |
371 } | |
372 ErrorFontCache: | |
373 ; | |
19901
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
374 } |
19340 | 375 } |
376 | |
18937 | 377 rc = FcConfigAppFontAddDir(priv->config, (const FcChar8*)dir); |
378 if (!rc) { | |
21066 | 379 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcConfigAppFontAddDirFailed); |
18937 | 380 } |
381 | |
382 priv->family_default = family ? strdup(family) : 0; | |
383 priv->index_default = 0; | |
384 | |
385 rc = stat(path, &st); | |
386 if (!rc && S_ISREG(st.st_mode)) | |
387 priv->path_default = path ? strdup(path) : 0; | |
388 else | |
389 priv->path_default = 0; | |
390 | |
391 return priv; | |
392 } | |
393 | |
21630 | 394 #else // HAVE_FONTCONFIG |
18937 | 395 |
396 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index) | |
397 { | |
398 *index = priv->index_default; | |
399 return priv->path_default; | |
400 } | |
401 | |
21460 | 402 fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path) |
18937 | 403 { |
19481 | 404 fc_instance_t* priv; |
405 | |
21066 | 406 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FontconfigDisabledDefaultFontWillBeUsed); |
18937 | 407 |
19481 | 408 priv = calloc(1, sizeof(fc_instance_t)); |
18937 | 409 |
410 priv->path_default = strdup(path); | |
411 priv->index_default = 0; | |
412 return priv; | |
413 } | |
414 | |
415 #endif | |
416 | |
417 void fontconfig_done(fc_instance_t* priv) | |
418 { | |
419 // don't call FcFini() here, library can still be used by some code | |
420 if (priv && priv->path_default) free(priv->path_default); | |
421 if (priv && priv->family_default) free(priv->family_default); | |
422 if (priv) free(priv); | |
423 } | |
424 | |
425 |