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 | 80f5465 | 2013-02-25 22:19:20 +0000 | [diff] [blame] | 14 | #include "SkTypeface.h" |
reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 15 | |
bungeman | 0265707 | 2016-05-02 11:54:13 -0700 | [diff] [blame] | 16 | class SkFontMgr; |
tomhudson | e438ddb | 2014-07-01 18:54:41 -0700 | [diff] [blame] | 17 | |
reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 18 | /** |
| 19 | * \class SkFontConfigInterface |
| 20 | * |
bungeman | 7be2eb8 | 2015-02-23 08:25:00 -0800 | [diff] [blame] | 21 | * A simple interface for remotable font management. |
| 22 | * The global instance can be found with RefGlobal(). |
reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 23 | */ |
reed@google.com | 86a44b8 | 2013-03-04 17:26:02 +0000 | [diff] [blame] | 24 | class SK_API SkFontConfigInterface : public SkRefCnt { |
reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 25 | public: |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 26 | |
reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 27 | /** |
| 28 | * Returns the global SkFontConfigInterface instance, and if it is not |
| 29 | * NULL, calls ref() on it. The caller must balance this with a call to |
| 30 | * unref(). |
| 31 | */ |
| 32 | static SkFontConfigInterface* RefGlobal(); |
| 33 | |
| 34 | /** |
| 35 | * Replace the current global instance with the specified one, safely |
| 36 | * ref'ing the new instance, and unref'ing the previous. Returns its |
| 37 | * parameter (the new global instance). |
| 38 | */ |
| 39 | static SkFontConfigInterface* SetGlobal(SkFontConfigInterface*); |
| 40 | |
| 41 | /** |
reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 42 | * This should be treated as private to the impl of SkFontConfigInterface. |
| 43 | * Callers should not change or expect any particular values. It is meant |
| 44 | * 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] | 45 | */ |
reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 46 | struct FontIdentity { |
reed@google.com | 8c9737e | 2013-03-06 13:06:03 +0000 | [diff] [blame] | 47 | FontIdentity() : fID(0), fTTCIndex(0) {} |
| 48 | |
| 49 | bool operator==(const FontIdentity& other) const { |
| 50 | return fID == other.fID && |
| 51 | fTTCIndex == other.fTTCIndex && |
| 52 | fString == other.fString; |
| 53 | } |
reed@google.com | f55061f | 2013-04-22 18:48:45 +0000 | [diff] [blame] | 54 | bool operator!=(const FontIdentity& other) const { |
| 55 | return !(*this == other); |
| 56 | } |
reed@google.com | 8c9737e | 2013-03-06 13:06:03 +0000 | [diff] [blame] | 57 | |
| 58 | uint32_t fID; |
| 59 | int32_t fTTCIndex; |
reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 60 | SkString fString; |
reed@google.com | 54c6914 | 2013-04-09 15:54:52 +0000 | [diff] [blame] | 61 | SkFontStyle fStyle; |
skia.committer@gmail.com | e36a168 | 2013-04-23 07:01:29 +0000 | [diff] [blame] | 62 | |
reed@google.com | f55061f | 2013-04-22 18:48:45 +0000 | [diff] [blame] | 63 | // If buffer is NULL, just return the number of bytes that would have |
| 64 | // been written. Will pad contents to a multiple of 4. |
| 65 | size_t writeToMemory(void* buffer = NULL) const; |
skia.committer@gmail.com | e36a168 | 2013-04-23 07:01:29 +0000 | [diff] [blame] | 66 | |
reed@google.com | f55061f | 2013-04-22 18:48:45 +0000 | [diff] [blame] | 67 | // Recreate from a flattened buffer, returning the number of bytes read. |
| 68 | size_t readFromMemory(const void* buffer, size_t length); |
reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 69 | }; |
reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 70 | |
| 71 | /** |
reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 72 | * Given a familyName and style, find the best match. |
| 73 | * |
| 74 | * If a match is found, return true and set its outFontIdentifier. |
| 75 | * If outFamilyName is not null, assign the found familyName to it |
| 76 | * (which may differ from the requested familyName). |
| 77 | * If outStyle is not null, assign the found style to it |
| 78 | * (which may differ from the requested style). |
| 79 | * |
| 80 | * 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] | 81 | */ |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 82 | virtual bool matchFamilyName(const char familyName[], |
| 83 | SkFontStyle requested, |
| 84 | FontIdentity* outFontIdentifier, |
| 85 | SkString* outFamilyName, |
| 86 | SkFontStyle* outStyle) = 0; |
reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 87 | |
| 88 | /** |
reed@google.com | f71a233 | 2013-02-27 19:06:30 +0000 | [diff] [blame] | 89 | * Given a FontRef, open a stream to access its data, or return null |
| 90 | * if the FontRef's data is not available. The caller is responsible for |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 91 | * deleting the stream when it is done accessing the data. |
reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 92 | */ |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 93 | virtual SkStreamAsset* openStream(const FontIdentity&) = 0; |
reed@google.com | d66045e | 2013-03-04 19:07:02 +0000 | [diff] [blame] | 94 | |
| 95 | /** |
fmalita | 9b137dc | 2015-11-20 13:47:55 -0800 | [diff] [blame] | 96 | * Return an SkTypeface for the given FontIdentity. |
| 97 | * |
| 98 | * The default implementation simply returns a new typeface built using data obtained from |
| 99 | * openStream(), but derived classes may implement more complex caching schemes. |
fmalita | 9b137dc | 2015-11-20 13:47:55 -0800 | [diff] [blame] | 100 | */ |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 101 | virtual sk_sp<SkTypeface> makeTypeface(const FontIdentity& identity) { |
| 102 | return SkTypeface::MakeFromStream(this->openStream(identity), identity.fTTCIndex); |
bungeman | 6296da7 | 2016-05-11 12:38:18 -0700 | [diff] [blame] | 103 | } |
fmalita | 9b137dc | 2015-11-20 13:47:55 -0800 | [diff] [blame] | 104 | |
| 105 | /** |
reed@google.com | d66045e | 2013-03-04 19:07:02 +0000 | [diff] [blame] | 106 | * Return a singleton instance of a direct subclass that calls into |
| 107 | * libfontconfig. This does not affect the refcnt of the returned instance. |
tomhudson | e438ddb | 2014-07-01 18:54:41 -0700 | [diff] [blame] | 108 | * The mutex may be used to guarantee the singleton is only constructed once. |
reed@google.com | d66045e | 2013-03-04 19:07:02 +0000 | [diff] [blame] | 109 | */ |
bungeman | 0265707 | 2016-05-02 11:54:13 -0700 | [diff] [blame] | 110 | static SkFontConfigInterface* GetSingletonDirectInterface(); |
reed@google.com | 54c6914 | 2013-04-09 15:54:52 +0000 | [diff] [blame] | 111 | |
| 112 | // New APIS, which have default impls for now (which do nothing) |
| 113 | |
bungeman | feb3c1a | 2016-08-05 06:51:50 -0700 | [diff] [blame^] | 114 | virtual sk_sp<SkDataTable> getFamilyNames() { return SkDataTable::MakeEmpty(); } |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame] | 115 | typedef SkRefCnt INHERITED; |
reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
bungeman | 0265707 | 2016-05-02 11:54:13 -0700 | [diff] [blame] | 118 | /** Creates a SkFontMgr which wraps a SkFontConfigInterface. */ |
| 119 | SK_API SkFontMgr* SkFontMgr_New_FCI(SkFontConfigInterface* fci); |
| 120 | |
reed@google.com | d71fe99 | 2013-02-25 20:38:07 +0000 | [diff] [blame] | 121 | #endif |