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