1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "Text/BsFont.h"
4#include "Private/RTTI/BsFontRTTI.h"
5#include "Resources/BsResources.h"
6
7namespace bs
8{
9 const CharDesc& FontBitmap::getCharDesc(UINT32 charId) const
10 {
11 auto iterFind = characters.find(charId);
12 if(iterFind != characters.end())
13 {
14 return characters.at(charId);
15 }
16
17 return missingGlyph;
18 }
19
20 RTTITypeBase* FontBitmap::getRTTIStatic()
21 {
22 return FontBitmapRTTI::instance();
23 }
24
25 RTTITypeBase* FontBitmap::getRTTI() const
26 {
27 return FontBitmap::getRTTIStatic();
28 }
29
30 Font::Font()
31 :Resource(false)
32 { }
33
34 void Font::initialize(const Vector<SPtr<FontBitmap>>& fontData)
35 {
36 for(auto iter = fontData.begin(); iter != fontData.end(); ++iter)
37 {
38 mFontDataPerSize[(*iter)->size] = *iter;
39
40 for (auto& texture : (*iter)->texturePages)
41 {
42 if (texture != nullptr)
43 addResourceDependency(texture);
44 }
45 }
46
47 Resource::initialize();
48 }
49
50 SPtr<FontBitmap> Font::getBitmap(UINT32 size) const
51 {
52 auto iterFind = mFontDataPerSize.find(size);
53
54 if(iterFind == mFontDataPerSize.end())
55 return nullptr;
56
57 return iterFind->second;
58 }
59
60 INT32 Font::getClosestSize(UINT32 size) const
61 {
62 UINT32 minDiff = std::numeric_limits<UINT32>::max();
63 UINT32 bestSize = size;
64
65 for(auto iter = mFontDataPerSize.begin(); iter != mFontDataPerSize.end(); ++iter)
66 {
67 if(iter->first == size)
68 return size;
69 else if(iter->first > size)
70 {
71 UINT32 diff = iter->first - size;
72 if(diff < minDiff)
73 {
74 minDiff = diff;
75 bestSize = iter->first;
76 }
77 }
78 else
79 {
80 UINT32 diff = size - iter->first;
81 if(diff < minDiff)
82 {
83 minDiff = diff;
84 bestSize = iter->first;
85 }
86 }
87 }
88
89 return bestSize;
90 }
91
92 void Font::getCoreDependencies(Vector<CoreObject*>& dependencies)
93 {
94 for (auto& fontDataEntry : mFontDataPerSize)
95 {
96 for (auto& texture : fontDataEntry.second->texturePages)
97 {
98 if (texture.isLoaded())
99 dependencies.push_back(texture.get());
100 }
101 }
102 }
103
104 HFont Font::create(const Vector<SPtr<FontBitmap>>& fontData)
105 {
106 SPtr<Font> newFont = _createPtr(fontData);
107
108 return static_resource_cast<Font>(gResources()._createResourceHandle(newFont));
109 }
110
111 SPtr<Font> Font::_createPtr(const Vector<SPtr<FontBitmap>>& fontData)
112 {
113 SPtr<Font> newFont = bs_core_ptr<Font>(new (bs_alloc<Font>()) Font());
114 newFont->_setThisPtr(newFont);
115 newFont->initialize(fontData);
116
117 return newFont;
118 }
119
120 SPtr<Font> Font::_createEmpty()
121 {
122 SPtr<Font> newFont = bs_core_ptr<Font>(new (bs_alloc<Font>()) Font());
123 newFont->_setThisPtr(newFont);
124
125 return newFont;
126 }
127
128 RTTITypeBase* Font::getRTTIStatic()
129 {
130 return FontRTTI::instance();
131 }
132
133 RTTITypeBase* Font::getRTTI() const
134 {
135 return Font::getRTTIStatic();
136 }
137}