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" |
reed@google.com | 0fc17c3 | 2013-03-21 13:33:49 +0000 | [diff] [blame] | 11 | #include "SkFontHost_FreeType_common.h" |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 12 | #include "SkFontDescriptor.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkDescriptor.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkOSFile.h" |
| 15 | #include "SkPaint.h" |
| 16 | #include "SkString.h" |
| 17 | #include "SkStream.h" |
| 18 | #include "SkThread.h" |
| 19 | #include "SkTSearch.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | #ifndef SK_FONT_FILE_PREFIX |
bungeman@google.com | 2cf84ec | 2012-09-26 19:16:54 +0000 | [diff] [blame] | 22 | #define SK_FONT_FILE_PREFIX "/usr/share/fonts/truetype/" |
| 23 | #endif |
| 24 | #ifndef SK_FONT_FILE_DIR_SEPERATOR |
| 25 | #define SK_FONT_FILE_DIR_SEPERATOR "/" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | #endif |
| 27 | |
djsollen@google.com | 4dc686d | 2012-02-15 21:03:45 +0000 | [diff] [blame] | 28 | bool find_name_and_attributes(SkStream* stream, SkString* name, |
| 29 | SkTypeface::Style* style, bool* isFixedWidth); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | |
| 33 | struct FamilyRec; |
| 34 | |
| 35 | /* This guy holds a mapping of a name -> family, used for looking up fonts. |
| 36 | Since it is stored in a stretchy array that doesn't preserve object |
| 37 | semantics, we don't use constructor/destructors, but just have explicit |
| 38 | helpers to manage our internal bookkeeping. |
| 39 | */ |
| 40 | struct NameFamilyPair { |
| 41 | const char* fName; // we own this |
| 42 | FamilyRec* fFamily; // we don't own this, we just reference it |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 43 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | void construct(const char name[], FamilyRec* family) |
| 45 | { |
| 46 | fName = strdup(name); |
| 47 | fFamily = family; // we don't own this, so just record the referene |
| 48 | } |
| 49 | void destruct() |
| 50 | { |
| 51 | free((char*)fName); |
| 52 | // we don't own family, so just ignore our reference |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | // we use atomic_inc to grow this for each typeface we create |
| 57 | static int32_t gUniqueFontID; |
| 58 | |
| 59 | // this is the mutex that protects these globals |
digit@google.com | 1771cbf | 2012-01-26 21:26:40 +0000 | [diff] [blame] | 60 | SK_DECLARE_STATIC_MUTEX(gFamilyMutex); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | static FamilyRec* gFamilyHead; |
| 62 | static SkTDArray<NameFamilyPair> gNameList; |
| 63 | |
| 64 | struct FamilyRec { |
| 65 | FamilyRec* fNext; |
| 66 | SkTypeface* fFaces[4]; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 67 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | FamilyRec() |
| 69 | { |
| 70 | fNext = gFamilyHead; |
| 71 | memset(fFaces, 0, sizeof(fFaces)); |
| 72 | gFamilyHead = this; |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | static SkTypeface* find_best_face(const FamilyRec* family, |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 77 | SkTypeface::Style style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | SkTypeface* const* faces = family->fFaces; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 79 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | if (faces[style] != NULL) { // exact match |
| 81 | return faces[style]; |
| 82 | } |
| 83 | // look for a matching bold |
| 84 | style = (SkTypeface::Style)(style ^ SkTypeface::kItalic); |
| 85 | if (faces[style] != NULL) { |
| 86 | return faces[style]; |
| 87 | } |
| 88 | // look for the plain |
| 89 | if (faces[SkTypeface::kNormal] != NULL) { |
| 90 | return faces[SkTypeface::kNormal]; |
| 91 | } |
| 92 | // look for anything |
| 93 | for (int i = 0; i < 4; i++) { |
| 94 | if (faces[i] != NULL) { |
| 95 | return faces[i]; |
| 96 | } |
| 97 | } |
| 98 | // 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] | 99 | SkDEBUGFAIL("faces list is empty"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | return NULL; |
| 101 | } |
| 102 | |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 103 | static FamilyRec* find_family(const SkTypeface* member) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | FamilyRec* curr = gFamilyHead; |
| 105 | while (curr != NULL) { |
| 106 | for (int i = 0; i < 4; i++) { |
| 107 | if (curr->fFaces[i] == member) { |
| 108 | return curr; |
| 109 | } |
| 110 | } |
| 111 | curr = curr->fNext; |
| 112 | } |
| 113 | return NULL; |
| 114 | } |
| 115 | |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 116 | static SkTypeface* find_from_uniqueID(uint32_t uniqueID) { |
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 | SkTypeface* face = curr->fFaces[i]; |
| 121 | if (face != NULL && face->uniqueID() == uniqueID) { |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 122 | return face; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | curr = curr->fNext; |
| 126 | } |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 127 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* Remove reference to this face from its family. If the resulting family |
| 131 | is empty (has no faces), return that family, otherwise return NULL |
| 132 | */ |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 133 | static FamilyRec* remove_from_family(const SkTypeface* face) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | FamilyRec* family = find_family(face); |
| 135 | SkASSERT(family->fFaces[face->style()] == face); |
| 136 | family->fFaces[face->style()] = NULL; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 137 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 138 | for (int i = 0; i < 4; i++) { |
| 139 | if (family->fFaces[i] != NULL) { // family is non-empty |
| 140 | return NULL; |
| 141 | } |
| 142 | } |
| 143 | return family; // return the empty family |
| 144 | } |
| 145 | |
| 146 | // maybe we should make FamilyRec be doubly-linked |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 147 | static void detach_and_delete_family(FamilyRec* family) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | FamilyRec* curr = gFamilyHead; |
| 149 | FamilyRec* prev = NULL; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 150 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | while (curr != NULL) { |
| 152 | FamilyRec* next = curr->fNext; |
| 153 | if (curr == family) { |
| 154 | if (prev == NULL) { |
| 155 | gFamilyHead = next; |
| 156 | } else { |
| 157 | prev->fNext = next; |
| 158 | } |
| 159 | SkDELETE(family); |
| 160 | return; |
| 161 | } |
| 162 | prev = curr; |
| 163 | curr = next; |
| 164 | } |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 165 | 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] | 166 | } |
| 167 | |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 168 | static const char* find_family_name(const SkTypeface* familyMember) { |
| 169 | const FamilyRec* familyRec = find_family(familyMember); |
| 170 | for (int i = 0; i < gNameList.count(); i++) { |
| 171 | if (gNameList[i].fFamily == familyRec) { |
| 172 | return gNameList[i].fName; |
| 173 | } |
| 174 | } |
| 175 | return NULL; |
| 176 | } |
| 177 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 178 | static FamilyRec* find_familyrec(const char name[]) { |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 179 | const NameFamilyPair* list = gNameList.begin(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 180 | int index = SkStrLCSearch(&list[0].fName, gNameList.count(), name, |
| 181 | sizeof(list[0])); |
| 182 | return index >= 0 ? list[index].fFamily : NULL; |
| 183 | } |
| 184 | |
| 185 | static SkTypeface* find_typeface(const char name[], SkTypeface::Style style) { |
| 186 | FamilyRec* rec = find_familyrec(name); |
| 187 | return rec ? find_best_face(rec, style) : NULL; |
| 188 | } |
| 189 | |
| 190 | static SkTypeface* find_typeface(const SkTypeface* familyMember, |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 191 | SkTypeface::Style style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 192 | const FamilyRec* family = find_family(familyMember); |
| 193 | return family ? find_best_face(family, style) : NULL; |
| 194 | } |
| 195 | |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 196 | static void add_name(const char name[], FamilyRec* family) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | SkAutoAsciiToLC tolc(name); |
| 198 | name = tolc.lc(); |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 199 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 200 | NameFamilyPair* list = gNameList.begin(); |
| 201 | int count = gNameList.count(); |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 202 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 203 | int index = SkStrLCSearch(&list[0].fName, count, name, sizeof(list[0])); |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 204 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 205 | if (index < 0) { |
| 206 | list = gNameList.insert(~index); |
| 207 | list->construct(name, family); |
| 208 | } |
| 209 | } |
| 210 | |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 211 | static void remove_from_names(FamilyRec* emptyFamily) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 212 | #ifdef SK_DEBUG |
| 213 | for (int i = 0; i < 4; i++) { |
| 214 | SkASSERT(emptyFamily->fFaces[i] == NULL); |
| 215 | } |
| 216 | #endif |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 217 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 218 | SkTDArray<NameFamilyPair>& list = gNameList; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 219 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 220 | // must go backwards when removing |
| 221 | for (int i = list.count() - 1; i >= 0; --i) { |
| 222 | NameFamilyPair* pair = &list[i]; |
| 223 | if (pair->fFamily == emptyFamily) { |
| 224 | pair->destruct(); |
| 225 | list.remove(i); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /////////////////////////////////////////////////////////////////////////////// |
| 231 | |
reed@google.com | 0fc17c3 | 2013-03-21 13:33:49 +0000 | [diff] [blame] | 232 | class FamilyTypeface : public SkTypeface_FreeType { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 233 | public: |
reed@google.com | 3681276 | 2011-02-23 14:49:33 +0000 | [diff] [blame] | 234 | FamilyTypeface(Style style, bool sysFont, FamilyRec* family, bool isFixedWidth) |
reed@google.com | 032fbb8 | 2013-03-21 13:38:18 +0000 | [diff] [blame] | 235 | : INHERITED(style, sk_atomic_inc(&gUniqueFontID) + 1, isFixedWidth) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 236 | fIsSysFont = sysFont; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 237 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 238 | SkAutoMutexAcquire ac(gFamilyMutex); |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 239 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 240 | if (NULL == family) { |
| 241 | family = SkNEW(FamilyRec); |
| 242 | } |
| 243 | family->fFaces[style] = this; |
| 244 | fFamilyRec = family; // just record it so we can return it if asked |
| 245 | } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 246 | |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 247 | virtual ~FamilyTypeface() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 248 | SkAutoMutexAcquire ac(gFamilyMutex); |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 249 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 250 | // remove us from our family. If the family is now empty, we return |
| 251 | // that and then remove that family from the name list |
| 252 | FamilyRec* family = remove_from_family(this); |
| 253 | if (NULL != family) { |
| 254 | remove_from_names(family); |
| 255 | detach_and_delete_family(family); |
| 256 | } |
| 257 | } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 258 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | bool isSysFont() const { return fIsSysFont; } |
| 260 | FamilyRec* getFamily() const { return fFamilyRec; } |
reed@google.com | 292b1d4 | 2013-03-22 17:21:59 +0000 | [diff] [blame^] | 261 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 262 | virtual const char* getUniqueString() const = 0; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 263 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 264 | private: |
| 265 | FamilyRec* fFamilyRec; // we don't own this, just point to it |
| 266 | bool fIsSysFont; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 267 | |
reed@google.com | 032fbb8 | 2013-03-21 13:38:18 +0000 | [diff] [blame] | 268 | typedef SkTypeface_FreeType INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | /////////////////////////////////////////////////////////////////////////////// |
| 272 | |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame] | 273 | /* This subclass is just a place holder for when we have no fonts available. |
| 274 | It exists so that our globals (e.g. gFamilyHead) that expect *something* |
| 275 | will not be null. |
| 276 | */ |
| 277 | class EmptyTypeface : public FamilyTypeface { |
| 278 | public: |
reed@google.com | 3681276 | 2011-02-23 14:49:33 +0000 | [diff] [blame] | 279 | EmptyTypeface() : INHERITED(SkTypeface::kNormal, true, NULL, false) {} |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 280 | |
reed@google.com | e1575aa | 2013-03-18 21:08:46 +0000 | [diff] [blame] | 281 | virtual const char* getUniqueString() SK_OVERRIDE const { return NULL; } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 282 | |
reed@google.com | 292b1d4 | 2013-03-22 17:21:59 +0000 | [diff] [blame^] | 283 | protected: |
| 284 | virtual SkStream* onOpenStream(int*) const SK_OVERRIDE { return NULL; } |
| 285 | |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame] | 286 | private: |
| 287 | typedef FamilyTypeface INHERITED; |
| 288 | }; |
| 289 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 290 | class StreamTypeface : public FamilyTypeface { |
| 291 | public: |
| 292 | StreamTypeface(Style style, bool sysFont, FamilyRec* family, |
reed@google.com | 3681276 | 2011-02-23 14:49:33 +0000 | [diff] [blame] | 293 | SkStream* stream, bool isFixedWidth) |
| 294 | : INHERITED(style, sysFont, family, isFixedWidth) { |
reed@android.com | 1c0c5a0 | 2010-04-12 20:16:49 +0000 | [diff] [blame] | 295 | stream->ref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 296 | fStream = stream; |
| 297 | } |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 298 | virtual ~StreamTypeface() { |
reed@android.com | 1c0c5a0 | 2010-04-12 20:16:49 +0000 | [diff] [blame] | 299 | fStream->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 300 | } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 301 | |
reed@google.com | e1575aa | 2013-03-18 21:08:46 +0000 | [diff] [blame] | 302 | virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 303 | |
reed@google.com | 292b1d4 | 2013-03-22 17:21:59 +0000 | [diff] [blame^] | 304 | protected: |
| 305 | virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { |
| 306 | *ttcIndex = 0; |
| 307 | fStream->ref(); |
| 308 | return fStream; |
| 309 | } |
| 310 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 311 | private: |
| 312 | SkStream* fStream; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 313 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 314 | typedef FamilyTypeface INHERITED; |
| 315 | }; |
| 316 | |
| 317 | class FileTypeface : public FamilyTypeface { |
| 318 | public: |
| 319 | FileTypeface(Style style, bool sysFont, FamilyRec* family, |
reed@google.com | 3681276 | 2011-02-23 14:49:33 +0000 | [diff] [blame] | 320 | const char path[], bool isFixedWidth) |
| 321 | : INHERITED(style, sysFont, family, isFixedWidth) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 322 | fPath.set(path); |
| 323 | } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 324 | |
reed@google.com | e1575aa | 2013-03-18 21:08:46 +0000 | [diff] [blame] | 325 | virtual const char* getUniqueString() const SK_OVERRIDE { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 326 | const char* str = strrchr(fPath.c_str(), '/'); |
| 327 | if (str) { |
| 328 | str += 1; // skip the '/' |
| 329 | } |
| 330 | return str; |
| 331 | } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 332 | |
reed@google.com | 292b1d4 | 2013-03-22 17:21:59 +0000 | [diff] [blame^] | 333 | protected: |
| 334 | virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { |
| 335 | *ttcIndex = 0; |
| 336 | return SkStream::NewFromFile(fPath.c_str()); |
| 337 | } |
| 338 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 339 | private: |
| 340 | SkString fPath; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 341 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 342 | typedef FamilyTypeface INHERITED; |
| 343 | }; |
| 344 | |
| 345 | /////////////////////////////////////////////////////////////////////////////// |
| 346 | /////////////////////////////////////////////////////////////////////////////// |
| 347 | |
| 348 | static bool get_name_and_style(const char path[], SkString* name, |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 349 | SkTypeface::Style* style, bool* isFixedWidth) { |
reed@google.com | e1575aa | 2013-03-18 21:08:46 +0000 | [diff] [blame] | 350 | SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); |
| 351 | if (stream.get()) { |
| 352 | return find_name_and_attributes(stream, name, style, isFixedWidth); |
| 353 | } else { |
| 354 | SkDebugf("---- failed to open <%s> as a font\n", path); |
| 355 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 356 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | // these globals are assigned (once) by load_system_fonts() |
| 360 | static SkTypeface* gFallBackTypeface; |
| 361 | static FamilyRec* gDefaultFamily; |
| 362 | static SkTypeface* gDefaultNormal; |
| 363 | |
bungeman@google.com | 2cf84ec | 2012-09-26 19:16:54 +0000 | [diff] [blame] | 364 | static void load_directory_fonts(const SkString& directory, unsigned int* count) { |
| 365 | SkOSFile::Iter iter(directory.c_str(), ".ttf"); |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame] | 366 | SkString name; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 367 | |
| 368 | while (iter.next(&name, false)) { |
bungeman@google.com | 2cf84ec | 2012-09-26 19:16:54 +0000 | [diff] [blame] | 369 | SkString filename(directory); |
| 370 | filename.append(name); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 371 | |
reed@google.com | 3681276 | 2011-02-23 14:49:33 +0000 | [diff] [blame] | 372 | bool isFixedWidth; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 373 | SkString realname; |
reed@google.com | 6963af2 | 2011-01-04 12:52:02 +0000 | [diff] [blame] | 374 | SkTypeface::Style style = SkTypeface::kNormal; // avoid uninitialized warning |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 375 | |
reed@google.com | 3681276 | 2011-02-23 14:49:33 +0000 | [diff] [blame] | 376 | if (!get_name_and_style(filename.c_str(), &realname, &style, &isFixedWidth)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 377 | SkDebugf("------ can't load <%s> as a font\n", filename.c_str()); |
| 378 | continue; |
| 379 | } |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 380 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 381 | FamilyRec* family = find_familyrec(realname.c_str()); |
reed@android.com | 887e4f3 | 2010-04-15 14:04:52 +0000 | [diff] [blame] | 382 | if (family && family->fFaces[style]) { |
reed@android.com | 887e4f3 | 2010-04-15 14:04:52 +0000 | [diff] [blame] | 383 | continue; |
| 384 | } |
| 385 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 386 | // this constructor puts us into the global gFamilyHead llist |
| 387 | FamilyTypeface* tf = SkNEW_ARGS(FileTypeface, |
| 388 | (style, |
| 389 | true, // system-font (cannot delete) |
| 390 | family, // what family to join |
reed@google.com | 3681276 | 2011-02-23 14:49:33 +0000 | [diff] [blame] | 391 | filename.c_str(), |
| 392 | isFixedWidth) // filename |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 393 | ); |
| 394 | |
| 395 | if (NULL == family) { |
| 396 | add_name(realname.c_str(), tf->getFamily()); |
| 397 | } |
bungeman@google.com | 2cf84ec | 2012-09-26 19:16:54 +0000 | [diff] [blame] | 398 | *count += 1; |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame] | 399 | } |
| 400 | |
bungeman@google.com | 2cf84ec | 2012-09-26 19:16:54 +0000 | [diff] [blame] | 401 | SkOSFile::Iter dirIter(directory.c_str()); |
| 402 | while (dirIter.next(&name, true)) { |
| 403 | if (name.startsWith(".")) { |
| 404 | continue; |
| 405 | } |
| 406 | SkString dirname(directory); |
| 407 | dirname.append(name); |
| 408 | dirname.append(SK_FONT_FILE_DIR_SEPERATOR); |
| 409 | load_directory_fonts(dirname, count); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | static void load_system_fonts() { |
| 414 | // check if we've already be called |
| 415 | if (NULL != gDefaultNormal) { |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | SkString baseDirectory(SK_FONT_FILE_PREFIX); |
| 420 | unsigned int count = 0; |
| 421 | load_directory_fonts(baseDirectory, &count); |
| 422 | |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame] | 423 | if (0 == count) { |
| 424 | SkNEW(EmptyTypeface); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 425 | } |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 426 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 427 | // do this after all fonts are loaded. This is our default font, and it |
| 428 | // acts as a sentinel so we only execute load_system_fonts() once |
| 429 | static const char* gDefaultNames[] = { |
| 430 | "Arial", "Verdana", "Times New Roman", NULL |
| 431 | }; |
| 432 | const char** names = gDefaultNames; |
| 433 | while (*names) { |
| 434 | SkTypeface* tf = find_typeface(*names++, SkTypeface::kNormal); |
| 435 | if (tf) { |
| 436 | gDefaultNormal = tf; |
| 437 | break; |
| 438 | } |
| 439 | } |
| 440 | // check if we found *something* |
| 441 | if (NULL == gDefaultNormal) { |
| 442 | if (NULL == gFamilyHead) { |
| 443 | sk_throw(); |
| 444 | } |
| 445 | for (int i = 0; i < 4; i++) { |
| 446 | if ((gDefaultNormal = gFamilyHead->fFaces[i]) != NULL) { |
| 447 | break; |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | if (NULL == gDefaultNormal) { |
| 452 | sk_throw(); |
| 453 | } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 454 | gFallBackTypeface = gDefaultNormal; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 455 | gDefaultFamily = find_family(gDefaultNormal); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /////////////////////////////////////////////////////////////////////////////// |
| 459 | |
| 460 | void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) { |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 461 | |
| 462 | SkFontDescriptor descriptor; |
bungeman@google.com | b6896e7 | 2012-06-01 15:53:06 +0000 | [diff] [blame] | 463 | descriptor.setFamilyName(find_family_name(face)); |
| 464 | descriptor.setStyle(face->style()); |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 465 | descriptor.setFontFileName(((FamilyTypeface*)face)->getUniqueString()); |
| 466 | |
| 467 | descriptor.serialize(stream); |
| 468 | |
| 469 | const bool isCustomFont = !((FamilyTypeface*)face)->isSysFont(); |
| 470 | if (isCustomFont) { |
| 471 | // store the entire font in the fontData |
reed@google.com | 292b1d4 | 2013-03-22 17:21:59 +0000 | [diff] [blame^] | 472 | SkStream* fontStream = face->openStream(NULL); |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 473 | const uint32_t length = fontStream->getLength(); |
| 474 | |
| 475 | stream->writePackedUInt(length); |
| 476 | stream->writeStream(fontStream, length); |
| 477 | |
| 478 | fontStream->unref(); |
djsollen@google.com | e6488ad | 2012-03-29 16:09:48 +0000 | [diff] [blame] | 479 | } else { |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 480 | stream->writePackedUInt(0); |
djsollen@google.com | e6488ad | 2012-03-29 16:09:48 +0000 | [diff] [blame] | 481 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | SkTypeface* SkFontHost::Deserialize(SkStream* stream) { |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 485 | load_system_fonts(); |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 486 | |
| 487 | SkFontDescriptor descriptor(stream); |
bungeman@google.com | b6896e7 | 2012-06-01 15:53:06 +0000 | [diff] [blame] | 488 | const char* familyName = descriptor.getFamilyName(); |
bungeman@google.com | b6896e7 | 2012-06-01 15:53:06 +0000 | [diff] [blame] | 489 | const SkTypeface::Style style = descriptor.getStyle(); |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 490 | |
| 491 | const uint32_t customFontDataLength = stream->readPackedUInt(); |
| 492 | if (customFontDataLength > 0) { |
| 493 | |
| 494 | // generate a new stream to store the custom typeface |
| 495 | SkMemoryStream* fontStream = new SkMemoryStream(customFontDataLength - 1); |
| 496 | stream->read((void*)fontStream->getMemoryBase(), customFontDataLength - 1); |
| 497 | |
| 498 | SkTypeface* face = CreateTypefaceFromStream(fontStream); |
| 499 | |
| 500 | fontStream->unref(); |
| 501 | return face; |
| 502 | } |
| 503 | |
| 504 | return SkFontHost::CreateTypeface(NULL, familyName, style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /////////////////////////////////////////////////////////////////////////////// |
| 508 | |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 509 | SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace, |
| 510 | const char familyName[], |
| 511 | SkTypeface::Style style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 512 | load_system_fonts(); |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 513 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 514 | SkAutoMutexAcquire ac(gFamilyMutex); |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 515 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 516 | // clip to legal style bits |
| 517 | style = (SkTypeface::Style)(style & SkTypeface::kBoldItalic); |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 518 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 519 | SkTypeface* tf = NULL; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 520 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 521 | if (NULL != familyFace) { |
| 522 | tf = find_typeface(familyFace, style); |
| 523 | } else if (NULL != familyName) { |
| 524 | // SkDebugf("======= familyName <%s>\n", familyName); |
| 525 | tf = find_typeface(familyName, style); |
| 526 | } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 527 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 528 | if (NULL == tf) { |
| 529 | tf = find_best_face(gDefaultFamily, style); |
| 530 | } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 531 | |
| 532 | SkSafeRef(tf); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 533 | return tf; |
| 534 | } |
| 535 | |
reed@google.com | 0da4861 | 2013-03-19 16:06:52 +0000 | [diff] [blame] | 536 | SkTypeface* SkFontHost::NextLogicalTypeface(SkFontID currFontID, SkFontID origFontID) { |
| 537 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | /////////////////////////////////////////////////////////////////////////////// |
| 541 | |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 542 | SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 543 | if (NULL == stream || stream->getLength() <= 0) { |
| 544 | SkDELETE(stream); |
| 545 | return NULL; |
| 546 | } |
reed@google.com | 3681276 | 2011-02-23 14:49:33 +0000 | [diff] [blame] | 547 | |
| 548 | bool isFixedWidth; |
djsollen@google.com | 4dc686d | 2012-02-15 21:03:45 +0000 | [diff] [blame] | 549 | SkTypeface::Style style; |
| 550 | if (find_name_and_attributes(stream, NULL, &style, &isFixedWidth)) { |
| 551 | return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream, isFixedWidth)); |
| 552 | } else { |
| 553 | return NULL; |
| 554 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 555 | } |
| 556 | |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 557 | SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { |
mike@reedtribe.org | f381162 | 2013-03-19 02:18:33 +0000 | [diff] [blame] | 558 | SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); |
| 559 | return stream.get() ? CreateTypefaceFromStream(stream) : NULL; |
reed@android.com | 0becfc5b | 2009-01-13 13:26:44 +0000 | [diff] [blame] | 560 | } |