|
1 /* |
|
2 SDL_FontCache v0.10.0: A font cache for SDL and SDL_ttf |
|
3 by Jonathan Dearborn |
|
4 (minor changes have been made by Migdyn for use in Devious Licks) |
|
5 Dedicated to the memory of Florian Hufsky |
|
6 |
|
7 License: |
|
8 The short: |
|
9 Use it however you'd like, but keep the copyright and license notice |
|
10 whenever these files or parts of them are distributed in uncompiled form. |
|
11 |
|
12 The long: |
|
13 Copyright (c) 2019 Jonathan Dearborn |
|
14 |
|
15 Permission is hereby granted, free of charge, to any person obtaining a copy |
|
16 of this software and associated documentation files (the "Software"), to deal |
|
17 in the Software without restriction, including without limitation the rights |
|
18 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
19 copies of the Software, and to permit persons to whom the Software is |
|
20 furnished to do so, subject to the following conditions: |
|
21 |
|
22 The above copyright notice and this permission notice shall be included in |
|
23 all copies or substantial portions of the Software. |
|
24 |
|
25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
26 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
27 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
28 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
29 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
30 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
31 THE SOFTWARE. |
|
32 */ |
|
33 |
|
34 #ifndef _SDL_FONTCACHE_H__ |
|
35 #define _SDL_FONTCACHE_H__ |
|
36 |
|
37 #include "SDL2/SDL.h" |
|
38 #include "SDL2/SDL_ttf.h" |
|
39 |
|
40 #ifdef FC_USE_SDL_GPU |
|
41 #include "SDL_gpu.h" |
|
42 #endif |
|
43 |
|
44 |
|
45 #include <stdarg.h> |
|
46 |
|
47 #ifdef __cplusplus |
|
48 extern "C" { |
|
49 #endif |
|
50 |
|
51 |
|
52 // Let's pretend this exists... |
|
53 #define TTF_STYLE_OUTLINE 16 |
|
54 |
|
55 |
|
56 |
|
57 // Differences between SDL_Renderer and SDL_gpu |
|
58 #ifdef FC_USE_SDL_GPU |
|
59 #define FC_Rect GPU_Rect |
|
60 #define FC_Target GPU_Target |
|
61 #define FC_Image GPU_Image |
|
62 #define FC_Log GPU_LogError |
|
63 #else |
|
64 #define FC_Rect SDL_Rect |
|
65 #define FC_Target SDL_Renderer |
|
66 #define FC_Image SDL_Texture |
|
67 #define FC_Log SDL_Log |
|
68 #endif |
|
69 |
|
70 |
|
71 // SDL_FontCache types |
|
72 |
|
73 typedef enum |
|
74 { |
|
75 FC_ALIGN_LEFT, |
|
76 FC_ALIGN_CENTER, |
|
77 FC_ALIGN_RIGHT |
|
78 } FC_AlignEnum; |
|
79 |
|
80 typedef enum |
|
81 { |
|
82 FC_FILTER_NEAREST, |
|
83 FC_FILTER_LINEAR |
|
84 } FC_FilterEnum; |
|
85 |
|
86 typedef struct FC_Scale |
|
87 { |
|
88 float x; |
|
89 float y; |
|
90 |
|
91 } FC_Scale; |
|
92 |
|
93 typedef struct FC_Effect |
|
94 { |
|
95 FC_AlignEnum alignment; |
|
96 FC_Scale scale; |
|
97 SDL_Color color; |
|
98 |
|
99 } FC_Effect; |
|
100 |
|
101 // Opaque type |
|
102 typedef struct FC_Font FC_Font; |
|
103 |
|
104 |
|
105 typedef struct FC_GlyphData |
|
106 { |
|
107 SDL_Rect rect; |
|
108 int cache_level; |
|
109 |
|
110 } FC_GlyphData; |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 // Object creation |
|
116 |
|
117 FC_Rect FC_MakeRect(float x, float y, float w, float h); |
|
118 |
|
119 FC_Scale FC_MakeScale(float x, float y); |
|
120 |
|
121 SDL_Color FC_MakeColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a); |
|
122 |
|
123 FC_Effect FC_MakeEffect(FC_AlignEnum alignment, FC_Scale scale, SDL_Color color); |
|
124 |
|
125 FC_GlyphData FC_MakeGlyphData(int cache_level, Sint16 x, Sint16 y, Uint16 w, Uint16 h); |
|
126 |
|
127 |
|
128 |
|
129 // Font object |
|
130 |
|
131 FC_Font* FC_CreateFont(void); |
|
132 |
|
133 #ifdef FC_USE_SDL_GPU |
|
134 Uint8 FC_LoadFont(FC_Font* font, const char* filename_ttf, Uint32 pointSize, SDL_Color color, int style); |
|
135 |
|
136 Uint8 FC_LoadFontFromTTF(FC_Font* font, TTF_Font* ttf, SDL_Color color); |
|
137 |
|
138 Uint8 FC_LoadFont_RW(FC_Font* font, SDL_RWops* file_rwops_ttf, Uint8 own_rwops, Uint32 pointSize, SDL_Color color, int style); |
|
139 #else |
|
140 Uint8 FC_LoadFont(FC_Font* font, SDL_Renderer* renderer, const char* filename_ttf, Uint32 pointSize, SDL_Color color, int style); |
|
141 |
|
142 Uint8 FC_LoadFontFromTTF(FC_Font* font, SDL_Renderer* renderer, TTF_Font* ttf, SDL_Color color); |
|
143 |
|
144 Uint8 FC_LoadFont_RW(FC_Font* font, SDL_Renderer* renderer, SDL_RWops* file_rwops_ttf, Uint8 own_rwops, Uint32 pointSize, SDL_Color color, int style); |
|
145 #endif |
|
146 |
|
147 #ifndef FC_USE_SDL_GPU |
|
148 // note: handle SDL event types SDL_RENDER_TARGETS_RESET(>= SDL 2.0.2) and SDL_RENDER_DEVICE_RESET(>= SDL 2.0.4) |
|
149 void FC_ResetFontFromRendererReset(FC_Font* font, SDL_Renderer* renderer, Uint32 evType); |
|
150 #endif |
|
151 |
|
152 void FC_ClearFont(FC_Font* font); |
|
153 |
|
154 void FC_FreeFont(FC_Font* font); |
|
155 |
|
156 |
|
157 |
|
158 // Built-in loading strings |
|
159 |
|
160 char* FC_GetStringASCII(void); |
|
161 |
|
162 char* FC_GetStringLatin1(void); |
|
163 |
|
164 char* FC_GetStringASCII_Latin1(void); |
|
165 |
|
166 |
|
167 // UTF-8 to SDL_FontCache codepoint conversion |
|
168 |
|
169 /*! |
|
170 Returns the Uint32 codepoint (not UTF-32) parsed from the given UTF-8 string. |
|
171 \param c A pointer to a string of proper UTF-8 character values. |
|
172 \param advance_pointer If true, the source pointer will be incremented to skip the extra bytes from multibyte codepoints. |
|
173 */ |
|
174 Uint32 FC_GetCodepointFromUTF8(const char** c, Uint8 advance_pointer); |
|
175 |
|
176 /*! |
|
177 Parses the given codepoint and stores the UTF-8 bytes in 'result'. The result is NULL terminated. |
|
178 \param result A memory buffer for the UTF-8 values. Must be at least 5 bytes long. |
|
179 \param codepoint The Uint32 codepoint to parse (not UTF-32). |
|
180 */ |
|
181 void FC_GetUTF8FromCodepoint(char* result, Uint32 codepoint); |
|
182 |
|
183 |
|
184 // UTF-8 string operations |
|
185 |
|
186 /*! Allocates a new string of 'size' bytes that is already NULL-terminated. The NULL byte counts toward the size limit, as usual. Returns NULL if size is 0. */ |
|
187 char* U8_alloc(unsigned int size); |
|
188 |
|
189 /*! Deallocates the given string. */ |
|
190 void U8_free(char* string); |
|
191 |
|
192 /*! Allocates a copy of the given string. */ |
|
193 char* U8_strdup(const char* string); |
|
194 |
|
195 /*! Returns the number of UTF-8 characters in the given string. */ |
|
196 int U8_strlen(const char* string); |
|
197 |
|
198 /*! Returns the number of bytes in the UTF-8 multibyte character pointed at by 'character'. */ |
|
199 int U8_charsize(const char* character); |
|
200 |
|
201 /*! Copies the source multibyte character into the given buffer without overrunning it. Returns 0 on failure. */ |
|
202 int U8_charcpy(char* buffer, const char* source, int buffer_size); |
|
203 |
|
204 /*! Returns a pointer to the next UTF-8 character. */ |
|
205 const char* U8_next(const char* string); |
|
206 |
|
207 /*! Inserts a UTF-8 string into 'string' at the given position. Use a position of -1 to append. Returns 0 when unable to insert the string. */ |
|
208 int U8_strinsert(char* string, int position, const char* source, int max_bytes); |
|
209 |
|
210 /*! Erases the UTF-8 character at the given position, moving the subsequent characters down. */ |
|
211 void U8_strdel(char* string, int position); |
|
212 |
|
213 |
|
214 // Internal settings |
|
215 |
|
216 /*! Sets the string from which to load the initial glyphs. Use this if you need upfront loading for any reason (such as lack of render-target support). */ |
|
217 void FC_SetLoadingString(FC_Font* font, const char* string); |
|
218 |
|
219 /*! Returns the size of the internal buffer which is used for unpacking variadic text data. This buffer is shared by all FC_Fonts. */ |
|
220 unsigned int FC_GetBufferSize(void); |
|
221 |
|
222 /*! Changes the size of the internal buffer which is used for unpacking variadic text data. This buffer is shared by all FC_Fonts. */ |
|
223 void FC_SetBufferSize(unsigned int size); |
|
224 |
|
225 /*! Returns the width of a single horizontal tab in multiples of the width of a space (default: 4) */ |
|
226 unsigned int FC_GetTabWidth(void); |
|
227 |
|
228 /*! Changes the width of a horizontal tab in multiples of the width of a space (default: 4) */ |
|
229 void FC_SetTabWidth(unsigned int width_in_spaces); |
|
230 |
|
231 void FC_SetRenderCallback(FC_Rect (*callback)(FC_Image* src, FC_Rect* srcrect, FC_Target* dest, float x, float y, float xscale, float yscale)); |
|
232 |
|
233 FC_Rect FC_DefaultRenderCallback(FC_Image* src, FC_Rect* srcrect, FC_Target* dest, float x, float y, float xscale, float yscale); |
|
234 |
|
235 |
|
236 // Custom caching |
|
237 |
|
238 /*! Returns the number of cache levels that are active. */ |
|
239 int FC_GetNumCacheLevels(FC_Font* font); |
|
240 |
|
241 /*! Returns the cache source texture at the given cache level. */ |
|
242 FC_Image* FC_GetGlyphCacheLevel(FC_Font* font, int cache_level); |
|
243 |
|
244 // TODO: Specify ownership of the texture (should be shareable) |
|
245 /*! Sets a cache source texture for rendering. New cache levels must be sequential. */ |
|
246 Uint8 FC_SetGlyphCacheLevel(FC_Font* font, int cache_level, FC_Image* cache_texture); |
|
247 |
|
248 /*! Copies the given surface to the given cache level as a texture. New cache levels must be sequential. */ |
|
249 Uint8 FC_UploadGlyphCache(FC_Font* font, int cache_level, SDL_Surface* data_surface); |
|
250 |
|
251 |
|
252 /*! Returns the number of codepoints that are stored in the font's glyph data map. */ |
|
253 unsigned int FC_GetNumCodepoints(FC_Font* font); |
|
254 |
|
255 /*! Copies the stored codepoints into the given array. */ |
|
256 void FC_GetCodepoints(FC_Font* font, Uint32* result); |
|
257 |
|
258 /*! Stores the glyph data for the given codepoint in 'result'. Returns 0 if the codepoint was not found in the cache. */ |
|
259 Uint8 FC_GetGlyphData(FC_Font* font, FC_GlyphData* result, Uint32 codepoint); |
|
260 |
|
261 /*! Sets the glyph data for the given codepoint. Duplicates are not checked. Returns a pointer to the stored data. */ |
|
262 FC_GlyphData* FC_SetGlyphData(FC_Font* font, Uint32 codepoint, FC_GlyphData glyph_data); |
|
263 |
|
264 |
|
265 // Rendering |
|
266 |
|
267 FC_Rect FC_Draw(FC_Font* font, FC_Target* dest, float x, float y, const char* formatted_text, ...); |
|
268 FC_Rect FC_DrawAlign(FC_Font* font, FC_Target* dest, float x, float y, FC_AlignEnum align, const char* formatted_text, ...); |
|
269 FC_Rect FC_DrawScale(FC_Font* font, FC_Target* dest, float x, float y, FC_Scale scale, const char* formatted_text, ...); |
|
270 FC_Rect FC_DrawColor(FC_Font* font, FC_Target* dest, float x, float y, SDL_Color color, const char* formatted_text, ...); |
|
271 FC_Rect FC_DrawEffect(FC_Font* font, FC_Target* dest, float x, float y, FC_Effect effect, const char* formatted_text, ...); |
|
272 |
|
273 FC_Rect FC_DrawBox(FC_Font* font, FC_Target* dest, FC_Rect box, const char* formatted_text, ...); |
|
274 FC_Rect FC_DrawBoxAlign(FC_Font* font, FC_Target* dest, FC_Rect box, FC_AlignEnum align, const char* formatted_text, ...); |
|
275 FC_Rect FC_DrawBoxScale(FC_Font* font, FC_Target* dest, FC_Rect box, FC_Scale scale, const char* formatted_text, ...); |
|
276 FC_Rect FC_DrawBoxColor(FC_Font* font, FC_Target* dest, FC_Rect box, SDL_Color color, const char* formatted_text, ...); |
|
277 FC_Rect FC_DrawBoxEffect(FC_Font* font, FC_Target* dest, FC_Rect box, FC_Effect effect, const char* formatted_text, ...); |
|
278 |
|
279 FC_Rect FC_DrawColumn(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, const char* formatted_text, ...); |
|
280 FC_Rect FC_DrawColumnAlign(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, FC_AlignEnum align, const char* formatted_text, ...); |
|
281 FC_Rect FC_DrawColumnScale(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, FC_Scale scale, const char* formatted_text, ...); |
|
282 FC_Rect FC_DrawColumnColor(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, SDL_Color color, const char* formatted_text, ...); |
|
283 FC_Rect FC_DrawColumnEffect(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, FC_Effect effect, const char* formatted_text, ...); |
|
284 |
|
285 |
|
286 // Getters |
|
287 |
|
288 FC_FilterEnum FC_GetFilterMode(FC_Font* font); |
|
289 Uint16 FC_GetLineHeight(FC_Font* font); |
|
290 Uint16 FC_GetHeight(FC_Font* font, const char* formatted_text, ...); |
|
291 Uint16 FC_GetWidth(FC_Font* font, const char* formatted_text, ...); |
|
292 |
|
293 // Returns a 1-pixel wide box in front of the character in the given position (index) |
|
294 FC_Rect FC_GetCharacterOffset(FC_Font* font, Uint16 position_index, int column_width, const char* formatted_text, ...); |
|
295 Uint16 FC_GetColumnHeight(FC_Font* font, Uint16 width, const char* formatted_text, ...); |
|
296 |
|
297 int FC_GetAscent(FC_Font* font, const char* formatted_text, ...); |
|
298 int FC_GetDescent(FC_Font* font, const char* formatted_text, ...); |
|
299 int FC_GetBaseline(FC_Font* font); |
|
300 int FC_GetSpacing(FC_Font* font); |
|
301 int FC_GetLineSpacing(FC_Font* font); |
|
302 Uint16 FC_GetMaxWidth(FC_Font* font); |
|
303 SDL_Color FC_GetDefaultColor(FC_Font* font); |
|
304 |
|
305 FC_Rect FC_GetBounds(FC_Font* font, float x, float y, FC_AlignEnum align, FC_Scale scale, const char* formatted_text, ...); |
|
306 |
|
307 Uint8 FC_InRect(float x, float y, FC_Rect input_rect); |
|
308 // Given an offset (x,y) from the text draw position (the upper-left corner), returns the character position (UTF-8 index) |
|
309 Uint16 FC_GetPositionFromOffset(FC_Font* font, float x, float y, int column_width, FC_AlignEnum align, const char* formatted_text, ...); |
|
310 |
|
311 // Returns the number of characters in the new wrapped text written into `result`. |
|
312 int FC_GetWrappedText(FC_Font* font, char* result, int max_result_size, Uint16 width, const char* formatted_text, ...); |
|
313 |
|
314 // Setters |
|
315 |
|
316 void FC_SetFilterMode(FC_Font* font, FC_FilterEnum filter); |
|
317 void FC_SetSpacing(FC_Font* font, int LetterSpacing); |
|
318 void FC_SetLineSpacing(FC_Font* font, int LineSpacing); |
|
319 void FC_SetDefaultColor(FC_Font* font, SDL_Color color); |
|
320 |
|
321 |
|
322 #ifdef __cplusplus |
|
323 } |
|
324 #endif |
|
325 |
|
326 |
|
327 |
|
328 #endif |