blob: 90511702bf480072e2fc02bf74f72a74adde8517 [file] [log] [blame]
reed@google.com381bb432013-05-13 19:43:59 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
jvanverth02802f62015-07-02 06:42:49 -07008#include "SkFontConfigInterface.h"
9#include "SkFontConfigTypeface.h"
mtklein1b249332015-07-07 12:21:21 -070010#include "SkFontMgr.h"
11#include "SkFontStyle.h"
reed@google.com381bb432013-05-13 19:43:59 +000012#include "SkMath.h"
mtklein1b249332015-07-07 12:21:21 -070013#include "SkMutex.h"
reed@google.com381bb432013-05-13 19:43:59 +000014#include "SkString.h"
15#include "SkTDArray.h"
16
17// for now we pull these in directly. eventually we will solely rely on the
18// SkFontConfigInterface instance.
19#include <fontconfig/fontconfig.h>
20#include <unistd.h>
21
commit-bot@chromium.org07421a52014-03-31 18:41:30 +000022namespace {
23
24// Fontconfig is not threadsafe before 2.10.91. Before that, we lock with a global mutex.
25// See skia:1497 for background.
26SK_DECLARE_STATIC_MUTEX(gFCMutex);
27static bool gFCSafeToUse;
28
29struct FCLocker {
30 FCLocker() {
31 if (FcGetVersion() < 21091) { // We assume FcGetVersion() has always been thread safe.
32 gFCMutex.acquire();
33 fUnlock = true;
34 } else {
35 fUnlock = false;
36 }
37 gFCSafeToUse = true;
38 }
39
40 ~FCLocker() {
commit-bot@chromium.org07421a52014-03-31 18:41:30 +000041 if (fUnlock) {
commit-bot@chromium.org6493ae22014-04-16 22:10:52 +000042 gFCSafeToUse = false;
commit-bot@chromium.org07421a52014-03-31 18:41:30 +000043 gFCMutex.release();
44 }
45 }
46
47private:
48 bool fUnlock;
49};
50
51} // namespace
52
reed@google.com381bb432013-05-13 19:43:59 +000053// borrow this global from SkFontHost_fontconfig. eventually that file should
54// go away, and be replaced with this one.
55extern SkFontConfigInterface* SkFontHost_fontconfig_ref_global();
56static SkFontConfigInterface* RefFCI() {
57 return SkFontHost_fontconfig_ref_global();
58}
59
60// look for the last substring after a '/' and return that, or return null.
61static const char* find_just_name(const char* str) {
62 const char* last = strrchr(str, '/');
halcanary96fcdcc2015-08-27 07:41:13 -070063 return last ? last + 1 : nullptr;
reed@google.com381bb432013-05-13 19:43:59 +000064}
65
66static bool is_lower(char c) {
67 return c >= 'a' && c <= 'z';
68}
69
70static int get_int(FcPattern* pattern, const char field[]) {
commit-bot@chromium.org07421a52014-03-31 18:41:30 +000071 SkASSERT(gFCSafeToUse);
reed@google.com381bb432013-05-13 19:43:59 +000072 int value;
73 if (FcPatternGetInteger(pattern, field, 0, &value) != FcResultMatch) {
74 value = SK_MinS32;
75 }
76 return value;
77}
78
79static const char* get_name(FcPattern* pattern, const char field[]) {
commit-bot@chromium.org07421a52014-03-31 18:41:30 +000080 SkASSERT(gFCSafeToUse);
reed@google.com381bb432013-05-13 19:43:59 +000081 const char* name;
82 if (FcPatternGetString(pattern, field, 0, (FcChar8**)&name) != FcResultMatch) {
83 name = "";
84 }
85 return name;
86}
87
88static bool valid_pattern(FcPattern* pattern) {
commit-bot@chromium.org07421a52014-03-31 18:41:30 +000089 SkASSERT(gFCSafeToUse);
reed@google.com381bb432013-05-13 19:43:59 +000090 FcBool is_scalable;
91 if (FcPatternGetBool(pattern, FC_SCALABLE, 0, &is_scalable) != FcResultMatch || !is_scalable) {
92 return false;
93 }
94
95 // fontconfig can also return fonts which are unreadable
96 const char* c_filename = get_name(pattern, FC_FILE);
97 if (0 == *c_filename) {
98 return false;
99 }
100 if (access(c_filename, R_OK) != 0) {
101 return false;
102 }
103 return true;
104}
105
106static bool match_name(FcPattern* pattern, const char family_name[]) {
107 return !strcasecmp(family_name, get_name(pattern, FC_FAMILY));
108}
109
110static FcPattern** MatchFont(FcFontSet* font_set,
111 const char post_config_family[],
112 int* count) {
113 // Older versions of fontconfig have a bug where they cannot select
114 // only scalable fonts so we have to manually filter the results.
115
116 FcPattern** iter = font_set->fonts;
117 FcPattern** stop = iter + font_set->nfont;
118 // find the first good match
119 for (; iter < stop; ++iter) {
120 if (valid_pattern(*iter)) {
121 break;
122 }
123 }
124
125 if (iter == stop || !match_name(*iter, post_config_family)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700126 return nullptr;
reed@google.com381bb432013-05-13 19:43:59 +0000127 }
128
129 FcPattern** firstIter = iter++;
130 for (; iter < stop; ++iter) {
131 if (!valid_pattern(*iter) || !match_name(*iter, post_config_family)) {
132 break;
133 }
134 }
135
136 *count = iter - firstIter;
137 return firstIter;
138}
139
140class SkFontStyleSet_FC : public SkFontStyleSet {
141public:
142 SkFontStyleSet_FC(FcPattern** matches, int count);
143 virtual ~SkFontStyleSet_FC();
144
mtklein36352bf2015-03-25 18:17:31 -0700145 int count() override { return fRecCount; }
146 void getStyle(int index, SkFontStyle*, SkString* style) override;
147 SkTypeface* createTypeface(int index) override;
148 SkTypeface* matchStyle(const SkFontStyle& pattern) override;
reed@google.com381bb432013-05-13 19:43:59 +0000149
150private:
151 struct Rec {
152 SkString fStyleName;
153 SkString fFileName;
154 SkFontStyle fStyle;
155 };
156 Rec* fRecs;
157 int fRecCount;
158};
159
160static int map_range(int value,
161 int old_min, int old_max, int new_min, int new_max) {
162 SkASSERT(old_min < old_max);
163 SkASSERT(new_min < new_max);
164 return new_min + SkMulDiv(value - old_min,
165 new_max - new_min, old_max - old_min);
166}
167
168static SkFontStyle make_fontconfig_style(FcPattern* match) {
169 int weight = get_int(match, FC_WEIGHT);
170 int width = get_int(match, FC_WIDTH);
bungemanb4bb7d82016-04-27 10:21:04 -0700171 int fcSlant = get_int(match, FC_SLANT);
reed@google.com381bb432013-05-13 19:43:59 +0000172
173 // fontconfig weight seems to be 0..200 or so, so we remap it here
174 weight = map_range(weight, 0, 80, 0, 400);
175 width = map_range(width, 0, 200, 0, 9);
bungemanb4bb7d82016-04-27 10:21:04 -0700176 SkFontStyle::Slant skSlant = SkFontStyle::kUpright_Slant;
177 switch (fcSlant) {
178 case FC_SLANT_ROMAN: skSlant = SkFontStyle::kUpright_Slant; break;
179 case FC_SLANT_ITALIC : skSlant = SkFontStyle::kItalic_Slant ; break;
180 case FC_SLANT_OBLIQUE: skSlant = SkFontStyle::kOblique_Slant; break;
181 default: SkASSERT(false); break;
182 }
183 return SkFontStyle(weight, width, skSlant);
reed@google.com381bb432013-05-13 19:43:59 +0000184}
185
186SkFontStyleSet_FC::SkFontStyleSet_FC(FcPattern** matches, int count) {
187 fRecCount = count;
halcanary385fe4d2015-08-26 13:07:48 -0700188 fRecs = new Rec[count];
reed@google.com381bb432013-05-13 19:43:59 +0000189 for (int i = 0; i < count; ++i) {
190 fRecs[i].fStyleName.set(get_name(matches[i], FC_STYLE));
191 fRecs[i].fFileName.set(get_name(matches[i], FC_FILE));
192 fRecs[i].fStyle = make_fontconfig_style(matches[i]);
193 }
194}
195
halcanary385fe4d2015-08-26 13:07:48 -0700196SkFontStyleSet_FC::~SkFontStyleSet_FC() { delete[] fRecs; }
reed@google.com381bb432013-05-13 19:43:59 +0000197
198void SkFontStyleSet_FC::getStyle(int index, SkFontStyle* style,
199 SkString* styleName) {
200 SkASSERT((unsigned)index < (unsigned)fRecCount);
201 if (style) {
202 *style = fRecs[index].fStyle;
203 }
204 if (styleName) {
205 *styleName = fRecs[index].fStyleName;
206 }
207}
208
209SkTypeface* SkFontStyleSet_FC::createTypeface(int index) {
halcanary96fcdcc2015-08-27 07:41:13 -0700210 return nullptr;
reed@google.com381bb432013-05-13 19:43:59 +0000211}
212
213SkTypeface* SkFontStyleSet_FC::matchStyle(const SkFontStyle& pattern) {
halcanary96fcdcc2015-08-27 07:41:13 -0700214 return nullptr;
reed@google.com381bb432013-05-13 19:43:59 +0000215}
216
217class SkFontMgr_fontconfig : public SkFontMgr {
218 SkAutoTUnref<SkFontConfigInterface> fFCI;
219 SkDataTable* fFamilyNames;
bungeman14df8332014-10-28 15:07:23 -0700220 SkTypeface_FreeType::Scanner fScanner;
reed@google.com381bb432013-05-13 19:43:59 +0000221
222public:
223 SkFontMgr_fontconfig(SkFontConfigInterface* fci)
224 : fFCI(fci)
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000225 , fFamilyNames(fFCI->getFamilyNames()) {}
reed@google.com381bb432013-05-13 19:43:59 +0000226
227 virtual ~SkFontMgr_fontconfig() {
228 SkSafeUnref(fFamilyNames);
229 }
230
231protected:
mtklein36352bf2015-03-25 18:17:31 -0700232 int onCountFamilies() const override {
reed@google.com381bb432013-05-13 19:43:59 +0000233 return fFamilyNames->count();
234 }
235
mtklein36352bf2015-03-25 18:17:31 -0700236 void onGetFamilyName(int index, SkString* familyName) const override {
reed@google.com381bb432013-05-13 19:43:59 +0000237 familyName->set(fFamilyNames->atStr(index));
238 }
239
mtklein36352bf2015-03-25 18:17:31 -0700240 SkFontStyleSet* onCreateStyleSet(int index) const override {
reed@google.com381bb432013-05-13 19:43:59 +0000241 return this->onMatchFamily(fFamilyNames->atStr(index));
242 }
243
mtklein36352bf2015-03-25 18:17:31 -0700244 SkFontStyleSet* onMatchFamily(const char familyName[]) const override {
commit-bot@chromium.org07421a52014-03-31 18:41:30 +0000245 FCLocker lock;
246
reed@google.com381bb432013-05-13 19:43:59 +0000247 FcPattern* pattern = FcPatternCreate();
248
249 FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName);
250#if 0
251 FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
252#endif
halcanary96fcdcc2015-08-27 07:41:13 -0700253 FcConfigSubstitute(nullptr, pattern, FcMatchPattern);
reed@google.com381bb432013-05-13 19:43:59 +0000254 FcDefaultSubstitute(pattern);
255
256 const char* post_config_family = get_name(pattern, FC_FAMILY);
257
258 FcResult result;
259 FcFontSet* font_set = FcFontSort(0, pattern, 0, 0, &result);
260 if (!font_set) {
261 FcPatternDestroy(pattern);
halcanary96fcdcc2015-08-27 07:41:13 -0700262 return nullptr;
reed@google.com381bb432013-05-13 19:43:59 +0000263 }
264
265 int count;
266 FcPattern** match = MatchFont(font_set, post_config_family, &count);
267 if (!match) {
268 FcPatternDestroy(pattern);
269 FcFontSetDestroy(font_set);
halcanary96fcdcc2015-08-27 07:41:13 -0700270 return nullptr;
reed@google.com381bb432013-05-13 19:43:59 +0000271 }
272
273 FcPatternDestroy(pattern);
274
275 SkTDArray<FcPattern*> trimmedMatches;
276 for (int i = 0; i < count; ++i) {
277 const char* justName = find_just_name(get_name(match[i], FC_FILE));
278 if (!is_lower(*justName)) {
279 *trimmedMatches.append() = match[i];
280 }
281 }
282
halcanary385fe4d2015-08-26 13:07:48 -0700283 SkFontStyleSet_FC* sset =
284 new SkFontStyleSet_FC(trimmedMatches.begin(), trimmedMatches.count());
reed@google.com381bb432013-05-13 19:43:59 +0000285 return sset;
286 }
287
tfarina37a92b42015-04-24 14:25:33 -0700288 SkTypeface* onMatchFamilyStyle(const char familyName[],
halcanary96fcdcc2015-08-27 07:41:13 -0700289 const SkFontStyle&) const override { return nullptr; }
tfarina37a92b42015-04-24 14:25:33 -0700290 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
291 const char* bcp47[], int bcp47Count,
292 SkUnichar character) const override {
halcanary96fcdcc2015-08-27 07:41:13 -0700293 return nullptr;
djsollen33068c12014-11-14 10:52:53 -0800294 }
tfarina37a92b42015-04-24 14:25:33 -0700295 SkTypeface* onMatchFaceStyle(const SkTypeface*,
halcanary96fcdcc2015-08-27 07:41:13 -0700296 const SkFontStyle&) const override { return nullptr; }
reed@google.com381bb432013-05-13 19:43:59 +0000297
halcanary96fcdcc2015-08-27 07:41:13 -0700298 SkTypeface* onCreateFromData(SkData*, int ttcIndex) const override { return nullptr; }
reed@google.comb6bd24d2013-07-31 18:49:33 +0000299
mtklein36352bf2015-03-25 18:17:31 -0700300 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) const override {
bungeman5f213d92015-01-27 05:39:10 -0800301 SkAutoTDelete<SkStreamAsset> stream(bareStream);
reed@google.comb6bd24d2013-07-31 18:49:33 +0000302 const size_t length = stream->getLength();
303 if (!length) {
halcanary96fcdcc2015-08-27 07:41:13 -0700304 return nullptr;
reed@google.comb6bd24d2013-07-31 18:49:33 +0000305 }
306 if (length >= 1024 * 1024 * 1024) {
halcanary96fcdcc2015-08-27 07:41:13 -0700307 return nullptr; // don't accept too large fonts (>= 1GB) for safety.
reed@google.comb6bd24d2013-07-31 18:49:33 +0000308 }
309
djsollen@google.com6d2fef92013-09-06 18:00:04 +0000310 // TODO should the caller give us the style or should we get it from freetype?
bungemana4c4a2d2014-10-20 13:33:19 -0700311 SkFontStyle style;
bungeman@google.com0cd74ad2013-11-11 19:52:29 +0000312 bool isFixedWidth = false;
halcanary96fcdcc2015-08-27 07:41:13 -0700313 if (!fScanner.scanFont(stream, 0, nullptr, &style, &isFixedWidth, nullptr)) {
314 return nullptr;
bungeman@google.com0cd74ad2013-11-11 19:52:29 +0000315 }
316
mtklein18300a32016-03-16 13:53:35 -0700317 SkTypeface* face = FontConfigTypeface::Create(style, isFixedWidth, stream.release());
reed@google.comb6bd24d2013-07-31 18:49:33 +0000318 return face;
reed@google.com381bb432013-05-13 19:43:59 +0000319 }
reed@google.comb6bd24d2013-07-31 18:49:33 +0000320
mtklein36352bf2015-03-25 18:17:31 -0700321 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override {
bungeman5f213d92015-01-27 05:39:10 -0800322 SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path));
mtklein18300a32016-03-16 13:53:35 -0700323 return stream.get() ? this->createFromStream(stream.release(), ttcIndex) : nullptr;
reed@google.com381bb432013-05-13 19:43:59 +0000324 }
skia.committer@gmail.com5d4b7732013-08-01 07:01:05 +0000325
bungeman11a77c62016-04-12 13:45:06 -0700326 SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle style) const override {
commit-bot@chromium.org07421a52014-03-31 18:41:30 +0000327 FCLocker lock;
bungeman11a77c62016-04-12 13:45:06 -0700328 return FontConfigTypeface::LegacyCreateTypeface(familyName, style);
reed@google.come027a6e2013-07-31 17:37:31 +0000329 }
reed@google.com381bb432013-05-13 19:43:59 +0000330};
331
332SkFontMgr* SkFontMgr::Factory() {
333 SkFontConfigInterface* fci = RefFCI();
halcanary96fcdcc2015-08-27 07:41:13 -0700334 return fci ? new SkFontMgr_fontconfig(fci) : nullptr;
reed@google.com381bb432013-05-13 19:43:59 +0000335}