1 /*---------------------------------------------------------------------------*
2 Project: NintendoWare
3 File: lyt_FontContainer.cpp
4
5 Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 $Revision: 13839 $
14 *---------------------------------------------------------------------------*/
15
16 #include "precompiled.h"
17 #include <nw/lyt/lyt_FontContainer.h>
18 #include <nw/lyt/lyt_Layout.h>
19
20 #ifdef NW_PLATFORM_CTR
21 #define stricmp std::strcasecmp
22 #endif
23
24 namespace nw
25 {
26 namespace lyt
27 {
28
FontRefLink()29 FontRefLink::FontRefLink()
30 : m_pFont(0),
31 m_Own(false)
32 {
33 }
34
~FontRefLink()35 FontRefLink::~FontRefLink()
36 {
37 if (m_Own)
38 {
39 // 所有するフォントはライブラリで生成されたもの。
40 // font::ResFontのインスタンスでなければならない。
41
42 font::ResFont* pResFont = (font::ResFont*)(m_pFont);
43 if (pResFont != NULL)
44 {
45 void* drawBuffer = pResFont->GetDrawBuffer();
46 if (drawBuffer != NULL)
47 {
48 pResFont->SetDrawBuffer(NULL);
49 Layout::FreeMemory(drawBuffer);
50 }
51 }
52 Layout::DeleteObj(m_pFont);
53 m_pFont = NULL;
54 }
55 }
56
57 void
Set(const char * name,font::Font * pFont,bool own)58 FontRefLink::Set(
59 const char* name,
60 font::Font* pFont,
61 bool own
62 )
63 {
64 ut::strcpy(m_FontName, sizeof(m_FontName), name);
65 m_pFont = pFont;
66 m_Own = own;
67 }
68
~FontContainer()69 FontContainer::~FontContainer()
70 {
71 this->Finalize();
72 }
73
74 void
Finalize()75 FontContainer::Finalize()
76 {
77 while (!this->empty())
78 {
79 FontRefLink* pLink = &this->front();
80 this->erase(pLink);
81 Layout::DeleteObj(pLink);
82 }
83 }
84
85 nw::font::Font*
FindFontByName(const char * name)86 FontContainer::FindFontByName(
87 const char* name
88 )
89 {
90 for (Iterator it = this->GetBeginIter(); it != this->GetEndIter(); ++it)
91 {
92 if (0 == std::strcmp(name, it->GetFontName()))
93 {
94 return it->GetFont();
95 }
96 }
97
98 return NULL;
99 }
100
101 FontKey
RegistFont(const char * name,font::Font * pFont,bool own)102 FontContainer::RegistFont(const char* name, font::Font* pFont, bool own)
103 {
104 FontRefLink* pLink = Layout::NewObj<FontRefLink>();
105
106 if (pLink == NULL)
107 {
108 return NULL;
109 }
110
111 pLink->Set(name, pFont, own);
112
113 this->push_back(pLink);
114
115 return reinterpret_cast<FontKey>(pLink);
116 }
117
118 void
UnregistFont(FontKey key)119 FontContainer::UnregistFont(FontKey key)
120 {
121 NW_NULL_ASSERT(key);
122
123 FontRefLink* pLink = (FontRefLink*)(key);
124 this->erase(pLink);
125 Layout::DeleteObj(pLink);
126 }
127
128 } // namespace lyt
129 } // namespace nw
130