| reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 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 | |
| reed@google.com | 027fd20 | 2013-04-19 20:45:30 +0000 | [diff] [blame] | 11 | #include "SkDataTable.h" |
| reed@google.com | 54c6914 | 2013-04-09 15:54:52 +0000 | [diff] [blame] | 12 | #include "SkFontStyle.h" |
| reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 13 | #include "SkRefCnt.h" |
| reed@google.com | 027fd20 | 2013-04-19 20:45:30 +0000 | [diff] [blame] | 14 | #include "SkTArray.h" |
| reed@google.com | 80f5465 | 2013-02-25 22:19:20 +0000 | [diff] [blame] | 15 | #include "SkTypeface.h" |
| reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * \class SkFontConfigInterface |
| 19 | * |
| 20 | * Provides SkFontHost clients with access to fontconfig services. They will |
| 21 | * access the global instance found in RefGlobal(). |
| 22 | */ |
| reed@google.com | 86a44b8 | 2013-03-04 17:26:02 +0000 | [diff] [blame] | 23 | class SK_API SkFontConfigInterface : public SkRefCnt { |
| reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 24 | public: |
| 25 | /** |
| 26 | * Returns the global SkFontConfigInterface instance, and if it is not |
| 27 | * NULL, calls ref() on it. The caller must balance this with a call to |
| 28 | * unref(). |
| 29 | */ |
| 30 | static SkFontConfigInterface* RefGlobal(); |
| 31 | |
| 32 | /** |
| 33 | * Replace the current global instance with the specified one, safely |
| 34 | * ref'ing the new instance, and unref'ing the previous. Returns its |
| 35 | * parameter (the new global instance). |
| 36 | */ |
| 37 | static SkFontConfigInterface* SetGlobal(SkFontConfigInterface*); |
| 38 | |
| 39 | /** |
| reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 40 | * This should be treated as private to the impl of SkFontConfigInterface. |
| 41 | * Callers should not change or expect any particular values. It is meant |
| 42 | * to be a union of possible storage types to aid the impl. |
| reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 43 | */ |
| reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 44 | struct FontIdentity { |
| reed@google.com | 8c9737e | 2013-03-06 13:06:03 +0000 | [diff] [blame] | 45 | FontIdentity() : fID(0), fTTCIndex(0) {} |
| 46 | |
| 47 | bool operator==(const FontIdentity& other) const { |
| 48 | return fID == other.fID && |
| 49 | fTTCIndex == other.fTTCIndex && |
| 50 | fString == other.fString; |
| 51 | } |
| reed@google.com | f55061f | 2013-04-22 18:48:45 +0000 | [diff] [blame^] | 52 | bool operator!=(const FontIdentity& other) const { |
| 53 | return !(*this == other); |
| 54 | } |
| reed@google.com | 8c9737e | 2013-03-06 13:06:03 +0000 | [diff] [blame] | 55 | |
| 56 | uint32_t fID; |
| 57 | int32_t fTTCIndex; |
| reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 58 | SkString fString; |
| reed@google.com | 54c6914 | 2013-04-09 15:54:52 +0000 | [diff] [blame] | 59 | SkFontStyle fStyle; |
| reed@google.com | f55061f | 2013-04-22 18:48:45 +0000 | [diff] [blame^] | 60 | |
| 61 | // If buffer is NULL, just return the number of bytes that would have |
| 62 | // been written. Will pad contents to a multiple of 4. |
| 63 | size_t writeToMemory(void* buffer = NULL) const; |
| 64 | |
| 65 | // Recreate from a flattened buffer, returning the number of bytes read. |
| 66 | size_t readFromMemory(const void* buffer, size_t length); |
| reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 67 | }; |
| reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 68 | |
| 69 | /** |
| reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 70 | * Given a familyName and style, find the best match. |
| 71 | * |
| 72 | * If a match is found, return true and set its outFontIdentifier. |
| 73 | * If outFamilyName is not null, assign the found familyName to it |
| 74 | * (which may differ from the requested familyName). |
| 75 | * If outStyle is not null, assign the found style to it |
| 76 | * (which may differ from the requested style). |
| 77 | * |
| 78 | * If a match is not found, return false, and ignore all out parameters. |
| reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 79 | */ |
| reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 80 | virtual bool matchFamilyName(const char familyName[], |
| 81 | SkTypeface::Style requested, |
| 82 | FontIdentity* outFontIdentifier, |
| 83 | SkString* outFamilyName, |
| 84 | SkTypeface::Style* outStyle) = 0; |
| reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 85 | |
| 86 | /** |
| reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 87 | * Given a FontRef, open a stream to access its data, or return null |
| 88 | * if the FontRef's data is not available. The caller is responsible for |
| 89 | * calling stream->unref() when it is done accessing the data. |
| reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 90 | */ |
| reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 91 | virtual SkStream* openStream(const FontIdentity&) = 0; |
| reed@google.com | d66045e | 2013-03-04 19:07:02 +0000 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Return a singleton instance of a direct subclass that calls into |
| 95 | * libfontconfig. This does not affect the refcnt of the returned instance. |
| 96 | */ |
| 97 | static SkFontConfigInterface* GetSingletonDirectInterface(); |
| reed@google.com | 54c6914 | 2013-04-09 15:54:52 +0000 | [diff] [blame] | 98 | |
| 99 | // New APIS, which have default impls for now (which do nothing) |
| 100 | |
| reed@google.com | 027fd20 | 2013-04-19 20:45:30 +0000 | [diff] [blame] | 101 | virtual SkDataTable* getFamilyNames() { return SkDataTable::NewEmpty(); } |
| 102 | virtual bool matchFamilySet(const char inFamilyName[], |
| 103 | SkString* outFamilyName, |
| 104 | SkTArray<FontIdentity>*) { |
| 105 | return false; |
| robertphillips@google.com | 21db1db | 2013-04-09 23:56:51 +0000 | [diff] [blame] | 106 | } |
| reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | #endif |