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