blob: 12f777d655a924b7d5d6c6554d94d83771ad69bb [file] [log] [blame]
reed@google.comd71fe992013-02-25 20:38:07 +00001/*
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.com54c69142013-04-09 15:54:52 +000011#include "SkFontStyle.h"
reed@google.comd71fe992013-02-25 20:38:07 +000012#include "SkRefCnt.h"
reed@google.com80f54652013-02-25 22:19:20 +000013#include "SkTypeface.h"
reed@google.comd71fe992013-02-25 20:38:07 +000014
bungeman02657072016-05-02 11:54:13 -070015class SkFontMgr;
tomhudsone438ddb2014-07-01 18:54:41 -070016
reed@google.comd71fe992013-02-25 20:38:07 +000017/**
18 * \class SkFontConfigInterface
19 *
bungeman7be2eb82015-02-23 08:25:00 -080020 * A simple interface for remotable font management.
21 * The global instance can be found with RefGlobal().
reed@google.comd71fe992013-02-25 20:38:07 +000022 */
reed@google.com86a44b82013-03-04 17:26:02 +000023class SK_API SkFontConfigInterface : public SkRefCnt {
reed@google.comd71fe992013-02-25 20:38:07 +000024public:
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +000025
reed@google.comd71fe992013-02-25 20:38:07 +000026 /**
bungeman1ae0e012016-09-19 12:13:16 -070027 * Returns the global SkFontConfigInterface instance. If it is not
28 * nullptr, calls ref() on it. The caller must balance this with a call to
29 * unref(). The default SkFontConfigInterface is the result of calling
30 * GetSingletonDirectInterface.
reed@google.comd71fe992013-02-25 20:38:07 +000031 */
Ben Wagnera20681c2018-05-14 13:51:10 -040032 static sk_sp<SkFontConfigInterface> RefGlobal();
reed@google.comd71fe992013-02-25 20:38:07 +000033
34 /**
Ben Wagnera20681c2018-05-14 13:51:10 -040035 * Replace the current global instance with the specified one.
reed@google.comd71fe992013-02-25 20:38:07 +000036 */
Ben Wagnera20681c2018-05-14 13:51:10 -040037 static void SetGlobal(sk_sp<SkFontConfigInterface> fc);
reed@google.comd71fe992013-02-25 20:38:07 +000038
39 /**
reed@google.comf71a2332013-02-27 19:06:30 +000040 * 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.comd71fe992013-02-25 20:38:07 +000043 */
reed@google.comf71a2332013-02-27 19:06:30 +000044 struct FontIdentity {
reed@google.com8c9737e2013-03-06 13:06:03 +000045 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.comf55061f2013-04-22 18:48:45 +000052 bool operator!=(const FontIdentity& other) const {
53 return !(*this == other);
54 }
reed@google.com8c9737e2013-03-06 13:06:03 +000055
56 uint32_t fID;
57 int32_t fTTCIndex;
reed@google.comf71a2332013-02-27 19:06:30 +000058 SkString fString;
reed@google.com54c69142013-04-09 15:54:52 +000059 SkFontStyle fStyle;
skia.committer@gmail.come36a1682013-04-23 07:01:29 +000060
reed@google.comf55061f2013-04-22 18:48:45 +000061 // If buffer is NULL, just return the number of bytes that would have
62 // been written. Will pad contents to a multiple of 4.
Ben Wagnera93a14a2017-08-28 10:34:05 -040063 size_t writeToMemory(void* buffer = nullptr) const;
skia.committer@gmail.come36a1682013-04-23 07:01:29 +000064
reed@google.comf55061f2013-04-22 18:48:45 +000065 // Recreate from a flattened buffer, returning the number of bytes read.
66 size_t readFromMemory(const void* buffer, size_t length);
reed@google.comf71a2332013-02-27 19:06:30 +000067 };
reed@google.comd71fe992013-02-25 20:38:07 +000068
69 /**
reed@google.comf71a2332013-02-27 19:06:30 +000070 * 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.comd71fe992013-02-25 20:38:07 +000079 */
bungeman11a77c62016-04-12 13:45:06 -070080 virtual bool matchFamilyName(const char familyName[],
81 SkFontStyle requested,
82 FontIdentity* outFontIdentifier,
83 SkString* outFamilyName,
84 SkFontStyle* outStyle) = 0;
reed@google.comd71fe992013-02-25 20:38:07 +000085
86 /**
reed@google.comf71a2332013-02-27 19:06:30 +000087 * 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
bungeman5f213d92015-01-27 05:39:10 -080089 * deleting the stream when it is done accessing the data.
reed@google.comd71fe992013-02-25 20:38:07 +000090 */
bungeman5f213d92015-01-27 05:39:10 -080091 virtual SkStreamAsset* openStream(const FontIdentity&) = 0;
reed@google.comd66045e2013-03-04 19:07:02 +000092
93 /**
fmalita9b137dc2015-11-20 13:47:55 -080094 * Return an SkTypeface for the given FontIdentity.
95 *
96 * The default implementation simply returns a new typeface built using data obtained from
97 * openStream(), but derived classes may implement more complex caching schemes.
fmalita9b137dc2015-11-20 13:47:55 -080098 */
bungeman13b9c952016-05-12 10:09:30 -070099 virtual sk_sp<SkTypeface> makeTypeface(const FontIdentity& identity) {
100 return SkTypeface::MakeFromStream(this->openStream(identity), identity.fTTCIndex);
bungeman6296da72016-05-11 12:38:18 -0700101 }
fmalita9b137dc2015-11-20 13:47:55 -0800102
103 /**
reed@google.comd66045e2013-03-04 19:07:02 +0000104 * Return a singleton instance of a direct subclass that calls into
105 * libfontconfig. This does not affect the refcnt of the returned instance.
106 */
bungeman02657072016-05-02 11:54:13 -0700107 static SkFontConfigInterface* GetSingletonDirectInterface();
reed@google.com54c69142013-04-09 15:54:52 +0000108
commit-bot@chromium.orgab1c1382013-12-05 12:08:12 +0000109 typedef SkRefCnt INHERITED;
reed@google.comd71fe992013-02-25 20:38:07 +0000110};
111
reed@google.comd71fe992013-02-25 20:38:07 +0000112#endif