reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* libs/graphics/ports/SkFontHost_android.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include "SkFontHost.h" |
| 19 | #include "SkDescriptor.h" |
| 20 | #include "SkMMapStream.h" |
| 21 | #include "SkOSFile.h" |
| 22 | #include "SkPaint.h" |
| 23 | #include "SkString.h" |
| 24 | #include "SkStream.h" |
| 25 | #include "SkThread.h" |
| 26 | #include "SkTSearch.h" |
| 27 | #include <stdio.h> |
| 28 | |
| 29 | #define FONT_CACHE_MEMORY_BUDGET (1 * 1024 * 1024) |
| 30 | |
| 31 | #ifndef SK_FONT_FILE_PREFIX |
| 32 | #define SK_FONT_FILE_PREFIX "/usr/share/fonts/truetype/msttcorefonts/" |
| 33 | #endif |
| 34 | |
| 35 | SkTypeface::Style find_name_and_style(SkStream* stream, SkString* name); |
| 36 | |
| 37 | static void GetFullPathForSysFonts(SkString* full, const char name[]) |
| 38 | { |
| 39 | full->append(SK_FONT_FILE_PREFIX); |
| 40 | full->append(name); |
| 41 | } |
| 42 | |
| 43 | /////////////////////////////////////////////////////////////////////////////// |
| 44 | |
| 45 | struct FamilyRec; |
| 46 | |
| 47 | /* This guy holds a mapping of a name -> family, used for looking up fonts. |
| 48 | Since it is stored in a stretchy array that doesn't preserve object |
| 49 | semantics, we don't use constructor/destructors, but just have explicit |
| 50 | helpers to manage our internal bookkeeping. |
| 51 | */ |
| 52 | struct NameFamilyPair { |
| 53 | const char* fName; // we own this |
| 54 | FamilyRec* fFamily; // we don't own this, we just reference it |
| 55 | |
| 56 | void construct(const char name[], FamilyRec* family) |
| 57 | { |
| 58 | fName = strdup(name); |
| 59 | fFamily = family; // we don't own this, so just record the referene |
| 60 | } |
| 61 | void destruct() |
| 62 | { |
| 63 | free((char*)fName); |
| 64 | // we don't own family, so just ignore our reference |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | // we use atomic_inc to grow this for each typeface we create |
| 69 | static int32_t gUniqueFontID; |
| 70 | |
| 71 | // this is the mutex that protects these globals |
| 72 | static SkMutex gFamilyMutex; |
| 73 | static FamilyRec* gFamilyHead; |
| 74 | static SkTDArray<NameFamilyPair> gNameList; |
| 75 | |
| 76 | struct FamilyRec { |
| 77 | FamilyRec* fNext; |
| 78 | SkTypeface* fFaces[4]; |
| 79 | |
| 80 | FamilyRec() |
| 81 | { |
| 82 | fNext = gFamilyHead; |
| 83 | memset(fFaces, 0, sizeof(fFaces)); |
| 84 | gFamilyHead = this; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | static SkTypeface* find_best_face(const FamilyRec* family, |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 89 | SkTypeface::Style style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | SkTypeface* const* faces = family->fFaces; |
| 91 | |
| 92 | if (faces[style] != NULL) { // exact match |
| 93 | return faces[style]; |
| 94 | } |
| 95 | // look for a matching bold |
| 96 | style = (SkTypeface::Style)(style ^ SkTypeface::kItalic); |
| 97 | if (faces[style] != NULL) { |
| 98 | return faces[style]; |
| 99 | } |
| 100 | // look for the plain |
| 101 | if (faces[SkTypeface::kNormal] != NULL) { |
| 102 | return faces[SkTypeface::kNormal]; |
| 103 | } |
| 104 | // look for anything |
| 105 | for (int i = 0; i < 4; i++) { |
| 106 | if (faces[i] != NULL) { |
| 107 | return faces[i]; |
| 108 | } |
| 109 | } |
| 110 | // should never get here, since the faces list should not be empty |
| 111 | SkASSERT(!"faces list is empty"); |
| 112 | return NULL; |
| 113 | } |
| 114 | |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 115 | static FamilyRec* find_family(const SkTypeface* member) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 116 | FamilyRec* curr = gFamilyHead; |
| 117 | while (curr != NULL) { |
| 118 | for (int i = 0; i < 4; i++) { |
| 119 | if (curr->fFaces[i] == member) { |
| 120 | return curr; |
| 121 | } |
| 122 | } |
| 123 | curr = curr->fNext; |
| 124 | } |
| 125 | return NULL; |
| 126 | } |
| 127 | |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 128 | static SkTypeface* find_from_uniqueID(uint32_t uniqueID) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | FamilyRec* curr = gFamilyHead; |
| 130 | while (curr != NULL) { |
| 131 | for (int i = 0; i < 4; i++) { |
| 132 | SkTypeface* face = curr->fFaces[i]; |
| 133 | if (face != NULL && face->uniqueID() == uniqueID) { |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 134 | return face; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | curr = curr->fNext; |
| 138 | } |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 139 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | } |
| 141 | |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 142 | static bool valid_uniqueID(uint32_t uniqueID) { |
| 143 | return find_from_uniqueID(uniqueID) != NULL; |
| 144 | } |
| 145 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | /* Remove reference to this face from its family. If the resulting family |
| 147 | is empty (has no faces), return that family, otherwise return NULL |
| 148 | */ |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 149 | static FamilyRec* remove_from_family(const SkTypeface* face) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | FamilyRec* family = find_family(face); |
| 151 | SkASSERT(family->fFaces[face->style()] == face); |
| 152 | family->fFaces[face->style()] = NULL; |
| 153 | |
| 154 | for (int i = 0; i < 4; i++) { |
| 155 | if (family->fFaces[i] != NULL) { // family is non-empty |
| 156 | return NULL; |
| 157 | } |
| 158 | } |
| 159 | return family; // return the empty family |
| 160 | } |
| 161 | |
| 162 | // maybe we should make FamilyRec be doubly-linked |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 163 | static void detach_and_delete_family(FamilyRec* family) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | FamilyRec* curr = gFamilyHead; |
| 165 | FamilyRec* prev = NULL; |
| 166 | |
| 167 | while (curr != NULL) { |
| 168 | FamilyRec* next = curr->fNext; |
| 169 | if (curr == family) { |
| 170 | if (prev == NULL) { |
| 171 | gFamilyHead = next; |
| 172 | } else { |
| 173 | prev->fNext = next; |
| 174 | } |
| 175 | SkDELETE(family); |
| 176 | return; |
| 177 | } |
| 178 | prev = curr; |
| 179 | curr = next; |
| 180 | } |
| 181 | SkASSERT(!"Yikes, couldn't find family in our list to remove/delete"); |
| 182 | } |
| 183 | |
| 184 | static FamilyRec* find_familyrec(const char name[]) { |
| 185 | const NameFamilyPair* list = gNameList.begin(); |
| 186 | int index = SkStrLCSearch(&list[0].fName, gNameList.count(), name, |
| 187 | sizeof(list[0])); |
| 188 | return index >= 0 ? list[index].fFamily : NULL; |
| 189 | } |
| 190 | |
| 191 | static SkTypeface* find_typeface(const char name[], SkTypeface::Style style) { |
| 192 | FamilyRec* rec = find_familyrec(name); |
| 193 | return rec ? find_best_face(rec, style) : NULL; |
| 194 | } |
| 195 | |
| 196 | static SkTypeface* find_typeface(const SkTypeface* familyMember, |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 197 | SkTypeface::Style style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 198 | const FamilyRec* family = find_family(familyMember); |
| 199 | return family ? find_best_face(family, style) : NULL; |
| 200 | } |
| 201 | |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 202 | static void add_name(const char name[], FamilyRec* family) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 203 | SkAutoAsciiToLC tolc(name); |
| 204 | name = tolc.lc(); |
| 205 | |
| 206 | NameFamilyPair* list = gNameList.begin(); |
| 207 | int count = gNameList.count(); |
| 208 | |
| 209 | int index = SkStrLCSearch(&list[0].fName, count, name, sizeof(list[0])); |
| 210 | |
| 211 | if (index < 0) { |
| 212 | list = gNameList.insert(~index); |
| 213 | list->construct(name, family); |
| 214 | } |
| 215 | } |
| 216 | |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 217 | static void remove_from_names(FamilyRec* emptyFamily) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 218 | #ifdef SK_DEBUG |
| 219 | for (int i = 0; i < 4; i++) { |
| 220 | SkASSERT(emptyFamily->fFaces[i] == NULL); |
| 221 | } |
| 222 | #endif |
| 223 | |
| 224 | SkTDArray<NameFamilyPair>& list = gNameList; |
| 225 | |
| 226 | // must go backwards when removing |
| 227 | for (int i = list.count() - 1; i >= 0; --i) { |
| 228 | NameFamilyPair* pair = &list[i]; |
| 229 | if (pair->fFamily == emptyFamily) { |
| 230 | pair->destruct(); |
| 231 | list.remove(i); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /////////////////////////////////////////////////////////////////////////////// |
| 237 | |
| 238 | class FamilyTypeface : public SkTypeface { |
| 239 | public: |
| 240 | FamilyTypeface(Style style, bool sysFont, FamilyRec* family) |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 241 | : SkTypeface(style, sk_atomic_inc(&gUniqueFontID) + 1) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 242 | fIsSysFont = sysFont; |
| 243 | |
| 244 | SkAutoMutexAcquire ac(gFamilyMutex); |
| 245 | |
| 246 | if (NULL == family) { |
| 247 | family = SkNEW(FamilyRec); |
| 248 | } |
| 249 | family->fFaces[style] = this; |
| 250 | fFamilyRec = family; // just record it so we can return it if asked |
| 251 | } |
| 252 | |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 253 | virtual ~FamilyTypeface() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 254 | SkAutoMutexAcquire ac(gFamilyMutex); |
| 255 | |
| 256 | // remove us from our family. If the family is now empty, we return |
| 257 | // that and then remove that family from the name list |
| 258 | FamilyRec* family = remove_from_family(this); |
| 259 | if (NULL != family) { |
| 260 | remove_from_names(family); |
| 261 | detach_and_delete_family(family); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | bool isSysFont() const { return fIsSysFont; } |
| 266 | FamilyRec* getFamily() const { return fFamilyRec; } |
reed@android.com | 22dbaaf | 2009-05-18 00:43:58 +0000 | [diff] [blame] | 267 | // openStream returns a SkStream that has been ref-ed |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 268 | virtual SkStream* openStream() = 0; |
| 269 | virtual void closeStream(SkStream*) = 0; |
| 270 | virtual const char* getUniqueString() const = 0; |
| 271 | |
| 272 | private: |
| 273 | FamilyRec* fFamilyRec; // we don't own this, just point to it |
| 274 | bool fIsSysFont; |
| 275 | |
| 276 | typedef SkTypeface INHERITED; |
| 277 | }; |
| 278 | |
| 279 | /////////////////////////////////////////////////////////////////////////////// |
| 280 | |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame^] | 281 | /* This subclass is just a place holder for when we have no fonts available. |
| 282 | It exists so that our globals (e.g. gFamilyHead) that expect *something* |
| 283 | will not be null. |
| 284 | */ |
| 285 | class EmptyTypeface : public FamilyTypeface { |
| 286 | public: |
| 287 | EmptyTypeface() : INHERITED(SkTypeface::kNormal, true, NULL) {} |
| 288 | |
| 289 | // overrides |
| 290 | virtual SkStream* openStream() { return NULL; } |
| 291 | virtual void closeStream(SkStream*) {} |
| 292 | virtual const char* getUniqueString() const { return NULL; } |
| 293 | |
| 294 | private: |
| 295 | typedef FamilyTypeface INHERITED; |
| 296 | }; |
| 297 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 298 | class StreamTypeface : public FamilyTypeface { |
| 299 | public: |
| 300 | StreamTypeface(Style style, bool sysFont, FamilyRec* family, |
| 301 | SkStream* stream) |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 302 | : INHERITED(style, sysFont, family) { |
reed@android.com | 1c0c5a0 | 2010-04-12 20:16:49 +0000 | [diff] [blame] | 303 | stream->ref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 304 | fStream = stream; |
| 305 | } |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 306 | virtual ~StreamTypeface() { |
reed@android.com | 1c0c5a0 | 2010-04-12 20:16:49 +0000 | [diff] [blame] | 307 | fStream->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // overrides |
reed@android.com | 22dbaaf | 2009-05-18 00:43:58 +0000 | [diff] [blame] | 311 | virtual SkStream* openStream() |
| 312 | { |
| 313 | // openStream returns a refed stream. |
| 314 | fStream->ref(); |
| 315 | return fStream; |
| 316 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 317 | virtual void closeStream(SkStream*) {} |
| 318 | virtual const char* getUniqueString() const { return NULL; } |
| 319 | |
| 320 | private: |
| 321 | SkStream* fStream; |
| 322 | |
| 323 | typedef FamilyTypeface INHERITED; |
| 324 | }; |
| 325 | |
| 326 | class FileTypeface : public FamilyTypeface { |
| 327 | public: |
| 328 | FileTypeface(Style style, bool sysFont, FamilyRec* family, |
| 329 | const char path[]) |
| 330 | : INHERITED(style, sysFont, family) { |
| 331 | fPath.set(path); |
| 332 | } |
| 333 | |
| 334 | // overrides |
| 335 | virtual SkStream* openStream() |
| 336 | { |
| 337 | SkStream* stream = SkNEW_ARGS(SkMMAPStream, (fPath.c_str())); |
| 338 | |
| 339 | // check for failure |
| 340 | if (stream->getLength() <= 0) { |
| 341 | SkDELETE(stream); |
| 342 | // maybe MMAP isn't supported. try FILE |
| 343 | stream = SkNEW_ARGS(SkFILEStream, (fPath.c_str())); |
| 344 | if (stream->getLength() <= 0) { |
| 345 | SkDELETE(stream); |
| 346 | stream = NULL; |
| 347 | } |
| 348 | } |
| 349 | return stream; |
| 350 | } |
| 351 | virtual void closeStream(SkStream* stream) |
| 352 | { |
| 353 | SkDELETE(stream); |
| 354 | } |
| 355 | virtual const char* getUniqueString() const { |
| 356 | const char* str = strrchr(fPath.c_str(), '/'); |
| 357 | if (str) { |
| 358 | str += 1; // skip the '/' |
| 359 | } |
| 360 | return str; |
| 361 | } |
| 362 | |
| 363 | private: |
| 364 | SkString fPath; |
| 365 | |
| 366 | typedef FamilyTypeface INHERITED; |
| 367 | }; |
| 368 | |
| 369 | /////////////////////////////////////////////////////////////////////////////// |
| 370 | /////////////////////////////////////////////////////////////////////////////// |
| 371 | |
| 372 | static bool get_name_and_style(const char path[], SkString* name, |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 373 | SkTypeface::Style* style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 374 | SkMMAPStream stream(path); |
| 375 | if (stream.getLength() > 0) { |
| 376 | *style = find_name_and_style(&stream, name); |
| 377 | return true; |
| 378 | } |
| 379 | else { |
| 380 | SkFILEStream stream(path); |
| 381 | if (stream.getLength() > 0) { |
| 382 | *style = find_name_and_style(&stream, name); |
| 383 | return true; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | SkDebugf("---- failed to open <%s> as a font\n", path); |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | // these globals are assigned (once) by load_system_fonts() |
| 392 | static SkTypeface* gFallBackTypeface; |
| 393 | static FamilyRec* gDefaultFamily; |
| 394 | static SkTypeface* gDefaultNormal; |
| 395 | |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 396 | static void load_system_fonts() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 397 | // check if we've already be called |
| 398 | if (NULL != gDefaultNormal) { |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 399 | printf("---- default font %p\n", gDefaultNormal); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 400 | return; |
| 401 | } |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 402 | |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame^] | 403 | SkOSFile::Iter iter(SK_FONT_FILE_PREFIX, ".ttf"); |
| 404 | SkString name; |
| 405 | int count = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 406 | |
| 407 | while (iter.next(&name, false)) { |
| 408 | SkString filename; |
| 409 | GetFullPathForSysFonts(&filename, name.c_str()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 410 | |
| 411 | SkString realname; |
| 412 | SkTypeface::Style style; |
| 413 | |
| 414 | if (!get_name_and_style(filename.c_str(), &realname, &style)) { |
| 415 | SkDebugf("------ can't load <%s> as a font\n", filename.c_str()); |
| 416 | continue; |
| 417 | } |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 418 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 419 | // SkDebugf("font: <%s> %d <%s>\n", realname.c_str(), style, filename.c_str()); |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 420 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 421 | FamilyRec* family = find_familyrec(realname.c_str()); |
reed@android.com | 887e4f3 | 2010-04-15 14:04:52 +0000 | [diff] [blame] | 422 | if (family && family->fFaces[style]) { |
| 423 | // SkDebugf("---- skipping duplicate typeface %s style %d\n", |
| 424 | // realname.c_str(), style); |
| 425 | continue; |
| 426 | } |
| 427 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 428 | // this constructor puts us into the global gFamilyHead llist |
| 429 | FamilyTypeface* tf = SkNEW_ARGS(FileTypeface, |
| 430 | (style, |
| 431 | true, // system-font (cannot delete) |
| 432 | family, // what family to join |
| 433 | filename.c_str()) // filename |
| 434 | ); |
| 435 | |
| 436 | if (NULL == family) { |
| 437 | add_name(realname.c_str(), tf->getFamily()); |
| 438 | } |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame^] | 439 | count += 1; |
| 440 | } |
| 441 | |
| 442 | if (0 == count) { |
| 443 | SkNEW(EmptyTypeface); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 444 | } |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 445 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 446 | // do this after all fonts are loaded. This is our default font, and it |
| 447 | // acts as a sentinel so we only execute load_system_fonts() once |
| 448 | static const char* gDefaultNames[] = { |
| 449 | "Arial", "Verdana", "Times New Roman", NULL |
| 450 | }; |
| 451 | const char** names = gDefaultNames; |
| 452 | while (*names) { |
| 453 | SkTypeface* tf = find_typeface(*names++, SkTypeface::kNormal); |
| 454 | if (tf) { |
| 455 | gDefaultNormal = tf; |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | // check if we found *something* |
| 460 | if (NULL == gDefaultNormal) { |
| 461 | if (NULL == gFamilyHead) { |
| 462 | sk_throw(); |
| 463 | } |
| 464 | for (int i = 0; i < 4; i++) { |
| 465 | if ((gDefaultNormal = gFamilyHead->fFaces[i]) != NULL) { |
| 466 | break; |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | if (NULL == gDefaultNormal) { |
| 471 | sk_throw(); |
| 472 | } |
| 473 | gFallBackTypeface = gDefaultNormal; |
| 474 | gDefaultFamily = find_family(gDefaultNormal); |
| 475 | |
| 476 | // SkDebugf("---- default %p head %p family %p\n", gDefaultNormal, gFamilyHead, gDefaultFamily); |
| 477 | } |
| 478 | |
| 479 | /////////////////////////////////////////////////////////////////////////////// |
| 480 | |
| 481 | void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) { |
| 482 | #if 0 |
| 483 | const char* name = ((FamilyTypeface*)face)->getUniqueString(); |
| 484 | |
| 485 | stream->write8((uint8_t)face->getStyle()); |
| 486 | |
| 487 | if (NULL == name || 0 == *name) { |
| 488 | stream->writePackedUInt(0); |
| 489 | // SkDebugf("--- fonthost serialize null\n"); |
| 490 | } else { |
| 491 | uint32_t len = strlen(name); |
| 492 | stream->writePackedUInt(len); |
| 493 | stream->write(name, len); |
| 494 | // SkDebugf("--- fonthost serialize <%s> %d\n", name, face->getStyle()); |
| 495 | } |
| 496 | #endif |
| 497 | sk_throw(); |
| 498 | } |
| 499 | |
| 500 | SkTypeface* SkFontHost::Deserialize(SkStream* stream) { |
| 501 | #if 0 |
| 502 | load_system_fonts(); |
| 503 | |
| 504 | int style = stream->readU8(); |
| 505 | |
| 506 | int len = stream->readPackedUInt(); |
| 507 | if (len > 0) { |
| 508 | SkString str; |
| 509 | str.resize(len); |
| 510 | stream->read(str.writable_str(), len); |
| 511 | |
| 512 | const FontInitRec* rec = gSystemFonts; |
| 513 | for (size_t i = 0; i < SK_ARRAY_COUNT(gSystemFonts); i++) { |
| 514 | if (strcmp(rec[i].fFileName, str.c_str()) == 0) { |
| 515 | // backup until we hit the fNames |
| 516 | for (int j = i; j >= 0; --j) { |
| 517 | if (rec[j].fNames != NULL) { |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 518 | return SkFontHost::CreateTypeface(NULL, rec[j].fNames[0], |
| 519 | (SkTypeface::Style)style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | } |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 525 | return SkFontHost::CreateTypeface(NULL, NULL, (SkTypeface::Style)style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 526 | #endif |
| 527 | sk_throw(); |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 528 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /////////////////////////////////////////////////////////////////////////////// |
| 532 | |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 533 | SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace, |
| 534 | const char familyName[], |
| 535 | SkTypeface::Style style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 536 | load_system_fonts(); |
| 537 | |
| 538 | SkAutoMutexAcquire ac(gFamilyMutex); |
| 539 | |
| 540 | // clip to legal style bits |
| 541 | style = (SkTypeface::Style)(style & SkTypeface::kBoldItalic); |
| 542 | |
| 543 | SkTypeface* tf = NULL; |
| 544 | |
| 545 | if (NULL != familyFace) { |
| 546 | tf = find_typeface(familyFace, style); |
| 547 | } else if (NULL != familyName) { |
| 548 | // SkDebugf("======= familyName <%s>\n", familyName); |
| 549 | tf = find_typeface(familyName, style); |
| 550 | } |
| 551 | |
| 552 | if (NULL == tf) { |
| 553 | tf = find_best_face(gDefaultFamily, style); |
| 554 | } |
reed@android.com | 887e4f3 | 2010-04-15 14:04:52 +0000 | [diff] [blame] | 555 | |
| 556 | SkSafeRef(tf); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 557 | return tf; |
| 558 | } |
| 559 | |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 560 | bool SkFontHost::ValidFontID(uint32_t fontID) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 561 | SkAutoMutexAcquire ac(gFamilyMutex); |
| 562 | |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 563 | return valid_uniqueID(fontID); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 564 | } |
| 565 | |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 566 | SkStream* SkFontHost::OpenStream(uint32_t fontID) { |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 567 | FamilyTypeface* tf = (FamilyTypeface*)find_from_uniqueID(fontID); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 568 | SkStream* stream = tf ? tf->openStream() : NULL; |
| 569 | |
reed@android.com | 1c0c5a0 | 2010-04-12 20:16:49 +0000 | [diff] [blame] | 570 | if (stream && stream->getLength() == 0) { |
| 571 | stream->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 572 | stream = NULL; |
| 573 | } |
| 574 | return stream; |
| 575 | } |
| 576 | |
reed@android.com | ac98154 | 2009-07-31 16:17:01 +0000 | [diff] [blame] | 577 | size_t SkFontHost::GetFileName(SkFontID fontID, char path[], size_t length, |
| 578 | int32_t* index) { |
| 579 | SkDebugf("SkFontHost::GetFileName unimplemented\n"); |
| 580 | return 0; |
| 581 | } |
| 582 | |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 583 | uint32_t SkFontHost::NextLogicalFont(uint32_t fontID) { |
| 584 | return 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /////////////////////////////////////////////////////////////////////////////// |
| 588 | |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 589 | SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 590 | if (NULL == stream || stream->getLength() <= 0) { |
| 591 | SkDELETE(stream); |
| 592 | return NULL; |
| 593 | } |
| 594 | |
| 595 | SkString name; |
| 596 | SkTypeface::Style style = find_name_and_style(stream, &name); |
| 597 | |
| 598 | return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream)); |
| 599 | } |
| 600 | |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 601 | SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { |
| 602 | SkTypeface* face = NULL; |
| 603 | SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path)); |
| 604 | |
| 605 | if (stream->isValid()) { |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 606 | face = CreateTypefaceFromStream(stream); |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 607 | } |
| 608 | stream->unref(); |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 609 | return face; |
reed@android.com | 0becfc5b | 2009-01-13 13:26:44 +0000 | [diff] [blame] | 610 | } |
| 611 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 612 | /////////////////////////////////////////////////////////////////////////////// |
| 613 | |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 614 | size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 615 | if (sizeAllocatedSoFar > FONT_CACHE_MEMORY_BUDGET) |
| 616 | return sizeAllocatedSoFar - FONT_CACHE_MEMORY_BUDGET; |
| 617 | else |
| 618 | return 0; // nothing to do |
| 619 | } |
| 620 | |