OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4FontLoader Class Reference

#include <C4FontLoader.h>

Collaboration diagram for C4FontLoader:
[legend]

Public Types

enum  FontType {
  C4FT_Log, C4FT_MainSmall, C4FT_Main, C4FT_Caption,
  C4FT_Title
}
 

Public Member Functions

 C4FontLoader ()
 
 ~C4FontLoader ()
 
void Clear ()
 
bool InitFont (CStdFont *Font, const char *szFontName, FontType eType, int32_t iSize, C4GroupSet *pGfxGroups, bool fDoShadow=true)
 

Protected Member Functions

CStdVectorFontCreateFont (StdBuf &Data)
 
CStdVectorFontCreateFont (const char *szFaceName)
 
void DestroyFont (CStdVectorFont *pFont)
 

Protected Attributes

CStdVectorFontpLastUsedFont
 
StdCopyStrBuf LastUsedName
 
int32_t LastUsedGrpID
 

Friends

class CStdFont
 

Detailed Description

Definition at line 49 of file C4FontLoader.h.

Member Enumeration Documentation

Constructor & Destructor Documentation

C4FontLoader::C4FontLoader ( )
inline

Definition at line 55 of file C4FontLoader.h.

57  : pLastUsedFont(nullptr), LastUsedGrpID(0)
58 #endif
59  { } // ctor
CStdVectorFont * pLastUsedFont
Definition: C4FontLoader.h:69
int32_t LastUsedGrpID
Definition: C4FontLoader.h:71
C4FontLoader::~C4FontLoader ( )
inline

Definition at line 60 of file C4FontLoader.h.

References Clear().

60 { Clear(); } // dtor

Here is the call graph for this function:

Member Function Documentation

void C4FontLoader::Clear ( )

Definition at line 178 of file C4FontLoader.cpp.

References DestroyFont(), and pLastUsedFont.

Referenced by C4Game::Clear(), and ~C4FontLoader().

179 {
180 #ifndef USE_CONSOLE
181  // delete vector font cache
183  pLastUsedFont = nullptr;
184 #endif
185 }
CStdVectorFont * pLastUsedFont
Definition: C4FontLoader.h:69
void DestroyFont(CStdVectorFont *pFont)

Here is the call graph for this function:

Here is the caller graph for this function:

CStdVectorFont * C4FontLoader::CreateFont ( StdBuf Data)
protected

Definition at line 269 of file C4FontLoader.cpp.

Referenced by InitFont().

270 {
271  return new CStdVectorFont(Data);
272 }

Here is the caller graph for this function:

CStdVectorFont * C4FontLoader::CreateFont ( const char *  szFaceName)
protected

Definition at line 265 of file C4FontLoader.cpp.

266 {
267  return new CStdVectorFont(szFaceName);
268 }
void C4FontLoader::DestroyFont ( CStdVectorFont pFont)
protected

Definition at line 273 of file C4FontLoader.cpp.

References CStdVectorFont::RefCnt.

Referenced by Clear(), CStdFont::Clear(), and InitFont().

274 {
275  if (!pFont) return;
276  --(pFont->RefCnt);
277  if (!pFont->RefCnt)
278  delete pFont;
279 }

Here is the caller graph for this function:

bool C4FontLoader::InitFont ( CStdFont Font,
const char *  szFontName,
FontType  eType,
int32_t  iSize,
C4GroupSet pGfxGroups,
bool  fDoShadow = true 
)

Definition at line 46 of file C4FontLoader.cpp.

References _MAX_PATH, C4FT_Caption, C4FT_Log, C4FT_Main, C4FT_MainSmall, C4FT_Title, C4MaxName, CStdFont::Clear(), CreateFont(), DestroyFont(), C4GroupSet::FindSuitableFile(), FormatString(), FW_NORMAL, CStdFont::Init(), CStdFont::IsInitialized(), iSize, CStdFont::IsSameAs(), LastUsedGrpID, LastUsedName, C4Group::LoadEntry(), LoadResStr(), LogF(), LogFatal(), pLastUsedFont, SCopySegment(), and SCopyUntil().

Referenced by C4StartupGraphics::InitFonts(), C4GraphicsResource::InitFonts(), and C4Application::SetGameFont().

