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: 27719 $
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 nw::font::Font*
FindFontByKey(FontKey key)102 FontContainer::FindFontByKey(FontKey key)
103 {
104     NW_NULL_ASSERT(key);
105     FontRefLink* pLink = (FontRefLink*)(key);
106 
107     for (Iterator it = this->GetBeginIter(); it != this->GetEndIter(); ++it)
108     {
109         if (&(*it) == pLink)
110         {
111             return it->GetFont();
112         }
113     }
114 
115     return NULL;
116 }
117 
118 FontKey
RegistFont(const char * name,font::Font * pFont,bool own)119 FontContainer::RegistFont(const char* name, font::Font* pFont, bool own)
120 {
121     FontRefLink* pLink = Layout::NewObj<FontRefLink>();
122 
123     if (pLink == NULL)
124     {
125         return NULL;
126     }
127 
128     pLink->Set(name, pFont, own);
129 
130     this->push_back(pLink);
131 
132     return reinterpret_cast<FontKey>(pLink);
133 }
134 
135 void
UnregistFont(FontKey key)136 FontContainer::UnregistFont(FontKey key)
137 {
138     NW_NULL_ASSERT(key);
139 
140     FontRefLink* pLink = (FontRefLink*)(key);
141     this->erase(pLink);
142     Layout::DeleteObj(pLink);
143 }
144 
145 }   // namespace lyt
146 }   // namespace nw
147