Mercurial > mplayer.hg
annotate libass/ass_fontconfig.c @ 26615:75154771f2f3
Rewrite font family check in a simpler way.
author | eugeni |
---|---|
date | Fri, 02 May 2008 23:32:28 +0000 |
parents | 875a7878626c |
children | c37a85037fc2 |
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> | |
22292 | 29 #include <inttypes.h> |
30 #include <ft2build.h> | |
31 #include FT_FREETYPE_H | |
18937 | 32 |
21026
d138463e820b
Collect all includes of mplayer headers in libass in a single file (mputils.h).
eugeni
parents:
20629
diff
changeset
|
33 #include "mputils.h" |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
34 #include "ass.h" |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
35 #include "ass_library.h" |
18937 | 36 #include "ass_fontconfig.h" |
37 | |
38 #ifdef HAVE_FONTCONFIG | |
39 #include <fontconfig/fontconfig.h> | |
21460 | 40 #include <fontconfig/fcfreetype.h> |
18937 | 41 #endif |
42 | |
43 struct fc_instance_s { | |
44 #ifdef HAVE_FONTCONFIG | |
45 FcConfig* config; | |
46 #endif | |
47 char* family_default; | |
48 char* path_default; | |
49 int index_default; | |
50 }; | |
51 | |
52 #ifdef HAVE_FONTCONFIG | |
53 /** | |
54 * \brief Low-level font selection. | |
55 * \param priv private data | |
56 * \param family font family | |
57 * \param bold font weight value | |
58 * \param italic font slant value | |
59 * \param index out: font index inside a file | |
23980 | 60 * \param code: the character that should be present in the font, can be 0 |
18937 | 61 * \return font file path |
62 */ | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
63 static char* _select_font(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index, |
23980 | 64 uint32_t code) |
18937 | 65 { |
66 FcBool rc; | |
67 FcResult result; | |
23215 | 68 FcPattern *pat = 0, *rpat; |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
69 int r_index; |
26615 | 70 FcChar8 *r_family, *r_style, *r_file, *r_fullname; |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
71 FcBool r_outline; |
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
72 FcCharSet* r_charset; |
23215 | 73 FcFontSet* fset = 0; |
23980 | 74 int curf; |
23215 | 75 char* retval = 0; |
18937 | 76 |
77 *index = 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 pat = FcPatternCreate(); |
18937 | 80 if (!pat) |
23215 | 81 goto error; |
18937 | 82 |
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
|
83 FcPatternAddString(pat, FC_FAMILY, (const FcChar8*)family); |
26591 | 84 |
85 // In SSA/ASS fonts are sometimes referenced by their "full name", | |
86 // which is usually a concatenation of family name and font | |
87 // style (ex. Ottawa Bold). Full name is available from | |
88 // FontConfig pattern element FC_FULLNAME, but it is never | |
89 // used for font matching. | |
90 // Therefore, I'm removing words from the end of the name one | |
91 // by one, and adding shortened names to the pattern. It seems | |
92 // that the first value (full name in this case) has | |
93 // precedence in matching. | |
94 // An alternative approach could be to reimplement FcFontSort | |
95 // using FC_FULLNAME instead of FC_FAMILY. | |
96 if (strchr(family, ' ')) { | |
97 char *p, *s = strdup(family); | |
98 while (p = strrchr(s, ' ')) { | |
99 *p = '\0'; | |
100 FcPatternAddString(pat, FC_FAMILY, (const FcChar8*)s); | |
101 } | |
102 free(s); | |
103 } | |
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
|
104 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
|
105 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
|
106 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
|
107 |
18937 | 108 FcDefaultSubstitute(pat); |
109 | |
110 rc = FcConfigSubstitute(priv->config, pat, FcMatchPattern); | |
111 if (!rc) | |
23215 | 112 goto error; |
21351
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 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
|
115 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
116 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
|
117 rpat = fset->fonts[curf]; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
118 |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
119 result = FcPatternGetBool(rpat, FC_OUTLINE, 0, &r_outline); |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
120 if (result != FcResultMatch) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
121 continue; |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
122 if (r_outline != FcTrue) |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
123 continue; |
23980 | 124 if (!code) |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
125 break; |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
126 result = FcPatternGetCharSet(rpat, FC_CHARSET, 0, &r_charset); |
23980 | 127 if (result != FcResultMatch) |
128 continue; | |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
129 if (FcCharSetHasChar(r_charset, code)) |
23980 | 130 break; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
131 } |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
132 |
23980 | 133 if (curf >= fset->nfont) |
23215 | 134 goto error; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
135 |
23980 | 136 rpat = fset->fonts[curf]; |
19001 | 137 |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
138 result = FcPatternGetInteger(rpat, FC_INDEX, 0, &r_index); |
18937 | 139 if (result != FcResultMatch) |
23215 | 140 goto error; |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
141 *index = r_index; |
18937 | 142 |
26614 | 143 result = FcPatternGetString(rpat, FC_FILE, 0, &r_file); |
144 if (result != FcResultMatch) | |
145 goto error; | |
146 retval = strdup((const char*)r_file); | |
147 | |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
148 result = FcPatternGetString(rpat, FC_FAMILY, 0, &r_family); |
18937 | 149 if (result != FcResultMatch) |
26615 | 150 r_family = NULL; |
18937 | 151 |
26615 | 152 result = FcPatternGetString(rpat, FC_FULLNAME, 0, &r_fullname); |
153 if (result != FcResultMatch) | |
154 r_fullname = NULL; | |
18937 | 155 |
26615 | 156 if (!(r_family && strcasecmp((const char*)r_family, family) == 0) && |
157 !(r_fullname && strcasecmp((const char*)r_fullname, family) == 0)) | |
158 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_SelectedFontFamilyIsNotTheRequestedOne, | |
159 (const char*)(r_family ? r_family : r_fullname), family); | |
23215 | 160 error: |
161 if (pat) FcPatternDestroy(pat); | |
162 if (fset) FcFontSetDestroy(fset); | |
163 return retval; | |
18937 | 164 } |
165 | |
166 /** | |
167 * \brief Find a font. Use default family or path if necessary. | |
168 * \param priv_ private data | |
169 * \param family font family | |
170 * \param bold font weight value | |
171 * \param italic font slant value | |
172 * \param index out: font index inside a file | |
23980 | 173 * \param code: the character that should be present in the font, can be 0 |
18937 | 174 * \return font file path |
175 */ | |
23980 | 176 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index, |
177 uint32_t code) | |
18937 | 178 { |
179 char* res = 0; | |
26582
62ac4f8062ee
Remove libass dependency on global font_fontconfig variable.
eugeni
parents:
25351
diff
changeset
|
180 if (!priv->config) { |
25351 | 181 *index = priv->index_default; |
182 return priv->path_default; | |
183 } | |
18937 | 184 if (family && *family) |
23980 | 185 res = _select_font(priv, family, bold, italic, index, code); |
18937 | 186 if (!res && priv->family_default) { |
23980 | 187 res = _select_font(priv, priv->family_default, bold, italic, index, code); |
21279 | 188 if (res) |
21066 | 189 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFontFamily, |
18937 | 190 family, bold, italic, res, *index); |
191 } | |
192 if (!res && priv->path_default) { | |
193 res = priv->path_default; | |
194 *index = priv->index_default; | |
21279 | 195 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFont, |
196 family, bold, italic, res, *index); | |
18937 | 197 } |
198 if (!res) { | |
23980 | 199 res = _select_font(priv, "Arial", bold, italic, index, code); |
21279 | 200 if (res) |
21066 | 201 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingArialFontFamily, |
18937 | 202 family, bold, italic, res, *index); |
203 } | |
204 if (res) | |
20629
e8885ec63928
Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents:
20105
diff
changeset
|
205 mp_msg(MSGT_ASS, MSGL_V, "fontconfig_select: (%s, %d, %d) -> %s, %d\n", |
18937 | 206 family, bold, italic, res, *index); |
207 return res; | |
208 } | |
209 | |
23216
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
210 #if (FC_VERSION < 20402) |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
211 static char* validate_fname(char* name) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
212 { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
213 char* fname; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
214 char* p; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
215 char* q; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
216 unsigned code; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
217 int sz = strlen(name); |
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 q = fname = malloc(sz + 1); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
220 p = name; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
221 while (*p) { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
222 code = utf8_get_char(&p); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
223 if (code == 0) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
224 break; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
225 if ( (code > 0x7F) || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
226 (code == '\\') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
227 (code == '/') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
228 (code == ':') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
229 (code == '*') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
230 (code == '?') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
231 (code == '<') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
232 (code == '>') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
233 (code == '|') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
234 (code == 0)) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
235 { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
236 *q++ = '_'; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
237 } else { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
238 *q++ = code; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
239 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
240 if (p - name > sz) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
241 break; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
242 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
243 *q = 0; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
244 return fname; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
245 } |
23216
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
246 #endif |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
247 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
248 /** |
21630 | 249 * \brief Process memory font. |
250 * \param priv private data | |
251 * \param library library object | |
252 * \param ftlibrary freetype library object | |
253 * \param idx index of the processed font in library->fontdata | |
254 * With FontConfig >= 2.4.2, builds a font pattern in memory via FT_New_Memory_Face/FcFreeTypeQueryFace. | |
255 * 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
|
256 */ |
21460 | 257 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
|
258 { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
259 int rc; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
260 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
|
261 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
|
262 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
|
263 |
21460 | 264 #if (FC_VERSION < 20402) |
23216
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
265 struct stat st; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
266 char* fname; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
267 const char* fonts_dir = library->fonts_dir; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
268 char buf[1000]; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
269 FILE* fp = 0; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
270 |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
271 if (!fonts_dir) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
272 return; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
273 rc = stat(fonts_dir, &st); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
274 if (rc) { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
275 int res; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
276 #ifndef __MINGW32__ |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
277 res = mkdir(fonts_dir, 0700); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
278 #else |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
279 res = mkdir(fonts_dir); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
280 #endif |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
281 if (res) { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
282 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
|
283 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
284 } 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
|
285 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
|
286 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
287 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
288 fname = validate_fname((char*)name); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
289 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
290 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
|
291 free(fname); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
292 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
293 fp = fopen(buf, "wb"); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
294 if (!fp) return; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
295 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
296 fwrite(data, data_size, 1, fp); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
297 fclose(fp); |
21460 | 298 |
299 #else // (FC_VERSION >= 20402) | |
23216
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
300 FT_Face face; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
301 FcPattern* pattern; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
302 FcFontSet* fset; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
303 FcBool res; |
21460 | 304 |
21615
62989854d340
Avoid "pointer targets differ in signedness" warnings.
eugeni
parents:
21460
diff
changeset
|
305 rc = FT_New_Memory_Face(ftlibrary, (unsigned char*)data, data_size, 0, &face); |
21460 | 306 if (rc) { |
307 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont, name); | |
308 return; | |
309 } | |
310 | |
21615
62989854d340
Avoid "pointer targets differ in signedness" warnings.
eugeni
parents:
21460
diff
changeset
|
311 pattern = FcFreeTypeQueryFace(face, (unsigned char*)name, 0, FcConfigGetBlanks(priv->config)); |
21460 | 312 if (!pattern) { |
313 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcFreeTypeQueryFace"); | |
314 FT_Done_Face(face); | |
315 return; | |
316 } | |
317 | |
318 fset = FcConfigGetFonts(priv->config, FcSetSystem); // somehow it failes when asked for FcSetApplication | |
319 if (!fset) { | |
320 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcConfigGetFonts"); | |
321 FT_Done_Face(face); | |
322 return; | |
323 } | |
324 | |
325 res = FcFontSetAdd(fset, pattern); | |
326 if (!res) { | |
327 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcFontSetAdd"); | |
328 FT_Done_Face(face); | |
329 return; | |
330 } | |
331 | |
332 FT_Done_Face(face); | |
333 #endif | |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
334 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
335 |
18937 | 336 /** |
337 * \brief Init fontconfig. | |
21630 | 338 * \param library libass library object |
339 * \param ftlibrary freetype library object | |
18937 | 340 * \param family default font family |
341 * \param path default font path | |
342 * \return pointer to fontconfig private data | |
343 */ | |
26582
62ac4f8062ee
Remove libass dependency on global font_fontconfig variable.
eugeni
parents:
25351
diff
changeset
|
344 fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path, int fc) |
18937 | 345 { |
346 int rc; | |
347 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
|
348 const char* dir = library->fonts_dir; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
349 int i; |
18937 | 350 |
26582
62ac4f8062ee
Remove libass dependency on global font_fontconfig variable.
eugeni
parents:
25351
diff
changeset
|
351 if (!fc) { |
25351 | 352 mp_msg(MSGT_ASS, MSGL_WARN, |
353 MSGTR_LIBASS_FontconfigDisabledDefaultFontWillBeUsed); | |
26582
62ac4f8062ee
Remove libass dependency on global font_fontconfig variable.
eugeni
parents:
25351
diff
changeset
|
354 priv->config = NULL; |
25351 | 355 priv->path_default = strdup(path); |
356 priv->index_default = 0; | |
357 return priv; | |
358 } | |
359 | |
18937 | 360 rc = FcInit(); |
361 assert(rc); | |
362 | |
363 priv->config = FcConfigGetCurrent(); | |
364 if (!priv->config) { | |
21066 | 365 mp_msg(MSGT_ASS, MSGL_FATAL, MSGTR_LIBASS_FcInitLoadConfigAndFontsFailed); |
18937 | 366 return 0; |
367 } | |
368 | |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
369 for (i = 0; i < library->num_fontdata; ++i) |
21460 | 370 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
|
371 |
19340 | 372 if (FcDirCacheValid((const FcChar8 *)dir) == FcFalse) |
373 { | |
21066 | 374 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
|
375 if (FcGetVersion() >= 20390 && FcGetVersion() < 20400) |
20629
e8885ec63928
Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents:
20105
diff
changeset
|
376 mp_msg(MSGT_ASS, MSGL_WARN, |
21066 | 377 MSGTR_LIBASS_BetaVersionsOfFontconfigAreNotSupported); |
19901
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
378 // 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
|
379 if (FcGetVersion() < 20390) { |
19902 | 380 FcFontSet* fcs; |
381 FcStrSet* fss; | |
382 fcs = FcFontSetCreate(); | |
383 fss = FcStrSetCreate(); | |
384 rc = FcStrSetAdd(fss, (const FcChar8*)dir); | |
385 if (!rc) { | |
21066 | 386 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcStrSetAddFailed); |
19902 | 387 goto ErrorFontCache; |
388 } | |
19340 | 389 |
19902 | 390 rc = FcDirScan(fcs, fss, NULL, FcConfigGetBlanks(priv->config), (const FcChar8 *)dir, FcFalse); |
391 if (!rc) { | |
21066 | 392 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirScanFailed); |
19902 | 393 goto ErrorFontCache; |
394 } | |
19340 | 395 |
19902 | 396 rc = FcDirSave(fcs, fss, (const FcChar8 *)dir); |
397 if (!rc) { | |
21066 | 398 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirSave); |
19902 | 399 goto ErrorFontCache; |
400 } | |
401 ErrorFontCache: | |
402 ; | |
19901
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
403 } |
19340 | 404 } |
405 | |
18937 | 406 rc = FcConfigAppFontAddDir(priv->config, (const FcChar8*)dir); |
407 if (!rc) { | |
21066 | 408 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcConfigAppFontAddDirFailed); |
18937 | 409 } |
410 | |
411 priv->family_default = family ? strdup(family) : 0; | |
22946
e7f1374cac88
Don't check if user-supplied default font path exists.
eugeni
parents:
22292
diff
changeset
|
412 priv->path_default = path ? strdup(path) : 0; |
18937 | 413 priv->index_default = 0; |
414 | |
415 return priv; | |
416 } | |
417 | |
21630 | 418 #else // HAVE_FONTCONFIG |
18937 | 419 |
23988
3b830c7e0eb2
Fix compilation of ass_fontconfig.c when fontconfig is disabled.
iive
parents:
23980
diff
changeset
|
420 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index, |
3b830c7e0eb2
Fix compilation of ass_fontconfig.c when fontconfig is disabled.
iive
parents:
23980
diff
changeset
|
421 uint32_t code) |
18937 | 422 { |
423 *index = priv->index_default; | |
424 return priv->path_default; | |
425 } | |
426 | |
21460 | 427 fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path) |
18937 | 428 { |
19481 | 429 fc_instance_t* priv; |
430 | |
21066 | 431 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FontconfigDisabledDefaultFontWillBeUsed); |
18937 | 432 |
19481 | 433 priv = calloc(1, sizeof(fc_instance_t)); |
18937 | 434 |
435 priv->path_default = strdup(path); | |
436 priv->index_default = 0; | |
437 return priv; | |
438 } | |
439 | |
440 #endif | |
441 | |
442 void fontconfig_done(fc_instance_t* priv) | |
443 { | |
444 // don't call FcFini() here, library can still be used by some code | |
445 if (priv && priv->path_default) free(priv->path_default); | |
446 if (priv && priv->family_default) free(priv->family_default); | |
447 if (priv) free(priv); | |
448 } | |
449 | |
450 |