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 ()=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

Constructor & Destructor Documentation

C4FontLoader::C4FontLoader ( )
default
C4FontLoader::~C4FontLoader ( )
inline

Definition at line 53 of file C4FontLoader.h.

References Clear().

53 { Clear(); } // dtor

Here is the call graph for this function:

Member Function Documentation

void C4FontLoader::Clear ( )

Definition at line 172 of file C4FontLoader.cpp.

References DestroyFont(), and pLastUsedFont.

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

173 {
174 #ifndef USE_CONSOLE
175  // delete vector font cache
177  pLastUsedFont = nullptr;
178 #endif
179 }
CStdVectorFont * pLastUsedFont
Definition: C4FontLoader.h:62
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 263 of file C4FontLoader.cpp.

Referenced by InitFont().

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

Here is the caller graph for this function:

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

Definition at line 259 of file C4FontLoader.cpp.

260 {
261  return new CStdVectorFont(szFaceName);
262 }
void C4FontLoader::DestroyFont ( CStdVectorFont pFont)
protected

Definition at line 267 of file C4FontLoader.cpp.

References CStdVectorFont::RefCnt.

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

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

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 40 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().

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+1];
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 }
Definition: StdBuf.h:29
CStdVectorFont * pLastUsedFont
Definition: C4FontLoader.h:62
bool SCopySegment(const char *szString, int iSegment, char *sTarget, char cSeparator, int iMaxL, bool fSkipWhitespace)
Definition: Standard.cpp:251
C4Group * FindSuitableFile(const char *szName, const char *const extensions[], char *szFileName, int32_t *pID=nullptr)
Definition: C4GroupSet.cpp:191
bool LoadEntry(const char *szEntryName, char **lpbpBuf, size_t *ipSize=nullptr, int iAppendZeros=0)
Definition: C4Group.cpp:1893
StdCopyStrBuf LastUsedName
Definition: C4FontLoader.h:63
#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:227
CStdVectorFont * CreateFont(StdBuf &Data)
int32_t LastUsedGrpID
Definition: C4FontLoader.h:64
void DestroyFont(CStdVectorFont *pFont)
#define FW_NORMAL
Definition: C4FontLoader.h:36
uint32_t DWORD
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:250
void SCopyUntil(const char *szSource, char *sTarget, char cUntil, int iMaxL, int iIndex)
Definition: Standard.cpp:146
int iSize
Definition: TstC4NetIO.cpp:32
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270

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 69 of file C4FontLoader.h.

Member Data Documentation

int32_t C4FontLoader::LastUsedGrpID {0}
protected

Definition at line 64 of file C4FontLoader.h.

Referenced by InitFont().

StdCopyStrBuf C4FontLoader::LastUsedName
protected

Definition at line 63 of file C4FontLoader.h.

Referenced by InitFont().

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: