Magic_Game/src/kiwano/render/Font.h

233 lines
4.9 KiB
C
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
2020-01-21 10:09:55 +08:00
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
2020-02-16 20:14:01 +08:00
#include <kiwano/render/NativeObject.h>
2019-12-26 19:25:43 +08:00
#include <kiwano/core/Resource.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
KGE_DECLARE_SMART_PTR(Font);
2019-12-26 19:25:43 +08:00
2020-01-21 10:09:55 +08:00
class Renderer;
2020-01-21 10:09:55 +08:00
/**
* \addtogroup Render
* @{
*/
2019-12-31 16:01:41 +08:00
2020-08-04 16:31:20 +08:00
/**
* \~chinese
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϸֵ
*/
struct FontWeight
{
enum Value : uint32_t
{
Thin = 100U,
ExtraLight = 200U,
Light = 300U,
Normal = 400U, ///< <20><><EFBFBD><EFBFBD>
Medium = 500U,
2020-08-05 01:55:42 +08:00
Bold = 700U, ///< <20>Ӵ<EFBFBD>
2020-08-04 16:31:20 +08:00
ExtraBold = 800U,
Black = 900U,
ExtraBlack = 950U
};
};
/**
* \~chinese
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̬
*/
enum class FontPosture
{
2020-08-05 01:55:42 +08:00
Normal, ///< <20><><EFBFBD><EFBFBD>
Oblique, ///< <20><>б<EFBFBD><D0B1>
2020-08-04 16:31:20 +08:00
Italic, ///< б<><D0B1>
};
2020-08-05 01:55:42 +08:00
/**
* \~chinese
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
enum class FontStretch
{
Unknown,
UltraCondensed,
ExtraCondensed,
Condensed, ///< ѹ<><D1B9>
SemiCondensed,
Normal, ///< <20><><EFBFBD><EFBFBD>
SemiExpanded,
Expanded, ///< <20><><EFBFBD><EFBFBD>
ExtraExpanded,
UltraExpanded,
};
2020-01-21 10:09:55 +08:00
/**
* \~chinese
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2020-01-21 10:09:55 +08:00
*/
2020-02-16 20:14:01 +08:00
class Font : public NativeObject
2020-01-21 10:09:55 +08:00
{
public:
2020-02-06 16:54:47 +08:00
/// \~chinese
2020-08-04 02:19:23 +08:00
/// @brief Ԥ<><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @param file <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
static FontPtr Preload(const String& file);
2020-02-06 16:54:47 +08:00
/// \~chinese
2020-08-04 02:19:23 +08:00
/// @brief Ԥ<><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @param resource <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
static FontPtr Preload(const Resource& resource);
2020-02-06 16:54:47 +08:00
2020-08-04 16:31:20 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>ϵͳĬ<CDB3><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2020-01-21 10:09:55 +08:00
Font();
2019-12-26 19:25:43 +08:00
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-08-04 02:19:23 +08:00
/// @brief ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E5B4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @param family_name <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2020-08-04 16:31:20 +08:00
/// @param size <20>ֺ<EFBFBD>
/// @param weight <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϸ
/// @param posture <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̬
Font(const String& family_name, float size, uint32_t weight = FontWeight::Normal,
2020-08-05 01:55:42 +08:00
FontPosture posture = FontPosture::Normal, FontStretch stretch = FontStretch::Normal);
2020-08-04 02:19:23 +08:00
2020-08-03 19:54:29 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
String GetFamilyName() const;
2020-08-03 19:54:29 +08:00
2020-08-04 16:31:20 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD>ֺ<EFBFBD>
float GetSize() const;
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϸֵ
uint32_t GetWeight() const;
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̬
FontPosture GetPosture() const;
2020-08-05 01:55:42 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
FontStretch GetStretch() const;
2020-08-04 02:19:23 +08:00
protected:
2020-08-03 19:54:29 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void SetFamilyName(const String& name);
2020-08-03 19:54:29 +08:00
protected:
2020-08-04 16:31:20 +08:00
float size_;
uint32_t weight_;
FontPosture posture_;
2020-08-05 01:55:42 +08:00
FontStretch stretch_;
2020-08-04 16:31:20 +08:00
String family_name_;
2020-01-21 10:09:55 +08:00
};
2019-12-26 19:25:43 +08:00
2020-08-04 02:19:23 +08:00
/**
* \~chinese
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
class KGE_API FontCache final : public Singleton<FontCache>
{
friend Singleton<FontCache>;
public:
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E5BBBA>
void AddFont(size_t key, FontPtr font);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><D3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E5BBBA>
void AddFontByFamily(const String& font_family, FontPtr font);
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><E5BBBA>
FontPtr GetFont(size_t key) const;
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><D3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E5BBBA>
FontPtr GetFontByFamily(const String& font_family) const;
/// \~chinese
/// @brief <20>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD><E5BBBA>
void RemoveFont(size_t key);
/// \~chinese
/// @brief <20>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><D3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E5BBBA>
void RemoveFontByFamily(const String& font_family);
/// \~chinese
/// @brief <20><><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD>
void Clear();
~FontCache();
private:
FontCache();
String TransformFamily(String family) const;
private:
using FontMap = UnorderedMap<size_t, FontPtr>;
FontMap font_cache_;
using FontFamilyMap = UnorderedMap<String, FontPtr>;
FontFamilyMap font_family_cache_;
};
2020-01-21 10:09:55 +08:00
/** @} */
2019-12-31 16:01:41 +08:00
inline String Font::GetFamilyName() const
2020-08-03 19:54:29 +08:00
{
return family_name_;
2020-08-03 19:54:29 +08:00
}
2020-08-04 16:31:20 +08:00
inline float Font::GetSize() const
{
return size_;
}
inline uint32_t Font::GetWeight() const
{
return weight_;
}
inline FontPosture Font::GetPosture() const
{
return posture_;
}
2020-08-05 01:55:42 +08:00
inline FontStretch Font::GetStretch() const
{
return stretch_;
}
inline void Font::SetFamilyName(const String& name)
2020-08-03 19:54:29 +08:00
{
family_name_ = name;
2020-08-03 19:54:29 +08:00
}
2020-01-21 10:09:55 +08:00
} // namespace kiwano