SkTypeface.cpp revision ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976e
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//#define TRACE_LIFECYCLE
15
16#ifdef TRACE_LIFECYCLE
17    static int32_t gTypefaceCounter;
18#endif
19
20SkTypeface::SkTypeface(Style style, SkFontID fontID, bool isFixedWidth)
21    : fUniqueID(fontID), fStyle(style), fIsFixedWidth(isFixedWidth) {
22#ifdef TRACE_LIFECYCLE
23    SkDebugf("SkTypeface: create  %p fontID %d total %d\n",
24             this, fontID, ++gTypefaceCounter);
25#endif
26}
27
28SkTypeface::~SkTypeface() {
29#ifdef TRACE_LIFECYCLE
30    SkDebugf("SkTypeface: destroy %p fontID %d total %d\n",
31             this, fUniqueID, --gTypefaceCounter);
32#endif
33}
34
35///////////////////////////////////////////////////////////////////////////////
36
37uint32_t SkTypeface::UniqueID(const SkTypeface* face) {
38    if (face) {
39        return face->uniqueID();
40    }
41
42    // We cache the default fontID, assuming it will not change during a boot
43    // The initial value of 0 is fine, since a typeface's uniqueID should not
44    // be zero.
45    static uint32_t gDefaultFontID;
46
47    if (0 == gDefaultFontID) {
48        SkTypeface* defaultFace =
49                SkFontHost::CreateTypeface(NULL, NULL, NULL, 0,
50                                           SkTypeface::kNormal);
51        SkASSERT(defaultFace);
52        gDefaultFontID = defaultFace->uniqueID();
53        defaultFace->unref();
54    }
55    return gDefaultFontID;
56}
57
58bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) {
59    return SkTypeface::UniqueID(facea) == SkTypeface::UniqueID(faceb);
60}
61
62///////////////////////////////////////////////////////////////////////////////
63
64SkTypeface* SkTypeface::CreateFromName(const char name[], Style style) {
65    return SkFontHost::CreateTypeface(NULL, name, NULL, 0, style);
66}
67
68SkTypeface* SkTypeface::CreateForChars(const void* data, size_t bytelength,
69                                       Style s) {
70    return SkFontHost::CreateTypeface(NULL, NULL, data, bytelength, s);
71}
72
73SkTypeface* SkTypeface::CreateFromTypeface(const SkTypeface* family, Style s) {
74    return SkFontHost::CreateTypeface(family, NULL, NULL, 0, s);
75}
76
77SkTypeface* SkTypeface::CreateFromStream(SkStream* stream) {
78    return SkFontHost::CreateTypefaceFromStream(stream);
79}
80
81SkTypeface* SkTypeface::CreateFromFile(const char path[]) {
82    return SkFontHost::CreateTypefaceFromFile(path);
83}
84
85///////////////////////////////////////////////////////////////////////////////
86
87void SkTypeface::serialize(SkWStream* stream) const {
88    SkFontHost::Serialize(this, stream);
89}
90
91SkTypeface* SkTypeface::Deserialize(SkStream* stream) {
92    return SkFontHost::Deserialize(stream);
93}
94
95SkAdvancedTypefaceMetrics* SkTypeface::getAdvancedTypefaceMetrics(
96        SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo) const {
97    return SkFontHost::GetAdvancedTypefaceMetrics(fUniqueID, perGlyphInfo);
98}
99