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