Home | History | Annotate | Download | only in core
      1 
      2 /*
      3  * Copyright 2011 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 "SkAdvancedTypefaceMetrics.h"
     11 #include "SkTypeface.h"
     12 #include "SkFontHost.h"
     13 
     14 SK_DEFINE_INST_COUNT(SkTypeface)
     15 
     16 //#define TRACE_LIFECYCLE
     17 
     18 #ifdef TRACE_LIFECYCLE
     19     static int32_t gTypefaceCounter;
     20 #endif
     21 
     22 SkTypeface::SkTypeface(Style style, SkFontID fontID, bool isFixedWidth)
     23     : fUniqueID(fontID), fStyle(style), fIsFixedWidth(isFixedWidth) {
     24 #ifdef TRACE_LIFECYCLE
     25     SkDebugf("SkTypeface: create  %p fontID %d total %d\n",
     26              this, fontID, ++gTypefaceCounter);
     27 #endif
     28 }
     29 
     30 SkTypeface::~SkTypeface() {
     31 #ifdef TRACE_LIFECYCLE
     32     SkDebugf("SkTypeface: destroy %p fontID %d total %d\n",
     33              this, fUniqueID, --gTypefaceCounter);
     34 #endif
     35 }
     36 
     37 ///////////////////////////////////////////////////////////////////////////////
     38 
     39 SkTypeface* SkTypeface::GetDefaultTypeface() {
     40     // we keep a reference to this guy for all time, since if we return its
     41     // fontID, the font cache may later on ask to resolve that back into a
     42     // typeface object.
     43     static SkTypeface* gDefaultTypeface;
     44 
     45     if (NULL == gDefaultTypeface) {
     46         gDefaultTypeface =
     47         SkFontHost::CreateTypeface(NULL, NULL, SkTypeface::kNormal);
     48     }
     49     return gDefaultTypeface;
     50 }
     51 
     52 uint32_t SkTypeface::UniqueID(const SkTypeface* face) {
     53     if (NULL == face) {
     54         face = GetDefaultTypeface();
     55     }
     56     return face->uniqueID();
     57 }
     58 
     59 bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) {
     60     return SkTypeface::UniqueID(facea) == SkTypeface::UniqueID(faceb);
     61 }
     62 
     63 ///////////////////////////////////////////////////////////////////////////////
     64 
     65 SkTypeface* SkTypeface::CreateFromName(const char name[], Style style) {
     66     return SkFontHost::CreateTypeface(NULL, name, style);
     67 }
     68 
     69 SkTypeface* SkTypeface::CreateFromTypeface(const SkTypeface* family, Style s) {
     70     return SkFontHost::CreateTypeface(family, NULL, s);
     71 }
     72 
     73 SkTypeface* SkTypeface::CreateFromStream(SkStream* stream) {
     74     return SkFontHost::CreateTypefaceFromStream(stream);
     75 }
     76 
     77 SkTypeface* SkTypeface::CreateFromFile(const char path[]) {
     78     return SkFontHost::CreateTypefaceFromFile(path);
     79 }
     80 
     81 ///////////////////////////////////////////////////////////////////////////////
     82 
     83 void SkTypeface::serialize(SkWStream* stream) const {
     84     SkFontHost::Serialize(this, stream);
     85 }
     86 
     87 SkTypeface* SkTypeface::Deserialize(SkStream* stream) {
     88     return SkFontHost::Deserialize(stream);
     89 }
     90 
     91 SkAdvancedTypefaceMetrics* SkTypeface::getAdvancedTypefaceMetrics(
     92         SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
     93         const uint32_t* glyphIDs,
     94         uint32_t glyphIDsCount) const {
     95     return SkFontHost::GetAdvancedTypefaceMetrics(fUniqueID,
     96                                                   perGlyphInfo,
     97                                                   glyphIDs,
     98                                                   glyphIDsCount);
     99 }
    100 
    101 ///////////////////////////////////////////////////////////////////////////////
    102 
    103 int SkTypeface::countTables() const {
    104     return SkFontHost::CountTables(fUniqueID);
    105 }
    106 
    107 int SkTypeface::getTableTags(SkFontTableTag tags[]) const {
    108     return SkFontHost::GetTableTags(fUniqueID, tags);
    109 }
    110 
    111 size_t SkTypeface::getTableSize(SkFontTableTag tag) const {
    112     return SkFontHost::GetTableSize(fUniqueID, tag);
    113 }
    114 
    115 size_t SkTypeface::getTableData(SkFontTableTag tag, size_t offset, size_t length,
    116                                 void* data) const {
    117     return SkFontHost::GetTableData(fUniqueID, tag, offset, length, data);
    118 }
    119 
    120 int SkTypeface::getUnitsPerEm() const {
    121     int upem = 0;
    122 
    123 #ifdef SK_BUILD_FOR_ANDROID
    124     upem = SkFontHost::GetUnitsPerEm(fUniqueID);
    125 #else
    126     SkAdvancedTypefaceMetrics* metrics;
    127     metrics = SkFontHost::GetAdvancedTypefaceMetrics(fUniqueID,
    128                                  SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo,
    129                                  NULL, 0);
    130     if (metrics) {
    131         upem = metrics->fEmSize;
    132         metrics->unref();
    133     }
    134 #endif
    135     return upem;
    136 }
    137