Magic_Game/src/kiwano/render/Font.cpp

167 lines
4.5 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.
2020-01-17 16:55:47 +08:00
#include <kiwano/render/Font.h>
#include <kiwano/render/Renderer.h>
2020-08-04 02:19:23 +08:00
#include <functional> // std::hash
2020-08-04 03:01:08 +08:00
#include <cctype> // std::tolower
2019-04-11 14:40:54 +08:00
namespace kiwano
{
FontPtr Font::Preload(const String& file)
2020-01-21 10:09:55 +08:00
{
2020-08-04 02:19:23 +08:00
size_t hash_code = std::hash<String>{}(file);
if (FontPtr ptr = FontCache::GetInstance().GetFont(hash_code))
2020-01-21 10:09:55 +08:00
{
2020-08-04 02:19:23 +08:00
return ptr;
2020-01-21 10:09:55 +08:00
}
2020-08-04 02:19:23 +08:00
FontPtr ptr = MakePtr<Font>();
if (ptr)
2020-01-21 10:09:55 +08:00
{
2020-08-04 02:19:23 +08:00
Vector<String> family_names;
Renderer::GetInstance().CreateFontCollection(*ptr, family_names, file);
if (ptr->IsValid())
{
FontCache::GetInstance().AddFont(hash_code, ptr);
if (!family_names.empty())
{
ptr->SetFamilyName(family_names[0]);
}
for (const auto& name : family_names)
{
FontCache::GetInstance().AddFontByFamily(name, ptr);
}
}
2020-01-21 10:09:55 +08:00
}
return ptr;
2020-01-21 10:09:55 +08:00
}
2019-08-18 10:23:54 +08:00
FontPtr Font::Preload(const Resource& resource)
2020-01-21 10:09:55 +08:00
{
2020-08-04 02:19:23 +08:00
size_t hash_code = resource.GetId();
if (FontPtr ptr = FontCache::GetInstance().GetFont(hash_code))
2020-01-21 10:09:55 +08:00
{
2020-08-04 02:19:23 +08:00
return ptr;
2020-01-21 10:09:55 +08:00
}
2020-08-04 02:19:23 +08:00
FontPtr ptr = MakePtr<Font>();
if (ptr)
2020-01-21 10:09:55 +08:00
{
2020-08-04 02:19:23 +08:00
Vector<String> family_names;
Renderer::GetInstance().CreateFontCollection(*ptr, family_names, resource);
if (ptr->IsValid())
{
FontCache::GetInstance().AddFont(hash_code, ptr);
if (!family_names.empty())
{
ptr->SetFamilyName(family_names[0]);
}
for (const auto& name : family_names)
{
FontCache::GetInstance().AddFontByFamily(name, ptr);
}
}
2020-01-21 10:09:55 +08:00
}
return ptr;
}
Font::Font() {}
Font::Font(const String& family_name)
{
2020-08-04 02:19:23 +08:00
Load(family_name);
}
bool Font::Load(const String& family_name)
{
if (family_name.empty())
return true;
if (FontPtr font = FontCache::GetInstance().GetFontByFamily(family_name))
{
ResetNativePointer(font->GetNativePointer());
SetFamilyName(family_name);
return true;
}
Fail(strings::Format("Font::Load failed: cannot find family name \"%s\"", family_name.c_str()));
return false;
}
FontCache::FontCache() {}
FontCache::~FontCache() {}
void FontCache::AddFont(size_t key, FontPtr font)
{
font_cache_.insert(std::make_pair(key, font));
}
void FontCache::AddFontByFamily(const String& font_family, FontPtr font)
{
String family = TransformFamily(font_family);
font_family_cache_.insert(std::make_pair(family, font));
}
FontPtr FontCache::GetFont(size_t key) const
{
if (font_cache_.count(key))
{
return font_cache_.at(key);
}
return FontPtr();
}
FontPtr FontCache::GetFontByFamily(const String& font_family) const
{
String family = TransformFamily(font_family);
if (font_family_cache_.count(family))
{
return font_family_cache_.at(family);
}
return FontPtr();
}
void FontCache::RemoveFont(size_t key)
{
font_cache_.erase(key);
}
void FontCache::RemoveFontByFamily(const String& font_family)
{
String family = TransformFamily(font_family);
font_family_cache_.erase(family);
}
void FontCache::Clear()
{
font_cache_.clear();
font_family_cache_.clear();
}
String FontCache::TransformFamily(String family) const
{
2020-08-04 03:01:08 +08:00
std::transform(family.begin(), family.end(), family.begin(), [](unsigned char c) { return std::tolower(c); });
2020-08-04 02:19:23 +08:00
return family;
}
2020-01-21 10:09:55 +08:00
} // namespace kiwano