blob: 01346f253175eb606bcf0838d73b76b1a0fb9369 [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"
Mike Reed41b67732018-09-07 11:19:39 -040013#include "SkStream.h"
reed@google.com80f54652013-02-25 22:19:20 +000014#include "SkTypeface.h"
reed@google.comd71fe992013-02-25 20:38:07 +000015
bungeman02657072016-05-02 11:54:13 -070016class SkFontMgr;
tomhudsone438ddb2014-07-01 18:54:41 -070017
reed@google.comd71fe992013-02-25 20:38:07 +000018/**
19 * \class SkFontConfigInterface
20 *
bungeman7be2eb82015-02-23 08:25:00 -080021 * A simple interface for remotable font management.
22 * The global instance can be found with RefGlobal().
reed@google.comd71fe992013-02-25 20:38:07 +000023 */
reed@google.com86a44b82013-03-04 17:26:02 +000024class SK_API SkFontConfigInterface : public SkRefCnt {
reed@google.comd71fe992013-02-25 20:38:07 +000025public:
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +000026
reed@google.comd71fe992013-02-25 20:38:07 +000027 /**
bungeman1ae0e012016-09-19 12:13:16 -070028 * 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.
reed@google.comd71fe992013-02-25 20:38:07 +000032 */
Ben Wagnera20681c2018-05-14 13:51:10 -040033 static sk_sp<SkFontConfigInterface> RefGlobal();
reed@google.comd71fe992013-02-25 20:38:07 +000034
35 /**
Ben Wagnera20681c2018-05-14 13:51:10 -040036 * Replace the current global instance with the specified one.
reed@google.comd71fe992013-02-25 20:38:07 +000037 */
Ben Wagnera20681c2018-05-14 13:51:10 -040038 static void SetGlobal(sk_sp<SkFontConfigInterface> fc);
reed@google.comd71fe992013-02-25 20:38:07 +000039
40 /**
reed@google.comf71a2332013-02-27 19:06:30 +000041 * 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.
reed@google.comd71fe992013-02-25 20:38:07 +000044 */
reed@google.comf71a2332013-02-27 19:06:30 +000045 struct FontIdentity {
reed@google.com8c9737e2013-03-06 13:06:03 +000046 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 }
reed@google.comf55061f2013-04-22 18:48:45 +000053 bool operator!=(const FontIdentity& other) const {
54 return !(*this == other);
55 }
reed@google.com8c9737e2013-03-06 13:06:03 +000056
57 uint32_t fID;
58 int32_t fTTCIndex;
reed@google.comf71a2332013-02-27 19:06:30 +000059 SkString fString;
reed@google.com54c69142013-04-09 15:54:52 +000060 SkFontStyle fStyle;
skia.committer@gmail.come36a1682013-04-23 07:01:29 +000061
reed@google.comf55061f2013-04-22 18:48:45 +000062 // If buffer is NULL, just return the number of bytes that would have
63 // been written. Will pad contents to a multiple of 4.
Ben Wagnera93a14a2017-08-28 10:34:05 -040064 size_t writeToMemory(void* buffer = nullptr) const;
skia.committer@gmail.come36a1682013-04-23 07:01:29 +000065
reed@google.comf55061f2013-04-22 18:48:45 +000066 // Recreate from a flattened buffer, returning the number of bytes read.
67 size_t readFromMemory(const void* buffer, size_t length);
reed@google.comf71a2332013-02-27 19:06:30 +000068 };
reed@google.comd71fe992013-02-25 20:38:07 +000069
70 /**
reed@google.comf71a2332013-02-27 19:06:30 +000071 * 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.
reed@google.comd71fe992013-02-25 20:38:07 +000080 */
bungeman11a77c62016-04-12 13:45:06 -070081 virtual bool matchFamilyName(const char familyName[],
82 SkFontStyle requested,
83 FontIdentity* outFontIdentifier,
84 SkString* outFamilyName,
85 SkFontStyle* outStyle) = 0;
reed@google.comd71fe992013-02-25 20:38:07 +000086
87 /**
reed@google.comf71a2332013-02-27 19:06:30 +000088 * 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
bungeman5f213d92015-01-27 05:39:10 -080090 * deleting the stream when it is done accessing the data.
reed@google.comd71fe992013-02-25 20:38:07 +000091 */
bungeman5f213d92015-01-27 05:39:10 -080092 virtual SkStreamAsset* openStream(const FontIdentity&) = 0;
reed@google.comd66045e2013-03-04 19:07:02 +000093
94 /**
fmalita9b137dc2015-11-20 13:47:55 -080095 * 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.
fmalita9b137dc2015-11-20 13:47:55 -080099 */
bungeman13b9c952016-05-12 10:09:30 -0700100 virtual sk_sp<SkTypeface> makeTypeface(const FontIdentity& identity) {
Mike Reed41b67732018-09-07 11:19:39 -0400101 return SkTypeface::MakeFromStream(std::unique_ptr<SkStreamAsset>(this->openStream(identity)),
102 identity.fTTCIndex);
103
bungeman6296da72016-05-11 12:38:18 -0700104 }
fmalita9b137dc2015-11-20 13:47:55 -0800105
106 /**
reed@google.comd66045e2013-03-04 19:07:02 +0000107 * Return a singleton instance of a direct subclass that calls into
108 * libfontconfig. This does not affect the refcnt of the returned instance.
109 */
bungeman02657072016-05-02 11:54:13 -0700110 static SkFontConfigInterface* GetSingletonDirectInterface();
reed@google.com54c69142013-04-09 15:54:52 +0000111
commit-bot@chromium.orgab1c1382013-12-05 12:08:12 +0000112 typedef SkRefCnt INHERITED;
reed@google.comd71fe992013-02-25 20:38:07 +0000113};
114
reed@google.comd71fe992013-02-25 20:38:07 +0000115#endif