annotate libass/ass_fontconfig.c @ 36905:bff7c54aa8f1

Increase arithmetic precision.
author ib
date Wed, 12 Mar 2014 13:24:34 +0000
parents c3aaaf17c721
children
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 /*
26723
0f892cd714b2 Use standard license header.
diego
parents: 26710
diff changeset
2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
0f892cd714b2 Use standard license header.
diego
parents: 26710
diff changeset
3 *
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
4 * This file is part of libass.
26723
0f892cd714b2 Use standard license header.
diego
parents: 26710
diff changeset
5 *
34011
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31875
diff changeset
6 * Permission to use, copy, modify, and distribute this software for any
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31875
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31875
diff changeset
8 * copyright notice and this permission notice appear in all copies.
26723
0f892cd714b2 Use standard license header.
diego
parents: 26710
diff changeset
9 *
34011
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31875
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31875
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31875
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31875
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31875
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31875
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31875
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26723
0f892cd714b2 Use standard license header.
diego
parents: 26710
diff changeset
17 */
20008
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19902
diff changeset
18
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
19 #include "config.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
20
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
21 #include <stdlib.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
22 #include <stdio.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
23 #include <assert.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
24 #include <string.h>
31875
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
25 #include <strings.h>
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
26 #include <sys/types.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
27 #include <sys/stat.h>
22292
2f3f27a014ef Add some missing includes.
eugeni
parents: 21630
diff changeset
28 #include <inttypes.h>
2f3f27a014ef Add some missing includes.
eugeni
parents: 21630
diff changeset
29 #include <ft2build.h>
2f3f27a014ef Add some missing includes.
eugeni
parents: 21630
diff changeset
30 #include FT_FREETYPE_H
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
31
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
32 #include "ass_utils.h"
21458
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 21351
diff changeset
33 #include "ass.h"
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 21351
diff changeset
34 #include "ass_library.h"
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
35 #include "ass_fontconfig.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
36
27393
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 27096
diff changeset
37 #ifdef CONFIG_FONTCONFIG
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
38 #include <fontconfig/fontconfig.h>
21460
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
39 #include <fontconfig/fcfreetype.h>
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
40 #endif
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
41
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
42 struct fc_instance {
27393
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 27096
diff changeset
43 #ifdef CONFIG_FONTCONFIG
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
44 FcConfig *config;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
45 #endif
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
46 char *family_default;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
47 char *path_default;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
48 int index_default;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
49 };
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
50
27393
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 27096
diff changeset
51 #ifdef CONFIG_FONTCONFIG
26660
ab5729095d68 Define FC_FULLNAME and FC_EMBOLDEN to fix compilation with ancient fontconfig.
eugeni
parents: 26659
diff changeset
52
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
53 /**
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
54 * \brief Case-insensitive match ASS/SSA font family against full name. (also
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
55 * known as "name for humans")
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
56 *
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
57 * \param lib library instance
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
58 * \param priv fontconfig instance
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
59 * \param family font fullname
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
60 * \param bold weight attribute
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
61 * \param italic italic attribute
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
62 * \return font set
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
63 */
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
64 static FcFontSet *
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
65 match_fullname(ASS_Library *lib, FCInstance *priv, const char *family,
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
66 unsigned bold, unsigned italic)
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
67 {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
68 FcFontSet *sets[2];
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
69 FcFontSet *result = FcFontSetCreate();
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
70 int nsets = 0;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
71 int i, fi;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
72
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
73 if ((sets[nsets] = FcConfigGetFonts(priv->config, FcSetSystem)))
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
74 nsets++;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
75 if ((sets[nsets] = FcConfigGetFonts(priv->config, FcSetApplication)))
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
76 nsets++;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
77
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
78 // Run over font sets and patterns and try to match against full name
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
79 for (i = 0; i < nsets; i++) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
80 FcFontSet *set = sets[i];
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
81 for (fi = 0; fi < set->nfont; fi++) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
82 FcPattern *pat = set->fonts[fi];
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
83 char *fullname;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
84 int pi = 0, at;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
85 FcBool ol;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
86 while (FcPatternGetString(pat, FC_FULLNAME, pi++,
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
87 (FcChar8 **) &fullname) == FcResultMatch) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
88 if (FcPatternGetBool(pat, FC_OUTLINE, 0, &ol) != FcResultMatch
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
89 || ol != FcTrue)
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
90 continue;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
91 if (FcPatternGetInteger(pat, FC_SLANT, 0, &at) != FcResultMatch
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
92 || at < italic)
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
93 continue;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
94 if (FcPatternGetInteger(pat, FC_WEIGHT, 0, &at) != FcResultMatch
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
95 || at < bold)
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
96 continue;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
97 if (strcasecmp(fullname, family) == 0) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
98 FcFontSetAdd(result, FcPatternDuplicate(pat));
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
99 break;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
100 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
101 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
102 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
103 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
104
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
105 return result;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
106 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
107
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
108 /**
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
109 * \brief Low-level font selection.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
110 * \param priv private data
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
111 * \param family font family
28860
7fcc0bf5b27a Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents: 27842
diff changeset
112 * \param treat_family_as_pattern treat family as fontconfig pattern
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
113 * \param bold font weight value
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
114 * \param italic font slant value
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
115 * \param index out: font index inside a file
23980
27bac14b4ce4 More simple and correct font reselection.
eugeni
parents: 23216
diff changeset
116 * \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
117 * \return font file path
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28942
diff changeset
118 */
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
119 static char *select_font(ASS_Library *library, FCInstance *priv,
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
120 const char *family, int treat_family_as_pattern,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
121 unsigned bold, unsigned italic, int *index,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
122 uint32_t code)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
123 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
124 FcBool rc;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
125 FcResult result;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
126 FcPattern *pat = NULL, *rpat = NULL;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
127 int r_index, r_slant, r_weight;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
128 FcChar8 *r_family, *r_style, *r_file, *r_fullname;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
129 FcBool r_outline, r_embolden;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
130 FcCharSet *r_charset;
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
131 FcFontSet *ffullname = NULL, *fsorted = NULL, *fset = NULL;
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
132 int curf;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
133 char *retval = NULL;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
134 int family_cnt = 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28942
diff changeset
135
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
136 *index = 0;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
137
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
138 if (treat_family_as_pattern)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
139 pat = FcNameParse((const FcChar8 *) family);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
140 else
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
141 pat = FcPatternCreate();
28860
7fcc0bf5b27a Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents: 27842
diff changeset
142
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
143 if (!pat)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
144 goto error;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28942
diff changeset
145
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
146 if (!treat_family_as_pattern) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
147 FcPatternAddString(pat, FC_FAMILY, (const FcChar8 *) family);
26591
0f069e41d8d2 Allow inexact font family matching.
eugeni
parents: 26582
diff changeset
148
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
149 // In SSA/ASS fonts are sometimes referenced by their "full name",
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
150 // which is usually a concatenation of family name and font
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
151 // style (ex. Ottawa Bold). Full name is available from
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
152 // FontConfig pattern element FC_FULLNAME, but it is never
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
153 // used for font matching.
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
154 // Therefore, I'm removing words from the end of the name one
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
155 // by one, and adding shortened names to the pattern. It seems
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
156 // that the first value (full name in this case) has
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
157 // precedence in matching.
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
158 // An alternative approach could be to reimplement FcFontSort
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
159 // using FC_FULLNAME instead of FC_FAMILY.
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
160 family_cnt = 1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
161 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
162 char *s = strdup(family);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
163 char *p = s + strlen(s);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
164 while (--p > s)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
165 if (*p == ' ' || *p == '-') {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
166 *p = '\0';
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
167 FcPatternAddString(pat, FC_FAMILY, (const FcChar8 *) s);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
168 ++family_cnt;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
169 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
170 free(s);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
171 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
172 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
173 FcPatternAddBool(pat, FC_OUTLINE, FcTrue);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
174 FcPatternAddInteger(pat, FC_SLANT, italic);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
175 FcPatternAddInteger(pat, FC_WEIGHT, bold);
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
176
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
177 FcDefaultSubstitute(pat);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28942
diff changeset
178
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
179 rc = FcConfigSubstitute(priv->config, pat, FcMatchPattern);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
180 if (!rc)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
181 goto error;
36363
c3aaaf17c721 Update libass to latest git version.
reimar
parents: 34011
diff changeset
182 /* Fontconfig defaults include a language setting, which it sets based on
c3aaaf17c721 Update libass to latest git version.
reimar
parents: 34011
diff changeset
183 * some environment variables or defaults to "en". Unset this as we don't
c3aaaf17c721 Update libass to latest git version.
reimar
parents: 34011
diff changeset
184 * know the real language, and because some some attached fonts lack
c3aaaf17c721 Update libass to latest git version.
reimar
parents: 34011
diff changeset
185 * non-ascii characters included in fontconfig's list of characters
c3aaaf17c721 Update libass to latest git version.
reimar
parents: 34011
diff changeset
186 * required for English support and therefore don't match the lang=en
c3aaaf17c721 Update libass to latest git version.
reimar
parents: 34011
diff changeset
187 * criterion.
c3aaaf17c721 Update libass to latest git version.
reimar
parents: 34011
diff changeset
188 */
c3aaaf17c721 Update libass to latest git version.
reimar
parents: 34011
diff changeset
189 FcPatternDel(pat, "lang");
21351
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21279
diff changeset
190
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
191 fsorted = FcFontSort(priv->config, pat, FcTrue, NULL, &result);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
192 ffullname = match_fullname(library, priv, family, bold, italic);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
193 if (!fsorted || !ffullname)
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
194 goto error;
21351
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21279
diff changeset
195
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
196 fset = FcFontSetCreate();
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
197 for (curf = 0; curf < ffullname->nfont; ++curf) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
198 FcPattern *curp = ffullname->fonts[curf];
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
199 FcPatternReference(curp);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
200 FcFontSetAdd(fset, curp);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
201 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
202 for (curf = 0; curf < fsorted->nfont; ++curf) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
203 FcPattern *curp = fsorted->fonts[curf];
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
204 FcPatternReference(curp);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
205 FcFontSetAdd(fset, curp);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
206 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
207
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
208 for (curf = 0; curf < fset->nfont; ++curf) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
209 FcPattern *curp = fset->fonts[curf];
26658
1e1ebebc8f5b Remove extra family names from the search pattern after FcFontSort and
eugeni
parents: 26620
diff changeset
210
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
211 result = FcPatternGetBool(curp, FC_OUTLINE, 0, &r_outline);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
212 if (result != FcResultMatch)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
213 continue;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
214 if (r_outline != FcTrue)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
215 continue;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
216 if (!code)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
217 break;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
218 result = FcPatternGetCharSet(curp, FC_CHARSET, 0, &r_charset);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
219 if (result != FcResultMatch)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
220 continue;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
221 if (FcCharSetHasChar(r_charset, code))
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
222 break;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
223 }
21351
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21279
diff changeset
224
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
225 if (curf >= fset->nfont)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
226 goto error;
21351
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21279
diff changeset
227
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
228 if (!treat_family_as_pattern) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
229 // Remove all extra family names from original pattern.
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
230 // After this, FcFontRenderPrepare will select the most relevant family
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
231 // name in case there are more than one of them.
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
232 for (; family_cnt > 1; --family_cnt)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
233 FcPatternRemove(pat, FC_FAMILY, family_cnt - 1);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
234 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
235
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
236 rpat = FcFontRenderPrepare(priv->config, pat, fset->fonts[curf]);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
237 if (!rpat)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
238 goto error;
26658
1e1ebebc8f5b Remove extra family names from the search pattern after FcFontSort and
eugeni
parents: 26620
diff changeset
239
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
240 result = FcPatternGetInteger(rpat, FC_INDEX, 0, &r_index);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
241 if (result != FcResultMatch)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
242 goto error;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
243 *index = r_index;
26658
1e1ebebc8f5b Remove extra family names from the search pattern after FcFontSort and
eugeni
parents: 26620
diff changeset
244
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
245 result = FcPatternGetString(rpat, FC_FILE, 0, &r_file);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
246 if (result != FcResultMatch)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
247 goto error;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
248 retval = strdup((const char *) r_file);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
249
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
250 result = FcPatternGetString(rpat, FC_FAMILY, 0, &r_family);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
251 if (result != FcResultMatch)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
252 r_family = NULL;
26614
875a7878626c Move font family check to the end of the list.
eugeni
parents: 26613
diff changeset
253
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
254 result = FcPatternGetString(rpat, FC_FULLNAME, 0, &r_fullname);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
255 if (result != FcResultMatch)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
256 r_fullname = NULL;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
257
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
258 if (!treat_family_as_pattern &&
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
259 !(r_family && strcasecmp((const char *) r_family, family) == 0) &&
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
260 !(r_fullname && strcasecmp((const char *) r_fullname, family) == 0))
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
261 ass_msg(library, MSGL_WARN,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
262 "fontconfig: Selected font is not the requested one: "
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
263 "'%s' != '%s'",
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
264 (const char *) (r_fullname ? r_fullname : r_family), family);
26616
c37a85037fc2 Print more info about selected font.
eugeni
parents: 26615
diff changeset
265
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
266 result = FcPatternGetString(rpat, FC_STYLE, 0, &r_style);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
267 if (result != FcResultMatch)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
268 r_style = NULL;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
269
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
270 result = FcPatternGetInteger(rpat, FC_SLANT, 0, &r_slant);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
271 if (result != FcResultMatch)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
272 r_slant = 0;
26616
c37a85037fc2 Print more info about selected font.
eugeni
parents: 26615
diff changeset
273
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
274 result = FcPatternGetInteger(rpat, FC_WEIGHT, 0, &r_weight);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
275 if (result != FcResultMatch)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
276 r_weight = 0;
26616
c37a85037fc2 Print more info about selected font.
eugeni
parents: 26615
diff changeset
277
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
278 result = FcPatternGetBool(rpat, FC_EMBOLDEN, 0, &r_embolden);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
279 if (result != FcResultMatch)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
280 r_embolden = 0;
26616
c37a85037fc2 Print more info about selected font.
eugeni
parents: 26615
diff changeset
281
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
282 ass_msg(library, MSGL_V,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
283 "Font info: family '%s', style '%s', fullname '%s',"
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
284 " slant %d, weight %d%s", (const char *) r_family,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
285 (const char *) r_style, (const char *) r_fullname, r_slant,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
286 r_weight, r_embolden ? ", embolden" : "");
26616
c37a85037fc2 Print more info about selected font.
eugeni
parents: 26615
diff changeset
287
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
288 error:
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
289 if (pat)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
290 FcPatternDestroy(pat);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
291 if (rpat)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
292 FcPatternDestroy(rpat);
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
293 if (fsorted)
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
294 FcFontSetDestroy(fsorted);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
295 if (ffullname)
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
296 FcFontSetDestroy(ffullname);
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
297 if (fset)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
298 FcFontSetDestroy(fset);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
299 return retval;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
300 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
301
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
302 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
303 * \brief Find a font. Use default family or path if necessary.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
304 * \param priv_ private data
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
305 * \param family font family
28860
7fcc0bf5b27a Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents: 27842
diff changeset
306 * \param treat_family_as_pattern treat family as fontconfig pattern
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
307 * \param bold font weight value
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
308 * \param italic font slant value
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
309 * \param index out: font index inside a file
23980
27bac14b4ce4 More simple and correct font reselection.
eugeni
parents: 23216
diff changeset
310 * \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
311 * \return font file path
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28942
diff changeset
312 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
313 char *fontconfig_select(ASS_Library *library, FCInstance *priv,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
314 const char *family, int treat_family_as_pattern,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
315 unsigned bold, unsigned italic, int *index,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
316 uint32_t code)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
317 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
318 char *res = 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
319 if (!priv->config) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
320 *index = priv->index_default;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
321 res = priv->path_default ? strdup(priv->path_default) : 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
322 return res;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
323 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
324 if (family && *family)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
325 res =
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
326 select_font(library, priv, family, treat_family_as_pattern,
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
327 bold, italic, index, code);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
328 if (!res && priv->family_default) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
329 res =
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
330 select_font(library, priv, priv->family_default, 0, bold,
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
331 italic, index, code);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
332 if (res)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
333 ass_msg(library, MSGL_WARN, "fontconfig_select: Using default "
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
334 "font family: (%s, %d, %d) -> %s, %d",
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
335 family, bold, italic, res, *index);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
336 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
337 if (!res && priv->path_default) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
338 res = strdup(priv->path_default);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
339 *index = priv->index_default;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
340 ass_msg(library, MSGL_WARN, "fontconfig_select: Using default font: "
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
341 "(%s, %d, %d) -> %s, %d", family, bold, italic,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
342 res, *index);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
343 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
344 if (!res) {
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
345 res = select_font(library, priv, "Arial", 0, bold, italic,
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
346 index, code);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
347 if (res)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
348 ass_msg(library, MSGL_WARN, "fontconfig_select: Using 'Arial' "
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
349 "font family: (%s, %d, %d) -> %s, %d", family, bold,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
350 italic, res, *index);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
351 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
352 if (res)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
353 ass_msg(library, MSGL_V,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
354 "fontconfig_select: (%s, %d, %d) -> %s, %d", family, bold,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
355 italic, res, *index);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
356 return res;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
357 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
358
21458
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 21351
diff changeset
359 /**
21630
0d8005d2fe5c Update some comments.
eugeni
parents: 21615
diff changeset
360 * \brief Process memory font.
0d8005d2fe5c Update some comments.
eugeni
parents: 21615
diff changeset
361 * \param priv private data
0d8005d2fe5c Update some comments.
eugeni
parents: 21615
diff changeset
362 * \param library library object
0d8005d2fe5c Update some comments.
eugeni
parents: 21615
diff changeset
363 * \param ftlibrary freetype library object
0d8005d2fe5c Update some comments.
eugeni
parents: 21615
diff changeset
364 * \param idx index of the processed font in library->fontdata
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
365 *
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
366 * Builds a font pattern in memory via FT_New_Memory_Face/FcFreeTypeQueryFace.
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28942
diff changeset
367 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
368 static void process_fontdata(FCInstance *priv, ASS_Library *library,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
369 FT_Library ftlibrary, int idx)
21458
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 21351
diff changeset
370 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
371 int rc;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
372 const char *name = library->fontdata[idx].name;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
373 const char *data = library->fontdata[idx].data;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
374 int data_size = library->fontdata[idx].size;
23216
b863ed752149 Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents: 23215
diff changeset
375
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
376 FT_Face face;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
377 FcPattern *pattern;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
378 FcFontSet *fset;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
379 FcBool res;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
380 int face_index, num_faces = 1;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28942
diff changeset
381
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
382 for (face_index = 0; face_index < num_faces; ++face_index) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
383 rc = FT_New_Memory_Face(ftlibrary, (unsigned char *) data,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
384 data_size, face_index, &face);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
385 if (rc) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
386 ass_msg(library, MSGL_WARN, "Error opening memory font: %s",
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
387 name);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
388 return;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
389 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
390 num_faces = face->num_faces;
21458
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 21351
diff changeset
391
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
392 pattern =
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
393 FcFreeTypeQueryFace(face, (unsigned char *) name, face_index,
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
394 FcConfigGetBlanks(priv->config));
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
395 if (!pattern) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
396 ass_msg(library, MSGL_WARN, "%s failed", "FcFreeTypeQueryFace");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
397 FT_Done_Face(face);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
398 return;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
399 }
21460
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
400
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
401 fset = FcConfigGetFonts(priv->config, FcSetSystem); // somehow it failes when asked for FcSetApplication
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
402 if (!fset) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
403 ass_msg(library, MSGL_WARN, "%s failed", "FcConfigGetFonts");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
404 FT_Done_Face(face);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
405 return;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
406 }
21460
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
407
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
408 res = FcFontSetAdd(fset, pattern);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
409 if (!res) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
410 ass_msg(library, MSGL_WARN, "%s failed", "FcFontSetAdd");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
411 FT_Done_Face(face);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
412 return;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
413 }
21460
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
414
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
415 FT_Done_Face(face);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
416 }
21458
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 21351
diff changeset
417 }
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 21351
diff changeset
418
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
419 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
420 * \brief Init fontconfig.
21630
0d8005d2fe5c Update some comments.
eugeni
parents: 21615
diff changeset
421 * \param library libass library object
0d8005d2fe5c Update some comments.
eugeni
parents: 21615
diff changeset
422 * \param ftlibrary freetype library object
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
423 * \param family default font family
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
424 * \param path default font path
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
425 * \param fc whether fontconfig should be used
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
426 * \param config path to a fontconfig configuration file, or NULL
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
427 * \param update whether the fontconfig cache should be built/updated
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
428 * \return pointer to fontconfig private data
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28942
diff changeset
429 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
430 FCInstance *fontconfig_init(ASS_Library *library,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
431 FT_Library ftlibrary, const char *family,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
432 const char *path, int fc, const char *config,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
433 int update)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
434 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
435 int rc;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
436 FCInstance *priv = calloc(1, sizeof(FCInstance));
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
437 const char *dir = library->fonts_dir;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
438 int i;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28942
diff changeset
439
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
440 if (!fc) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
441 ass_msg(library, MSGL_WARN,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
442 "Fontconfig disabled, only default font will be used.");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
443 goto exit;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
444 }
21458
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 21351
diff changeset
445
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
446 priv->config = FcConfigCreate();
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
447 rc = FcConfigParseAndLoad(priv->config, (unsigned char *) config, FcTrue);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
448 if (!rc) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
449 ass_msg(library, MSGL_WARN, "No usable fontconfig configuration "
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
450 "file found, using fallback.");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
451 FcConfigDestroy(priv->config);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
452 priv->config = FcInitLoadConfig();
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
453 rc++;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
454 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
455 if (rc && update) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
456 FcConfigBuildFonts(priv->config);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
457 }
27095
a0e00d065593 Reindent.
eugeni
parents: 27094
diff changeset
458
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
459 if (!rc || !priv->config) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
460 ass_msg(library, MSGL_FATAL,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
461 "No valid fontconfig configuration found!");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
462 FcConfigDestroy(priv->config);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
463 goto exit;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
464 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
465
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
466 for (i = 0; i < library->num_fontdata; ++i)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
467 process_fontdata(priv, library, ftlibrary, i);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
468
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
469 if (dir) {
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
470 ass_msg(library, MSGL_V, "Updating font cache");
27095
a0e00d065593 Reindent.
eugeni
parents: 27094
diff changeset
471
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
472 rc = FcConfigAppFontAddDir(priv->config, (const FcChar8 *) dir);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
473 if (!rc) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
474 ass_msg(library, MSGL_WARN, "%s failed", "FcConfigAppFontAddDir");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
475 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
476 }
19340
f0d49f38ce97 Use FontConfig cache to speedup mplayer startup.
eugeni
parents: 19064
diff changeset
477
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
478 priv->family_default = family ? strdup(family) : NULL;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
479 exit:
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
480 priv->path_default = path ? strdup(path) : NULL;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
481 priv->index_default = 0;
19340
f0d49f38ce97 Use FontConfig cache to speedup mplayer startup.
eugeni
parents: 19064
diff changeset
482
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
483 return priv;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
484 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
485
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
486 int fontconfig_update(FCInstance *priv)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
487 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
488 return FcConfigBuildFonts(priv->config);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
489 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
490
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
491 #else /* CONFIG_FONTCONFIG */
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
492
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
493 char *fontconfig_select(ASS_Library *library, FCInstance *priv,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
494 const char *family, int treat_family_as_pattern,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
495 unsigned bold, unsigned italic, int *index,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
496 uint32_t code)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
497 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
498 *index = priv->index_default;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
499 char* res = priv->path_default ? strdup(priv->path_default) : 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
500 return res;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
501 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
502
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
503 FCInstance *fontconfig_init(ASS_Library *library,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
504 FT_Library ftlibrary, const char *family,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
505 const char *path, int fc, const char *config,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
506 int update)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
507 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
508 FCInstance *priv;
19481
5890c54b755c no c++ decls!
rfelker
parents: 19340
diff changeset
509
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
510 ass_msg(library, MSGL_WARN,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
511 "Fontconfig disabled, only default font will be used.");
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28942
diff changeset
512
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
513 priv = calloc(1, sizeof(FCInstance));
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28942
diff changeset
514
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
515 priv->path_default = path ? strdup(path) : 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
516 priv->index_default = 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
517 return priv;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
518 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
519
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
520 int fontconfig_update(FCInstance *priv)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
521 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
522 // Do nothing
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
523 return 1;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
524 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
525
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
526 #endif
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
527
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
528 void fontconfig_done(FCInstance *priv)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
529 {
31875
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
530
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
531 if (priv) {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
532 #ifdef CONFIG_FONTCONFIG
34011
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31875
diff changeset
533 if (priv->config)
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31875
diff changeset
534 FcConfigDestroy(priv->config);
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
535 #endif
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
536 free(priv->path_default);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
537 free(priv->family_default);
31875
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
538 }
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
539 free(priv);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
540 }