annotate libass/ass_fontconfig.c @ 26592:cc9b88c49d90

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