1 /* 2 * Copyright 2011 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #include "SkFontHost.h" 9 #include "SkMMapStream.h" 10 #include "SkTypefaceCache.h" 11 12 #define FONT_PATH "/Library/Fonts/Skia.ttf" 13 14 class FTMacTypeface : public SkTypeface { 15 public: 16 FTMacTypeface(Style style, uint32_t id, SkStream* stream) : SkTypeface(style, id) { 17 // we take ownership of the stream 18 fStream = stream; 19 } 20 21 virtual ~FTMacTypeface() { 22 fStream->unref(); 23 } 24 25 SkStream* fStream; 26 }; 27 28 static FTMacTypeface* create_from_path(const char path[]) { 29 SkStream* stream = new SkMMAPStream(path); 30 size_t size = stream->getLength(); 31 SkASSERT(size); 32 FTMacTypeface* tf = new FTMacTypeface(SkTypeface::kNormal, 33 SkTypefaceCache::NewFontID(), 34 stream); 35 SkTypefaceCache::Add(tf, SkTypeface::kNormal); 36 return tf; 37 } 38 39 static SkTypeface* ref_default_typeface() { 40 static SkTypeface* gDef; 41 42 if (NULL == gDef) { 43 gDef = create_from_path(FONT_PATH); 44 } 45 46 gDef->ref(); 47 return gDef; 48 } 49 50 /////////////////////////////////////////////////////////////////////////////// 51 52 SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace, 53 const char familyName[], 54 SkTypeface::Style style) { 55 return ref_default_typeface(); 56 } 57 58 SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) { 59 SkDEBUGFAIL("SkFontHost::CreateTypefaceFromStream unimplemented"); 60 return NULL; 61 } 62 63 SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { 64 return create_from_path(path); 65 } 66 67 SkStream* SkFontHost::OpenStream(uint32_t fontID) { 68 FTMacTypeface* tf = (FTMacTypeface*)SkTypefaceCache::FindByID(fontID); 69 if (tf) { 70 tf->fStream->ref(); 71 return tf->fStream; 72 } 73 return NULL; 74 } 75 76 size_t SkFontHost::GetFileName(SkFontID fontID, char path[], size_t length, 77 int32_t* index) { 78 if (path) { 79 strncpy(path, "font", length); 80 } 81 if (index) { 82 *index = 0; 83 } 84 return 4; 85 } 86 87 void SkFontHost::Serialize(const SkTypeface*, SkWStream*) { 88 SkDEBUGFAIL("SkFontHost::Serialize unimplemented"); 89 } 90 91 SkTypeface* SkFontHost::Deserialize(SkStream* stream) { 92 SkDEBUGFAIL("SkFontHost::Deserialize unimplemented"); 93 return NULL; 94 } 95 96 SkFontID SkFontHost::NextLogicalFont(SkFontID currFontID, SkFontID origFontID) { 97 return 0; 98 } 99 100 #include "SkTypeface_mac.h" 101 102 SkTypeface* SkCreateTypefaceFromCTFont(CTFontRef fontRef) { 103 SkDEBUGFAIL("Not supported"); 104 return NULL; 105 } 106