47 {
48 #ifdef USE_CONSOLE
49  return true;
50 #else
51  // safety
52  assert(szFontName);
53  if (!szFontName || !*szFontName)
54  {
55  LogFatal(FormatString("%s (\"%s\")", LoadResStr("IDS_ERR_INITFONTS"), szFontName ? szFontName : "(null)").getData());
56  return false;
57  }
58  // if def has not been found, use the def as font name
59  // determine font def string
60  const char *szFontString = szFontName;
61  // font not assigned?
62  assert(*szFontString);
63  if (!*szFontString)
64  {
65  // invalid call or spec
66  LogFatal(LoadResStr("IDS_ERR_INITFONTS")); return false;
67  }
68  // get font name
69  char FontFaceName[C4MaxName+1], FontParam[C4MaxName+1];
70  SCopyUntil(szFontString, FontFaceName, ',', C4MaxName);
71  // is it an image file?
72  int32_t iDefFontSize; DWORD dwDefWeight=FW_NORMAL;
73  switch (eType)
74  {
75  case C4FT_Log: iDefFontSize = iSize*12/14; break;
76  case C4FT_MainSmall:iDefFontSize = iSize*13/14; break;
77  case C4FT_Main: iDefFontSize = iSize; break;
78  case C4FT_Caption: iDefFontSize = iSize*16/14; break;
79  case C4FT_Title: iDefFontSize = iSize*22/14; break;
80  default: assert(false); LogFatal(LoadResStr("IDS_ERR_INITFONTS")); return false; // invalid call
81  }
82  // regular font name: let WinGDI or Freetype draw a font with the given parameters
83  // font size given?
84  if (SCopySegment(szFontString, 1, FontParam, ',', C4MaxName))
85  sscanf(FontParam, "%i", &iDefFontSize);
86  // font weight given?
87  if (SCopySegment(szFontString, 2, FontParam, ',', C4MaxName))
88  {
89  int iDefWeight;
90  sscanf(FontParam, "%i", &iDefWeight);
91  dwDefWeight = iDefWeight;
92  }
93  // check if it's already loaded from that group with that parameters
94  if (rFont->IsSameAs(FontFaceName, iDefFontSize, dwDefWeight))
95  return true;
96  // it's not; so (re-)load it now!
97  if (rFont->IsInitialized())
98  {
99  // reloading
100  rFont->Clear();
101  LogF(LoadResStr("IDS_PRC_UPDATEFONT"), FontFaceName, iDefFontSize, dwDefWeight);
102  }
103  // check if one of the internally listed fonts should be used
104  const char * const extensions[] = { "ttf", "otf", "ttc", "fon", "fnt", "fot", nullptr };
105  char FileName[_MAX_PATH+1];
106  int32_t ID;
107  C4Group * pGrp = pGfxGroups->FindSuitableFile(FontFaceName, extensions, FileName, &ID);
108  if (pGrp)
109  {
110  if (LastUsedGrpID != ID || LastUsedName != FontFaceName)
111  {
113  pLastUsedFont = nullptr;
114  }
115  if (!pLastUsedFont)
116  {
117  StdBuf Data;
118  if (pGrp->LoadEntry(FileName, &Data))
119  {
120  try
121  {
122  pLastUsedFont = CreateFont(Data);
123  LastUsedGrpID = ID;
124  LastUsedName = FontFaceName;
125  }
126  catch (std::runtime_error & e)
127  {
128  LogFatal(e.what());
129  pGrp = nullptr;
130  }
131  }
132  }
133  }
134  // no internal font match? Then create one using the given face/filename (using a system font)
135  if (!pGrp)
136  {
137  if (LastUsedGrpID != -1 || LastUsedName != FontFaceName)
138  {
140  pLastUsedFont = nullptr;
141  }
142  if (!pLastUsedFont)
143  {
144  try
145  {
146  pLastUsedFont = CreateFont(FontFaceName);
147  if (!pLastUsedFont)
148  // no match for font face found
149  throw std::runtime_error(FormatString("Font face %s undefined", FontFaceName).getData());
150  LastUsedGrpID = -1;
151  LastUsedName = FontFaceName;
152  }
153  catch (std::runtime_error & e)
154  {
155  LogFatal(e.what());
156  }
157  }
158  }
159  if (!pLastUsedFont)
160  {
161  LogFatal(LoadResStr("IDS_ERR_INITFONTS"));
162  return false;
163  }
164  try
165  {
166  rFont->Init(*pLastUsedFont, FontFaceName, iDefFontSize, dwDefWeight, fDoShadow); // throws exception on error
167  return true;
168  }
169  catch (std::runtime_error & e)
170  {
171  LogFatal(e.what());
172  LogFatal(LoadResStr("IDS_ERR_INITFONTS"));
173  return false;
174  }
175 #endif
176 }
Definition: StdBuf.h:37
CStdVectorFont * pLastUsedFont
Definition: C4FontLoader.h:69
bool SCopySegment(const char *szString, int iSegment, char *sTarget, char cSeparator, int iMaxL, bool fSkipWhitespace)
Definition: Standard.cpp:243
C4Group * FindSuitableFile(const char *szName, const char *const extensions[], char *szFileName, int32_t *pID=nullptr)
Definition: C4GroupSet.cpp:193
bool LoadEntry(const char *szEntryName, char **lpbpBuf, size_t *ipSize=nullptr, int iAppendZeros=0)
Definition: C4Group.cpp:1893
StdCopyStrBuf LastUsedName
Definition: C4FontLoader.h:70
#define _MAX_PATH
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
const unsigned int C4MaxName
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:230
CStdVectorFont * CreateFont(StdBuf &Data)
int32_t LastUsedGrpID
Definition: C4FontLoader.h:71
void DestroyFont(CStdVectorFont *pFont)
#define FW_NORMAL
Definition: C4FontLoader.h:39
uint32_t DWORD
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:253
void SCopyUntil(const char *szSource, char *sTarget, char cUntil, int iMaxL, int iIndex)
Definition: Standard.cpp:138
int iSize
Definition: TstC4NetIO.cpp:35
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:277

Here is the call graph for this function:

Here is the caller graph for this function:

Friends And Related Function Documentation

friend class CStdFont
friend

Definition at line 76 of file C4FontLoader.h.

Member Data Documentation

int32_t C4FontLoader::LastUsedGrpID
protected

Definition at line 71 of file C4FontLoader.h.

Referenced by InitFont().

StdCopyStrBuf C4FontLoader::LastUsedName
protected

Definition at line 70 of file C4FontLoader.h.

Referenced by InitFont().

CStdVectorFont* C4FontLoader::pLastUsedFont
protected

Definition at line 69 of file C4FontLoader.h.

Referenced by Clear(), and InitFont().


The documentation for this class was generated from the following files: