1 /* 2 * Copyright 2013 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 #ifndef SkFontConfigInterface_DEFINED 9 #define SkFontConfigInterface_DEFINED 10 11 #include "include/core/SkFontStyle.h" 12 #include "include/core/SkRefCnt.h" 13 #include "include/core/SkStream.h" 14 #include "include/core/SkTypeface.h" 15 16 class SkFontMgr; 17 18 /** 19 * \class SkFontConfigInterface 20 * 21 * A simple interface for remotable font management. 22 * The global instance can be found with RefGlobal(). 23 */ 24 class SK_API SkFontConfigInterface : public SkRefCnt { 25 public: 26 27 /** 28 * Returns the global SkFontConfigInterface instance. If it is not 29 * nullptr, calls ref() on it. The caller must balance this with a call to 30 * unref(). The default SkFontConfigInterface is the result of calling 31 * GetSingletonDirectInterface. 32 */ 33 static sk_sp<SkFontConfigInterface> RefGlobal(); 34 35 /** 36 * Replace the current global instance with the specified one. 37 */ 38 static void SetGlobal(sk_sp<SkFontConfigInterface> fc); 39 40 /** 41 * This should be treated as private to the impl of SkFontConfigInterface. 42 * Callers should not change or expect any particular values. It is meant 43 * to be a union of possible storage types to aid the impl. 44 */ 45 struct FontIdentity { FontIdentityFontIdentity46 FontIdentity() : fID(0), fTTCIndex(0) {} 47 48 bool operator==(const FontIdentity& other) const { 49 return fID == other.fID && 50 fTTCIndex == other.fTTCIndex && 51 fString == other.fString; 52 } 53 bool operator!=(const FontIdentity& other) const { 54 return !(*this == other); 55 } 56 57 uint32_t fID; 58 int32_t fTTCIndex; 59 SkString fString; 60 SkFontStyle fStyle; 61 62 // If buffer is NULL, just return the number of bytes that would have 63 // been written. Will pad contents to a multiple of 4. 64 size_t writeToMemory(void* buffer = nullptr) const; 65 66 // Recreate from a flattened buffer, returning the number of bytes read. 67 size_t readFromMemory(const void* buffer, size_t length); 68 }; 69 70 /** 71 * Given a familyName and style, find the best match. 72 * 73 * If a match is found, return true and set its outFontIdentifier. 74 * If outFamilyName is not null, assign the found familyName to it 75 * (which may differ from the requested familyName). 76 * If outStyle is not null, assign the found style to it 77 * (which may differ from the requested style). 78 * 79 * If a match is not found, return false, and ignore all out parameters. 80 */ 81 virtual bool matchFamilyName(const char familyName[], 82 SkFontStyle requested, 83 FontIdentity* outFontIdentifier, 84 SkString* outFamilyName, 85 SkFontStyle* outStyle) = 0; 86 87 /** 88 * Given a FontRef, open a stream to access its data, or return null 89 * if the FontRef's data is not available. The caller is responsible for 90 * deleting the stream when it is done accessing the data. 91 */ 92 virtual SkStreamAsset* openStream(const FontIdentity&) = 0; 93 94 /** 95 * Return an SkTypeface for the given FontIdentity. 96 * 97 * The default implementation simply returns a new typeface built using data obtained from 98 * openStream(), but derived classes may implement more complex caching schemes. 99 */ makeTypeface(const FontIdentity & identity)100 virtual sk_sp<SkTypeface> makeTypeface(const FontIdentity& identity) { 101 return SkTypeface::MakeFromStream(std::unique_ptr<SkStreamAsset>(this->openStream(identity)), 102 identity.fTTCIndex); 103 104 } 105 106 /** 107 * Return a singleton instance of a direct subclass that calls into 108 * libfontconfig. This does not affect the refcnt of the returned instance. 109 */ 110 static SkFontConfigInterface* GetSingletonDirectInterface(); 111 112 typedef SkRefCnt INHERITED; 113 }; 114 115 #endif 116