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" |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 11 | #include "SkFontDescriptor.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkDescriptor.h" |
| 13 | #include "SkMMapStream.h" |
| 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 | |
| 232 | class FamilyTypeface : public SkTypeface { |
| 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) |
| 235 | : SkTypeface(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@android.com | 22dbaaf | 2009-05-18 00:43:58 +0000 | [diff] [blame] | 261 | // openStream returns a SkStream that has been ref-ed |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 262 | virtual SkStream* openStream() = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 263 | virtual const char* getUniqueString() const = 0; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 264 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 265 | private: |
| 266 | FamilyRec* fFamilyRec; // we don't own this, just point to it |
| 267 | bool fIsSysFont; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 268 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 269 | typedef SkTypeface INHERITED; |
| 270 | }; |
| 271 | |
| 272 | /////////////////////////////////////////////////////////////////////////////// |
| 273 | |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame] | 274 | /* This subclass is just a place holder for when we have no fonts available. |
| 275 | It exists so that our globals (e.g. gFamilyHead) that expect *something* |
| 276 | will not be null. |
| 277 | */ |
| 278 | class EmptyTypeface : public FamilyTypeface { |
| 279 | public: |
reed@google.com | 3681276 | 2011-02-23 14:49:33 +0000 | [diff] [blame] | 280 | EmptyTypeface() : INHERITED(SkTypeface::kNormal, true, NULL, false) {} |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 281 | |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame] | 282 | // overrides |
| 283 | virtual SkStream* openStream() { return NULL; } |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame] | 284 | virtual const char* getUniqueString() const { return NULL; } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 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@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 302 | // overrides |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 303 | virtual SkStream* openStream() |
| 304 | { |
reed@android.com | 22dbaaf | 2009-05-18 00:43:58 +0000 | [diff] [blame] | 305 | // openStream returns a refed stream. |
| 306 | fStream->ref(); |
| 307 | return fStream; |
| 308 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 309 | virtual const char* getUniqueString() const { return NULL; } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 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@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 325 | // overrides |
| 326 | virtual SkStream* openStream() |
| 327 | { |
| 328 | SkStream* stream = SkNEW_ARGS(SkMMAPStream, (fPath.c_str())); |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 329 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 330 | // check for failure |
| 331 | if (stream->getLength() <= 0) { |
| 332 | SkDELETE(stream); |
| 333 | // maybe MMAP isn't supported. try FILE |
| 334 | stream = SkNEW_ARGS(SkFILEStream, (fPath.c_str())); |
| 335 | if (stream->getLength() <= 0) { |
| 336 | SkDELETE(stream); |
| 337 | stream = NULL; |
| 338 | } |
| 339 | } |
| 340 | return stream; |
| 341 | } |
reed@android.com | 51709c7 | 2010-04-16 12:51:29 +0000 | [diff] [blame] | 342 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 343 | virtual const char* getUniqueString() const { |
| 344 | const char* str = strrchr(fPath.c_str(), '/'); |
| 345 | if (str) { |
| 346 | str += 1; // skip the '/' |
| 347 | } |
| 348 | return str; |
| 349 | } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 350 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 351 | private: |
| 352 | SkString fPath; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 353 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 354 | typedef FamilyTypeface INHERITED; |
| 355 | }; |
| 356 | |
| 357 | /////////////////////////////////////////////////////////////////////////////// |
| 358 | /////////////////////////////////////////////////////////////////////////////// |
| 359 | |
| 360 | static bool get_name_and_style(const char path[], SkString* name, |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 361 | SkTypeface::Style* style, bool* isFixedWidth) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 362 | SkMMAPStream stream(path); |
| 363 | if (stream.getLength() > 0) { |
djsollen@google.com | 4dc686d | 2012-02-15 21:03:45 +0000 | [diff] [blame] | 364 | return find_name_and_attributes(&stream, name, style, isFixedWidth); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 365 | } |
| 366 | else { |
| 367 | SkFILEStream stream(path); |
| 368 | if (stream.getLength() > 0) { |
djsollen@google.com | 4dc686d | 2012-02-15 21:03:45 +0000 | [diff] [blame] | 369 | return find_name_and_attributes(&stream, name, style, isFixedWidth); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 372 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 373 | SkDebugf("---- failed to open <%s> as a font\n", path); |
| 374 | return false; |
| 375 | } |
| 376 | |
| 377 | // these globals are assigned (once) by load_system_fonts() |
| 378 | static SkTypeface* gFallBackTypeface; |
| 379 | static FamilyRec* gDefaultFamily; |
| 380 | static SkTypeface* gDefaultNormal; |
| 381 | |
bungeman@google.com | 2cf84ec | 2012-09-26 19:16:54 +0000 | [diff] [blame^] | 382 | static void load_directory_fonts(const SkString& directory, unsigned int* count) { |
| 383 | SkOSFile::Iter iter(directory.c_str(), ".ttf"); |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame] | 384 | SkString name; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 385 | |
| 386 | while (iter.next(&name, false)) { |
bungeman@google.com | 2cf84ec | 2012-09-26 19:16:54 +0000 | [diff] [blame^] | 387 | SkString filename(directory); |
| 388 | filename.append(name); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 389 | |
reed@google.com | 3681276 | 2011-02-23 14:49:33 +0000 | [diff] [blame] | 390 | bool isFixedWidth; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 391 | SkString realname; |
reed@google.com | 6963af2 | 2011-01-04 12:52:02 +0000 | [diff] [blame] | 392 | SkTypeface::Style style = SkTypeface::kNormal; // avoid uninitialized warning |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 393 | |
reed@google.com | 3681276 | 2011-02-23 14:49:33 +0000 | [diff] [blame] | 394 | if (!get_name_and_style(filename.c_str(), &realname, &style, &isFixedWidth)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 395 | SkDebugf("------ can't load <%s> as a font\n", filename.c_str()); |
| 396 | continue; |
| 397 | } |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 398 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 399 | FamilyRec* family = find_familyrec(realname.c_str()); |
reed@android.com | 887e4f3 | 2010-04-15 14:04:52 +0000 | [diff] [blame] | 400 | if (family && family->fFaces[style]) { |
reed@android.com | 887e4f3 | 2010-04-15 14:04:52 +0000 | [diff] [blame] | 401 | continue; |
| 402 | } |
| 403 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 404 | // this constructor puts us into the global gFamilyHead llist |
| 405 | FamilyTypeface* tf = SkNEW_ARGS(FileTypeface, |
| 406 | (style, |
| 407 | true, // system-font (cannot delete) |
| 408 | family, // what family to join |
reed@google.com | 3681276 | 2011-02-23 14:49:33 +0000 | [diff] [blame] | 409 | filename.c_str(), |
| 410 | isFixedWidth) // filename |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 411 | ); |
| 412 | |
| 413 | if (NULL == family) { |
| 414 | add_name(realname.c_str(), tf->getFamily()); |
| 415 | } |
bungeman@google.com | 2cf84ec | 2012-09-26 19:16:54 +0000 | [diff] [blame^] | 416 | *count += 1; |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame] | 417 | } |
| 418 | |
bungeman@google.com | 2cf84ec | 2012-09-26 19:16:54 +0000 | [diff] [blame^] | 419 | SkOSFile::Iter dirIter(directory.c_str()); |
| 420 | while (dirIter.next(&name, true)) { |
| 421 | if (name.startsWith(".")) { |
| 422 | continue; |
| 423 | } |
| 424 | SkString dirname(directory); |
| 425 | dirname.append(name); |
| 426 | dirname.append(SK_FONT_FILE_DIR_SEPERATOR); |
| 427 | load_directory_fonts(dirname, count); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | static void load_system_fonts() { |
| 432 | // check if we've already be called |
| 433 | if (NULL != gDefaultNormal) { |
| 434 | return; |
| 435 | } |
| 436 | |
| 437 | SkString baseDirectory(SK_FONT_FILE_PREFIX); |
| 438 | unsigned int count = 0; |
| 439 | load_directory_fonts(baseDirectory, &count); |
| 440 | |
reed@android.com | f244f1b | 2010-04-16 12:40:08 +0000 | [diff] [blame] | 441 | if (0 == count) { |
| 442 | SkNEW(EmptyTypeface); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 443 | } |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 444 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 445 | // do this after all fonts are loaded. This is our default font, and it |
| 446 | // acts as a sentinel so we only execute load_system_fonts() once |
| 447 | static const char* gDefaultNames[] = { |
| 448 | "Arial", "Verdana", "Times New Roman", NULL |
| 449 | }; |
| 450 | const char** names = gDefaultNames; |
| 451 | while (*names) { |
| 452 | SkTypeface* tf = find_typeface(*names++, SkTypeface::kNormal); |
| 453 | if (tf) { |
| 454 | gDefaultNormal = tf; |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | // check if we found *something* |
| 459 | if (NULL == gDefaultNormal) { |
| 460 | if (NULL == gFamilyHead) { |
| 461 | sk_throw(); |
| 462 | } |
| 463 | for (int i = 0; i < 4; i++) { |
| 464 | if ((gDefaultNormal = gFamilyHead->fFaces[i]) != NULL) { |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | if (NULL == gDefaultNormal) { |
| 470 | sk_throw(); |
| 471 | } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 472 | gFallBackTypeface = gDefaultNormal; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 473 | gDefaultFamily = find_family(gDefaultNormal); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /////////////////////////////////////////////////////////////////////////////// |
| 477 | |
| 478 | void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) { |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 479 | |
| 480 | SkFontDescriptor descriptor; |
bungeman@google.com | b6896e7 | 2012-06-01 15:53:06 +0000 | [diff] [blame] | 481 | descriptor.setFamilyName(find_family_name(face)); |
| 482 | descriptor.setStyle(face->style()); |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 483 | descriptor.setFontFileName(((FamilyTypeface*)face)->getUniqueString()); |
| 484 | |
| 485 | descriptor.serialize(stream); |
| 486 | |
| 487 | const bool isCustomFont = !((FamilyTypeface*)face)->isSysFont(); |
| 488 | if (isCustomFont) { |
| 489 | // store the entire font in the fontData |
| 490 | SkStream* fontStream = ((FamilyTypeface*)face)->openStream(); |
| 491 | const uint32_t length = fontStream->getLength(); |
| 492 | |
| 493 | stream->writePackedUInt(length); |
| 494 | stream->writeStream(fontStream, length); |
| 495 | |
| 496 | fontStream->unref(); |
djsollen@google.com | e6488ad | 2012-03-29 16:09:48 +0000 | [diff] [blame] | 497 | } else { |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 498 | stream->writePackedUInt(0); |
djsollen@google.com | e6488ad | 2012-03-29 16:09:48 +0000 | [diff] [blame] | 499 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | SkTypeface* SkFontHost::Deserialize(SkStream* stream) { |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 503 | load_system_fonts(); |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 504 | |
| 505 | SkFontDescriptor descriptor(stream); |
bungeman@google.com | b6896e7 | 2012-06-01 15:53:06 +0000 | [diff] [blame] | 506 | const char* familyName = descriptor.getFamilyName(); |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 507 | const char* typefaceName = descriptor.getFontFileName(); |
bungeman@google.com | b6896e7 | 2012-06-01 15:53:06 +0000 | [diff] [blame] | 508 | const SkTypeface::Style style = descriptor.getStyle(); |
djsollen@google.com | 9714516 | 2012-05-31 19:55:08 +0000 | [diff] [blame] | 509 | |
| 510 | const uint32_t customFontDataLength = stream->readPackedUInt(); |
| 511 | if (customFontDataLength > 0) { |
| 512 | |
| 513 | // generate a new stream to store the custom typeface |
| 514 | SkMemoryStream* fontStream = new SkMemoryStream(customFontDataLength - 1); |
| 515 | stream->read((void*)fontStream->getMemoryBase(), customFontDataLength - 1); |
| 516 | |
| 517 | SkTypeface* face = CreateTypefaceFromStream(fontStream); |
| 518 | |
| 519 | fontStream->unref(); |
| 520 | return face; |
| 521 | } |
| 522 | |
| 523 | return SkFontHost::CreateTypeface(NULL, familyName, style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | /////////////////////////////////////////////////////////////////////////////// |
| 527 | |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 528 | SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace, |
| 529 | const char familyName[], |
| 530 | SkTypeface::Style style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 531 | load_system_fonts(); |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 532 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 533 | SkAutoMutexAcquire ac(gFamilyMutex); |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 534 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 535 | // clip to legal style bits |
| 536 | style = (SkTypeface::Style)(style & SkTypeface::kBoldItalic); |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 537 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 538 | SkTypeface* tf = NULL; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 539 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 540 | if (NULL != familyFace) { |
| 541 | tf = find_typeface(familyFace, style); |
| 542 | } else if (NULL != familyName) { |
| 543 | // SkDebugf("======= familyName <%s>\n", familyName); |
| 544 | tf = find_typeface(familyName, style); |
| 545 | } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 546 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 547 | if (NULL == tf) { |
| 548 | tf = find_best_face(gDefaultFamily, style); |
| 549 | } |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 550 | |
| 551 | SkSafeRef(tf); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 552 | return tf; |
| 553 | } |
| 554 | |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 555 | SkStream* SkFontHost::OpenStream(uint32_t fontID) { |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 556 | FamilyTypeface* tf = (FamilyTypeface*)find_from_uniqueID(fontID); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 557 | SkStream* stream = tf ? tf->openStream() : NULL; |
chudy@google.com | ada4480 | 2012-07-30 12:59:12 +0000 | [diff] [blame] | 558 | |
reed@android.com | 1c0c5a0 | 2010-04-12 20:16:49 +0000 | [diff] [blame] | 559 | if (stream && stream->getLength() == 0) { |
| 560 | stream->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 561 | stream = NULL; |
| 562 | } |
| 563 | return stream; |
| 564 | } |
| 565 | |
reed@android.com | ac98154 | 2009-07-31 16:17:01 +0000 | [diff] [blame] | 566 | size_t SkFontHost::GetFileName(SkFontID fontID, char path[], size_t length, |
| 567 | int32_t* index) { |
| 568 | SkDebugf("SkFontHost::GetFileName unimplemented\n"); |
| 569 | return 0; |
| 570 | } |
| 571 | |
reed@google.com | 7d26c59 | 2011-06-13 13:01:10 +0000 | [diff] [blame] | 572 | SkFontID SkFontHost::NextLogicalFont(SkFontID currFontID, SkFontID origFontID) { |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 573 | return 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /////////////////////////////////////////////////////////////////////////////// |
| 577 | |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 578 | SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 579 | if (NULL == stream || stream->getLength() <= 0) { |
| 580 | SkDELETE(stream); |
| 581 | return NULL; |
| 582 | } |
reed@google.com | 3681276 | 2011-02-23 14:49:33 +0000 | [diff] [blame] | 583 | |
| 584 | bool isFixedWidth; |
djsollen@google.com | 4dc686d | 2012-02-15 21:03:45 +0000 | [diff] [blame] | 585 | SkTypeface::Style style; |
| 586 | if (find_name_and_attributes(stream, NULL, &style, &isFixedWidth)) { |
| 587 | return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream, isFixedWidth)); |
| 588 | } else { |
| 589 | return NULL; |
| 590 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 591 | } |
| 592 | |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 593 | SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { |
| 594 | SkTypeface* face = NULL; |
| 595 | SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path)); |
| 596 | |
| 597 | if (stream->isValid()) { |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 598 | face = CreateTypefaceFromStream(stream); |
reed@android.com | 1bfd0ca | 2009-02-20 14:22:36 +0000 | [diff] [blame] | 599 | } |
| 600 | stream->unref(); |
reed@android.com | f2afb67 | 2009-09-28 16:12:48 +0000 | [diff] [blame] | 601 | return face; |
reed@android.com | 0becfc5b | 2009-01-13 13:26:44 +0000 | [diff] [blame] | 602 | } |
| 603 | |