Mercurial > mplayer.hg
annotate libass/ass_fontconfig.c @ 21563:c08b8066a1c6
Only increase buffer size to avoid crashes when seeking in
multi-resolution video files.
author | reimar |
---|---|
date | Sun, 10 Dec 2006 18:59:28 +0000 |
parents | 62bd8e0d3a0f |
children | 62989854d340 |
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 | |
57 * \return font file path | |
58 */ | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
59 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
|
60 FcCharSet* charset) |
18937 | 61 { |
62 FcBool rc; | |
63 FcResult result; | |
64 FcPattern *pat, *rpat; | |
65 int val_i; | |
66 FcChar8* val_s; | |
19001 | 67 FcBool val_b; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
68 FcCharSet* val_cs; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
69 FcFontSet* fset; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
70 int curf, bestf, bestdiff = 0; |
18937 | 71 |
72 *index = 0; | |
73 | |
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
|
74 pat = FcPatternCreate(); |
18937 | 75 if (!pat) |
76 return 0; | |
77 | |
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
|
78 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
|
79 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
|
80 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
|
81 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
|
82 |
18937 | 83 FcDefaultSubstitute(pat); |
84 | |
85 rc = FcConfigSubstitute(priv->config, pat, FcMatchPattern); | |
86 if (!rc) | |
87 return 0; | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
88 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
89 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
|
90 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
91 bestf = -1; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
92 if (charset) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
93 bestdiff = FcCharSetCount(charset) + 1; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
94 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
|
95 rpat = fset->fonts[curf]; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
96 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
97 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
|
98 if (result != FcResultMatch) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
99 continue; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
100 if (val_b != FcTrue) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
101 continue; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
102 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
103 if (charset) { |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
104 int diff; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
105 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
|
106 if (result != FcResultMatch) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
107 continue; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
108 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
|
109 if (diff < bestdiff) { |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
110 bestdiff = diff; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
111 bestf = curf; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
112 } |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
113 if (diff == 0) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
114 break; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
115 } else { |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
116 bestf = curf; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
117 break; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
118 } |
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 if (bestf < 0) |
18937 | 122 return 0; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
123 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
124 rpat = fset->fonts[bestf]; |
19001 | 125 |
18937 | 126 result = FcPatternGetInteger(rpat, FC_INDEX, 0, &val_i); |
127 if (result != FcResultMatch) | |
128 return 0; | |
129 *index = val_i; | |
130 | |
131 result = FcPatternGetString(rpat, FC_FAMILY, 0, &val_s); | |
132 if (result != FcResultMatch) | |
133 return 0; | |
134 | |
135 if (strcasecmp((const char*)val_s, family) != 0) | |
21066 | 136 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_SelectedFontFamilyIsNotTheRequestedOne, |
18937 | 137 (const char*)val_s, family); |
138 | |
139 result = FcPatternGetString(rpat, FC_FILE, 0, &val_s); | |
140 if (result != FcResultMatch) | |
141 return 0; | |
142 | |
143 return strdup((const char*)val_s); | |
144 } | |
145 | |
146 /** | |
147 * \brief Find a font. Use default family or path if necessary. | |
148 * \param priv_ private data | |
149 * \param family font family | |
150 * \param bold font weight value | |
151 * \param italic font slant value | |
152 * \param index out: font index inside a file | |
153 * \return font file path | |
154 */ | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
155 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
|
156 FcCharSet* charset) |
18937 | 157 { |
158 char* res = 0; | |
159 if (family && *family) | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
160 res = _select_font(priv, family, bold, italic, index, charset); |
18937 | 161 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
|
162 res = _select_font(priv, priv->family_default, bold, italic, index, charset); |
21279 | 163 if (res) |
21066 | 164 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFontFamily, |
18937 | 165 family, bold, italic, res, *index); |
166 } | |
167 if (!res && priv->path_default) { | |
168 res = priv->path_default; | |
169 *index = priv->index_default; | |
21279 | 170 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFont, |
171 family, bold, italic, res, *index); | |
18937 | 172 } |
173 if (!res) { | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
174 res = _select_font(priv, "Arial", bold, italic, index, charset); |
21279 | 175 if (res) |
21066 | 176 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingArialFontFamily, |
18937 | 177 family, bold, italic, res, *index); |
178 } | |
179 if (res) | |
20629
e8885ec63928
Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents:
20105
diff
changeset
|
180 mp_msg(MSGT_ASS, MSGL_V, "fontconfig_select: (%s, %d, %d) -> %s, %d\n", |
18937 | 181 family, bold, italic, res, *index); |
182 return res; | |
183 } | |
184 | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
185 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
|
186 { |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
187 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
|
188 } |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
189 |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
190 static char* validate_fname(char* name) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
191 { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
192 char* fname; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
193 char* p; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
194 char* q; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
195 unsigned code; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
196 int sz = strlen(name); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
197 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
198 q = fname = malloc(sz + 1); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
199 p = name; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
200 while (*p) { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
201 code = utf8_get_char(&p); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
202 if (code == 0) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
203 break; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
204 if ( (code > 0x7F) || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
205 (code == '\\') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
206 (code == '/') || |
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 == 0)) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
214 { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
215 *q++ = '_'; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
216 } else { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
217 *q++ = code; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
218 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
219 if (p - name > sz) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
220 break; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
221 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
222 *q = 0; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
223 return fname; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
224 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
225 |
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 * \brief Process embedded matroska font. Saves it to ~/.mplayer/fonts. |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
228 * \param name attachment name |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
229 * \param data binary font data |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
230 * \param data_size data size |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
231 */ |
21460 | 232 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
|
233 { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
234 char buf[1000]; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
235 FILE* fp = 0; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
236 int rc; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
237 struct stat st; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
238 char* fname; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
239 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
|
240 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
|
241 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
|
242 const char* fonts_dir = library->fonts_dir; |
21460 | 243 FT_Face face; |
244 FcPattern* pattern; | |
245 FcFontSet* fset; | |
246 FcBool res; | |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
247 |
21460 | 248 #if (FC_VERSION < 20402) |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
249 if (!fonts_dir) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
250 return; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
251 rc = stat(fonts_dir, &st); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
252 if (rc) { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
253 int res; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
254 #ifndef __MINGW32__ |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
255 res = mkdir(fonts_dir, 0700); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
256 #else |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
257 res = mkdir(fonts_dir); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
258 #endif |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
259 if (res) { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
260 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
|
261 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
262 } 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
|
263 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
|
264 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
265 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
266 fname = validate_fname((char*)name); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
267 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
268 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
|
269 free(fname); |
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 fp = fopen(buf, "wb"); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
272 if (!fp) return; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
273 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
274 fwrite(data, data_size, 1, fp); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
275 fclose(fp); |
21460 | 276 |
277 #else // (FC_VERSION >= 20402) | |
278 | |
279 rc = FT_New_Memory_Face(ftlibrary, data, data_size, 0, &face); | |
280 if (rc) { | |
281 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont, name); | |
282 return; | |
283 } | |
284 | |
285 pattern = FcFreeTypeQueryFace(face, name, 0, FcConfigGetBlanks(priv->config)); | |
286 if (!pattern) { | |
287 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcFreeTypeQueryFace"); | |
288 FT_Done_Face(face); | |
289 return; | |
290 } | |
291 | |
292 fset = FcConfigGetFonts(priv->config, FcSetSystem); // somehow it failes when asked for FcSetApplication | |
293 if (!fset) { | |
294 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcConfigGetFonts"); | |
295 FT_Done_Face(face); | |
296 return; | |
297 } | |
298 | |
299 res = FcFontSetAdd(fset, pattern); | |
300 if (!res) { | |
301 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcFontSetAdd"); | |
302 FT_Done_Face(face); | |
303 return; | |
304 } | |
305 | |
306 FT_Done_Face(face); | |
307 #endif | |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
308 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
309 |
18937 | 310 /** |
311 * \brief Init fontconfig. | |
312 * \param dir additional directoryu for fonts | |
313 * \param family default font family | |
314 * \param path default font path | |
315 * \return pointer to fontconfig private data | |
316 */ | |
21460 | 317 fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path) |
18937 | 318 { |
319 int rc; | |
320 struct stat st; | |
321 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
|
322 const char* dir = library->fonts_dir; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
323 int i; |
18937 | 324 |
325 rc = FcInit(); | |
326 assert(rc); | |
327 | |
328 priv->config = FcConfigGetCurrent(); | |
329 if (!priv->config) { | |
21066 | 330 mp_msg(MSGT_ASS, MSGL_FATAL, MSGTR_LIBASS_FcInitLoadConfigAndFontsFailed); |
18937 | 331 return 0; |
332 } | |
333 | |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
334 for (i = 0; i < library->num_fontdata; ++i) |
21460 | 335 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
|
336 |
19340 | 337 if (FcDirCacheValid((const FcChar8 *)dir) == FcFalse) |
338 { | |
21066 | 339 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
|
340 if (FcGetVersion() >= 20390 && FcGetVersion() < 20400) |
20629
e8885ec63928
Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents:
20105
diff
changeset
|
341 mp_msg(MSGT_ASS, MSGL_WARN, |
21066 | 342 MSGTR_LIBASS_BetaVersionsOfFontconfigAreNotSupported); |
19901
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
343 // 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
|
344 if (FcGetVersion() < 20390) { |
19902 | 345 FcFontSet* fcs; |
346 FcStrSet* fss; | |
347 fcs = FcFontSetCreate(); | |
348 fss = FcStrSetCreate(); | |
349 rc = FcStrSetAdd(fss, (const FcChar8*)dir); | |
350 if (!rc) { | |
21066 | 351 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcStrSetAddFailed); |
19902 | 352 goto ErrorFontCache; |
353 } | |
19340 | 354 |
19902 | 355 rc = FcDirScan(fcs, fss, NULL, FcConfigGetBlanks(priv->config), (const FcChar8 *)dir, FcFalse); |
356 if (!rc) { | |
21066 | 357 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirScanFailed); |
19902 | 358 goto ErrorFontCache; |
359 } | |
19340 | 360 |
19902 | 361 rc = FcDirSave(fcs, fss, (const FcChar8 *)dir); |
362 if (!rc) { | |
21066 | 363 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirSave); |
19902 | 364 goto ErrorFontCache; |
365 } | |
366 ErrorFontCache: | |
367 ; | |
19901
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
368 } |
19340 | 369 } |
370 | |
18937 | 371 rc = FcConfigAppFontAddDir(priv->config, (const FcChar8*)dir); |
372 if (!rc) { | |
21066 | 373 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcConfigAppFontAddDirFailed); |
18937 | 374 } |
375 | |
376 priv->family_default = family ? strdup(family) : 0; | |
377 priv->index_default = 0; | |
378 | |
379 rc = stat(path, &st); | |
380 if (!rc && S_ISREG(st.st_mode)) | |
381 priv->path_default = path ? strdup(path) : 0; | |
382 else | |
383 priv->path_default = 0; | |
384 | |
385 return priv; | |
386 } | |
387 | |
388 #else | |
389 | |
390 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index) | |
391 { | |
392 *index = priv->index_default; | |
393 return priv->path_default; | |
394 } | |
395 | |
21460 | 396 fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path) |
18937 | 397 { |
19481 | 398 fc_instance_t* priv; |
399 | |
21066 | 400 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FontconfigDisabledDefaultFontWillBeUsed); |
18937 | 401 |
19481 | 402 priv = calloc(1, sizeof(fc_instance_t)); |
18937 | 403 |
404 priv->path_default = strdup(path); | |
405 priv->index_default = 0; | |
406 return priv; | |
407 } | |
408 | |
409 #endif | |
410 | |
411 void fontconfig_done(fc_instance_t* priv) | |
412 { | |
413 // don't call FcFini() here, library can still be used by some code | |
414 if (priv && priv->path_default) free(priv->path_default); | |
415 if (priv && priv->family_default) free(priv->family_default); | |
416 if (priv) free(priv); | |
417 } | |
418 | |
419 |