172cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com/*
272cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com * Copyright 2014 Google Inc.
372cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com *
472cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com * Use of this source code is governed by a BSD-style license that can be
572cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com * found in the LICENSE file.
672cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com */
772cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
872cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com#ifndef SkRemotableFontMgr_DEFINED
972cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com#define SkRemotableFontMgr_DEFINED
1072cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
1172cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com#include "SkFontStyle.h"
1272cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com#include "SkRefCnt.h"
1372cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com#include "SkTemplates.h"
1472cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
1572cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.comclass SkDataTable;
1672cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.comclass SkStreamAsset;
1772cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.comclass SkString;
1872cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
1972cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.comstruct SK_API SkFontIdentity {
2072cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    static const uint32_t kInvalidDataId = 0xFFFFFFFF;
2172cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
2272cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    // Note that fDataId is a data identifier, not a font identifier.
2372cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    // (fDataID, fTtcIndex) can be seen as a font identifier.
2472cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    uint32_t fDataId;
2572cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    uint32_t fTtcIndex;
2672cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
2772cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    // On Linux/FontConfig there is also the ability to specify preferences for rendering
2872cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    // antialias, embedded bitmaps, autohint, hinting, hintstyle, lcd rendering
2972cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    // may all be set or set to no-preference
3072cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    // (No-preference is resolved against globals set by the platform)
3172cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    // Since they may be selected against, these are really 'extensions' to SkFontStyle.
3272cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    // SkFontStyle should pick these up.
3372cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    SkFontStyle fFontStyle;
3472cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com};
3572cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
3672cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.comclass SK_API SkRemotableFontIdentitySet : public SkRefCnt {
3772cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.compublic:
3872cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    SK_DECLARE_INST_COUNT(SkRemotableFontIdentitySet)
3972cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
4072cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    SkRemotableFontIdentitySet(int count, SkFontIdentity** data);
4172cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
4272cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    int count() const { return fCount; }
4372cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    const SkFontIdentity& at(int index) const { return fData[index]; }
4472cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
4572cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    static SkRemotableFontIdentitySet* NewEmpty();
4672cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
4772cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.comprivate:
4872cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    SkRemotableFontIdentitySet() : fCount(0), fData() { }
4978358bf624c7e7c09ffccf638c50870808d884d6mtklein    static SkRemotableFontIdentitySet* NewEmptyImpl();
5072cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
5172cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    int fCount;
5272cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    SkAutoTMalloc<SkFontIdentity> fData;
5372cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
5472cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    typedef SkRefCnt INHERITED;
5572cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com};
5672cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
5772cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.comclass SK_API SkRemotableFontMgr : public SkRefCnt {
5872cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.compublic:
5972cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    SK_DECLARE_INST_COUNT(SkRemotableFontMgr)
6072cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
6172cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    /**
6272cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  Returns the names of the known fonts on the system.
6372cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  Will not return NULL, will return an empty table if no families exist.
6472cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *
6572cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  The indexes may be used with getIndex(int) and
6672cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  matchIndexStyle(int, SkFontStyle).
6772cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *
6872cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  The caller must unref() the returned object.
6972cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     */
7072cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    virtual SkDataTable* getFamilyNames() const = 0;
7172cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
7272cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    /**
7372cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  Returns all of the fonts with the given familyIndex.
7472cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  Returns NULL if the index is out of bounds.
7572cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  Returns empty if there are no fonts at the given index.
7672cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *
7772cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  The caller must unref() the returned object.
7872cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     */
7972cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    virtual SkRemotableFontIdentitySet* getIndex(int familyIndex) const = 0;
8072cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
8172cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    /**
8272cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  Returns the closest match to the given style in the given index.
8372cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  If there are no available fonts at the given index, the return value's
8472cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  data id will be kInvalidDataId.
8572cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     */
8672cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    virtual SkFontIdentity matchIndexStyle(int familyIndex, const SkFontStyle&) const = 0;
8772cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
8872cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    /**
8972cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  Returns all the fonts on the system with the given name.
9072cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  If the given name is NULL, will return the default font family.
9172cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  Never returns NULL; will return an empty set if the name is not found.
9272cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *
9372cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  It is possible that this will return fonts not accessible from
9472cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  getIndex(int) or matchIndexStyle(int, SkFontStyle) due to
9572cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  hidden or auto-activated fonts.
9672cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *
9772cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  The matching may be done in a system dependent way. The name may be
9872cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  matched case-insensitive, there may be system aliases which resolve,
9972cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  and names outside the current locale may be considered. However, this
10072cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  should only return fonts which are somehow associated with the requested
10172cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  name.
10272cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *
10372cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  The caller must unref() the returned object.
10472cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     */
10572cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    virtual SkRemotableFontIdentitySet* matchName(const char familyName[]) const = 0;
10672cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
10772cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    /**
10872cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  Returns the closest matching font to the specified name and style.
10972cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  If there are no available fonts which match the name, the return value's
11072cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  data id will be kInvalidDataId.
11172cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  If the given name is NULL, the match will be against any default fonts.
11272cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *
11372cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  It is possible that this will return a font identity not accessible from
11472cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  methods returning sets due to hidden or auto-activated fonts.
11572cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *
11672cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  The matching may be done in a system dependent way. The name may be
11772cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  matched case-insensitive, there may be system aliases which resolve,
11872cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  and names outside the current locale may be considered. However, this
11972cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  should only return a font which is somehow associated with the requested
12072cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  name.
12172cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *
12272cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  The caller must unref() the returned object.
12372cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     */
12472cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    virtual SkFontIdentity matchNameStyle(const char familyName[], const SkFontStyle&) const = 0;
12572cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
12672cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    /**
12772cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  Use the system fall-back to find a font for the given character.
12872cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  If no font can be found for the character, the return value's data id
12972cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  will be kInvalidDataId.
13072cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  If the name is NULL, the match will start against any default fonts.
13172cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  If the bpc47 is NULL, a default locale will be assumed.
13272cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *
13372cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  Note that bpc47 is a combination of ISO 639, 15924, and 3166-1 codes,
13472cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  so it is fine to just pass a ISO 639 here.
13572cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     */
136b4f3d69b8e18128598b8f62dd6fc7ae0e761cd57bungeman#ifdef SK_FM_NEW_MATCH_FAMILY_STYLE_CHARACTER
13772cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    virtual SkFontIdentity matchNameStyleCharacter(const char familyName[], const SkFontStyle&,
138b4f3d69b8e18128598b8f62dd6fc7ae0e761cd57bungeman                                                   const char* bcp47[], int bcp47Count,
139b4f3d69b8e18128598b8f62dd6fc7ae0e761cd57bungeman                                                   SkUnichar character) const=0;
140b4f3d69b8e18128598b8f62dd6fc7ae0e761cd57bungeman#else
141b4f3d69b8e18128598b8f62dd6fc7ae0e761cd57bungeman    virtual SkFontIdentity matchNameStyleCharacter(const char familyName[], const SkFontStyle&,
142b4f3d69b8e18128598b8f62dd6fc7ae0e761cd57bungeman                                                   const char bcp47[], SkUnichar character) const=0;
143b4f3d69b8e18128598b8f62dd6fc7ae0e761cd57bungeman#endif
14472cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
14572cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    /**
14672cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  Returns the data for the given data id.
14772cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  Will return NULL if the data id is invalid.
14872cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  Note that this is a data id, not a font id.
14972cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *
15072cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  The caller must unref() the returned object.
15172cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     */
15272cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    virtual SkStreamAsset* getData(int dataId) const = 0;
15372cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
15472cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.comprivate:
15572cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    typedef SkRefCnt INHERITED;
15672cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com};
15772cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
15872cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com#endif
159