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