OpenClonk
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 ()=default
 
 ~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 {nullptr}
 
StdCopyStrBuf LastUsedName
 
int32_t LastUsedGrpID {0}
 

Friends

class CStdFont
 

Detailed Description

Definition at line 46 of file C4FontLoader.h.

Member Enumeration Documentation

◆ FontType

Enumerator
C4FT_Log 
C4FT_MainSmall 
C4FT_Main 
C4FT_Caption 
C4FT_Title 

Definition at line 50 of file C4FontLoader.h.

Constructor & Destructor Documentation

◆ C4FontLoader()

C4FontLoader::C4FontLoader ( )
default

◆ ~C4FontLoader()

C4FontLoader::~C4FontLoader ( )
inline

Definition at line 53 of file C4FontLoader.h.

53 { Clear(); } // dtor

References Clear().

Here is the call graph for this function:

Member Function Documentation

◆ Clear()

void C4FontLoader::Clear ( )

Definition at line 172 of file C4FontLoader.cpp.

173 {
174 #ifndef USE_CONSOLE
175  // delete vector font cache
177  pLastUsedFont = nullptr;
178 #endif
179 }
void DestroyFont(CStdVectorFont *pFont)
CStdVectorFont * pLastUsedFont
Definition: C4FontLoader.h:62

References DestroyFont(), and pLastUsedFont.

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CreateFont() [1/2]

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

Definition at line 259 of file C4FontLoader.cpp.

260 {
261  return new CStdVectorFont(szFaceName);
262 }

◆ CreateFont() [2/2]

CStdVectorFont * C4FontLoader::CreateFont ( StdBuf Data)
protected

Definition at line 263 of file C4FontLoader.cpp.

264 {
265  return new CStdVectorFont(Data);
266 }

Referenced by InitFont().

Here is the caller graph for this function:

◆ DestroyFont()

void C4FontLoader::DestroyFont ( CStdVectorFont pFont)
protected

Definition at line 267 of file C4FontLoader.cpp.

268 {
269  if (!pFont) return;
270  --(pFont->RefCnt);
271  if (!pFont->RefCnt)
272  delete pFont;
273 }

References CStdVectorFont::RefCnt.

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

Here is the caller graph for this function:

◆ InitFont()

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

Definition at line 40 of file C4FontLoader.cpp.

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

References _MAX_PATH_LEN, 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 C4GraphicsResource::InitFonts(), C4StartupGraphics::InitFonts(), and C4Application::SetGameFont().

Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ CStdFont

friend class CStdFont
friend

Definition at line 69 of file C4FontLoader.h.

Member Data Documentation

◆ LastUsedGrpID

int32_t C4FontLoader::LastUsedGrpID {0}
protected

Definition at line 64 of file C4FontLoader.h.

Referenced by InitFont().

◆ LastUsedName

StdCopyStrBuf C4FontLoader::LastUsedName
protected

Definition at line 63 of file C4FontLoader.h.

Referenced by InitFont().

◆ pLastUsedFont

CStdVectorFont* C4FontLoader::pLastUsedFont {nullptr}
protected

Definition at line 62 of file C4FontLoader.h.

Referenced by Clear(), and InitFont().


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