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