reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 1 | /* |
benjaminwagner | 2211a7b | 2015-12-01 11:12:05 -0800 | [diff] [blame] | 2 | * Copyright 2009-2015 Google Inc. |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 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 | |
| 8 | /* migrated from chrome/src/skia/ext/SkFontHost_fontconfig_direct.cpp */ |
| 9 | |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 10 | #include "SkAutoMalloc.h" |
reed@google.com | f55061f | 2013-04-22 18:48:45 +0000 | [diff] [blame] | 11 | #include "SkBuffer.h" |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 12 | #include "SkFixed.h" |
benjaminwagner | 2211a7b | 2015-12-01 11:12:05 -0800 | [diff] [blame] | 13 | #include "SkFontConfigInterface_direct.h" |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 14 | #include "SkFontStyle.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 15 | #include "SkMutex.h" |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 16 | #include "SkStream.h" |
mtklein | ba59a67 | 2014-08-04 10:18:27 -0700 | [diff] [blame] | 17 | #include "SkString.h" |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 18 | #include "SkTArray.h" |
| 19 | #include "SkTDArray.h" |
| 20 | #include "SkTemplates.h" |
| 21 | #include "SkTypeface.h" |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 22 | |
| 23 | #include <fontconfig/fontconfig.h> |
| 24 | #include <unistd.h> |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 25 | |
bungeman | 0265707 | 2016-05-02 11:54:13 -0700 | [diff] [blame] | 26 | #ifdef SK_DEBUG |
| 27 | # include "SkTLS.h" |
| 28 | #endif |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | // Fontconfig is not threadsafe before 2.10.91. Before that, we lock with a global mutex. |
| 33 | // See https://bug.skia.org/1497 for background. |
reed | 086eea9 | 2016-05-04 17:12:46 -0700 | [diff] [blame] | 34 | SK_DECLARE_STATIC_MUTEX(gFCMutex); |
bungeman | 0265707 | 2016-05-02 11:54:13 -0700 | [diff] [blame] | 35 | |
| 36 | #ifdef SK_DEBUG |
| 37 | void* CreateThreadFcLocked() { return new bool(false); } |
| 38 | void DeleteThreadFcLocked(void* v) { delete static_cast<bool*>(v); } |
| 39 | # define THREAD_FC_LOCKED \ |
| 40 | static_cast<bool*>(SkTLS::Get(CreateThreadFcLocked, DeleteThreadFcLocked)) |
| 41 | #endif |
| 42 | |
| 43 | struct FCLocker { |
| 44 | // Assume FcGetVersion() has always been thread safe. |
| 45 | |
| 46 | FCLocker() { |
| 47 | if (FcGetVersion() < 21091) { |
| 48 | gFCMutex.acquire(); |
| 49 | } else { |
| 50 | SkDEBUGCODE(bool* threadLocked = THREAD_FC_LOCKED); |
| 51 | SkASSERT(false == *threadLocked); |
| 52 | SkDEBUGCODE(*threadLocked = true); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | ~FCLocker() { |
| 57 | AssertHeld(); |
| 58 | if (FcGetVersion() < 21091) { |
| 59 | gFCMutex.release(); |
| 60 | } else { |
| 61 | SkDEBUGCODE(*THREAD_FC_LOCKED = false); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static void AssertHeld() { SkDEBUGCODE( |
| 66 | if (FcGetVersion() < 21091) { |
| 67 | gFCMutex.assertHeld(); |
| 68 | } else { |
| 69 | SkASSERT(true == *THREAD_FC_LOCKED); |
| 70 | } |
| 71 | ) } |
| 72 | }; |
| 73 | |
| 74 | } // namespace |
| 75 | |
reed@google.com | f55061f | 2013-04-22 18:48:45 +0000 | [diff] [blame] | 76 | size_t SkFontConfigInterface::FontIdentity::writeToMemory(void* addr) const { |
| 77 | size_t size = sizeof(fID) + sizeof(fTTCIndex); |
| 78 | size += sizeof(int32_t) + sizeof(int32_t) + sizeof(uint8_t); // weight, width, italic |
| 79 | size += sizeof(int32_t) + fString.size(); // store length+data |
| 80 | if (addr) { |
| 81 | SkWBuffer buffer(addr, size); |
| 82 | |
| 83 | buffer.write32(fID); |
| 84 | buffer.write32(fTTCIndex); |
| 85 | buffer.write32(fString.size()); |
| 86 | buffer.write32(fStyle.weight()); |
| 87 | buffer.write32(fStyle.width()); |
| 88 | buffer.write8(fStyle.slant()); |
| 89 | buffer.write(fString.c_str(), fString.size()); |
| 90 | buffer.padToAlign4(); |
| 91 | |
| 92 | SkASSERT(buffer.pos() == size); |
| 93 | } |
| 94 | return size; |
| 95 | } |
| 96 | |
| 97 | size_t SkFontConfigInterface::FontIdentity::readFromMemory(const void* addr, |
| 98 | size_t size) { |
| 99 | SkRBuffer buffer(addr, size); |
| 100 | |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 101 | (void)buffer.readU32(&fID); |
| 102 | (void)buffer.readS32(&fTTCIndex); |
| 103 | uint32_t strLen, weight, width; |
| 104 | (void)buffer.readU32(&strLen); |
| 105 | (void)buffer.readU32(&weight); |
| 106 | (void)buffer.readU32(&width); |
| 107 | uint8_t u8; |
| 108 | (void)buffer.readU8(&u8); |
| 109 | SkFontStyle::Slant slant = (SkFontStyle::Slant)u8; |
reed@google.com | f55061f | 2013-04-22 18:48:45 +0000 | [diff] [blame] | 110 | fStyle = SkFontStyle(weight, width, slant); |
| 111 | fString.resize(strLen); |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 112 | (void)buffer.read(fString.writable_str(), strLen); |
reed@google.com | f55061f | 2013-04-22 18:48:45 +0000 | [diff] [blame] | 113 | buffer.skipToAlign4(); |
| 114 | |
| 115 | return buffer.pos(); // the actual number of bytes read |
| 116 | } |
| 117 | |
| 118 | #ifdef SK_DEBUG |
| 119 | static void make_iden(SkFontConfigInterface::FontIdentity* iden) { |
| 120 | iden->fID = 10; |
| 121 | iden->fTTCIndex = 2; |
| 122 | iden->fString.set("Hello world"); |
| 123 | iden->fStyle = SkFontStyle(300, 6, SkFontStyle::kItalic_Slant); |
| 124 | } |
| 125 | |
| 126 | static void test_writeToMemory(const SkFontConfigInterface::FontIdentity& iden0, |
| 127 | int initValue) { |
| 128 | SkFontConfigInterface::FontIdentity iden1; |
| 129 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 130 | size_t size0 = iden0.writeToMemory(nullptr); |
reed@google.com | f55061f | 2013-04-22 18:48:45 +0000 | [diff] [blame] | 131 | |
| 132 | SkAutoMalloc storage(size0); |
| 133 | memset(storage.get(), initValue, size0); |
| 134 | |
| 135 | size_t size1 = iden0.writeToMemory(storage.get()); |
| 136 | SkASSERT(size0 == size1); |
| 137 | |
| 138 | SkASSERT(iden0 != iden1); |
| 139 | size_t size2 = iden1.readFromMemory(storage.get(), size1); |
| 140 | SkASSERT(size2 == size1); |
| 141 | SkASSERT(iden0 == iden1); |
| 142 | } |
| 143 | |
| 144 | static void fontconfiginterface_unittest() { |
| 145 | SkFontConfigInterface::FontIdentity iden0, iden1; |
| 146 | |
| 147 | SkASSERT(iden0 == iden1); |
| 148 | |
| 149 | make_iden(&iden0); |
| 150 | SkASSERT(iden0 != iden1); |
| 151 | |
| 152 | make_iden(&iden1); |
| 153 | SkASSERT(iden0 == iden1); |
| 154 | |
| 155 | test_writeToMemory(iden0, 0); |
| 156 | test_writeToMemory(iden0, 0); |
| 157 | } |
| 158 | #endif |
| 159 | |
reed@google.com | 54c6914 | 2013-04-09 15:54:52 +0000 | [diff] [blame] | 160 | /////////////////////////////////////////////////////////////////////////////// |
| 161 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 162 | // Returns the string from the pattern, or nullptr |
bungeman | 875b8f6 | 2016-09-16 13:19:49 -0700 | [diff] [blame] | 163 | static const char* get_string(FcPattern* pattern, const char field[], int index = 0) { |
reed@google.com | e49d67e | 2013-04-22 18:00:06 +0000 | [diff] [blame] | 164 | const char* name; |
bungeman | 875b8f6 | 2016-09-16 13:19:49 -0700 | [diff] [blame] | 165 | if (FcPatternGetString(pattern, field, index, (FcChar8**)&name) != FcResultMatch) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 166 | name = nullptr; |
reed@google.com | e49d67e | 2013-04-22 18:00:06 +0000 | [diff] [blame] | 167 | } |
| 168 | return name; |
| 169 | } |
| 170 | |
| 171 | /////////////////////////////////////////////////////////////////////////////// |
| 172 | |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 173 | namespace { |
| 174 | |
| 175 | // Equivalence classes, used to match the Liberation and other fonts |
| 176 | // with their metric-compatible replacements. See the discussion in |
| 177 | // GetFontEquivClass(). |
| 178 | enum FontEquivClass |
| 179 | { |
| 180 | OTHER, |
| 181 | SANS, |
| 182 | SERIF, |
| 183 | MONO, |
| 184 | SYMBOL, |
| 185 | PGOTHIC, |
| 186 | GOTHIC, |
| 187 | PMINCHO, |
| 188 | MINCHO, |
| 189 | SIMSUN, |
| 190 | NSIMSUN, |
| 191 | SIMHEI, |
| 192 | PMINGLIU, |
| 193 | MINGLIU, |
| 194 | PMINGLIUHK, |
| 195 | MINGLIUHK, |
commit-bot@chromium.org | c4de776 | 2013-03-20 13:33:33 +0000 | [diff] [blame] | 196 | CAMBRIA, |
bungeman@google.com | c526c71 | 2013-09-20 17:41:52 +0000 | [diff] [blame] | 197 | CALIBRI, |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | // Match the font name against a whilelist of fonts, returning the equivalence |
| 201 | // class. |
| 202 | FontEquivClass GetFontEquivClass(const char* fontname) |
| 203 | { |
| 204 | // It would be nice for fontconfig to tell us whether a given suggested |
| 205 | // replacement is a "strong" match (that is, an equivalent font) or |
| 206 | // a "weak" match (that is, fontconfig's next-best attempt at finding a |
| 207 | // substitute). However, I played around with the fontconfig API for |
| 208 | // a good few hours and could not make it reveal this information. |
| 209 | // |
| 210 | // So instead, we hardcode. Initially this function emulated |
| 211 | // /etc/fonts/conf.d/30-metric-aliases.conf |
| 212 | // from my Ubuntu system, but we're better off being very conservative. |
| 213 | |
| 214 | // Arimo, Tinos and Cousine are a set of fonts metric-compatible with |
| 215 | // Arial, Times New Roman and Courier New with a character repertoire |
| 216 | // much larger than Liberation. Note that Cousine is metrically |
| 217 | // compatible with Courier New, but the former is sans-serif while |
| 218 | // the latter is serif. |
| 219 | |
| 220 | |
| 221 | struct FontEquivMap { |
| 222 | FontEquivClass clazz; |
| 223 | const char name[40]; |
| 224 | }; |
| 225 | |
| 226 | static const FontEquivMap kFontEquivMap[] = { |
| 227 | { SANS, "Arial" }, |
| 228 | { SANS, "Arimo" }, |
| 229 | { SANS, "Liberation Sans" }, |
| 230 | |
| 231 | { SERIF, "Times New Roman" }, |
| 232 | { SERIF, "Tinos" }, |
| 233 | { SERIF, "Liberation Serif" }, |
| 234 | |
| 235 | { MONO, "Courier New" }, |
| 236 | { MONO, "Cousine" }, |
| 237 | { MONO, "Liberation Mono" }, |
| 238 | |
| 239 | { SYMBOL, "Symbol" }, |
| 240 | { SYMBOL, "Symbol Neu" }, |
| 241 | |
| 242 | // MS Pゴシック |
| 243 | { PGOTHIC, "MS PGothic" }, |
| 244 | { PGOTHIC, "\xef\xbc\xad\xef\xbc\xb3 \xef\xbc\xb0" |
| 245 | "\xe3\x82\xb4\xe3\x82\xb7\xe3\x83\x83\xe3\x82\xaf" }, |
jshin | 7476cf5 | 2014-09-09 12:30:57 -0700 | [diff] [blame] | 246 | { PGOTHIC, "Noto Sans CJK JP" }, |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 247 | { PGOTHIC, "IPAPGothic" }, |
| 248 | { PGOTHIC, "MotoyaG04Gothic" }, |
| 249 | |
| 250 | // MS ゴシック |
| 251 | { GOTHIC, "MS Gothic" }, |
| 252 | { GOTHIC, "\xef\xbc\xad\xef\xbc\xb3 " |
| 253 | "\xe3\x82\xb4\xe3\x82\xb7\xe3\x83\x83\xe3\x82\xaf" }, |
jshin | dd4e568 | 2015-05-12 12:08:36 -0700 | [diff] [blame] | 254 | { GOTHIC, "Noto Sans Mono CJK JP" }, |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 255 | { GOTHIC, "IPAGothic" }, |
| 256 | { GOTHIC, "MotoyaG04GothicMono" }, |
| 257 | |
| 258 | // MS P明朝 |
| 259 | { PMINCHO, "MS PMincho" }, |
| 260 | { PMINCHO, "\xef\xbc\xad\xef\xbc\xb3 \xef\xbc\xb0" |
| 261 | "\xe6\x98\x8e\xe6\x9c\x9d"}, |
Jungshik Shin | cae60da | 2017-07-18 16:42:32 -0700 | [diff] [blame] | 262 | { PMINCHO, "Noto Serif CJK JP" }, |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 263 | { PMINCHO, "IPAPMincho" }, |
| 264 | { PMINCHO, "MotoyaG04Mincho" }, |
| 265 | |
| 266 | // MS 明朝 |
| 267 | { MINCHO, "MS Mincho" }, |
| 268 | { MINCHO, "\xef\xbc\xad\xef\xbc\xb3 \xe6\x98\x8e\xe6\x9c\x9d" }, |
Jungshik Shin | cae60da | 2017-07-18 16:42:32 -0700 | [diff] [blame] | 269 | { MINCHO, "Noto Serif CJK JP" }, |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 270 | { MINCHO, "IPAMincho" }, |
| 271 | { MINCHO, "MotoyaG04MinchoMono" }, |
| 272 | |
| 273 | // 宋体 |
| 274 | { SIMSUN, "Simsun" }, |
| 275 | { SIMSUN, "\xe5\xae\x8b\xe4\xbd\x93" }, |
Jungshik Shin | cae60da | 2017-07-18 16:42:32 -0700 | [diff] [blame] | 276 | { SIMSUN, "Noto Serif CJK SC" }, |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 277 | { SIMSUN, "MSung GB18030" }, |
| 278 | { SIMSUN, "Song ASC" }, |
| 279 | |
| 280 | // 新宋体 |
| 281 | { NSIMSUN, "NSimsun" }, |
| 282 | { NSIMSUN, "\xe6\x96\xb0\xe5\xae\x8b\xe4\xbd\x93" }, |
Jungshik Shin | cae60da | 2017-07-18 16:42:32 -0700 | [diff] [blame] | 283 | { NSIMSUN, "Noto Serif CJK SC" }, |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 284 | { NSIMSUN, "MSung GB18030" }, |
| 285 | { NSIMSUN, "N Song ASC" }, |
| 286 | |
| 287 | // 黑体 |
| 288 | { SIMHEI, "Simhei" }, |
| 289 | { SIMHEI, "\xe9\xbb\x91\xe4\xbd\x93" }, |
jshin | 7476cf5 | 2014-09-09 12:30:57 -0700 | [diff] [blame] | 290 | { SIMHEI, "Noto Sans CJK SC" }, |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 291 | { SIMHEI, "MYingHeiGB18030" }, |
| 292 | { SIMHEI, "MYingHeiB5HK" }, |
| 293 | |
commit-bot@chromium.org | c4de776 | 2013-03-20 13:33:33 +0000 | [diff] [blame] | 294 | // 新細明體 |
| 295 | { PMINGLIU, "PMingLiU"}, |
| 296 | { PMINGLIU, "\xe6\x96\xb0\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94" }, |
Jungshik Shin | cae60da | 2017-07-18 16:42:32 -0700 | [diff] [blame] | 297 | { PMINGLIU, "Noto Serif CJK TC"}, |
commit-bot@chromium.org | c4de776 | 2013-03-20 13:33:33 +0000 | [diff] [blame] | 298 | { PMINGLIU, "MSung B5HK"}, |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 299 | |
commit-bot@chromium.org | c4de776 | 2013-03-20 13:33:33 +0000 | [diff] [blame] | 300 | // 細明體 |
| 301 | { MINGLIU, "MingLiU"}, |
| 302 | { MINGLIU, "\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94" }, |
Jungshik Shin | cae60da | 2017-07-18 16:42:32 -0700 | [diff] [blame] | 303 | { MINGLIU, "Noto Serif CJK TC"}, |
commit-bot@chromium.org | c4de776 | 2013-03-20 13:33:33 +0000 | [diff] [blame] | 304 | { MINGLIU, "MSung B5HK"}, |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 305 | |
commit-bot@chromium.org | c4de776 | 2013-03-20 13:33:33 +0000 | [diff] [blame] | 306 | // 新細明體 |
| 307 | { PMINGLIUHK, "PMingLiU_HKSCS"}, |
| 308 | { PMINGLIUHK, "\xe6\x96\xb0\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94_HKSCS" }, |
Jungshik Shin | cae60da | 2017-07-18 16:42:32 -0700 | [diff] [blame] | 309 | { PMINGLIUHK, "Noto Serif CJK TC"}, |
commit-bot@chromium.org | c4de776 | 2013-03-20 13:33:33 +0000 | [diff] [blame] | 310 | { PMINGLIUHK, "MSung B5HK"}, |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 311 | |
commit-bot@chromium.org | c4de776 | 2013-03-20 13:33:33 +0000 | [diff] [blame] | 312 | // 細明體 |
| 313 | { MINGLIUHK, "MingLiU_HKSCS"}, |
| 314 | { MINGLIUHK, "\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94_HKSCS" }, |
Jungshik Shin | cae60da | 2017-07-18 16:42:32 -0700 | [diff] [blame] | 315 | { MINGLIUHK, "Noto Serif CJK TC"}, |
commit-bot@chromium.org | c4de776 | 2013-03-20 13:33:33 +0000 | [diff] [blame] | 316 | { MINGLIUHK, "MSung B5HK"}, |
| 317 | |
| 318 | // Cambria |
| 319 | { CAMBRIA, "Cambria" }, |
| 320 | { CAMBRIA, "Caladea" }, |
bungeman@google.com | c526c71 | 2013-09-20 17:41:52 +0000 | [diff] [blame] | 321 | |
| 322 | // Calibri |
| 323 | { CALIBRI, "Calibri" }, |
| 324 | { CALIBRI, "Carlito" }, |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 325 | }; |
| 326 | |
| 327 | static const size_t kFontCount = |
| 328 | sizeof(kFontEquivMap)/sizeof(kFontEquivMap[0]); |
| 329 | |
| 330 | // TODO(jungshik): If this loop turns out to be hot, turn |
| 331 | // the array to a static (hash)map to speed it up. |
| 332 | for (size_t i = 0; i < kFontCount; ++i) { |
| 333 | if (strcasecmp(kFontEquivMap[i].name, fontname) == 0) |
| 334 | return kFontEquivMap[i].clazz; |
| 335 | } |
| 336 | return OTHER; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | // Return true if |font_a| and |font_b| are visually and at the metrics |
| 341 | // level interchangeable. |
| 342 | bool IsMetricCompatibleReplacement(const char* font_a, const char* font_b) |
| 343 | { |
| 344 | FontEquivClass class_a = GetFontEquivClass(font_a); |
| 345 | FontEquivClass class_b = GetFontEquivClass(font_b); |
| 346 | |
| 347 | return class_a != OTHER && class_a == class_b; |
| 348 | } |
| 349 | |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 350 | // Normally we only return exactly the font asked for. In last-resort |
| 351 | // cases, the request either doesn't specify a font or is one of the |
| 352 | // basic font names like "Sans", "Serif" or "Monospace". This function |
| 353 | // tells you whether a given request is for such a fallback. |
mtklein | ba59a67 | 2014-08-04 10:18:27 -0700 | [diff] [blame] | 354 | bool IsFallbackFontAllowed(const SkString& family) { |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 355 | const char* family_cstr = family.c_str(); |
mtklein | ba59a67 | 2014-08-04 10:18:27 -0700 | [diff] [blame] | 356 | return family.isEmpty() || |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 357 | strcasecmp(family_cstr, "sans") == 0 || |
| 358 | strcasecmp(family_cstr, "serif") == 0 || |
| 359 | strcasecmp(family_cstr, "monospace") == 0; |
| 360 | } |
| 361 | |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 362 | // Retrieves |is_bold|, |is_italic| and |font_family| properties from |font|. |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 363 | static int get_int(FcPattern* pattern, const char object[], int missing) { |
| 364 | int value; |
| 365 | if (FcPatternGetInteger(pattern, object, 0, &value) != FcResultMatch) { |
| 366 | return missing; |
| 367 | } |
| 368 | return value; |
| 369 | } |
| 370 | |
Ben Wagner | 6fbafc0 | 2018-06-12 17:52:29 -0400 | [diff] [blame] | 371 | static int map_range(SkScalar value, |
| 372 | SkScalar old_min, SkScalar old_max, |
| 373 | SkScalar new_min, SkScalar new_max) |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 374 | { |
| 375 | SkASSERT(old_min < old_max); |
| 376 | SkASSERT(new_min <= new_max); |
Ben Wagner | 6fbafc0 | 2018-06-12 17:52:29 -0400 | [diff] [blame] | 377 | return new_min + ((value - old_min) * (new_max - new_min) / (old_max - old_min)); |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | struct MapRanges { |
Ben Wagner | 6fbafc0 | 2018-06-12 17:52:29 -0400 | [diff] [blame] | 381 | SkScalar old_val; |
| 382 | SkScalar new_val; |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 383 | }; |
| 384 | |
Ben Wagner | 6fbafc0 | 2018-06-12 17:52:29 -0400 | [diff] [blame] | 385 | static SkScalar map_ranges(SkScalar val, MapRanges const ranges[], int rangesCount) { |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 386 | // -Inf to [0] |
| 387 | if (val < ranges[0].old_val) { |
| 388 | return ranges[0].new_val; |
| 389 | } |
| 390 | |
| 391 | // Linear from [i] to [i+1] |
| 392 | for (int i = 0; i < rangesCount - 1; ++i) { |
| 393 | if (val < ranges[i+1].old_val) { |
| 394 | return map_range(val, ranges[i].old_val, ranges[i+1].old_val, |
| 395 | ranges[i].new_val, ranges[i+1].new_val); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // From [n] to +Inf |
| 400 | // if (fcweight < Inf) |
| 401 | return ranges[rangesCount-1].new_val; |
| 402 | } |
| 403 | |
Ben Wagner | 13197b8 | 2017-08-14 11:06:12 -0400 | [diff] [blame] | 404 | #ifndef FC_WEIGHT_DEMILIGHT |
| 405 | #define FC_WEIGHT_DEMILIGHT 65 |
| 406 | #endif |
| 407 | |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 408 | static SkFontStyle skfontstyle_from_fcpattern(FcPattern* pattern) { |
| 409 | typedef SkFontStyle SkFS; |
| 410 | |
Ben Wagner | 6fbafc0 | 2018-06-12 17:52:29 -0400 | [diff] [blame] | 411 | static constexpr MapRanges weightRanges[] = { |
| 412 | { FC_WEIGHT_THIN, SkFS::kThin_Weight }, |
| 413 | { FC_WEIGHT_EXTRALIGHT, SkFS::kExtraLight_Weight }, |
| 414 | { FC_WEIGHT_LIGHT, SkFS::kLight_Weight }, |
| 415 | { FC_WEIGHT_DEMILIGHT, 350 }, |
| 416 | { FC_WEIGHT_BOOK, 380 }, |
| 417 | { FC_WEIGHT_REGULAR, SkFS::kNormal_Weight }, |
| 418 | { FC_WEIGHT_MEDIUM, SkFS::kMedium_Weight }, |
| 419 | { FC_WEIGHT_DEMIBOLD, SkFS::kSemiBold_Weight }, |
| 420 | { FC_WEIGHT_BOLD, SkFS::kBold_Weight }, |
| 421 | { FC_WEIGHT_EXTRABOLD, SkFS::kExtraBold_Weight }, |
| 422 | { FC_WEIGHT_BLACK, SkFS::kBlack_Weight }, |
| 423 | { FC_WEIGHT_EXTRABLACK, SkFS::kExtraBlack_Weight }, |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 424 | }; |
Ben Wagner | 6fbafc0 | 2018-06-12 17:52:29 -0400 | [diff] [blame] | 425 | SkScalar weight = map_ranges(get_int(pattern, FC_WEIGHT, FC_WEIGHT_REGULAR), |
| 426 | weightRanges, SK_ARRAY_COUNT(weightRanges)); |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 427 | |
Ben Wagner | 6fbafc0 | 2018-06-12 17:52:29 -0400 | [diff] [blame] | 428 | static constexpr MapRanges widthRanges[] = { |
| 429 | { FC_WIDTH_ULTRACONDENSED, SkFS::kUltraCondensed_Width }, |
| 430 | { FC_WIDTH_EXTRACONDENSED, SkFS::kExtraCondensed_Width }, |
| 431 | { FC_WIDTH_CONDENSED, SkFS::kCondensed_Width }, |
| 432 | { FC_WIDTH_SEMICONDENSED, SkFS::kSemiCondensed_Width }, |
| 433 | { FC_WIDTH_NORMAL, SkFS::kNormal_Width }, |
| 434 | { FC_WIDTH_SEMIEXPANDED, SkFS::kSemiExpanded_Width }, |
| 435 | { FC_WIDTH_EXPANDED, SkFS::kExpanded_Width }, |
| 436 | { FC_WIDTH_EXTRAEXPANDED, SkFS::kExtraExpanded_Width }, |
| 437 | { FC_WIDTH_ULTRAEXPANDED, SkFS::kUltraExpanded_Width }, |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 438 | }; |
Ben Wagner | 6fbafc0 | 2018-06-12 17:52:29 -0400 | [diff] [blame] | 439 | SkScalar width = map_ranges(get_int(pattern, FC_WIDTH, FC_WIDTH_NORMAL), |
| 440 | widthRanges, SK_ARRAY_COUNT(widthRanges)); |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 441 | |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 442 | SkFS::Slant slant = SkFS::kUpright_Slant; |
| 443 | switch (get_int(pattern, FC_SLANT, FC_SLANT_ROMAN)) { |
| 444 | case FC_SLANT_ROMAN: slant = SkFS::kUpright_Slant; break; |
| 445 | case FC_SLANT_ITALIC : slant = SkFS::kItalic_Slant ; break; |
| 446 | case FC_SLANT_OBLIQUE: slant = SkFS::kOblique_Slant; break; |
| 447 | default: SkASSERT(false); break; |
| 448 | } |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 449 | |
Ben Wagner | 6fbafc0 | 2018-06-12 17:52:29 -0400 | [diff] [blame] | 450 | return SkFontStyle(SkScalarRoundToInt(weight), SkScalarRoundToInt(width), slant); |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static void fcpattern_from_skfontstyle(SkFontStyle style, FcPattern* pattern) { |
| 454 | typedef SkFontStyle SkFS; |
| 455 | |
Ben Wagner | 6fbafc0 | 2018-06-12 17:52:29 -0400 | [diff] [blame] | 456 | static constexpr MapRanges weightRanges[] = { |
| 457 | { SkFS::kThin_Weight, FC_WEIGHT_THIN }, |
| 458 | { SkFS::kExtraLight_Weight, FC_WEIGHT_EXTRALIGHT }, |
| 459 | { SkFS::kLight_Weight, FC_WEIGHT_LIGHT }, |
| 460 | { 350, FC_WEIGHT_DEMILIGHT }, |
| 461 | { 380, FC_WEIGHT_BOOK }, |
| 462 | { SkFS::kNormal_Weight, FC_WEIGHT_REGULAR }, |
| 463 | { SkFS::kMedium_Weight, FC_WEIGHT_MEDIUM }, |
| 464 | { SkFS::kSemiBold_Weight, FC_WEIGHT_DEMIBOLD }, |
| 465 | { SkFS::kBold_Weight, FC_WEIGHT_BOLD }, |
| 466 | { SkFS::kExtraBold_Weight, FC_WEIGHT_EXTRABOLD }, |
| 467 | { SkFS::kBlack_Weight, FC_WEIGHT_BLACK }, |
| 468 | { SkFS::kExtraBlack_Weight, FC_WEIGHT_EXTRABLACK }, |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 469 | }; |
| 470 | int weight = map_ranges(style.weight(), weightRanges, SK_ARRAY_COUNT(weightRanges)); |
| 471 | |
Ben Wagner | 6fbafc0 | 2018-06-12 17:52:29 -0400 | [diff] [blame] | 472 | static constexpr MapRanges widthRanges[] = { |
| 473 | { SkFS::kUltraCondensed_Width, FC_WIDTH_ULTRACONDENSED }, |
| 474 | { SkFS::kExtraCondensed_Width, FC_WIDTH_EXTRACONDENSED }, |
| 475 | { SkFS::kCondensed_Width, FC_WIDTH_CONDENSED }, |
| 476 | { SkFS::kSemiCondensed_Width, FC_WIDTH_SEMICONDENSED }, |
| 477 | { SkFS::kNormal_Width, FC_WIDTH_NORMAL }, |
| 478 | { SkFS::kSemiExpanded_Width, FC_WIDTH_SEMIEXPANDED }, |
| 479 | { SkFS::kExpanded_Width, FC_WIDTH_EXPANDED }, |
| 480 | { SkFS::kExtraExpanded_Width, FC_WIDTH_EXTRAEXPANDED }, |
| 481 | { SkFS::kUltraExpanded_Width, FC_WIDTH_ULTRAEXPANDED }, |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 482 | }; |
| 483 | int width = map_ranges(style.width(), widthRanges, SK_ARRAY_COUNT(widthRanges)); |
| 484 | |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 485 | int slant = FC_SLANT_ROMAN; |
| 486 | switch (style.slant()) { |
| 487 | case SkFS::kUpright_Slant: slant = FC_SLANT_ROMAN ; break; |
| 488 | case SkFS::kItalic_Slant : slant = FC_SLANT_ITALIC ; break; |
| 489 | case SkFS::kOblique_Slant: slant = FC_SLANT_OBLIQUE; break; |
| 490 | default: SkASSERT(false); break; |
| 491 | } |
| 492 | |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 493 | FcPatternAddInteger(pattern, FC_WEIGHT, weight); |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 494 | FcPatternAddInteger(pattern, FC_WIDTH , width); |
| 495 | FcPatternAddInteger(pattern, FC_SLANT , slant); |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 496 | } |
| 497 | |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 498 | } // anonymous namespace |
| 499 | |
| 500 | /////////////////////////////////////////////////////////////////////////////// |
| 501 | |
reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 502 | #define kMaxFontFamilyLength 2048 |
drott | 358f93d | 2016-08-26 10:08:45 -0700 | [diff] [blame] | 503 | #ifdef SK_FONT_CONFIG_INTERFACE_ONLY_ALLOW_SFNT_FONTS |
| 504 | const char* kFontFormatTrueType = "TrueType"; |
| 505 | const char* kFontFormatCFF = "CFF"; |
| 506 | #endif |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 507 | |
reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 508 | SkFontConfigInterfaceDirect::SkFontConfigInterfaceDirect() { |
bungeman | 0265707 | 2016-05-02 11:54:13 -0700 | [diff] [blame] | 509 | FCLocker lock; |
reed@google.com | f55061f | 2013-04-22 18:48:45 +0000 | [diff] [blame] | 510 | |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 511 | FcInit(); |
reed@google.com | f55061f | 2013-04-22 18:48:45 +0000 | [diff] [blame] | 512 | |
| 513 | SkDEBUGCODE(fontconfiginterface_unittest();) |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | SkFontConfigInterfaceDirect::~SkFontConfigInterfaceDirect() { |
| 517 | } |
| 518 | |
benjaminwagner | 2211a7b | 2015-12-01 11:12:05 -0800 | [diff] [blame] | 519 | bool SkFontConfigInterfaceDirect::isAccessible(const char* filename) { |
| 520 | if (access(filename, R_OK) != 0) { |
| 521 | return false; |
| 522 | } |
| 523 | return true; |
| 524 | } |
| 525 | |
| 526 | bool SkFontConfigInterfaceDirect::isValidPattern(FcPattern* pattern) { |
drott | 358f93d | 2016-08-26 10:08:45 -0700 | [diff] [blame] | 527 | #ifdef SK_FONT_CONFIG_INTERFACE_ONLY_ALLOW_SFNT_FONTS |
bungeman | 875b8f6 | 2016-09-16 13:19:49 -0700 | [diff] [blame] | 528 | const char* font_format = get_string(pattern, FC_FONTFORMAT); |
drott | 358f93d | 2016-08-26 10:08:45 -0700 | [diff] [blame] | 529 | if (font_format |
| 530 | && strcmp(font_format, kFontFormatTrueType) != 0 |
| 531 | && strcmp(font_format, kFontFormatCFF) != 0) |
| 532 | { |
| 533 | return false; |
| 534 | } |
| 535 | #endif |
| 536 | |
benjaminwagner | 2211a7b | 2015-12-01 11:12:05 -0800 | [diff] [blame] | 537 | // fontconfig can also return fonts which are unreadable |
bungeman | 875b8f6 | 2016-09-16 13:19:49 -0700 | [diff] [blame] | 538 | const char* c_filename = get_string(pattern, FC_FILE); |
benjaminwagner | 2211a7b | 2015-12-01 11:12:05 -0800 | [diff] [blame] | 539 | if (!c_filename) { |
| 540 | return false; |
| 541 | } |
| 542 | return this->isAccessible(c_filename); |
| 543 | } |
| 544 | |
| 545 | // Find matching font from |font_set| for the given font family. |
| 546 | FcPattern* SkFontConfigInterfaceDirect::MatchFont(FcFontSet* font_set, |
| 547 | const char* post_config_family, |
| 548 | const SkString& family) { |
| 549 | // Older versions of fontconfig have a bug where they cannot select |
| 550 | // only scalable fonts so we have to manually filter the results. |
| 551 | FcPattern* match = nullptr; |
| 552 | for (int i = 0; i < font_set->nfont; ++i) { |
| 553 | FcPattern* current = font_set->fonts[i]; |
| 554 | if (this->isValidPattern(current)) { |
| 555 | match = current; |
| 556 | break; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | if (match && !IsFallbackFontAllowed(family)) { |
| 561 | bool acceptable_substitute = false; |
| 562 | for (int id = 0; id < 255; ++id) { |
bungeman | 875b8f6 | 2016-09-16 13:19:49 -0700 | [diff] [blame] | 563 | const char* post_match_family = get_string(match, FC_FAMILY, id); |
benjaminwagner | 2211a7b | 2015-12-01 11:12:05 -0800 | [diff] [blame] | 564 | if (!post_match_family) |
| 565 | break; |
| 566 | acceptable_substitute = |
| 567 | (strcasecmp(post_config_family, post_match_family) == 0 || |
| 568 | // Workaround for Issue 12530: |
| 569 | // requested family: "Bitstream Vera Sans" |
| 570 | // post_config_family: "Arial" |
| 571 | // post_match_family: "Bitstream Vera Sans" |
| 572 | // -> We should treat this case as a good match. |
| 573 | strcasecmp(family.c_str(), post_match_family) == 0) || |
| 574 | IsMetricCompatibleReplacement(family.c_str(), post_match_family); |
| 575 | if (acceptable_substitute) |
| 576 | break; |
| 577 | } |
| 578 | if (!acceptable_substitute) |
| 579 | return nullptr; |
| 580 | } |
| 581 | |
| 582 | return match; |
| 583 | } |
| 584 | |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 585 | bool SkFontConfigInterfaceDirect::matchFamilyName(const char familyName[], |
| 586 | SkFontStyle style, |
| 587 | FontIdentity* outIdentity, |
| 588 | SkString* outFamilyName, |
| 589 | SkFontStyle* outStyle) { |
mtklein | ba59a67 | 2014-08-04 10:18:27 -0700 | [diff] [blame] | 590 | SkString familyStr(familyName ? familyName : ""); |
| 591 | if (familyStr.size() > kMaxFontFamilyLength) { |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 592 | return false; |
| 593 | } |
| 594 | |
bungeman | 0265707 | 2016-05-02 11:54:13 -0700 | [diff] [blame] | 595 | FCLocker lock; |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 596 | |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 597 | FcPattern* pattern = FcPatternCreate(); |
| 598 | |
reed@google.com | ee619a0 | 2013-02-26 22:58:09 +0000 | [diff] [blame] | 599 | if (familyName) { |
| 600 | FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName); |
| 601 | } |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 602 | fcpattern_from_skfontstyle(style, pattern); |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 603 | |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 604 | FcPatternAddBool(pattern, FC_SCALABLE, FcTrue); |
| 605 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 606 | FcConfigSubstitute(nullptr, pattern, FcMatchPattern); |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 607 | FcDefaultSubstitute(pattern); |
| 608 | |
| 609 | // Font matching: |
| 610 | // CSS often specifies a fallback list of families: |
| 611 | // font-family: a, b, c, serif; |
| 612 | // However, fontconfig will always do its best to find *a* font when asked |
| 613 | // for something so we need a way to tell if the match which it has found is |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 614 | // "good enough" for us. Otherwise, we can return nullptr which gets piped up |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 615 | // and lets WebKit know to try the next CSS family name. However, fontconfig |
| 616 | // configs allow substitutions (mapping "Arial -> Helvetica" etc) and we |
| 617 | // wish to support that. |
| 618 | // |
| 619 | // Thus, if a specific family is requested we set @family_requested. Then we |
| 620 | // record two strings: the family name after config processing and the |
| 621 | // family name after resolving. If the two are equal, it's a good match. |
| 622 | // |
| 623 | // So consider the case where a user has mapped Arial to Helvetica in their |
| 624 | // config. |
| 625 | // requested family: "Arial" |
| 626 | // post_config_family: "Helvetica" |
| 627 | // post_match_family: "Helvetica" |
| 628 | // -> good match |
| 629 | // |
| 630 | // and for a missing font: |
| 631 | // requested family: "Monaco" |
| 632 | // post_config_family: "Monaco" |
| 633 | // post_match_family: "Times New Roman" |
| 634 | // -> BAD match |
| 635 | // |
| 636 | // However, we special-case fallback fonts; see IsFallbackFontAllowed(). |
reed@google.com | e49d67e | 2013-04-22 18:00:06 +0000 | [diff] [blame] | 637 | |
bungeman | 875b8f6 | 2016-09-16 13:19:49 -0700 | [diff] [blame] | 638 | const char* post_config_family = get_string(pattern, FC_FAMILY); |
reed@google.com | e49d67e | 2013-04-22 18:00:06 +0000 | [diff] [blame] | 639 | if (!post_config_family) { |
reed@google.com | ab79282 | 2013-04-23 16:35:09 +0000 | [diff] [blame] | 640 | // we can just continue with an empty name, e.g. default font |
| 641 | post_config_family = ""; |
reed@google.com | e49d67e | 2013-04-22 18:00:06 +0000 | [diff] [blame] | 642 | } |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 643 | |
| 644 | FcResult result; |
bungeman | 6d195b2 | 2016-05-24 08:08:20 -0700 | [diff] [blame] | 645 | FcFontSet* font_set = FcFontSort(nullptr, pattern, 0, nullptr, &result); |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 646 | if (!font_set) { |
| 647 | FcPatternDestroy(pattern); |
| 648 | return false; |
| 649 | } |
| 650 | |
benjaminwagner | 2211a7b | 2015-12-01 11:12:05 -0800 | [diff] [blame] | 651 | FcPattern* match = this->MatchFont(font_set, post_config_family, familyStr); |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 652 | if (!match) { |
| 653 | FcPatternDestroy(pattern); |
| 654 | FcFontSetDestroy(font_set); |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | FcPatternDestroy(pattern); |
| 659 | |
reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 660 | // From here out we just extract our results from 'match' |
| 661 | |
bungeman | 875b8f6 | 2016-09-16 13:19:49 -0700 | [diff] [blame] | 662 | post_config_family = get_string(match, FC_FAMILY); |
reed@google.com | e49d67e | 2013-04-22 18:00:06 +0000 | [diff] [blame] | 663 | if (!post_config_family) { |
reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 664 | FcFontSetDestroy(font_set); |
| 665 | return false; |
| 666 | } |
| 667 | |
bungeman | 875b8f6 | 2016-09-16 13:19:49 -0700 | [diff] [blame] | 668 | const char* c_filename = get_string(match, FC_FILE); |
reed@google.com | e49d67e | 2013-04-22 18:00:06 +0000 | [diff] [blame] | 669 | if (!c_filename) { |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 670 | FcFontSetDestroy(font_set); |
| 671 | return false; |
| 672 | } |
reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 673 | |
bungeman | 875b8f6 | 2016-09-16 13:19:49 -0700 | [diff] [blame] | 674 | int face_index = get_int(match, FC_INDEX, 0); |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 675 | |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 676 | FcFontSetDestroy(font_set); |
| 677 | |
reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 678 | if (outIdentity) { |
reed@google.com | 8c9737e | 2013-03-06 13:06:03 +0000 | [diff] [blame] | 679 | outIdentity->fTTCIndex = face_index; |
reed@google.com | e49d67e | 2013-04-22 18:00:06 +0000 | [diff] [blame] | 680 | outIdentity->fString.set(c_filename); |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 681 | } |
reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 682 | if (outFamilyName) { |
reed@google.com | e49d67e | 2013-04-22 18:00:06 +0000 | [diff] [blame] | 683 | outFamilyName->set(post_config_family); |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 684 | } |
reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 685 | if (outStyle) { |
bungeman | ed2edab | 2016-04-13 05:23:35 -0700 | [diff] [blame] | 686 | *outStyle = skfontstyle_from_fcpattern(match); |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 687 | } |
reed@google.com | ee619a0 | 2013-02-26 22:58:09 +0000 | [diff] [blame] | 688 | return true; |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 689 | } |
| 690 | |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 691 | SkStreamAsset* SkFontConfigInterfaceDirect::openStream(const FontIdentity& identity) { |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 692 | return SkStream::MakeFromFile(identity.fString.c_str()).release(); |
reed@google.com | b1c65b6 | 2013-02-26 15:50:51 +0000 | [diff] [blame] | 693 | } |