epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 1 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 3 | * Copyright 2011 The Android Open Source Project |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 9 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 10 | #include "SkFontHost.h" |
| 11 | #include "SkDescriptor.h" |
| 12 | #include "SkMMapStream.h" |
| 13 | #include "SkPaint.h" |
| 14 | #include "SkString.h" |
| 15 | #include "SkStream.h" |
| 16 | #include "SkThread.h" |
| 17 | #include "SkTSearch.h" |
| 18 | #include <stdio.h> |
| 19 | |
| 20 | #define FONT_CACHE_MEMORY_BUDGET (768 * 1024) |
| 21 | |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 22 | #ifdef SK_BUILD_FOR_MAC |
| 23 | #define SK_FONT_FILE_PREFIX "/Library/Fonts/" |
| 24 | #else |
| 25 | #define SK_FONT_FILE_PREFIX "/skimages/" |
| 26 | #endif |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 27 | |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 28 | SkTypeface::Style find_name_and_attributes(SkStream* stream, SkString* name, |
| 29 | bool* isFixedWidth); |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 30 | |
| 31 | static void GetFullPathForSysFonts(SkString* full, const char name[]) { |
| 32 | full->set(SK_FONT_FILE_PREFIX); |
| 33 | full->append(name); |
| 34 | } |
| 35 | |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | |
| 38 | struct FamilyRec; |
| 39 | |
| 40 | /* This guy holds a mapping of a name -> family, used for looking up fonts. |
| 41 | Since it is stored in a stretchy array that doesn't preserve object |
| 42 | semantics, we don't use constructor/destructors, but just have explicit |
| 43 | helpers to manage our internal bookkeeping. |
| 44 | */ |
| 45 | struct NameFamilyPair { |
| 46 | const char* fName; // we own this |
| 47 | FamilyRec* fFamily; // we don't own this, we just reference it |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 48 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 49 | void construct(const char name[], FamilyRec* family) { |
| 50 | fName = strdup(name); |
| 51 | fFamily = family; // we don't own this, so just record the referene |
| 52 | } |
| 53 | |
| 54 | void destruct() { |
| 55 | free((char*)fName); |
| 56 | // we don't own family, so just ignore our reference |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | // we use atomic_inc to grow this for each typeface we create |
| 61 | static int32_t gUniqueFontID; |
| 62 | |
| 63 | // this is the mutex that protects these globals |
| 64 | static SkMutex gFamilyMutex; |
| 65 | static FamilyRec* gFamilyHead; |
| 66 | static SkTDArray<NameFamilyPair> gNameList; |
| 67 | |
| 68 | struct FamilyRec { |
| 69 | FamilyRec* fNext; |
| 70 | SkTypeface* fFaces[4]; |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 71 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 72 | FamilyRec() |
| 73 | { |
| 74 | fNext = gFamilyHead; |
| 75 | memset(fFaces, 0, sizeof(fFaces)); |
| 76 | gFamilyHead = this; |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | static SkTypeface* find_best_face(const FamilyRec* family, |
| 81 | SkTypeface::Style style) { |
| 82 | SkTypeface* const* faces = family->fFaces; |
| 83 | |
| 84 | if (faces[style] != NULL) { // exact match |
| 85 | return faces[style]; |
| 86 | } |
| 87 | // look for a matching bold |
| 88 | style = (SkTypeface::Style)(style ^ SkTypeface::kItalic); |
| 89 | if (faces[style] != NULL) { |
| 90 | return faces[style]; |
| 91 | } |
| 92 | // look for the plain |
| 93 | if (faces[SkTypeface::kNormal] != NULL) { |
| 94 | return faces[SkTypeface::kNormal]; |
| 95 | } |
| 96 | // look for anything |
| 97 | for (int i = 0; i < 4; i++) { |
| 98 | if (faces[i] != NULL) { |
| 99 | return faces[i]; |
| 100 | } |
| 101 | } |
| 102 | // should never get here, since the faces list should not be empty |
| 103 | SkASSERT(!"faces list is empty"); |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | static FamilyRec* find_family(const SkTypeface* member) { |
| 108 | FamilyRec* curr = gFamilyHead; |
| 109 | while (curr != NULL) { |
| 110 | for (int i = 0; i < 4; i++) { |
| 111 | if (curr->fFaces[i] == member) { |
| 112 | return curr; |
| 113 | } |
| 114 | } |
| 115 | curr = curr->fNext; |
| 116 | } |
| 117 | return NULL; |
| 118 | } |
| 119 | |
| 120 | /* Returns the matching typeface, or NULL. If a typeface is found, its refcnt |
| 121 | is not modified. |
| 122 | */ |
| 123 | static SkTypeface* find_from_uniqueID(uint32_t uniqueID) { |
| 124 | FamilyRec* curr = gFamilyHead; |
| 125 | while (curr != NULL) { |
| 126 | for (int i = 0; i < 4; i++) { |
| 127 | SkTypeface* face = curr->fFaces[i]; |
| 128 | if (face != NULL && face->uniqueID() == uniqueID) { |
| 129 | return face; |
| 130 | } |
| 131 | } |
| 132 | curr = curr->fNext; |
| 133 | } |
| 134 | return NULL; |
| 135 | } |
| 136 | |
| 137 | /* Remove reference to this face from its family. If the resulting family |
| 138 | is empty (has no faces), return that family, otherwise return NULL |
| 139 | */ |
| 140 | static FamilyRec* remove_from_family(const SkTypeface* face) { |
| 141 | FamilyRec* family = find_family(face); |
| 142 | SkASSERT(family->fFaces[face->style()] == face); |
| 143 | family->fFaces[face->style()] = NULL; |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 144 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 145 | for (int i = 0; i < 4; i++) { |
| 146 | if (family->fFaces[i] != NULL) { // family is non-empty |
| 147 | return NULL; |
| 148 | } |
| 149 | } |
| 150 | return family; // return the empty family |
| 151 | } |
| 152 | |
| 153 | // maybe we should make FamilyRec be doubly-linked |
| 154 | static void detach_and_delete_family(FamilyRec* family) { |
| 155 | FamilyRec* curr = gFamilyHead; |
| 156 | FamilyRec* prev = NULL; |
| 157 | |
| 158 | while (curr != NULL) { |
| 159 | FamilyRec* next = curr->fNext; |
| 160 | if (curr == family) { |
| 161 | if (prev == NULL) { |
| 162 | gFamilyHead = next; |
| 163 | } else { |
| 164 | prev->fNext = next; |
| 165 | } |
| 166 | SkDELETE(family); |
| 167 | return; |
| 168 | } |
| 169 | prev = curr; |
| 170 | curr = next; |
| 171 | } |
| 172 | SkASSERT(!"Yikes, couldn't find family in our list to remove/delete"); |
| 173 | } |
| 174 | |
| 175 | static SkTypeface* find_typeface(const char name[], SkTypeface::Style style) { |
| 176 | NameFamilyPair* list = gNameList.begin(); |
| 177 | int count = gNameList.count(); |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 178 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 179 | int index = SkStrLCSearch(&list[0].fName, count, name, sizeof(list[0])); |
| 180 | |
| 181 | if (index >= 0) { |
| 182 | return find_best_face(list[index].fFamily, style); |
| 183 | } |
| 184 | return NULL; |
| 185 | } |
| 186 | |
| 187 | static SkTypeface* find_typeface(const SkTypeface* familyMember, |
| 188 | SkTypeface::Style style) { |
| 189 | const FamilyRec* family = find_family(familyMember); |
| 190 | return family ? find_best_face(family, style) : NULL; |
| 191 | } |
| 192 | |
| 193 | static void add_name(const char name[], FamilyRec* family) { |
| 194 | SkAutoAsciiToLC tolc(name); |
| 195 | name = tolc.lc(); |
| 196 | |
| 197 | NameFamilyPair* list = gNameList.begin(); |
| 198 | int count = gNameList.count(); |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 199 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 200 | int index = SkStrLCSearch(&list[0].fName, count, name, sizeof(list[0])); |
| 201 | |
| 202 | if (index < 0) { |
| 203 | list = gNameList.insert(~index); |
| 204 | list->construct(name, family); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | static void remove_from_names(FamilyRec* emptyFamily) |
| 209 | { |
| 210 | #ifdef SK_DEBUG |
| 211 | for (int i = 0; i < 4; i++) { |
| 212 | SkASSERT(emptyFamily->fFaces[i] == NULL); |
| 213 | } |
| 214 | #endif |
| 215 | |
| 216 | SkTDArray<NameFamilyPair>& list = gNameList; |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 217 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 218 | // must go backwards when removing |
| 219 | for (int i = list.count() - 1; i >= 0; --i) { |
| 220 | NameFamilyPair* pair = &list[i]; |
| 221 | if (pair->fFamily == emptyFamily) { |
| 222 | pair->destruct(); |
| 223 | list.remove(i); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /////////////////////////////////////////////////////////////////////////////// |
| 229 | |
| 230 | class FamilyTypeface : public SkTypeface { |
| 231 | public: |
| 232 | FamilyTypeface(Style style, bool sysFont, SkTypeface* familyMember) |
| 233 | : SkTypeface(style, sk_atomic_inc(&gUniqueFontID) + 1) { |
| 234 | fIsSysFont = sysFont; |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 235 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 236 | SkAutoMutexAcquire ac(gFamilyMutex); |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 237 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 238 | FamilyRec* rec = NULL; |
| 239 | if (familyMember) { |
| 240 | rec = find_family(familyMember); |
| 241 | SkASSERT(rec); |
| 242 | } else { |
| 243 | rec = SkNEW(FamilyRec); |
| 244 | } |
| 245 | rec->fFaces[style] = this; |
| 246 | } |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 247 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 248 | virtual ~FamilyTypeface() { |
| 249 | SkAutoMutexAcquire ac(gFamilyMutex); |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 250 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 251 | // remove us from our family. If the family is now empty, we return |
| 252 | // that and then remove that family from the name list |
| 253 | FamilyRec* family = remove_from_family(this); |
| 254 | if (NULL != family) { |
| 255 | remove_from_names(family); |
| 256 | detach_and_delete_family(family); |
| 257 | } |
| 258 | } |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 259 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 260 | bool isSysFont() const { return fIsSysFont; } |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 261 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 262 | virtual SkStream* openStream() = 0; |
| 263 | virtual const char* getUniqueString() const = 0; |
| 264 | virtual const char* getFilePath() const = 0; |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 265 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 266 | private: |
| 267 | bool fIsSysFont; |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 268 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 269 | typedef SkTypeface INHERITED; |
| 270 | }; |
| 271 | |
| 272 | /////////////////////////////////////////////////////////////////////////////// |
| 273 | |
| 274 | class StreamTypeface : public FamilyTypeface { |
| 275 | public: |
| 276 | StreamTypeface(Style style, bool sysFont, SkTypeface* familyMember, |
| 277 | SkStream* stream) |
| 278 | : INHERITED(style, sysFont, familyMember) { |
| 279 | SkASSERT(stream); |
| 280 | stream->ref(); |
| 281 | fStream = stream; |
| 282 | } |
| 283 | virtual ~StreamTypeface() { |
| 284 | fStream->unref(); |
| 285 | } |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 286 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 287 | // overrides |
| 288 | virtual SkStream* openStream() { |
| 289 | // we just ref our existing stream, since the caller will call unref() |
| 290 | // when they are through |
| 291 | fStream->ref(); |
| 292 | return fStream; |
| 293 | } |
| 294 | virtual const char* getUniqueString() const { return NULL; } |
| 295 | virtual const char* getFilePath() const { return NULL; } |
| 296 | |
| 297 | private: |
| 298 | SkStream* fStream; |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 299 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 300 | typedef FamilyTypeface INHERITED; |
| 301 | }; |
| 302 | |
| 303 | class FileTypeface : public FamilyTypeface { |
| 304 | public: |
| 305 | FileTypeface(Style style, bool sysFont, SkTypeface* familyMember, |
| 306 | const char path[]) |
| 307 | : INHERITED(style, sysFont, familyMember) { |
| 308 | SkString fullpath; |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 309 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 310 | if (sysFont) { |
| 311 | GetFullPathForSysFonts(&fullpath, path); |
| 312 | path = fullpath.c_str(); |
| 313 | } |
| 314 | fPath.set(path); |
| 315 | } |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 316 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 317 | // overrides |
| 318 | virtual SkStream* openStream() { |
| 319 | SkStream* stream = SkNEW_ARGS(SkMMAPStream, (fPath.c_str())); |
| 320 | |
| 321 | // check for failure |
| 322 | if (stream->getLength() <= 0) { |
| 323 | SkDELETE(stream); |
| 324 | // maybe MMAP isn't supported. try FILE |
| 325 | stream = SkNEW_ARGS(SkFILEStream, (fPath.c_str())); |
| 326 | if (stream->getLength() <= 0) { |
| 327 | SkDELETE(stream); |
| 328 | stream = NULL; |
| 329 | } |
| 330 | } |
| 331 | return stream; |
| 332 | } |
| 333 | virtual const char* getUniqueString() const { |
| 334 | const char* str = strrchr(fPath.c_str(), '/'); |
| 335 | if (str) { |
| 336 | str += 1; // skip the '/' |
| 337 | } |
| 338 | return str; |
| 339 | } |
| 340 | virtual const char* getFilePath() const { |
| 341 | return fPath.c_str(); |
| 342 | } |
| 343 | |
| 344 | private: |
| 345 | SkString fPath; |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 346 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 347 | typedef FamilyTypeface INHERITED; |
| 348 | }; |
| 349 | |
| 350 | /////////////////////////////////////////////////////////////////////////////// |
| 351 | /////////////////////////////////////////////////////////////////////////////// |
| 352 | |
| 353 | static bool get_name_and_style(const char path[], SkString* name, |
| 354 | SkTypeface::Style* style, bool isExpected) { |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 355 | bool isFixedWidth; |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 356 | SkString fullpath; |
| 357 | GetFullPathForSysFonts(&fullpath, path); |
| 358 | |
| 359 | SkMMAPStream stream(fullpath.c_str()); |
| 360 | if (stream.getLength() > 0) { |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 361 | *style = find_name_and_attributes(&stream, name, &isFixedWidth); |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 362 | return true; |
| 363 | } |
| 364 | else { |
| 365 | SkFILEStream stream(fullpath.c_str()); |
| 366 | if (stream.getLength() > 0) { |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 367 | *style = find_name_and_attributes(&stream, name, &isFixedWidth); |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 368 | return true; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | if (isExpected) { |
| 373 | SkDebugf("---- failed to open <%s> as a font\n", fullpath.c_str()); |
| 374 | } |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | // used to record our notion of the pre-existing fonts |
| 379 | struct FontInitRec { |
| 380 | const char* fFileName; |
| 381 | const char* const* fNames; // null-terminated list |
| 382 | }; |
| 383 | |
| 384 | static const char* gSansNames[] = { |
| 385 | "sans-serif", "arial", "helvetica", "tahoma", "verdana", NULL |
| 386 | }; |
| 387 | |
| 388 | static const char* gSerifNames[] = { |
| 389 | "serif", "times", "times new roman", "palatino", "georgia", "baskerville", |
| 390 | "goudy", "fantasy", "cursive", "ITC Stone Serif", NULL |
| 391 | }; |
| 392 | |
| 393 | static const char* gMonoNames[] = { |
| 394 | "monospace", "courier", "courier new", "monaco", NULL |
| 395 | }; |
| 396 | |
| 397 | // deliberately empty, but we use the address to identify fallback fonts |
| 398 | static const char* gFBNames[] = { NULL }; |
| 399 | |
| 400 | /* Fonts must be grouped by family, with the first font in a family having the |
| 401 | list of names (even if that list is empty), and the following members having |
| 402 | null for the list. The names list must be NULL-terminated |
| 403 | */ |
| 404 | static const FontInitRec gSystemFonts[] = { |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 405 | { "Arial.ttf", gSansNames }, |
| 406 | { "Times.ttf", gSerifNames }, |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 407 | { "samplefont.ttf", gSansNames }, |
| 408 | }; |
| 409 | |
| 410 | #define DEFAULT_NAMES gSansNames |
| 411 | |
| 412 | // these globals are assigned (once) by load_system_fonts() |
| 413 | static FamilyRec* gDefaultFamily; |
| 414 | static SkTypeface* gDefaultNormal; |
| 415 | |
| 416 | /* This is sized conservatively, assuming that it will never be a size issue. |
| 417 | It will be initialized in load_system_fonts(), and will be filled with the |
| 418 | fontIDs that can be used for fallback consideration, in sorted order (sorted |
| 419 | meaning element[0] should be used first, then element[1], etc. When we hit |
| 420 | a fontID==0 in the array, the list is done, hence our allocation size is |
| 421 | +1 the total number of possible system fonts. Also see NextLogicalFont(). |
| 422 | */ |
| 423 | static uint32_t gFallbackFonts[SK_ARRAY_COUNT(gSystemFonts)+1]; |
| 424 | |
| 425 | /* Called once (ensured by the sentinel check at the beginning of our body). |
| 426 | Initializes all the globals, and register the system fonts. |
| 427 | */ |
| 428 | static void load_system_fonts() { |
| 429 | // check if we've already be called |
| 430 | if (NULL != gDefaultNormal) { |
| 431 | return; |
| 432 | } |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 433 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 434 | const FontInitRec* rec = gSystemFonts; |
| 435 | SkTypeface* firstInFamily = NULL; |
| 436 | int fallbackCount = 0; |
| 437 | |
| 438 | for (size_t i = 0; i < SK_ARRAY_COUNT(gSystemFonts); i++) { |
| 439 | // if we're the first in a new family, clear firstInFamily |
| 440 | if (rec[i].fNames != NULL) { |
| 441 | firstInFamily = NULL; |
| 442 | } |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 443 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 444 | SkString name; |
| 445 | SkTypeface::Style style; |
| 446 | |
| 447 | // we expect all the fonts, except the "fallback" fonts |
| 448 | bool isExpected = (rec[i].fNames != gFBNames); |
| 449 | if (!get_name_and_style(rec[i].fFileName, &name, &style, isExpected)) { |
| 450 | continue; |
| 451 | } |
| 452 | |
| 453 | SkTypeface* tf = SkNEW_ARGS(FileTypeface, |
| 454 | (style, |
| 455 | true, // system-font (cannot delete) |
| 456 | firstInFamily, // what family to join |
| 457 | rec[i].fFileName) // filename |
| 458 | ); |
| 459 | |
| 460 | if (rec[i].fNames != NULL) { |
| 461 | // see if this is one of our fallback fonts |
| 462 | if (rec[i].fNames == gFBNames) { |
| 463 | // SkDebugf("---- adding %s as fallback[%d] fontID %d\n", |
| 464 | // rec[i].fFileName, fallbackCount, tf->uniqueID()); |
| 465 | gFallbackFonts[fallbackCount++] = tf->uniqueID(); |
| 466 | } |
| 467 | |
| 468 | firstInFamily = tf; |
| 469 | FamilyRec* family = find_family(tf); |
| 470 | const char* const* names = rec[i].fNames; |
| 471 | |
| 472 | // record the default family if this is it |
| 473 | if (names == DEFAULT_NAMES) { |
| 474 | gDefaultFamily = family; |
| 475 | } |
| 476 | // add the names to map to this family |
| 477 | while (*names) { |
| 478 | add_name(*names, family); |
| 479 | names += 1; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | // do this after all fonts are loaded. This is our default font, and it |
| 485 | // acts as a sentinel so we only execute load_system_fonts() once |
| 486 | gDefaultNormal = find_best_face(gDefaultFamily, SkTypeface::kNormal); |
| 487 | // now terminate our fallback list with the sentinel value |
| 488 | gFallbackFonts[fallbackCount] = 0; |
| 489 | } |
| 490 | |
| 491 | /////////////////////////////////////////////////////////////////////////////// |
| 492 | |
| 493 | void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) { |
| 494 | const char* name = ((FamilyTypeface*)face)->getUniqueString(); |
| 495 | |
| 496 | stream->write8((uint8_t)face->style()); |
| 497 | |
| 498 | if (NULL == name || 0 == *name) { |
| 499 | stream->writePackedUInt(0); |
| 500 | // SkDebugf("--- fonthost serialize null\n"); |
| 501 | } else { |
| 502 | uint32_t len = strlen(name); |
| 503 | stream->writePackedUInt(len); |
| 504 | stream->write(name, len); |
| 505 | // SkDebugf("--- fonthost serialize <%s> %d\n", name, face->style()); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | SkTypeface* SkFontHost::Deserialize(SkStream* stream) { |
| 510 | load_system_fonts(); |
| 511 | |
| 512 | int style = stream->readU8(); |
| 513 | |
| 514 | int len = stream->readPackedUInt(); |
| 515 | if (len > 0) { |
| 516 | SkString str; |
| 517 | str.resize(len); |
| 518 | stream->read(str.writable_str(), len); |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 519 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 520 | const FontInitRec* rec = gSystemFonts; |
| 521 | for (size_t i = 0; i < SK_ARRAY_COUNT(gSystemFonts); i++) { |
| 522 | if (strcmp(rec[i].fFileName, str.c_str()) == 0) { |
| 523 | // backup until we hit the fNames |
| 524 | for (int j = i; j >= 0; --j) { |
| 525 | if (rec[j].fNames != NULL) { |
| 526 | return SkFontHost::CreateTypeface(NULL, |
agl@chromium.org | 5f6a076 | 2010-04-20 22:06:40 +0000 | [diff] [blame] | 527 | rec[j].fNames[0], NULL, 0, (SkTypeface::Style)style); |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | return NULL; |
| 534 | } |
| 535 | |
| 536 | /////////////////////////////////////////////////////////////////////////////// |
| 537 | |
| 538 | SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace, |
| 539 | const char familyName[], |
agl@chromium.org | 5f6a076 | 2010-04-20 22:06:40 +0000 | [diff] [blame] | 540 | const void* data, size_t bytelength, |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 541 | SkTypeface::Style style) { |
| 542 | load_system_fonts(); |
| 543 | |
| 544 | SkAutoMutexAcquire ac(gFamilyMutex); |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 545 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 546 | // clip to legal style bits |
| 547 | style = (SkTypeface::Style)(style & SkTypeface::kBoldItalic); |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 548 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 549 | SkTypeface* tf = NULL; |
| 550 | |
| 551 | if (NULL != familyFace) { |
| 552 | tf = find_typeface(familyFace, style); |
| 553 | } else if (NULL != familyName) { |
| 554 | // SkDebugf("======= familyName <%s>\n", familyName); |
| 555 | tf = find_typeface(familyName, style); |
| 556 | } |
| 557 | |
| 558 | if (NULL == tf) { |
| 559 | tf = find_best_face(gDefaultFamily, style); |
| 560 | } |
| 561 | |
| 562 | // we ref(), since the symantic is to return a new instance |
| 563 | tf->ref(); |
| 564 | return tf; |
| 565 | } |
| 566 | |
| 567 | bool SkFontHost::ValidFontID(uint32_t fontID) { |
| 568 | SkAutoMutexAcquire ac(gFamilyMutex); |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 569 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 570 | return find_from_uniqueID(fontID) != NULL; |
| 571 | } |
| 572 | |
| 573 | SkStream* SkFontHost::OpenStream(uint32_t fontID) { |
| 574 | SkAutoMutexAcquire ac(gFamilyMutex); |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 575 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 576 | FamilyTypeface* tf = (FamilyTypeface*)find_from_uniqueID(fontID); |
| 577 | SkStream* stream = tf ? tf->openStream() : NULL; |
| 578 | |
| 579 | if (stream && stream->getLength() == 0) { |
| 580 | stream->unref(); |
| 581 | stream = NULL; |
| 582 | } |
| 583 | return stream; |
| 584 | } |
| 585 | |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 586 | #if 0 |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 587 | SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics( |
vandebo@chromium.org | 325cb9a | 2011-03-30 18:36:29 +0000 | [diff] [blame] | 588 | uint32_t fontID, |
| 589 | SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 590 | SkASSERT(!"SkFontHost::GetAdvancedTypefaceMetrics unimplemented"); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 591 | return NULL; |
| 592 | } |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 593 | #endif |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 594 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 595 | size_t SkFontHost::GetFileName(SkFontID fontID, char path[], size_t length, |
| 596 | int32_t* index) { |
| 597 | SkAutoMutexAcquire ac(gFamilyMutex); |
| 598 | |
| 599 | FamilyTypeface* tf = (FamilyTypeface*)find_from_uniqueID(fontID); |
| 600 | const char* src = tf ? tf->getFilePath() : NULL; |
| 601 | |
| 602 | if (src) { |
| 603 | size_t size = strlen(src); |
| 604 | if (path) { |
| 605 | memcpy(path, src, SkMin32(size, length)); |
| 606 | } |
| 607 | if (index) { |
| 608 | *index = 0; // we don't have collections (yet) |
| 609 | } |
| 610 | return size; |
| 611 | } else { |
| 612 | return 0; |
| 613 | } |
| 614 | } |
| 615 | |
reed@google.com | 7d26c59 | 2011-06-13 13:01:10 +0000 | [diff] [blame] | 616 | SkFontID SkFontHost::NextLogicalFont(SkFontID currFontID, SkFontID origFontID) { |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 617 | load_system_fonts(); |
| 618 | |
| 619 | /* First see if fontID is already one of our fallbacks. If so, return |
| 620 | its successor. If fontID is not in our list, then return the first one |
| 621 | in our list. Note: list is zero-terminated, and returning zero means |
| 622 | we have no more fonts to use for fallbacks. |
| 623 | */ |
| 624 | const uint32_t* list = gFallbackFonts; |
| 625 | for (int i = 0; list[i] != 0; i++) { |
reed@google.com | 7d26c59 | 2011-06-13 13:01:10 +0000 | [diff] [blame] | 626 | if (list[i] == currFontID) { |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 627 | return list[i+1]; |
| 628 | } |
| 629 | } |
| 630 | return list[0]; |
| 631 | } |
| 632 | |
| 633 | /////////////////////////////////////////////////////////////////////////////// |
| 634 | |
| 635 | SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) { |
| 636 | if (NULL == stream || stream->getLength() <= 0) { |
| 637 | return NULL; |
| 638 | } |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 639 | |
| 640 | bool isFixedWidth; |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 641 | SkString name; |
reed@google.com | 6016962 | 2011-03-14 15:08:44 +0000 | [diff] [blame] | 642 | SkTypeface::Style style = find_name_and_attributes(stream, &name, |
| 643 | &isFixedWidth); |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 644 | |
| 645 | return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream)); |
| 646 | } |
| 647 | |
| 648 | SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { |
| 649 | SkStream* stream = SkNEW_ARGS(SkMMAPStream, (path)); |
| 650 | SkTypeface* face = SkFontHost::CreateTypefaceFromStream(stream); |
| 651 | // since we created the stream, we let go of our ref() here |
| 652 | stream->unref(); |
| 653 | return face; |
| 654 | } |
| 655 | |
| 656 | /////////////////////////////////////////////////////////////////////////////// |
| 657 | |
| 658 | size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) { |
| 659 | if (sizeAllocatedSoFar > FONT_CACHE_MEMORY_BUDGET) |
| 660 | return sizeAllocatedSoFar - FONT_CACHE_MEMORY_BUDGET; |
| 661 | else |
| 662 | return 0; // nothing to do |
| 663 | } |
| 664 | |