SkFontHost_linux.cpp revision 1fa793fa6b83219a266124aa70455540b98a4633
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. 7 */ 8 9 10#include "SkFontHost.h" 11#include "SkFontHost_FreeType_common.h" 12#include "SkFontDescriptor.h" 13#include "SkDescriptor.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" 20 21#ifndef SK_FONT_FILE_PREFIX 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 "/" 26#endif 27 28bool find_name_and_attributes(SkStream* stream, SkString* name, 29 SkTypeface::Style* style, bool* isFixedPitch); 30 31/////////////////////////////////////////////////////////////////////////////// 32 33struct 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 */ 40struct NameFamilyPair { 41 const char* fName; // we own this 42 FamilyRec* fFamily; // we don't own this, we just reference it 43 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 57static int32_t gUniqueFontID; 58 59// this is the mutex that protects these globals 60SK_DECLARE_STATIC_MUTEX(gFamilyMutex); 61static FamilyRec* gFamilyHead; 62static SkTDArray<NameFamilyPair> gNameList; 63 64struct FamilyRec { 65 FamilyRec* fNext; 66 SkTypeface* fFaces[4]; 67 68 FamilyRec() 69 { 70 fNext = gFamilyHead; 71 memset(fFaces, 0, sizeof(fFaces)); 72 gFamilyHead = this; 73 } 74}; 75 76static SkTypeface* find_best_face(const FamilyRec* family, 77 SkTypeface::Style style) { 78 SkTypeface* const* faces = family->fFaces; 79 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 99 SkDEBUGFAIL("faces list is empty"); 100 return NULL; 101} 102 103static FamilyRec* find_family(const SkTypeface* member) { 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 116static SkTypeface* find_from_uniqueID(uint32_t uniqueID) { 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) { 122 return face; 123 } 124 } 125 curr = curr->fNext; 126 } 127 return NULL; 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 */ 133static FamilyRec* remove_from_family(const SkTypeface* face) { 134 FamilyRec* family = find_family(face); 135 SkASSERT(family->fFaces[face->style()] == face); 136 family->fFaces[face->style()] = NULL; 137 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 147static void detach_and_delete_family(FamilyRec* family) { 148 FamilyRec* curr = gFamilyHead; 149 FamilyRec* prev = NULL; 150 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 } 165 SkDEBUGFAIL("Yikes, couldn't find family in our list to remove/delete"); 166} 167 168static 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 178static FamilyRec* find_familyrec(const char name[]) { 179 const NameFamilyPair* list = gNameList.begin(); 180 int index = SkStrLCSearch(&list[0].fName, gNameList.count(), name, 181 sizeof(list[0])); 182 return index >= 0 ? list[index].fFamily : NULL; 183} 184 185static 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 190static SkTypeface* find_typeface(const SkTypeface* familyMember, 191 SkTypeface::Style style) { 192 const FamilyRec* family = find_family(familyMember); 193 return family ? find_best_face(family, style) : NULL; 194} 195 196static void add_name(const char name[], FamilyRec* family) { 197 SkAutoAsciiToLC tolc(name); 198 name = tolc.lc(); 199 200 NameFamilyPair* list = gNameList.begin(); 201 int count = gNameList.count(); 202 203 int index = SkStrLCSearch(&list[0].fName, count, name, sizeof(list[0])); 204 205 if (index < 0) { 206 list = gNameList.insert(~index); 207 list->construct(name, family); 208 } 209} 210 211static void remove_from_names(FamilyRec* emptyFamily) { 212#ifdef SK_DEBUG 213 for (int i = 0; i < 4; i++) { 214 SkASSERT(emptyFamily->fFaces[i] == NULL); 215 } 216#endif 217 218 SkTDArray<NameFamilyPair>& list = gNameList; 219 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 232class FamilyTypeface : public SkTypeface_FreeType { 233public: 234 FamilyTypeface(Style style, bool sysFont, FamilyRec* family, bool isFixedPitch) 235 : INHERITED(style, sk_atomic_inc(&gUniqueFontID) + 1, isFixedPitch) { 236 fIsSysFont = sysFont; 237 238 SkAutoMutexAcquire ac(gFamilyMutex); 239 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 } 246 247 virtual ~FamilyTypeface() { 248 SkAutoMutexAcquire ac(gFamilyMutex); 249 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 } 258 259 bool isSysFont() const { return fIsSysFont; } 260 FamilyRec* getFamily() const { return fFamilyRec; } 261 262 virtual const char* getUniqueString() const = 0; 263 264protected: 265 virtual void onGetFontDescriptor(SkFontDescriptor*, bool*) const SK_OVERRIDE; 266 267private: 268 FamilyRec* fFamilyRec; // we don't own this, just point to it 269 bool fIsSysFont; 270 271 typedef SkTypeface_FreeType INHERITED; 272}; 273 274/////////////////////////////////////////////////////////////////////////////// 275 276/* This subclass is just a place holder for when we have no fonts available. 277 It exists so that our globals (e.g. gFamilyHead) that expect *something* 278 will not be null. 279 */ 280class EmptyTypeface : public FamilyTypeface { 281public: 282 EmptyTypeface() : INHERITED(SkTypeface::kNormal, true, NULL, false) {} 283 284 virtual const char* getUniqueString() SK_OVERRIDE const { return NULL; } 285 286protected: 287 virtual SkStream* onOpenStream(int*) const SK_OVERRIDE { return NULL; } 288 289private: 290 typedef FamilyTypeface INHERITED; 291}; 292 293class StreamTypeface : public FamilyTypeface { 294public: 295 StreamTypeface(Style style, bool sysFont, FamilyRec* family, 296 SkStream* stream, bool isFixedPitch) 297 : INHERITED(style, sysFont, family, isFixedPitch) { 298 stream->ref(); 299 fStream = stream; 300 } 301 virtual ~StreamTypeface() { 302 fStream->unref(); 303 } 304 305 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; } 306 307protected: 308 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { 309 *ttcIndex = 0; 310 fStream->ref(); 311 return fStream; 312 } 313 314private: 315 SkStream* fStream; 316 317 typedef FamilyTypeface INHERITED; 318}; 319 320class FileTypeface : public FamilyTypeface { 321public: 322 FileTypeface(Style style, bool sysFont, FamilyRec* family, 323 const char path[], bool isFixedPitch) 324 : INHERITED(style, sysFont, family, isFixedPitch) { 325 fPath.set(path); 326 } 327 328 virtual const char* getUniqueString() const SK_OVERRIDE { 329 const char* str = strrchr(fPath.c_str(), '/'); 330 if (str) { 331 str += 1; // skip the '/' 332 } 333 return str; 334 } 335 336protected: 337 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { 338 *ttcIndex = 0; 339 return SkStream::NewFromFile(fPath.c_str()); 340 } 341 342private: 343 SkString fPath; 344 345 typedef FamilyTypeface INHERITED; 346}; 347 348/////////////////////////////////////////////////////////////////////////////// 349/////////////////////////////////////////////////////////////////////////////// 350 351static bool get_name_and_style(const char path[], SkString* name, 352 SkTypeface::Style* style, bool* isFixedPitch) { 353 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); 354 if (stream.get()) { 355 return find_name_and_attributes(stream, name, style, isFixedPitch); 356 } else { 357 SkDebugf("---- failed to open <%s> as a font\n", path); 358 return false; 359 } 360} 361 362// these globals are assigned (once) by load_system_fonts() 363static SkTypeface* gFallBackTypeface; 364static FamilyRec* gDefaultFamily; 365static SkTypeface* gDefaultNormal; 366 367static void load_directory_fonts(const SkString& directory, unsigned int* count) { 368 SkOSFile::Iter iter(directory.c_str(), ".ttf"); 369 SkString name; 370 371 while (iter.next(&name, false)) { 372 SkString filename(directory); 373 filename.append(name); 374 375 bool isFixedPitch; 376 SkString realname; 377 SkTypeface::Style style = SkTypeface::kNormal; // avoid uninitialized warning 378 379 if (!get_name_and_style(filename.c_str(), &realname, &style, &isFixedPitch)) { 380 SkDebugf("------ can't load <%s> as a font\n", filename.c_str()); 381 continue; 382 } 383 384 FamilyRec* family = find_familyrec(realname.c_str()); 385 if (family && family->fFaces[style]) { 386 continue; 387 } 388 389 // this constructor puts us into the global gFamilyHead llist 390 FamilyTypeface* tf = SkNEW_ARGS(FileTypeface, 391 (style, 392 true, // system-font (cannot delete) 393 family, // what family to join 394 filename.c_str(), 395 isFixedPitch) // filename 396 ); 397 398 if (NULL == family) { 399 add_name(realname.c_str(), tf->getFamily()); 400 } 401 *count += 1; 402 } 403 404 SkOSFile::Iter dirIter(directory.c_str()); 405 while (dirIter.next(&name, true)) { 406 if (name.startsWith(".")) { 407 continue; 408 } 409 SkString dirname(directory); 410 dirname.append(name); 411 dirname.append(SK_FONT_FILE_DIR_SEPERATOR); 412 load_directory_fonts(dirname, count); 413 } 414} 415 416static void load_system_fonts() { 417 // check if we've already be called 418 if (NULL != gDefaultNormal) { 419 return; 420 } 421 422 SkString baseDirectory(SK_FONT_FILE_PREFIX); 423 unsigned int count = 0; 424 load_directory_fonts(baseDirectory, &count); 425 426 if (0 == count) { 427 SkNEW(EmptyTypeface); 428 } 429 430 // do this after all fonts are loaded. This is our default font, and it 431 // acts as a sentinel so we only execute load_system_fonts() once 432 static const char* gDefaultNames[] = { 433 "Arial", "Verdana", "Times New Roman", NULL 434 }; 435 const char** names = gDefaultNames; 436 while (*names) { 437 SkTypeface* tf = find_typeface(*names++, SkTypeface::kNormal); 438 if (tf) { 439 gDefaultNormal = tf; 440 break; 441 } 442 } 443 // check if we found *something* 444 if (NULL == gDefaultNormal) { 445 if (NULL == gFamilyHead) { 446 sk_throw(); 447 } 448 for (int i = 0; i < 4; i++) { 449 if ((gDefaultNormal = gFamilyHead->fFaces[i]) != NULL) { 450 break; 451 } 452 } 453 } 454 if (NULL == gDefaultNormal) { 455 sk_throw(); 456 } 457 gFallBackTypeface = gDefaultNormal; 458 gDefaultFamily = find_family(gDefaultNormal); 459} 460 461/////////////////////////////////////////////////////////////////////////////// 462 463void FamilyTypeface::onGetFontDescriptor(SkFontDescriptor* desc, 464 bool* isLocalStream) const { 465 desc->setFamilyName(find_family_name(this)); 466 desc->setFontFileName(this->getUniqueString()); 467 *isLocalStream = !this->isSysFont(); 468} 469 470/////////////////////////////////////////////////////////////////////////////// 471 472SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace, 473 const char familyName[], 474 SkTypeface::Style style) { 475 load_system_fonts(); 476 477 SkAutoMutexAcquire ac(gFamilyMutex); 478 479 // clip to legal style bits 480 style = (SkTypeface::Style)(style & SkTypeface::kBoldItalic); 481 482 SkTypeface* tf = NULL; 483 484 if (NULL != familyFace) { 485 tf = find_typeface(familyFace, style); 486 } else if (NULL != familyName) { 487 // SkDebugf("======= familyName <%s>\n", familyName); 488 tf = find_typeface(familyName, style); 489 } 490 491 if (NULL == tf) { 492 tf = find_best_face(gDefaultFamily, style); 493 } 494 495 SkSafeRef(tf); 496 return tf; 497} 498 499/////////////////////////////////////////////////////////////////////////////// 500 501SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) { 502 if (NULL == stream || stream->getLength() <= 0) { 503 SkDELETE(stream); 504 return NULL; 505 } 506 507 bool isFixedPitch; 508 SkTypeface::Style style; 509 if (find_name_and_attributes(stream, NULL, &style, &isFixedPitch)) { 510 return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream, isFixedPitch)); 511 } else { 512 return NULL; 513 } 514} 515 516SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { 517 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); 518 return stream.get() ? CreateTypefaceFromStream(stream) : NULL; 519} 520