Mercurial > mplayer.hg
annotate libass/ass_fontconfig.c @ 25944:8a1c062707ff
Do not uselessly erase background, OpenGL will take care of drawing everything.
author | reimar |
---|---|
date | Sun, 10 Feb 2008 19:47:54 +0000 |
parents | f46e91c64b39 |
children | 62ac4f8062ee |
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 | |
25351 | 43 extern int font_fontconfig; |
44 | |
18937 | 45 struct fc_instance_s { |
46 #ifdef HAVE_FONTCONFIG | |
47 FcConfig* config; | |
48 #endif | |
49 char* family_default; | |
50 char* path_default; | |
51 int index_default; | |
52 }; | |
53 | |
54 #ifdef HAVE_FONTCONFIG | |
55 /** | |
56 * \brief Low-level font selection. | |
57 * \param priv private data | |
58 * \param family font family | |
59 * \param bold font weight value | |
60 * \param italic font slant value | |
61 * \param index out: font index inside a file | |
23980 | 62 * \param code: the character that should be present in the font, can be 0 |
18937 | 63 * \return font file path |
64 */ | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
65 static char* _select_font(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index, |
23980 | 66 uint32_t code) |
18937 | 67 { |
68 FcBool rc; | |
69 FcResult result; | |
23215 | 70 FcPattern *pat = 0, *rpat; |
18937 | 71 int val_i; |
72 FcChar8* val_s; | |
19001 | 73 FcBool val_b; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
74 FcCharSet* val_cs; |
23215 | 75 FcFontSet* fset = 0; |
23980 | 76 int curf; |
23215 | 77 char* retval = 0; |
18937 | 78 |
79 *index = 0; | |
80 | |
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
|
81 pat = FcPatternCreate(); |
18937 | 82 if (!pat) |
23215 | 83 goto error; |
18937 | 84 |
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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 |
18937 | 90 FcDefaultSubstitute(pat); |
91 | |
92 rc = FcConfigSubstitute(priv->config, pat, FcMatchPattern); | |
93 if (!rc) | |
23215 | 94 goto error; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
95 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
96 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
|
97 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
98 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
|
99 rpat = fset->fonts[curf]; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
100 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
101 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
|
102 if (result != FcResultMatch) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
103 continue; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
104 if (val_b != FcTrue) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
105 continue; |
23980 | 106 if (!code) |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
107 break; |
23980 | 108 result = FcPatternGetCharSet(rpat, FC_CHARSET, 0, &val_cs); |
109 if (result != FcResultMatch) | |
110 continue; | |
111 if (FcCharSetHasChar(val_cs, code)) | |
112 break; | |
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 |
23980 | 115 if (curf >= fset->nfont) |
23215 | 116 goto error; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
117 |
23980 | 118 rpat = fset->fonts[curf]; |
19001 | 119 |
18937 | 120 result = FcPatternGetInteger(rpat, FC_INDEX, 0, &val_i); |
121 if (result != FcResultMatch) | |
23215 | 122 goto error; |
18937 | 123 *index = val_i; |
124 | |
125 result = FcPatternGetString(rpat, FC_FAMILY, 0, &val_s); | |
126 if (result != FcResultMatch) | |
23215 | 127 goto error; |
18937 | 128 |
129 if (strcasecmp((const char*)val_s, family) != 0) | |
21066 | 130 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_SelectedFontFamilyIsNotTheRequestedOne, |
18937 | 131 (const char*)val_s, family); |
132 | |
133 result = FcPatternGetString(rpat, FC_FILE, 0, &val_s); | |
134 if (result != FcResultMatch) | |
23215 | 135 goto error; |
18937 | 136 |
23215 | 137 retval = strdup((const char*)val_s); |
138 error: | |
139 if (pat) FcPatternDestroy(pat); | |
140 if (fset) FcFontSetDestroy(fset); | |
141 return retval; | |
18937 | 142 } |
143 | |
144 /** | |
145 * \brief Find a font. Use default family or path if necessary. | |
146 * \param priv_ private data | |
147 * \param family font family | |
148 * \param bold font weight value | |
149 * \param italic font slant value | |
150 * \param index out: font index inside a file | |
23980 | 151 * \param code: the character that should be present in the font, can be 0 |
18937 | 152 * \return font file path |
153 */ | |
23980 | 154 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index, |
155 uint32_t code) | |
18937 | 156 { |
157 char* res = 0; | |
25351 | 158 if (font_fontconfig < 0) { |
159 *index = priv->index_default; | |
160 return priv->path_default; | |
161 } | |
18937 | 162 if (family && *family) |
23980 | 163 res = _select_font(priv, family, bold, italic, index, code); |
18937 | 164 if (!res && priv->family_default) { |
23980 | 165 res = _select_font(priv, priv->family_default, bold, italic, index, code); |
21279 | 166 if (res) |
21066 | 167 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFontFamily, |
18937 | 168 family, bold, italic, res, *index); |
169 } | |
170 if (!res && priv->path_default) { | |
171 res = priv->path_default; | |
172 *index = priv->index_default; | |
21279 | 173 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFont, |
174 family, bold, italic, res, *index); | |
18937 | 175 } |
176 if (!res) { | |
23980 | 177 res = _select_font(priv, "Arial", bold, italic, index, code); |
21279 | 178 if (res) |
21066 | 179 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingArialFontFamily, |
18937 | 180 family, bold, italic, res, *index); |
181 } | |
182 if (res) | |
20629
e8885ec63928
Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents:
20105
diff
changeset
|
183 mp_msg(MSGT_ASS, MSGL_V, "fontconfig_select: (%s, %d, %d) -> %s, %d\n", |
18937 | 184 family, bold, italic, res, *index); |
185 return res; | |
186 } | |
187 | |
23216
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
188 #if (FC_VERSION < 20402) |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
189 static char* validate_fname(char* name) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
190 { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
191 char* fname; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
192 char* p; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
193 char* q; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
194 unsigned code; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
195 int sz = strlen(name); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
196 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
197 q = fname = malloc(sz + 1); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
198 p = name; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
199 while (*p) { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
200 code = utf8_get_char(&p); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
201 if (code == 0) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
202 break; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
203 if ( (code > 0x7F) || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
204 (code == '\\') || |
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 == 0)) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
213 { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
214 *q++ = '_'; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
215 } else { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
216 *q++ = code; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
217 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
218 if (p - name > sz) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
219 break; |
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 *q = 0; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
222 return fname; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
223 } |
23216
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
224 #endif |
21458
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 /** |
21630 | 227 * \brief Process memory font. |
228 * \param priv private data | |
229 * \param library library object | |
230 * \param ftlibrary freetype library object | |
231 * \param idx index of the processed font in library->fontdata | |
232 * With FontConfig >= 2.4.2, builds a font pattern in memory via FT_New_Memory_Face/FcFreeTypeQueryFace. | |
233 * 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
|
234 */ |
21460 | 235 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
|
236 { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
237 int rc; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
238 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
|
239 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
|
240 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
|
241 |
21460 | 242 #if (FC_VERSION < 20402) |
23216
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
243 struct stat st; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
244 char* fname; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
245 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
|
246 char buf[1000]; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
247 FILE* fp = 0; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
248 |
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) | |
23216
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
278 FT_Face face; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
279 FcPattern* pattern; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
280 FcFontSet* fset; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
281 FcBool res; |
21460 | 282 |
21615
62989854d340
Avoid "pointer targets differ in signedness" warnings.
eugeni
parents:
21460
diff
changeset
|
283 rc = FT_New_Memory_Face(ftlibrary, (unsigned char*)data, data_size, 0, &face); |
21460 | 284 if (rc) { |
285 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont, name); | |
286 return; | |
287 } | |
288 | |
21615
62989854d340
Avoid "pointer targets differ in signedness" warnings.
eugeni
parents:
21460
diff
changeset
|
289 pattern = FcFreeTypeQueryFace(face, (unsigned char*)name, 0, FcConfigGetBlanks(priv->config)); |
21460 | 290 if (!pattern) { |
291 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcFreeTypeQueryFace"); | |
292 FT_Done_Face(face); | |
293 return; | |
294 } | |
295 | |
296 fset = FcConfigGetFonts(priv->config, FcSetSystem); // somehow it failes when asked for FcSetApplication | |
297 if (!fset) { | |
298 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcConfigGetFonts"); | |
299 FT_Done_Face(face); | |
300 return; | |
301 } | |
302 | |
303 res = FcFontSetAdd(fset, pattern); | |
304 if (!res) { | |
305 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcFontSetAdd"); | |
306 FT_Done_Face(face); | |
307 return; | |
308 } | |
309 | |
310 FT_Done_Face(face); | |
311 #endif | |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
312 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
313 |
18937 | 314 /** |
315 * \brief Init fontconfig. | |
21630 | 316 * \param library libass library object |
317 * \param ftlibrary freetype library object | |
18937 | 318 * \param family default font family |
319 * \param path default font path | |
320 * \return pointer to fontconfig private data | |
321 */ | |
21460 | 322 fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path) |
18937 | 323 { |
324 int rc; | |
325 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
|
326 const char* dir = library->fonts_dir; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
327 int i; |
18937 | 328 |
25351 | 329 if (font_fontconfig < 0) { |
330 mp_msg(MSGT_ASS, MSGL_WARN, | |
331 MSGTR_LIBASS_FontconfigDisabledDefaultFontWillBeUsed); | |
332 priv->path_default = strdup(path); | |
333 priv->index_default = 0; | |
334 return priv; | |
335 } | |
336 | |
18937 | 337 rc = FcInit(); |
338 assert(rc); | |
339 | |
340 priv->config = FcConfigGetCurrent(); | |
341 if (!priv->config) { | |
21066 | 342 mp_msg(MSGT_ASS, MSGL_FATAL, MSGTR_LIBASS_FcInitLoadConfigAndFontsFailed); |
18937 | 343 return 0; |
344 } | |
345 | |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
346 for (i = 0; i < library->num_fontdata; ++i) |
21460 | 347 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
|
348 |
19340 | 349 if (FcDirCacheValid((const FcChar8 *)dir) == FcFalse) |
350 { | |
21066 | 351 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
|
352 if (FcGetVersion() >= 20390 && FcGetVersion() < 20400) |
20629
e8885ec63928
Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents:
20105
diff
changeset
|
353 mp_msg(MSGT_ASS, MSGL_WARN, |
21066 | 354 MSGTR_LIBASS_BetaVersionsOfFontconfigAreNotSupported); |
19901
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
355 // 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
|
356 if (FcGetVersion() < 20390) { |
19902 | 357 FcFontSet* fcs; |
358 FcStrSet* fss; | |
359 fcs = FcFontSetCreate(); | |
360 fss = FcStrSetCreate(); | |
361 rc = FcStrSetAdd(fss, (const FcChar8*)dir); | |
362 if (!rc) { | |
21066 | 363 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcStrSetAddFailed); |
19902 | 364 goto ErrorFontCache; |
365 } | |
19340 | 366 |
19902 | 367 rc = FcDirScan(fcs, fss, NULL, FcConfigGetBlanks(priv->config), (const FcChar8 *)dir, FcFalse); |
368 if (!rc) { | |
21066 | 369 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirScanFailed); |
19902 | 370 goto ErrorFontCache; |
371 } | |
19340 | 372 |
19902 | 373 rc = FcDirSave(fcs, fss, (const FcChar8 *)dir); |
374 if (!rc) { | |
21066 | 375 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirSave); |
19902 | 376 goto ErrorFontCache; |
377 } | |
378 ErrorFontCache: | |
379 ; | |
19901
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
380 } |
19340 | 381 } |
382 | |
18937 | 383 rc = FcConfigAppFontAddDir(priv->config, (const FcChar8*)dir); |
384 if (!rc) { | |
21066 | 385 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcConfigAppFontAddDirFailed); |
18937 | 386 } |
387 | |
388 priv->family_default = family ? strdup(family) : 0; | |
22946
e7f1374cac88
Don't check if user-supplied default font path exists.
eugeni
parents:
22292
diff
changeset
|
389 priv->path_default = path ? strdup(path) : 0; |
18937 | 390 priv->index_default = 0; |
391 | |
392 return priv; | |
393 } | |
394 | |
21630 | 395 #else // HAVE_FONTCONFIG |
18937 | 396 |
23988
3b830c7e0eb2
Fix compilation of ass_fontconfig.c when fontconfig is disabled.
iive
parents:
23980
diff
changeset
|
397 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
|
398 uint32_t code) |
18937 | 399 { |
400 *index = priv->index_default; | |
401 return priv->path_default; | |
402 } | |
403 | |
21460 | 404 fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path) |
18937 | 405 { |
19481 | 406 fc_instance_t* priv; |
407 | |
21066 | 408 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FontconfigDisabledDefaultFontWillBeUsed); |
18937 | 409 |
19481 | 410 priv = calloc(1, sizeof(fc_instance_t)); |
18937 | 411 |
412 priv->path_default = strdup(path); | |
413 priv->index_default = 0; | |
414 return priv; | |
415 } | |
416 | |
417 #endif | |
418 | |
419 void fontconfig_done(fc_instance_t* priv) | |
420 { | |
421 // don't call FcFini() here, library can still be used by some code | |
422 if (priv && priv->path_default) free(priv->path_default); | |
423 if (priv && priv->family_default) free(priv->family_default); | |
424 if (priv) free(priv); | |
425 } | |
426 | |
427 |