Magic_Game/src/kiwano/render/Font.h

233 lines
4.9 KiB
C++

// Copyright (c) 2016-2018 Kiwano - Nomango
//
// 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:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// 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
#include <kiwano/render/NativeObject.h>
#include <kiwano/core/Resource.h>
namespace kiwano
{
KGE_DECLARE_SMART_PTR(Font);
class Renderer;
/**
* \addtogroup Render
* @{
*/
/**
* \~chinese
* @brief ×ÖÌå´Öϸֵ
*/
struct FontWeight
{
enum Value : uint32_t
{
Thin = 100U,
ExtraLight = 200U,
Light = 300U,
Normal = 400U, ///< Õý³£
Medium = 500U,
Bold = 700U, ///< ¼Ó´Ö
ExtraBold = 800U,
Black = 900U,
ExtraBlack = 950U
};
};
/**
* \~chinese
* @brief ×ÖÌåÐÎ̬
*/
enum class FontPosture
{
Normal, ///< Õý³£
Oblique, ///< ÇãбÌå
Italic, ///< бÌå
};
/**
* \~chinese
* @brief ×ÖÌåÀ­Éì
*/
enum class FontStretch
{
Unknown,
UltraCondensed,
ExtraCondensed,
Condensed, ///< ѹËõ
SemiCondensed,
Normal, ///< Õý³£
SemiExpanded,
Expanded, ///< À©´ó
ExtraExpanded,
UltraExpanded,
};
/**
* \~chinese
* @brief ×ÖÌå
*/
class Font : public NativeObject
{
public:
/// \~chinese
/// @brief Ô¤¼ÓÔØ×ÖÌå
/// @param file ×ÖÌåÎļþ
static FontPtr Preload(const String& file);
/// \~chinese
/// @brief Ô¤¼ÓÔØ×ÖÌå
/// @param resource ×ÖÌå×ÊÔ´
static FontPtr Preload(const Resource& resource);
/// \~chinese
/// @brief ´´½¨ÏµÍ³Ä¬ÈÏ×ÖÌå
Font();
/// \~chinese
/// @brief ͨ¹ý×ÖÌå×å´´½¨×ÖÌå
/// @param family_name ×ÖÌå×å
/// @param size ×ÖºÅ
/// @param weight ×ÖÌå´Öϸ
/// @param posture ×ÖÌåÐÎ̬
Font(const String& family_name, float size, uint32_t weight = FontWeight::Normal,
FontPosture posture = FontPosture::Normal, FontStretch stretch = FontStretch::Normal);
/// \~chinese
/// @brief »ñÈ¡×ÖÌå×å
String GetFamilyName() const;
/// \~chinese
/// @brief »ñÈ¡×ÖºÅ
float GetSize() const;
/// \~chinese
/// @brief »ñÈ¡×ÖÌå´Öϸֵ
uint32_t GetWeight() const;
/// \~chinese
/// @brief »ñÈ¡×ÖÌåÐÎ̬
FontPosture GetPosture() const;
/// \~chinese
/// @brief »ñÈ¡×ÖÌåÀ­Éì
FontStretch GetStretch() const;
protected:
/// \~chinese
/// @brief »ñÈ¡×ÖÌå×å
void SetFamilyName(const String& name);
protected:
float size_;
uint32_t weight_;
FontPosture posture_;
FontStretch stretch_;
String family_name_;
};
/**
* \~chinese
* @brief ÎÆÀí»º´æ
*/
class KGE_API FontCache final : public Singleton<FontCache>
{
friend Singleton<FontCache>;
public:
/// \~chinese
/// @brief Ìí¼Ó×ÖÌ建´æ
void AddFont(size_t key, FontPtr font);
/// \~chinese
/// @brief Ìí¼Ó×ÖÌå×åÓ³Éä×ÖÌ建´æ
void AddFontByFamily(const String& font_family, FontPtr font);
/// \~chinese
/// @brief »ñÈ¡×ÖÌ建´æ
FontPtr GetFont(size_t key) const;
/// \~chinese
/// @brief »ñÈ¡×ÖÌå×åÓ³Éä×ÖÌ建´æ
FontPtr GetFontByFamily(const String& font_family) const;
/// \~chinese
/// @brief ÒÆ³ý×ÖÌ建´æ
void RemoveFont(size_t key);
/// \~chinese
/// @brief ÒÆ³ý×ÖÌå×åÓ³Éä×ÖÌ建´æ
void RemoveFontByFamily(const String& font_family);
/// \~chinese
/// @brief Çå¿Õ»º´æ
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_;
};
/** @} */
inline String Font::GetFamilyName() const
{
return family_name_;
}
inline float Font::GetSize() const
{
return size_;
}
inline uint32_t Font::GetWeight() const
{
return weight_;
}
inline FontPosture Font::GetPosture() const
{
return posture_;
}
inline FontStretch Font::GetStretch() const
{
return stretch_;
}
inline void Font::SetFamilyName(const String& name)
{
family_name_ = name;
}
} // namespace kiwano