OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4FontLoader.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2003-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2010-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // text drawing facility for C4Draw
17 
18 #ifndef INC_STDFONT
19 #define INC_STDFONT
20 
22 #include "lib/C4Markup.h"
23 #include "graphics/C4Facet.h"
24 #include "graphics/C4Surface.h"
26 
27 // Font rendering flags
28 #define STDFONT_CENTERED 0x0001
29 #define STDFONT_TWOSIDED 0x0002
30 #define STDFONT_FILTERED 0x0004
31 #define STDFONT_RIGHTALGN 0x0008
32 #define STDFONT_SHADOW 0x0010
33 #define STDFONT_NOMARKUP 0x0020
34 
35 #ifndef FW_NORMAL
36 #define FW_NORMAL 400
37 #define FW_MEDIUM 500
38 #define FW_SEMIBOLD 600
39 #define FW_BOLD 700
40 #endif
41 
42 class C4Markup;
43 class CStdVectorFont;
44 
45 // font loader
47 {
48 public:
49  // enum of different fonts used in the clonk engine
51 
52  C4FontLoader() = default; // ctor
53  ~C4FontLoader() { Clear(); } // dtor
54 
55  void Clear(); // clear loaded fonts
56  // init a font class of the given type
57  // iSize is always the size of the normal font, which is adjusted for larger (title) and smaller (log) font types
58  bool InitFont(CStdFont * Font, const char *szFontName, FontType eType, int32_t iSize, C4GroupSet *pGfxGroups, bool fDoShadow=true);
59 
60 protected:
61 #ifndef USE_CONSOLE
62  CStdVectorFont * pLastUsedFont{nullptr}; // cache
64  int32_t LastUsedGrpID{0};
65 
67  CStdVectorFont * CreateFont(const char *szFaceName);
68  void DestroyFont(CStdVectorFont * pFont);
69  friend class CStdFont;
70 #endif
71 };
72 
74 
75 class CStdFont
76 {
77 public:
79  int id; // used by the engine to keep track of where the font came from
80 
81 protected:
82 #ifndef USE_CONSOLE
83  DWORD dwDefFontHeight; // configured font size (in points)
84  char szFontName[80+1]; // used font name (or surface file name)
85 
86  std::vector<std::unique_ptr<C4Surface>> psfcFontData; // font resource surfaces - additional surfaces created as needed
87  int iSfcSizes; // size for font surfaces
88  int iFontZoom; // zoom of font in texture
89 
90  C4Surface *sfcCurrent; // current surface font data can be written to at runtime
91  int32_t iCurrentSfcX, iCurrentSfcY; // current character rendering position
92 
93  int iHSpace; // horizontal space to be added betwen two characters
94  int iGfxLineHgt; // height of chaacters; may be larger than line height
95  DWORD dwWeight; // font weight (usually FW_NORMAL or FW_BOLD)
96  bool fDoShadow; // if the font is shadowed
97 
98  C4Facet fctAsciiTexCoords[256-' ']; // texture coordinates of ASCII letters
99  std::map<uint32_t, C4Facet> fctUnicodeMap; // texture coordinates of Unicode letters
100 
101  CustomImages *pCustomImages; // callback class for custom images
102 
103  CStdVectorFont *pVectorFont; // class assumed to be held externally!
104 
105  bool AddSurface();
106  bool CheckRenderedCharSpace(uint32_t iCharWdt, uint32_t iCharHgt);
107  bool AddRenderedChar(uint32_t dwChar, C4Facet *pfctTarget);
108 
110  {
111  if (c<128) return fctAsciiTexCoords[c-' ']; else return GetUnicodeCharacterFacet(c);
112  }
113  C4Facet &GetUnicodeCharacterFacet(uint32_t c);
114 
115  int iLineHgt; // height of one line of font (in pixels)
116 #endif
117 
118 public:
119  // draw ine line of text
120  void DrawText(C4Surface * sfcDest, float iX, float iY, DWORD dwColor, const char *szText, DWORD dwFlags, C4Markup &Markup, float fZoom);
121 
122  // get text size
123  bool GetTextExtent(const char *szText, int32_t &rsx, int32_t &rsy, bool fCheckMarkup = true);
124  // get height of a line
125  inline int GetLineHeight() const
126  {
127 #ifdef USE_CONSOLE
128  return 1;
129 #else
130  return iLineHgt;
131 #endif
132  }
133  // get height of the font in pixels (without line spacing)
134  inline int GetFontHeight() const
135  {
136  // Currently, we do not use spacing between lines - if someone implements that, this needs to be adjusted.
137  return GetLineHeight();
138  }
139  // Sometimes, only the width of a text is needed
140  int32_t GetTextWidth(const char *szText, bool fCheckMarkup = true) { int32_t x, y; GetTextExtent(szText, x, y, fCheckMarkup); return x; }
141  // insert line breaks into a message and return overall height - uses and regards '|' as line breaks
142  std::tuple<std::string, int> BreakMessage(const char *szMsg, int iWdt, bool fCheckMarkup, float fZoom=1.0f);
143  int BreakMessage(const char *szMsg, int iWdt, char *szOut, int iMaxOutLen, bool fCheckMarkup, float fZoom=1.0f);
144  int BreakMessage(const char *szMsg, int iWdt, StdStrBuf *pOut, bool fCheckMarkup, float fZoom=1.0f);
145  // get message break and pos after message break - does not regard any manual line breaks!
146  int GetMessageBreak(const char *szMsg, const char **ppNewPos, int iBreakWidth, float fZoom=1.0f);
147 
148  // ctor
149  CStdFont();
150  ~CStdFont() { Clear(); }
151 
152  // function throws std::runtime_error in case of failure
153  // font initialization from vector font
154  void Init(CStdVectorFont & VectorFont, const char *font_face_name, DWORD dwHeight, DWORD dwFontWeight=FW_NORMAL, bool fDoShadow=true);
155 
156  void Clear(); // clear font
157 
158  // query whether font is initialized
159  bool IsInitialized() const {
160 #ifdef USE_CONSOLE
161  return true;
162 #else
163  return !!*szFontName;
164 #endif
165  }
166 
167  // query whether font is already initialized with certain data
168  bool IsSameAsID(const char *szCFontName, int iCID, int iCIndent) const
169  {
170 #ifdef USE_CONSOLE
171  return true;
172 #else
173  return SEqual(szCFontName, szFontName) && iCID==id && iCIndent==-iHSpace;
174 #endif
175  }
176  bool IsSameAs(const char *szCFontName, DWORD iCHeight, DWORD dwCWeight) const
177  {
178 #ifdef USE_CONSOLE
179  return true;
180 #else
181  return SEqual(szCFontName, szFontName) && !id && iCHeight==dwDefFontHeight && dwCWeight==dwWeight;
182 #endif
183  }
184 
185  // set custom image request handler
186  void SetCustomImages(CustomImages *pHandler)
187  {
188 #ifndef USE_CONSOLE
189  pCustomImages = pHandler;
190 #endif
191  }
192 
193  bool GetFontImageSize(const char* szTag, int& width, int& height) const;
194 };
195 
196 #endif // INC_STDFONT
int GetLineHeight() const
Definition: C4FontLoader.h:125
Definition: StdBuf.h:29
std::map< uint32_t, C4Facet > fctUnicodeMap
Definition: C4FontLoader.h:99
bool IsSameAs(const char *szCFontName, DWORD iCHeight, DWORD dwCWeight) const
Definition: C4FontLoader.h:176
CStdVectorFont * pLastUsedFont
Definition: C4FontLoader.h:62
bool CheckRenderedCharSpace(uint32_t iCharWdt, uint32_t iCharHgt)
C4FontLoader FontLoader
int GetMessageBreak(const char *szMsg, const char **ppNewPos, int iBreakWidth, float fZoom=1.0f)
bool AddSurface()
DWORD dwWeight
Definition: C4FontLoader.h:95
bool GetTextExtent(const char *szText, int32_t &rsx, int32_t &rsy, bool fCheckMarkup=true)
StdCopyStrBuf LastUsedName
Definition: C4FontLoader.h:63
int32_t GetTextWidth(const char *szText, bool fCheckMarkup=true)
Definition: C4FontLoader.h:140
int iFontZoom
Definition: C4FontLoader.h:88
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:93
int iLineHgt
Definition: C4FontLoader.h:115
int32_t iCurrentSfcX
Definition: C4FontLoader.h:91
int32_t iCurrentSfcY
Definition: C4FontLoader.h:91
void SetCustomImages(CustomImages *pHandler)
Definition: C4FontLoader.h:186
std::vector< std::unique_ptr< C4Surface > > psfcFontData
Definition: C4FontLoader.h:86
C4Facet & GetUnicodeCharacterFacet(uint32_t c)
C4FontLoader()=default
bool fDoShadow
Definition: C4FontLoader.h:96
int iGfxLineHgt
Definition: C4FontLoader.h:94
CStdFontCustomImages CustomImages
Definition: C4FontLoader.h:78
C4Facet fctAsciiTexCoords[256-' ']
Definition: C4FontLoader.h:98
bool GetFontImageSize(const char *szTag, int &width, int &height) const
bool IsSameAsID(const char *szCFontName, int iCID, int iCIndent) const
Definition: C4FontLoader.h:168
C4Surface * sfcCurrent
Definition: C4FontLoader.h:90
bool AddRenderedChar(uint32_t dwChar, C4Facet *pfctTarget)
void Clear()
CStdVectorFont * CreateFont(StdBuf &Data)
std::tuple< std::string, int > BreakMessage(const char *szMsg, int iWdt, bool fCheckMarkup, float fZoom=1.0f)
int GetFontHeight() const
Definition: C4FontLoader.h:134
void DrawText(C4Surface *sfcDest, float iX, float iY, DWORD dwColor, const char *szText, DWORD dwFlags, C4Markup &Markup, float fZoom)
DWORD dwDefFontHeight
Definition: C4FontLoader.h:83
int32_t LastUsedGrpID
Definition: C4FontLoader.h:64
void DestroyFont(CStdVectorFont *pFont)
void Init(CStdVectorFont &VectorFont, const char *font_face_name, DWORD dwHeight, DWORD dwFontWeight=FW_NORMAL, bool fDoShadow=true)
char szFontName[80+1]
Definition: C4FontLoader.h:84
CStdVectorFont * pVectorFont
Definition: C4FontLoader.h:103
#define FW_NORMAL
Definition: C4FontLoader.h:36
bool IsInitialized() const
Definition: C4FontLoader.h:159
uint32_t DWORD
C4Facet & GetCharacterFacet(uint32_t c)
Definition: C4FontLoader.h:109
int iHSpace
Definition: C4FontLoader.h:93
CustomImages * pCustomImages
Definition: C4FontLoader.h:101
int iSfcSizes
Definition: C4FontLoader.h:87
bool InitFont(CStdFont *Font, const char *szFontName, FontType eType, int32_t iSize, C4GroupSet *pGfxGroups, bool fDoShadow=true)
int iSize
Definition: TstC4NetIO.cpp:32