Home | History | Annotate | Download | only in ports
      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                                        const void* data, size_t bytelength,
     55                                        SkTypeface::Style style) {
     56     return ref_default_typeface();
     57 }
     58 
     59 SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
     60     SkDEBUGFAIL("SkFontHost::CreateTypefaceFromStream unimplemented");
     61     return NULL;
     62 }
     63 
     64 SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
     65     return create_from_path(path);
     66 }
     67 
     68 SkStream* SkFontHost::OpenStream(uint32_t fontID) {
     69     FTMacTypeface* tf = (FTMacTypeface*)SkTypefaceCache::FindByID(fontID);
     70     if (tf) {
     71         tf->fStream->ref();
     72         return tf->fStream;
     73     }
     74     return NULL;
     75 }
     76 
     77 size_t SkFontHost::GetFileName(SkFontID fontID, char path[], size_t length,
     78                                int32_t* index) {
     79     if (path) {
     80         strncpy(path, "font", length);
     81     }
     82     if (index) {
     83         *index = 0;
     84     }
     85     return 4;
     86 }
     87 
     88 void SkFontHost::Serialize(const SkTypeface*, SkWStream*) {
     89     SkDEBUGFAIL("SkFontHost::Serialize unimplemented");
     90 }
     91 
     92 SkTypeface* SkFontHost::Deserialize(SkStream* stream) {
     93     SkDEBUGFAIL("SkFontHost::Deserialize unimplemented");
     94     return NULL;
     95 }
     96 
     97 SkFontID SkFontHost::NextLogicalFont(SkFontID currFontID, SkFontID origFontID) {
     98     return 0;
     99 }
    100 
    101 #include "SkTypeface_mac.h"
    102 
    103 SkTypeface* SkCreateTypefaceFromCTFont(CTFontRef fontRef) {
    104     SkDEBUGFAIL("Not supported");
    105     return NULL;
    106 }
    107 
    108