blob: 8c12a5628ad05bf9e2ef625f7f7d53921474de10 [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.com027fd202013-04-19 20:45:30 +000011#include "SkDataTable.h"
reed@google.com54c69142013-04-09 15:54:52 +000012#include "SkFontStyle.h"
reed@google.comd71fe992013-02-25 20:38:07 +000013#include "SkRefCnt.h"
reed@google.com027fd202013-04-19 20:45:30 +000014#include "SkTArray.h"
reed@google.com80f54652013-02-25 22:19:20 +000015#include "SkTypeface.h"
reed@google.comd71fe992013-02-25 20:38:07 +000016
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.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 SK_DECLARE_INST_COUNT(SkFontConfigInterface)
26
reed@google.comd71fe992013-02-25 20:38:07 +000027 /**
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.comf71a2332013-02-27 19:06:30 +000042 * 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.comd71fe992013-02-25 20:38:07 +000045 */
reed@google.comf71a2332013-02-27 19:06:30 +000046 struct FontIdentity {
reed@google.com8c9737e2013-03-06 13:06:03 +000047 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.comf55061f2013-04-22 18:48:45 +000054 bool operator!=(const FontIdentity& other) const {
55 return !(*this == other);
56 }
reed@google.com8c9737e2013-03-06 13:06:03 +000057
58 uint32_t fID;
59 int32_t fTTCIndex;
reed@google.comf71a2332013-02-27 19:06:30 +000060 SkString fString;
reed@google.com54c69142013-04-09 15:54:52 +000061 SkFontStyle fStyle;
skia.committer@gmail.come36a1682013-04-23 07:01:29 +000062
reed@google.comf55061f2013-04-22 18:48:45 +000063 // 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.come36a1682013-04-23 07:01:29 +000066
reed@google.comf55061f2013-04-22 18:48:45 +000067 // Recreate from a flattened buffer, returning the number of bytes read.
68 size_t readFromMemory(const void* buffer, size_t length);
reed@google.comf71a2332013-02-27 19:06:30 +000069 };
reed@google.comd71fe992013-02-25 20:38:07 +000070
71 /**
reed@google.comf71a2332013-02-27 19:06:30 +000072 * 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.comd71fe992013-02-25 20:38:07 +000081 */
reed@google.comf71a2332013-02-27 19:06:30 +000082 virtual bool matchFamilyName(const char familyName[],
83 SkTypeface::Style requested,
84 FontIdentity* outFontIdentifier,
85 SkString* outFamilyName,
86 SkTypeface::Style* outStyle) = 0;
reed@google.comd71fe992013-02-25 20:38:07 +000087
88 /**
reed@google.comf71a2332013-02-27 19:06:30 +000089 * 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
91 * calling stream->unref() when it is done accessing the data.
reed@google.comd71fe992013-02-25 20:38:07 +000092 */
reed@google.comf71a2332013-02-27 19:06:30 +000093 virtual SkStream* openStream(const FontIdentity&) = 0;
reed@google.comd66045e2013-03-04 19:07:02 +000094
95 /**
96 * Return a singleton instance of a direct subclass that calls into
97 * libfontconfig. This does not affect the refcnt of the returned instance.
98 */
99 static SkFontConfigInterface* GetSingletonDirectInterface();
reed@google.com54c69142013-04-09 15:54:52 +0000100
101 // New APIS, which have default impls for now (which do nothing)
102
reed@google.com027fd202013-04-19 20:45:30 +0000103 virtual SkDataTable* getFamilyNames() { return SkDataTable::NewEmpty(); }
104 virtual bool matchFamilySet(const char inFamilyName[],
105 SkString* outFamilyName,
106 SkTArray<FontIdentity>*) {
107 return false;
robertphillips@google.com21db1db2013-04-09 23:56:51 +0000108 }
commit-bot@chromium.orgab1c1382013-12-05 12:08:12 +0000109 typedef SkRefCnt INHERITED;
reed@google.comd71fe992013-02-25 20:38:07 +0000110};
111
112#endif