2014-09-17 00:51:31 +04:00
|
|
|
/**********************************************************************************************
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2018-05-04 17:59:48 +03:00
|
|
|
* raylib.text - Basic functions to load Fonts and draw Text
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2017-02-16 02:50:02 +03:00
|
|
|
* CONFIGURATION:
|
2014-09-03 18:51:28 +04:00
|
|
|
*
|
2017-02-16 02:50:02 +03:00
|
|
|
* #define SUPPORT_FILEFORMAT_FNT
|
2017-03-26 23:49:01 +03:00
|
|
|
* #define SUPPORT_FILEFORMAT_TTF
|
2018-04-07 23:29:53 +03:00
|
|
|
* Selected desired fileformats to be supported for loading. Some of those formats are
|
2017-02-16 02:50:02 +03:00
|
|
|
* supported by default, to remove support, just comment unrequired #define in this module
|
|
|
|
*
|
2017-03-21 15:22:47 +03:00
|
|
|
* #define SUPPORT_DEFAULT_FONT
|
2017-02-16 02:50:02 +03:00
|
|
|
*
|
|
|
|
* DEPENDENCIES:
|
2016-11-16 20:46:13 +03:00
|
|
|
* stb_truetype - Load TTF file and rasterize characters data
|
|
|
|
*
|
2017-02-16 02:50:02 +03:00
|
|
|
*
|
|
|
|
* LICENSE: zlib/libpng
|
2016-11-16 20:46:13 +03:00
|
|
|
*
|
2017-12-20 14:37:08 +03:00
|
|
|
* Copyright (c) 2014-2018 Ramon Santamaria (@raysan5)
|
2014-03-16 23:59:02 +04:00
|
|
|
*
|
2014-09-03 18:51:28 +04:00
|
|
|
* This software is provided "as-is", without any express or implied warranty. In no event
|
2013-11-23 16:30:54 +04:00
|
|
|
* will the authors be held liable for any damages arising from the use of this software.
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2014-09-03 18:51:28 +04:00
|
|
|
* Permission is granted to anyone to use this software for any purpose, including commercial
|
2013-11-23 16:30:54 +04:00
|
|
|
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2014-09-03 18:51:28 +04:00
|
|
|
* 1. The origin of this software must not be misrepresented; you must not claim that you
|
|
|
|
* wrote the original software. If you use this software in a product, an acknowledgment
|
2013-11-23 16:30:54 +04:00
|
|
|
* in the product documentation would be appreciated but is not required.
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2013-11-23 16:30:54 +04:00
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
|
|
|
* as being the original software.
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2013-11-23 16:30:54 +04:00
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
|
|
|
**********************************************************************************************/
|
|
|
|
|
2018-05-17 01:04:36 +03:00
|
|
|
#include "raylib.h" // Declares module functions
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2018-12-18 02:20:08 +03:00
|
|
|
// Check if config flags have been externally provided on compilation line
|
|
|
|
#if !defined(EXTERNAL_CONFIG_FLAGS)
|
|
|
|
#include "config.h" // Defines module configuration flags
|
|
|
|
#endif
|
|
|
|
|
2016-06-02 02:26:44 +03:00
|
|
|
#include <stdlib.h> // Required for: malloc(), free()
|
|
|
|
#include <string.h> // Required for: strlen()
|
|
|
|
#include <stdarg.h> // Required for: va_list, va_start(), vfprintf(), va_end()
|
|
|
|
#include <stdio.h> // Required for: FILE, fopen(), fclose(), fscanf(), feof(), rewind(), fgets()
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2017-04-21 01:08:00 +03:00
|
|
|
#include "utils.h" // Required for: fopen() Android mapping
|
2014-04-19 18:36:49 +04:00
|
|
|
|
2017-03-26 23:49:01 +03:00
|
|
|
#if defined(SUPPORT_FILEFORMAT_TTF)
|
2018-06-20 01:52:14 +03:00
|
|
|
#define STB_RECT_PACK_IMPLEMENTATION
|
|
|
|
#include "external/stb_rect_pack.h" // Required for: ttf font rectangles packaging
|
|
|
|
|
|
|
|
#define STBTT_STATIC
|
2017-03-26 23:49:01 +03:00
|
|
|
#define STB_TRUETYPE_IMPLEMENTATION
|
2018-06-20 01:52:14 +03:00
|
|
|
#include "external/stb_truetype.h" // Required for: ttf font data reading
|
2017-03-26 23:49:01 +03:00
|
|
|
#endif
|
2015-01-18 12:58:04 +03:00
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Defines and Macros
|
|
|
|
//----------------------------------------------------------------------------------
|
2018-12-04 15:15:42 +03:00
|
|
|
#define MAX_FORMATTEXT_LENGTH 512
|
|
|
|
#define MAX_SUBTEXT_LENGTH 512
|
2013-11-19 02:38:44 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Types and Structures Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
2014-11-23 23:58:17 +03:00
|
|
|
// ...
|
2014-04-09 22:25:26 +04:00
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Global variables
|
|
|
|
//----------------------------------------------------------------------------------
|
2017-03-21 15:22:47 +03:00
|
|
|
#if defined(SUPPORT_DEFAULT_FONT)
|
2018-05-04 17:59:48 +03:00
|
|
|
static Font defaultFont; // Default font provided by raylib
|
2013-11-23 16:30:54 +04:00
|
|
|
// NOTE: defaultFont is loaded on InitWindow and disposed on CloseWindow [module: core]
|
2017-03-21 15:22:47 +03:00
|
|
|
#endif
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Other Modules Functions Declaration (required by text)
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
//...
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module specific Functions Declaration
|
|
|
|
//----------------------------------------------------------------------------------
|
2017-03-26 23:49:01 +03:00
|
|
|
#if defined(SUPPORT_FILEFORMAT_FNT)
|
2018-05-04 17:59:48 +03:00
|
|
|
static Font LoadBMFont(const char *fileName); // Load a BMFont file (AngelCode font file)
|
2017-03-26 23:49:01 +03:00
|
|
|
#endif
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2017-03-21 15:22:47 +03:00
|
|
|
#if defined(SUPPORT_DEFAULT_FONT)
|
2014-09-17 00:51:31 +04:00
|
|
|
extern void LoadDefaultFont(void);
|
|
|
|
extern void UnloadDefaultFont(void);
|
2017-03-21 15:22:47 +03:00
|
|
|
#endif
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module Functions Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
2017-03-21 15:22:47 +03:00
|
|
|
#if defined(SUPPORT_DEFAULT_FONT)
|
|
|
|
|
|
|
|
// Load raylib default font
|
2014-09-03 19:06:10 +04:00
|
|
|
extern void LoadDefaultFont(void)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2018-02-25 05:17:57 +03:00
|
|
|
#define BIT_CHECK(a,b) ((a) & (1u << (b)))
|
2017-03-21 15:22:47 +03:00
|
|
|
|
2015-03-09 19:13:42 +03:00
|
|
|
// NOTE: Using UTF8 encoding table for Unicode U+0000..U+00FF Basic Latin + Latin-1 Supplement
|
|
|
|
// http://www.utf8-chartable.de/unicode-utf8-table.pl
|
2015-04-06 15:02:29 +03:00
|
|
|
|
2017-02-05 04:59:39 +03:00
|
|
|
defaultFont.charsCount = 224; // Number of chars included in our default font
|
2014-03-25 15:40:35 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
// Default font is directly defined here (data generated from a sprite font image)
|
2018-05-04 17:59:48 +03:00
|
|
|
// This way, we reconstruct Font without creating large global variables
|
2013-11-23 16:30:54 +04:00
|
|
|
// This data is automatically allocated to Stack and automatically deallocated at the end of this function
|
2015-03-09 19:13:42 +03:00
|
|
|
int defaultFontData[512] = {
|
2015-04-06 15:02:29 +03:00
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00200020, 0x0001b000, 0x00000000, 0x00000000, 0x8ef92520, 0x00020a00, 0x7dbe8000, 0x1f7df45f,
|
|
|
|
0x4a2bf2a0, 0x0852091e, 0x41224000, 0x10041450, 0x2e292020, 0x08220812, 0x41222000, 0x10041450, 0x10f92020, 0x3efa084c, 0x7d22103c, 0x107df7de,
|
|
|
|
0xe8a12020, 0x08220832, 0x05220800, 0x10450410, 0xa4a3f000, 0x08520832, 0x05220400, 0x10450410, 0xe2f92020, 0x0002085e, 0x7d3e0281, 0x107df41f,
|
|
|
|
0x00200000, 0x8001b000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0xc0000fbe, 0xfbf7e00f, 0x5fbf7e7d, 0x0050bee8, 0x440808a2, 0x0a142fe8, 0x50810285, 0x0050a048,
|
|
|
|
0x49e428a2, 0x0a142828, 0x40810284, 0x0048a048, 0x10020fbe, 0x09f7ebaf, 0xd89f3e84, 0x0047a04f, 0x09e48822, 0x0a142aa1, 0x50810284, 0x0048a048,
|
|
|
|
0x04082822, 0x0a142fa0, 0x50810285, 0x0050a248, 0x00008fbe, 0xfbf42021, 0x5f817e7d, 0x07d09ce8, 0x00008000, 0x00000fe0, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x000c0180,
|
|
|
|
0xdfbf4282, 0x0bfbf7ef, 0x42850505, 0x004804bf, 0x50a142c6, 0x08401428, 0x42852505, 0x00a808a0, 0x50a146aa, 0x08401428, 0x42852505, 0x00081090,
|
|
|
|
0x5fa14a92, 0x0843f7e8, 0x7e792505, 0x00082088, 0x40a15282, 0x08420128, 0x40852489, 0x00084084, 0x40a16282, 0x0842022a, 0x40852451, 0x00088082,
|
|
|
|
0xc0bf4282, 0xf843f42f, 0x7e85fc21, 0x3e0900bf, 0x00000000, 0x00000004, 0x00000000, 0x000c0180, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x04000402, 0x41482000, 0x00000000, 0x00000800,
|
|
|
|
0x04000404, 0x4100203c, 0x00000000, 0x00000800, 0xf7df7df0, 0x514bef85, 0xbefbefbe, 0x04513bef, 0x14414500, 0x494a2885, 0xa28a28aa, 0x04510820,
|
|
|
|
0xf44145f0, 0x474a289d, 0xa28a28aa, 0x04510be0, 0x14414510, 0x494a2884, 0xa28a28aa, 0x02910a00, 0xf7df7df0, 0xd14a2f85, 0xbefbe8aa, 0x011f7be0,
|
|
|
|
0x00000000, 0x00400804, 0x20080000, 0x00000000, 0x00000000, 0x00600f84, 0x20080000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
2017-08-06 11:44:50 +03:00
|
|
|
0xac000000, 0x00000f01, 0x00000000, 0x00000000, 0x24000000, 0x00000f01, 0x00000000, 0x06000000, 0x24000000, 0x00000f01, 0x00000000, 0x09108000,
|
|
|
|
0x24fa28a2, 0x00000f01, 0x00000000, 0x013e0000, 0x2242252a, 0x00000f52, 0x00000000, 0x038a8000, 0x2422222a, 0x00000f29, 0x00000000, 0x010a8000,
|
|
|
|
0x2412252a, 0x00000f01, 0x00000000, 0x010a8000, 0x24fbe8be, 0x00000f01, 0x00000000, 0x0ebe8000, 0xac020000, 0x00000f01, 0x00000000, 0x00048000,
|
|
|
|
0x0003e000, 0x00000f00, 0x00000000, 0x00008000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000038, 0x8443b80e, 0x00203a03,
|
2015-04-06 15:02:29 +03:00
|
|
|
0x02bea080, 0xf0000020, 0xc452208a, 0x04202b02, 0xf8029122, 0x07f0003b, 0xe44b388e, 0x02203a02, 0x081e8a1c, 0x0411e92a, 0xf4420be0, 0x01248202,
|
|
|
|
0xe8140414, 0x05d104ba, 0xe7c3b880, 0x00893a0a, 0x283c0e1c, 0x04500902, 0xc4400080, 0x00448002, 0xe8208422, 0x04500002, 0x80400000, 0x05200002,
|
|
|
|
0x083e8e00, 0x04100002, 0x804003e0, 0x07000042, 0xf8008400, 0x07f00003, 0x80400000, 0x04000022, 0x00000000, 0x00000000, 0x80400000, 0x04000002,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00800702, 0x1848a0c2, 0x84010000, 0x02920921, 0x01042642, 0x00005121, 0x42023f7f, 0x00291002,
|
|
|
|
0xefc01422, 0x7efdfbf7, 0xefdfa109, 0x03bbbbf7, 0x28440f12, 0x42850a14, 0x20408109, 0x01111010, 0x28440408, 0x42850a14, 0x2040817f, 0x01111010,
|
|
|
|
0xefc78204, 0x7efdfbf7, 0xe7cf8109, 0x011111f3, 0x2850a932, 0x42850a14, 0x2040a109, 0x01111010, 0x2850b840, 0x42850a14, 0xefdfbf79, 0x03bbbbf7,
|
|
|
|
0x001fa020, 0x00000000, 0x00001000, 0x00000000, 0x00002070, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x08022800, 0x00012283, 0x02430802, 0x01010001, 0x8404147c, 0x20000144, 0x80048404, 0x00823f08, 0xdfbf4284, 0x7e03f7ef, 0x142850a1, 0x0000210a,
|
|
|
|
0x50a14684, 0x528a1428, 0x142850a1, 0x03efa17a, 0x50a14a9e, 0x52521428, 0x142850a1, 0x02081f4a, 0x50a15284, 0x4a221428, 0xf42850a1, 0x03efa14b,
|
|
|
|
0x50a16284, 0x4a521428, 0x042850a1, 0x0228a17a, 0xdfbf427c, 0x7e8bf7ef, 0xf7efdfbf, 0x03efbd0b, 0x00000000, 0x04000000, 0x00000000, 0x00000008,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00200508, 0x00840400, 0x11458122, 0x00014210,
|
|
|
|
0x00514294, 0x51420800, 0x20a22a94, 0x0050a508, 0x00200000, 0x00000000, 0x00050000, 0x08000000, 0xfefbefbe, 0xfbefbefb, 0xfbeb9114, 0x00fbefbe,
|
|
|
|
0x20820820, 0x8a28a20a, 0x8a289114, 0x3e8a28a2, 0xfefbefbe, 0xfbefbe0b, 0x8a289114, 0x008a28a2, 0x228a28a2, 0x08208208, 0x8a289114, 0x088a28a2,
|
|
|
|
0xfefbefbe, 0xfbefbefb, 0xfa2f9114, 0x00fbefbe, 0x00000000, 0x00000040, 0x00000000, 0x00000000, 0x00000000, 0x00000020, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00210100, 0x00000004, 0x00000000, 0x00000000, 0x14508200, 0x00001402, 0x00000000, 0x00000000,
|
|
|
|
0x00000010, 0x00000020, 0x00000000, 0x00000000, 0xa28a28be, 0x00002228, 0x00000000, 0x00000000, 0xa28a28aa, 0x000022e8, 0x00000000, 0x00000000,
|
|
|
|
0xa28a28aa, 0x000022a8, 0x00000000, 0x00000000, 0xa28a28aa, 0x000022e8, 0x00000000, 0x00000000, 0xbefbefbe, 0x00003e2f, 0x00000000, 0x00000000,
|
|
|
|
0x00000004, 0x00002028, 0x00000000, 0x00000000, 0x80000000, 0x00003e0f, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 };
|
2013-11-23 16:30:54 +04:00
|
|
|
|
|
|
|
int charsHeight = 10;
|
|
|
|
int charsDivisor = 1; // Every char is separated from the consecutive by a 1 pixel divisor, horizontally and vertically
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-03-09 19:13:42 +03:00
|
|
|
int charsWidth[224] = { 3, 1, 4, 6, 5, 7, 6, 2, 3, 3, 5, 5, 2, 4, 1, 7, 5, 2, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 3, 4, 3, 6,
|
2015-04-06 15:02:29 +03:00
|
|
|
7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 5, 6, 5, 7, 6, 6, 6, 6, 6, 6, 7, 6, 7, 7, 6, 6, 6, 2, 7, 2, 3, 5,
|
|
|
|
2, 5, 5, 5, 5, 5, 4, 5, 5, 1, 2, 5, 2, 5, 5, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 3, 1, 3, 4, 4,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 5, 5, 5, 7, 1, 5, 3, 7, 3, 5, 4, 1, 7, 4, 3, 5, 3, 3, 2, 5, 6, 1, 2, 2, 3, 5, 6, 6, 6, 6,
|
|
|
|
6, 6, 6, 6, 6, 6, 7, 6, 6, 6, 6, 6, 3, 3, 3, 3, 7, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 6, 6, 6, 4, 6,
|
|
|
|
5, 5, 5, 5, 5, 5, 9, 5, 5, 5, 5, 5, 2, 2, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 5 };
|
2013-11-23 16:30:54 +04:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
// Re-construct image from defaultFontData and generate OpenGL texture
|
|
|
|
//----------------------------------------------------------------------
|
2015-05-11 01:15:46 +03:00
|
|
|
int imWidth = 128;
|
|
|
|
int imHeight = 128;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2015-05-11 01:15:46 +03:00
|
|
|
Color *imagePixels = (Color *)malloc(imWidth*imHeight*sizeof(Color));
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-05-11 01:15:46 +03:00
|
|
|
for (int i = 0; i < imWidth*imHeight; i++) imagePixels[i] = BLANK; // Initialize array
|
2014-03-25 15:40:35 +04:00
|
|
|
|
|
|
|
int counter = 0; // Font data elements counter
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
// Fill imgData with defaultFontData (convert from bit to pixel!)
|
2015-05-11 01:15:46 +03:00
|
|
|
for (int i = 0; i < imWidth*imHeight; i += 32)
|
2014-03-25 15:40:35 +04:00
|
|
|
{
|
|
|
|
for (int j = 31; j >= 0; j--)
|
|
|
|
{
|
2015-05-05 00:46:31 +03:00
|
|
|
if (BIT_CHECK(defaultFontData[counter], j)) imagePixels[i+j] = WHITE;
|
2014-03-25 15:40:35 +04:00
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
counter++;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-03-09 19:13:42 +03:00
|
|
|
if (counter > 512) counter = 0; // Security check...
|
2014-03-25 15:40:35 +04:00
|
|
|
}
|
2015-04-06 15:02:29 +03:00
|
|
|
|
2015-07-05 19:21:01 +03:00
|
|
|
Image image = LoadImageEx(imagePixels, imWidth, imHeight);
|
2015-10-06 18:17:53 +03:00
|
|
|
ImageFormat(&image, UNCOMPRESSED_GRAY_ALPHA);
|
2015-05-05 00:46:31 +03:00
|
|
|
|
|
|
|
free(imagePixels);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-07-05 19:21:01 +03:00
|
|
|
defaultFont.texture = LoadTextureFromImage(image);
|
2014-03-25 15:40:35 +04:00
|
|
|
UnloadImage(image);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2017-02-05 04:59:39 +03:00
|
|
|
// Reconstruct charSet using charsWidth[], charsHeight, charsDivisor, charsCount
|
2013-11-23 16:30:54 +04:00
|
|
|
//------------------------------------------------------------------------------
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2017-02-05 04:59:39 +03:00
|
|
|
// Allocate space for our characters info data
|
|
|
|
// NOTE: This memory should be freed at end! --> CloseWindow()
|
2018-11-06 17:10:50 +03:00
|
|
|
defaultFont.chars = (CharInfo *)malloc(defaultFont.charsCount*sizeof(CharInfo));
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
int currentLine = 0;
|
|
|
|
int currentPosX = charsDivisor;
|
|
|
|
int testPosX = charsDivisor;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2017-02-05 04:59:39 +03:00
|
|
|
for (int i = 0; i < defaultFont.charsCount; i++)
|
2013-11-23 16:30:54 +04:00
|
|
|
{
|
2017-02-05 04:59:39 +03:00
|
|
|
defaultFont.chars[i].value = 32 + i; // First char is 32
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2018-08-06 21:49:47 +03:00
|
|
|
defaultFont.chars[i].rec.x = (float)currentPosX;
|
2018-09-05 11:47:40 +03:00
|
|
|
defaultFont.chars[i].rec.y = (float)(charsDivisor + currentLine*(charsHeight + charsDivisor));
|
2018-08-06 21:49:47 +03:00
|
|
|
defaultFont.chars[i].rec.width = (float)charsWidth[i];
|
|
|
|
defaultFont.chars[i].rec.height = (float)charsHeight;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2018-08-06 21:49:47 +03:00
|
|
|
testPosX += (int)(defaultFont.chars[i].rec.width + (float)charsDivisor);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
if (testPosX >= defaultFont.texture.width)
|
|
|
|
{
|
|
|
|
currentLine++;
|
2016-05-03 18:54:50 +03:00
|
|
|
currentPosX = 2*charsDivisor + charsWidth[i];
|
2013-11-23 16:30:54 +04:00
|
|
|
testPosX = currentPosX;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2018-08-04 11:32:16 +03:00
|
|
|
defaultFont.chars[i].rec.x = (float)charsDivisor;
|
2018-09-05 11:47:40 +03:00
|
|
|
defaultFont.chars[i].rec.y = (float)(charsDivisor + currentLine*(charsHeight + charsDivisor));
|
2013-11-23 16:30:54 +04:00
|
|
|
}
|
|
|
|
else currentPosX = testPosX;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
// NOTE: On default font character offsets and xAdvance are not required
|
2017-02-05 04:59:39 +03:00
|
|
|
defaultFont.chars[i].offsetX = 0;
|
|
|
|
defaultFont.chars[i].offsetY = 0;
|
|
|
|
defaultFont.chars[i].advanceX = 0;
|
2013-11-23 16:30:54 +04:00
|
|
|
}
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2018-09-05 11:47:40 +03:00
|
|
|
defaultFont.baseSize = (int)defaultFont.chars[0].rec.height;
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "[TEX ID %i] Default font loaded successfully", defaultFont.texture.id);
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2017-03-21 15:22:47 +03:00
|
|
|
// Unload raylib default font
|
2014-09-03 19:06:10 +04:00
|
|
|
extern void UnloadDefaultFont(void)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2015-04-06 15:02:29 +03:00
|
|
|
UnloadTexture(defaultFont.texture);
|
2017-02-05 04:59:39 +03:00
|
|
|
free(defaultFont.chars);
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
2017-03-21 15:22:47 +03:00
|
|
|
#endif // SUPPORT_DEFAULT_FONT
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2013-11-29 23:24:11 +04:00
|
|
|
// Get the default font, useful to be used with extended parameters
|
2018-07-05 20:17:06 +03:00
|
|
|
Font GetFontDefault()
|
2013-11-29 23:24:11 +04:00
|
|
|
{
|
2017-03-21 15:22:47 +03:00
|
|
|
#if defined(SUPPORT_DEFAULT_FONT)
|
2013-11-29 23:24:11 +04:00
|
|
|
return defaultFont;
|
2017-03-21 15:22:47 +03:00
|
|
|
#else
|
2018-05-04 17:59:48 +03:00
|
|
|
Font font = { 0 };
|
2017-03-21 15:22:47 +03:00
|
|
|
return font;
|
2018-11-06 17:10:50 +03:00
|
|
|
#endif
|
2013-11-29 23:24:11 +04:00
|
|
|
}
|
|
|
|
|
2018-05-04 17:59:48 +03:00
|
|
|
// Load Font from file into GPU memory (VRAM)
|
|
|
|
Font LoadFont(const char *fileName)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2016-08-07 14:38:48 +03:00
|
|
|
// Default hardcoded values for ttf file loading
|
|
|
|
#define DEFAULT_TTF_FONTSIZE 32 // Font first character (32 - space)
|
|
|
|
#define DEFAULT_TTF_NUMCHARS 95 // ASCII 32..126 is 95 glyphs
|
2018-06-20 01:52:14 +03:00
|
|
|
#define DEFAULT_FIRST_CHAR 32 // Expected first char for image sprite font
|
2016-08-07 14:38:48 +03:00
|
|
|
|
2018-06-20 01:52:14 +03:00
|
|
|
Font font = { 0 };
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2017-03-29 01:02:40 +03:00
|
|
|
#if defined(SUPPORT_FILEFORMAT_TTF)
|
2018-12-10 02:29:52 +03:00
|
|
|
if (IsFileExtension(fileName, ".ttf") || IsFileExtension(fileName, ".otf")) font = LoadFontEx(fileName, DEFAULT_TTF_FONTSIZE, DEFAULT_TTF_NUMCHARS, NULL);
|
2017-12-14 13:41:31 +03:00
|
|
|
else
|
2017-03-29 01:02:40 +03:00
|
|
|
#endif
|
|
|
|
#if defined(SUPPORT_FILEFORMAT_FNT)
|
2018-06-20 01:52:14 +03:00
|
|
|
if (IsFileExtension(fileName, ".fnt")) font = LoadBMFont(fileName);
|
2013-12-19 15:08:06 +04:00
|
|
|
else
|
2017-12-14 13:41:31 +03:00
|
|
|
#endif
|
2014-09-03 18:51:28 +04:00
|
|
|
{
|
2014-09-17 00:51:31 +04:00
|
|
|
Image image = LoadImage(fileName);
|
2018-12-20 14:58:27 +03:00
|
|
|
if (image.data != NULL) font = LoadFontFromImage(image, MAGENTA, DEFAULT_FIRST_CHAR);
|
2014-03-25 15:40:35 +04:00
|
|
|
UnloadImage(image);
|
2013-12-19 15:08:06 +04:00
|
|
|
}
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2018-06-20 01:52:14 +03:00
|
|
|
if (font.texture.id == 0)
|
2016-01-02 12:45:51 +03:00
|
|
|
{
|
2018-05-04 17:59:48 +03:00
|
|
|
TraceLog(LOG_WARNING, "[%s] Font could not be loaded, using default font", fileName);
|
2018-07-05 20:17:06 +03:00
|
|
|
font = GetFontDefault();
|
2016-01-02 12:45:51 +03:00
|
|
|
}
|
2018-06-20 01:52:14 +03:00
|
|
|
else SetTextureFilter(font.texture, FILTER_POINT); // By default we set point filter (best performance)
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2018-06-20 01:52:14 +03:00
|
|
|
return font;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2018-06-30 20:58:44 +03:00
|
|
|
// Load Font from TTF font file with generation parameters
|
|
|
|
// NOTE: You can pass an array with desired characters, those characters should be available in the font
|
|
|
|
// if array is NULL, default char set is selected 32..126
|
|
|
|
Font LoadFontEx(const char *fileName, int fontSize, int charsCount, int *fontChars)
|
|
|
|
{
|
|
|
|
Font font = { 0 };
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-06-30 20:58:44 +03:00
|
|
|
font.baseSize = fontSize;
|
|
|
|
font.charsCount = (charsCount > 0) ? charsCount : 95;
|
2018-09-06 17:56:21 +03:00
|
|
|
font.chars = LoadFontData(fileName, font.baseSize, fontChars, font.charsCount, FONT_DEFAULT);
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-18 18:48:33 +03:00
|
|
|
if (font.chars != NULL)
|
|
|
|
{
|
|
|
|
Image atlas = GenImageFontAtlas(font.chars, font.charsCount, font.baseSize, 2, 0);
|
|
|
|
font.texture = LoadTextureFromImage(atlas);
|
|
|
|
UnloadImage(atlas);
|
|
|
|
}
|
2018-10-24 14:45:17 +03:00
|
|
|
else font = GetFontDefault();
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-06-30 20:58:44 +03:00
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
2018-12-20 14:58:27 +03:00
|
|
|
// Load an Image font file (XNA style)
|
|
|
|
Font LoadFontFromImage(Image image, Color key, int firstChar)
|
|
|
|
{
|
|
|
|
#define COLOR_EQUAL(col1, col2) ((col1.r == col2.r)&&(col1.g == col2.g)&&(col1.b == col2.b)&&(col1.a == col2.a))
|
|
|
|
|
|
|
|
int charSpacing = 0;
|
|
|
|
int lineSpacing = 0;
|
|
|
|
|
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
|
|
|
|
|
|
|
// Default number of characters supported
|
|
|
|
#define MAX_FONTCHARS 256
|
|
|
|
|
|
|
|
// We allocate a temporal arrays for chars data measures,
|
|
|
|
// once we get the actual number of chars, we copy data to a sized arrays
|
|
|
|
int tempCharValues[MAX_FONTCHARS];
|
|
|
|
Rectangle tempCharRecs[MAX_FONTCHARS];
|
|
|
|
|
|
|
|
Color *pixels = GetImageData(image);
|
|
|
|
|
|
|
|
// Parse image data to get charSpacing and lineSpacing
|
|
|
|
for (y = 0; y < image.height; y++)
|
|
|
|
{
|
|
|
|
for (x = 0; x < image.width; x++)
|
|
|
|
{
|
|
|
|
if (!COLOR_EQUAL(pixels[y*image.width + x], key)) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!COLOR_EQUAL(pixels[y*image.width + x], key)) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
charSpacing = x;
|
|
|
|
lineSpacing = y;
|
|
|
|
|
|
|
|
int charHeight = 0;
|
|
|
|
int j = 0;
|
|
|
|
|
|
|
|
while (!COLOR_EQUAL(pixels[(lineSpacing + j)*image.width + charSpacing], key)) j++;
|
|
|
|
|
|
|
|
charHeight = j;
|
|
|
|
|
|
|
|
// Check array values to get characters: value, x, y, w, h
|
|
|
|
int index = 0;
|
|
|
|
int lineToRead = 0;
|
|
|
|
int xPosToRead = charSpacing;
|
|
|
|
|
|
|
|
// Parse image data to get rectangle sizes
|
|
|
|
while ((lineSpacing + lineToRead*(charHeight + lineSpacing)) < image.height)
|
|
|
|
{
|
|
|
|
while ((xPosToRead < image.width) &&
|
|
|
|
!COLOR_EQUAL((pixels[(lineSpacing + (charHeight+lineSpacing)*lineToRead)*image.width + xPosToRead]), key))
|
|
|
|
{
|
|
|
|
tempCharValues[index] = firstChar + index;
|
|
|
|
|
|
|
|
tempCharRecs[index].x = (float)xPosToRead;
|
|
|
|
tempCharRecs[index].y = (float)(lineSpacing + lineToRead*(charHeight + lineSpacing));
|
|
|
|
tempCharRecs[index].height = (float)charHeight;
|
|
|
|
|
|
|
|
int charWidth = 0;
|
|
|
|
|
|
|
|
while (!COLOR_EQUAL(pixels[(lineSpacing + (charHeight+lineSpacing)*lineToRead)*image.width + xPosToRead + charWidth], key)) charWidth++;
|
|
|
|
|
|
|
|
tempCharRecs[index].width = (float)charWidth;
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
|
|
|
xPosToRead += (charWidth + charSpacing);
|
|
|
|
}
|
|
|
|
|
|
|
|
lineToRead++;
|
|
|
|
xPosToRead = charSpacing;
|
|
|
|
}
|
|
|
|
|
|
|
|
TraceLog(LOG_DEBUG, "Font data parsed correctly from image");
|
|
|
|
|
|
|
|
// NOTE: We need to remove key color borders from image to avoid weird
|
|
|
|
// artifacts on texture scaling when using FILTER_BILINEAR or FILTER_TRILINEAR
|
|
|
|
for (int i = 0; i < image.height*image.width; i++) if (COLOR_EQUAL(pixels[i], key)) pixels[i] = BLANK;
|
|
|
|
|
|
|
|
// Create a new image with the processed color data (key color replaced by BLANK)
|
|
|
|
Image fontClear = LoadImageEx(pixels, image.width, image.height);
|
|
|
|
|
|
|
|
free(pixels); // Free pixels array memory
|
|
|
|
|
|
|
|
// Create spritefont with all data parsed from image
|
|
|
|
Font spriteFont = { 0 };
|
|
|
|
|
|
|
|
spriteFont.texture = LoadTextureFromImage(fontClear); // Convert processed image to OpenGL texture
|
|
|
|
spriteFont.charsCount = index;
|
|
|
|
|
|
|
|
UnloadImage(fontClear); // Unload processed image once converted to texture
|
|
|
|
|
|
|
|
// We got tempCharValues and tempCharsRecs populated with chars data
|
|
|
|
// Now we move temp data to sized charValues and charRecs arrays
|
|
|
|
spriteFont.chars = (CharInfo *)malloc(spriteFont.charsCount*sizeof(CharInfo));
|
|
|
|
|
|
|
|
for (int i = 0; i < spriteFont.charsCount; i++)
|
|
|
|
{
|
|
|
|
spriteFont.chars[i].value = tempCharValues[i];
|
|
|
|
spriteFont.chars[i].rec = tempCharRecs[i];
|
|
|
|
|
|
|
|
// NOTE: On image based fonts (XNA style), character offsets and xAdvance are not required (set to 0)
|
|
|
|
spriteFont.chars[i].offsetX = 0;
|
|
|
|
spriteFont.chars[i].offsetY = 0;
|
|
|
|
spriteFont.chars[i].advanceX = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
spriteFont.baseSize = (int)spriteFont.chars[0].rec.height;
|
|
|
|
|
|
|
|
TraceLog(LOG_INFO, "Image file loaded correctly as Font");
|
|
|
|
|
|
|
|
return spriteFont;
|
|
|
|
}
|
|
|
|
|
2018-06-20 01:52:14 +03:00
|
|
|
// Load font data for further use
|
|
|
|
// NOTE: Requires TTF font and can generate SDF data
|
2018-09-06 17:56:21 +03:00
|
|
|
CharInfo *LoadFontData(const char *fileName, int fontSize, int *fontChars, int charsCount, int type)
|
2016-10-27 14:39:47 +03:00
|
|
|
{
|
2018-06-20 01:52:14 +03:00
|
|
|
// NOTE: Using some SDF generation default values,
|
|
|
|
// trades off precision with ability to handle *smaller* sizes
|
|
|
|
#define SDF_CHAR_PADDING 4
|
|
|
|
#define SDF_ON_EDGE_VALUE 128
|
|
|
|
#define SDF_PIXEL_DIST_SCALE 64.0f
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-09-06 17:56:21 +03:00
|
|
|
#define BITMAP_ALPHA_THRESHOLD 80
|
2018-10-24 14:45:17 +03:00
|
|
|
|
2018-10-18 18:48:33 +03:00
|
|
|
CharInfo *chars = NULL;
|
2018-10-24 14:45:17 +03:00
|
|
|
|
2018-06-20 01:52:14 +03:00
|
|
|
// Load font data (including pixel data) from TTF file
|
|
|
|
// NOTE: Loaded information should be enough to generate font image atlas,
|
|
|
|
// using any packaging method
|
|
|
|
FILE *fontFile = fopen(fileName, "rb"); // Load font file
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-18 18:48:33 +03:00
|
|
|
if (fontFile != NULL)
|
|
|
|
{
|
|
|
|
fseek(fontFile, 0, SEEK_END);
|
|
|
|
long size = ftell(fontFile); // Get file size
|
|
|
|
fseek(fontFile, 0, SEEK_SET); // Reset file pointer
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-18 18:48:33 +03:00
|
|
|
unsigned char *fontBuffer = (unsigned char *)malloc(size);
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-18 18:48:33 +03:00
|
|
|
fread(fontBuffer, size, 1, fontFile);
|
|
|
|
fclose(fontFile);
|
2018-10-24 14:45:17 +03:00
|
|
|
|
2018-10-18 18:48:33 +03:00
|
|
|
// Init font for data reading
|
|
|
|
stbtt_fontinfo fontInfo;
|
|
|
|
if (!stbtt_InitFont(&fontInfo, fontBuffer, 0)) TraceLog(LOG_WARNING, "Failed to init font!");
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2018-10-18 18:48:33 +03:00
|
|
|
// Calculate font scale factor
|
|
|
|
float scaleFactor = stbtt_ScaleForPixelHeight(&fontInfo, (float)fontSize);
|
2018-06-20 01:52:14 +03:00
|
|
|
|
2018-10-18 18:48:33 +03:00
|
|
|
// Calculate font basic metrics
|
|
|
|
// NOTE: ascent is equivalent to font baseline
|
|
|
|
int ascent, descent, lineGap;
|
|
|
|
stbtt_GetFontVMetrics(&fontInfo, &ascent, &descent, &lineGap);
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-18 18:48:33 +03:00
|
|
|
// In case no chars count provided, default to 95
|
|
|
|
charsCount = (charsCount > 0) ? charsCount : 95;
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-18 18:48:33 +03:00
|
|
|
// Fill fontChars in case not provided externally
|
|
|
|
// NOTE: By default we fill charsCount consecutevely, starting at 32 (Space)
|
|
|
|
int genFontChars = false;
|
2018-10-24 14:45:17 +03:00
|
|
|
if (fontChars == NULL)
|
2018-10-18 18:48:33 +03:00
|
|
|
{
|
|
|
|
fontChars = (int *)malloc(charsCount*sizeof(int));
|
|
|
|
for (int i = 0; i < charsCount; i++) fontChars[i] = i + 32;
|
2018-10-24 14:45:17 +03:00
|
|
|
genFontChars = true;
|
2018-10-18 18:48:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
chars = (CharInfo *)malloc(charsCount*sizeof(CharInfo));
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-18 18:48:33 +03:00
|
|
|
// NOTE: Using simple packaging, one char after another
|
2018-11-06 17:10:50 +03:00
|
|
|
for (int i = 0; i < charsCount; i++)
|
2018-09-06 17:56:21 +03:00
|
|
|
{
|
2018-10-18 18:48:33 +03:00
|
|
|
int chw = 0, chh = 0; // Character width and height (on generation)
|
|
|
|
int ch = fontChars[i]; // Character value to get info for
|
|
|
|
chars[i].value = ch;
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-18 18:48:33 +03:00
|
|
|
// Render a unicode codepoint to a bitmap
|
|
|
|
// stbtt_GetCodepointBitmap() -- allocates and returns a bitmap
|
|
|
|
// stbtt_GetCodepointBitmapBox() -- how big the bitmap must be
|
|
|
|
// stbtt_MakeCodepointBitmap() -- renders into bitmap you provide
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-18 18:48:33 +03:00
|
|
|
if (type != FONT_SDF) chars[i].data = stbtt_GetCodepointBitmap(&fontInfo, scaleFactor, scaleFactor, ch, &chw, &chh, &chars[i].offsetX, &chars[i].offsetY);
|
|
|
|
else if (ch != 32) chars[i].data = stbtt_GetCodepointSDF(&fontInfo, scaleFactor, ch, SDF_CHAR_PADDING, SDF_ON_EDGE_VALUE, SDF_PIXEL_DIST_SCALE, &chw, &chh, &chars[i].offsetX, &chars[i].offsetY);
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-18 18:48:33 +03:00
|
|
|
if (type == FONT_BITMAP)
|
2018-09-06 17:56:21 +03:00
|
|
|
{
|
2018-10-18 18:48:33 +03:00
|
|
|
// Aliased bitmap (black & white) font generation, avoiding anti-aliasing
|
|
|
|
// NOTE: For optimum results, bitmap font should be generated at base pixel size
|
|
|
|
for (int p = 0; p < chw*chh; p++)
|
|
|
|
{
|
|
|
|
if (chars[i].data[p] < BITMAP_ALPHA_THRESHOLD) chars[i].data[p] = 0;
|
|
|
|
else chars[i].data[p] = 255;
|
|
|
|
}
|
2018-09-06 17:56:21 +03:00
|
|
|
}
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-18 18:48:33 +03:00
|
|
|
chars[i].rec.width = (float)chw;
|
|
|
|
chars[i].rec.height = (float)chh;
|
|
|
|
chars[i].offsetY += (int)((float)ascent*scaleFactor);
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-18 18:48:33 +03:00
|
|
|
// Get bounding box for character (may be offset to account for chars that dip above or below the line)
|
|
|
|
int chX1, chY1, chX2, chY2;
|
|
|
|
stbtt_GetCodepointBitmapBox(&fontInfo, ch, scaleFactor, scaleFactor, &chX1, &chY1, &chX2, &chY2);
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-18 18:48:33 +03:00
|
|
|
TraceLog(LOG_DEBUG, "Character box measures: %i, %i, %i, %i", chX1, chY1, chX2 - chX1, chY2 - chY1);
|
|
|
|
TraceLog(LOG_DEBUG, "Character offsetY: %i", (int)((float)ascent*scaleFactor) + chY1);
|
|
|
|
|
|
|
|
stbtt_GetCodepointHMetrics(&fontInfo, ch, &chars[i].advanceX, NULL);
|
|
|
|
chars[i].advanceX *= scaleFactor;
|
|
|
|
}
|
2018-10-24 14:45:17 +03:00
|
|
|
|
2018-10-18 18:48:33 +03:00
|
|
|
free(fontBuffer);
|
|
|
|
if (genFontChars) free(fontChars);
|
2018-06-20 01:52:14 +03:00
|
|
|
}
|
2018-10-18 18:48:33 +03:00
|
|
|
else TraceLog(LOG_WARNING, "[%s] TTF file could not be opened", fileName);
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-06-20 01:52:14 +03:00
|
|
|
return chars;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate image font atlas using chars info
|
|
|
|
// NOTE: Packing method: 0-Default, 1-Skyline
|
2018-06-21 01:18:13 +03:00
|
|
|
Image GenImageFontAtlas(CharInfo *chars, int charsCount, int fontSize, int padding, int packMethod)
|
2018-06-20 01:52:14 +03:00
|
|
|
{
|
|
|
|
Image atlas = { 0 };
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-07-15 21:03:05 +03:00
|
|
|
// In case no chars count provided we suppose default of 95
|
|
|
|
charsCount = (charsCount > 0) ? charsCount : 95;
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-07-15 21:03:05 +03:00
|
|
|
// Calculate image size based on required pixel area
|
|
|
|
// NOTE 1: Image is forced to be squared and POT... very conservative!
|
2018-11-06 17:10:50 +03:00
|
|
|
// NOTE 2: SDF font characters already contain an internal padding,
|
2018-07-15 21:03:05 +03:00
|
|
|
// so image size would result bigger than default font type
|
2018-06-20 01:52:14 +03:00
|
|
|
float requiredArea = 0;
|
|
|
|
for (int i = 0; i < charsCount; i++) requiredArea += ((chars[i].rec.width + 2*padding)*(chars[i].rec.height + 2*padding));
|
|
|
|
float guessSize = sqrtf(requiredArea)*1.25f;
|
2018-07-15 21:03:05 +03:00
|
|
|
int imageSize = (int)powf(2, ceilf(logf((float)guessSize)/logf(2))); // Calculate next POT
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-07-15 21:03:05 +03:00
|
|
|
atlas.width = imageSize; // Atlas bitmap width
|
|
|
|
atlas.height = imageSize; // Atlas bitmap height
|
2018-06-20 01:52:14 +03:00
|
|
|
atlas.data = (unsigned char *)calloc(1, atlas.width*atlas.height); // Create a bitmap to store characters (8 bpp)
|
|
|
|
atlas.format = UNCOMPRESSED_GRAYSCALE;
|
|
|
|
atlas.mipmaps = 1;
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-07-15 21:03:05 +03:00
|
|
|
// DEBUG: We can see padding in the generated image setting a gray background...
|
|
|
|
//for (int i = 0; i < atlas.width*atlas.height; i++) ((unsigned char *)atlas.data)[i] = 100;
|
2018-06-20 01:52:14 +03:00
|
|
|
|
2018-06-21 01:18:13 +03:00
|
|
|
if (packMethod == 0) // Use basic packing algorythm
|
2016-10-27 14:39:47 +03:00
|
|
|
{
|
2018-06-20 01:52:14 +03:00
|
|
|
int offsetX = padding;
|
|
|
|
int offsetY = padding;
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-06-20 01:52:14 +03:00
|
|
|
// NOTE: Using simple packaging, one char after another
|
2018-11-06 17:10:50 +03:00
|
|
|
for (int i = 0; i < charsCount; i++)
|
2016-10-31 17:38:15 +03:00
|
|
|
{
|
2018-06-20 01:52:14 +03:00
|
|
|
// Copy pixel data from fc.data to atlas
|
|
|
|
for (int y = 0; y < (int)chars[i].rec.height; y++)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < (int)chars[i].rec.width; x++)
|
|
|
|
{
|
|
|
|
((unsigned char *)atlas.data)[(offsetY + y)*atlas.width + (offsetX + x)] = chars[i].data[y*(int)chars[i].rec.width + x];
|
|
|
|
}
|
|
|
|
}
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-08-06 21:49:47 +03:00
|
|
|
chars[i].rec.x = (float)offsetX;
|
|
|
|
chars[i].rec.y = (float)offsetY;
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-06-20 01:52:14 +03:00
|
|
|
// Move atlas position X for next character drawing
|
2018-07-15 21:03:05 +03:00
|
|
|
offsetX += ((int)chars[i].rec.width + 2*padding);
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-06-20 01:52:14 +03:00
|
|
|
if (offsetX >= (atlas.width - (int)chars[i].rec.width - padding))
|
|
|
|
{
|
|
|
|
offsetX = padding;
|
2018-11-06 17:10:50 +03:00
|
|
|
|
|
|
|
// NOTE: Be careful on offsetY for SDF fonts, by default SDF
|
2018-07-15 21:03:05 +03:00
|
|
|
// use an internal padding of 4 pixels, it means char rectangle
|
|
|
|
// height is bigger than fontSize, it could be up to (fontSize + 8)
|
2018-06-20 01:52:14 +03:00
|
|
|
offsetY += (fontSize + 2*padding);
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-06-20 01:52:14 +03:00
|
|
|
if (offsetY > (atlas.height - fontSize - padding)) break;
|
|
|
|
}
|
2016-10-31 17:38:15 +03:00
|
|
|
}
|
2018-06-20 01:52:14 +03:00
|
|
|
}
|
2018-06-21 01:18:13 +03:00
|
|
|
else if (packMethod == 1) // Use Skyline rect packing algorythm (stb_pack_rect)
|
2018-06-20 01:52:14 +03:00
|
|
|
{
|
2018-07-15 21:03:05 +03:00
|
|
|
TraceLog(LOG_DEBUG, "Using Skyline packing algorythm!");
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-06-20 01:52:14 +03:00
|
|
|
stbrp_context *context = (stbrp_context *)malloc(sizeof(*context));
|
|
|
|
stbrp_node *nodes = (stbrp_node *)malloc(charsCount*sizeof(*nodes));
|
|
|
|
|
|
|
|
stbrp_init_target(context, atlas.width, atlas.height, nodes, charsCount);
|
|
|
|
stbrp_rect *rects = (stbrp_rect *)malloc(charsCount*sizeof(stbrp_rect));
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-06-20 01:52:14 +03:00
|
|
|
// Fill rectangles for packaging
|
|
|
|
for (int i = 0; i < charsCount; i++)
|
|
|
|
{
|
|
|
|
rects[i].id = i;
|
|
|
|
rects[i].w = (int)chars[i].rec.width + 2*padding;
|
|
|
|
rects[i].h = (int)chars[i].rec.height + 2*padding;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Package rectangles into atlas
|
|
|
|
stbrp_pack_rects(context, rects, charsCount);
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-06-20 01:52:14 +03:00
|
|
|
for (int i = 0; i < charsCount; i++)
|
|
|
|
{
|
2018-08-06 21:49:47 +03:00
|
|
|
chars[i].rec.x = rects[i].x + (float)padding;
|
|
|
|
chars[i].rec.y = rects[i].y + (float)padding;
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-06-20 01:52:14 +03:00
|
|
|
if (rects[i].was_packed)
|
|
|
|
{
|
|
|
|
// Copy pixel data from fc.data to atlas
|
|
|
|
for (int y = 0; y < (int)chars[i].rec.height; y++)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < (int)chars[i].rec.width; x++)
|
|
|
|
{
|
|
|
|
((unsigned char *)atlas.data)[(rects[i].y + padding + y)*atlas.width + (rects[i].x + padding + x)] = chars[i].data[y*(int)chars[i].rec.width + x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else TraceLog(LOG_WARNING, "Character could not be packed: %i", i);
|
|
|
|
}
|
|
|
|
|
2018-10-10 23:41:20 +03:00
|
|
|
free(rects);
|
2018-06-20 01:52:14 +03:00
|
|
|
free(nodes);
|
|
|
|
free(context);
|
2016-10-27 14:39:47 +03:00
|
|
|
}
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-07-15 21:03:05 +03:00
|
|
|
// TODO: Crop image if required for smaller size
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-06-20 01:52:14 +03:00
|
|
|
// Convert image data from GRAYSCALE to GRAY_ALPHA
|
2018-06-21 01:18:13 +03:00
|
|
|
// WARNING: ImageAlphaMask(&atlas, atlas) does not work in this case, requires manual operation
|
2018-07-15 21:03:05 +03:00
|
|
|
unsigned char *dataGrayAlpha = (unsigned char *)malloc(imageSize*imageSize*sizeof(unsigned char)*2); // Two channels
|
2016-10-27 14:39:47 +03:00
|
|
|
|
2018-06-20 01:52:14 +03:00
|
|
|
for (int i = 0, k = 0; i < atlas.width*atlas.height; i++, k += 2)
|
2016-10-27 14:39:47 +03:00
|
|
|
{
|
2018-06-20 01:52:14 +03:00
|
|
|
dataGrayAlpha[k] = 255;
|
|
|
|
dataGrayAlpha[k + 1] = ((unsigned char *)atlas.data)[i];
|
2016-10-27 14:39:47 +03:00
|
|
|
}
|
|
|
|
|
2018-06-20 01:52:14 +03:00
|
|
|
free(atlas.data);
|
|
|
|
atlas.data = dataGrayAlpha;
|
|
|
|
atlas.format = UNCOMPRESSED_GRAY_ALPHA;
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-06-20 01:52:14 +03:00
|
|
|
return atlas;
|
2016-10-27 14:39:47 +03:00
|
|
|
}
|
|
|
|
|
2018-05-04 17:59:48 +03:00
|
|
|
// Unload Font from GPU memory (VRAM)
|
|
|
|
void UnloadFont(Font font)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2016-01-02 12:45:51 +03:00
|
|
|
// NOTE: Make sure spriteFont is not default font (fallback)
|
2018-07-05 20:17:06 +03:00
|
|
|
if (font.texture.id != GetFontDefault().texture.id)
|
2016-01-02 12:45:51 +03:00
|
|
|
{
|
2018-02-18 20:07:57 +03:00
|
|
|
UnloadTexture(font.texture);
|
|
|
|
free(font.chars);
|
2016-01-18 14:04:54 +03:00
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_DEBUG, "Unloaded sprite font data");
|
2016-01-02 12:45:51 +03:00
|
|
|
}
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2018-10-23 16:43:19 +03:00
|
|
|
// Shows current FPS on top-left corner
|
|
|
|
// NOTE: Uses default font
|
|
|
|
void DrawFPS(int posX, int posY)
|
|
|
|
{
|
|
|
|
// NOTE: We are rendering fps every second for better viewing on high framerates
|
|
|
|
|
|
|
|
static int fps = 0;
|
|
|
|
static int counter = 0;
|
|
|
|
static int refreshRate = 20;
|
|
|
|
|
|
|
|
if (counter < refreshRate) counter++;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fps = GetFPS();
|
|
|
|
refreshRate = fps;
|
|
|
|
counter = 0;
|
|
|
|
}
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-23 16:43:19 +03:00
|
|
|
// NOTE: We have rounding errors every frame, so it oscillates a lot
|
|
|
|
DrawText(FormatText("%2i FPS", fps), posX, posY, 20, LIME);
|
|
|
|
}
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
// Draw text (using default font)
|
|
|
|
// NOTE: fontSize work like in any drawing program but if fontSize is lower than font-base-size, then font-base-size is used
|
2013-12-19 15:08:06 +04:00
|
|
|
// NOTE: chars spacing is proportional to fontSize
|
2014-09-17 00:51:31 +04:00
|
|
|
void DrawText(const char *text, int posX, int posY, int fontSize, Color color)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2016-10-09 14:09:08 +03:00
|
|
|
// Check if default font has been loaded
|
2018-07-05 20:17:06 +03:00
|
|
|
if (GetFontDefault().texture.id != 0)
|
2016-10-09 14:09:08 +03:00
|
|
|
{
|
|
|
|
Vector2 position = { (float)posX, (float)posY };
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2016-10-09 14:09:08 +03:00
|
|
|
int defaultFontSize = 10; // Default Font chars height in pixel
|
|
|
|
if (fontSize < defaultFontSize) fontSize = defaultFontSize;
|
|
|
|
int spacing = fontSize/defaultFontSize;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2018-07-05 20:17:06 +03:00
|
|
|
DrawTextEx(GetFontDefault(), text, position, (float)fontSize, (float)spacing, color);
|
2016-10-09 14:09:08 +03:00
|
|
|
}
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2018-05-04 17:59:48 +03:00
|
|
|
// Draw text using Font
|
2013-12-19 15:08:06 +04:00
|
|
|
// NOTE: chars spacing is NOT proportional to fontSize
|
2018-05-04 17:59:48 +03:00
|
|
|
void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, float spacing, Color tint)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
int length = strlen(text);
|
2016-11-03 20:57:46 +03:00
|
|
|
int textOffsetX = 0; // Offset between characters
|
|
|
|
int textOffsetY = 0; // Required for line break!
|
2018-10-18 17:00:11 +03:00
|
|
|
float scaleFactor = 0.0f;
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2018-10-18 17:00:11 +03:00
|
|
|
unsigned char letter = 0; // Current character
|
|
|
|
int index = 0; // Index position in sprite font
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2018-02-18 20:07:57 +03:00
|
|
|
scaleFactor = fontSize/font.baseSize;
|
2013-11-23 16:30:54 +04:00
|
|
|
|
2016-08-16 12:09:55 +03:00
|
|
|
// NOTE: Some ugly hacks are made to support Latin-1 Extended characters directly
|
2015-10-06 18:17:53 +03:00
|
|
|
// written in C code files (codified by default as UTF-8)
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-08-31 11:27:29 +03:00
|
|
|
for (int i = 0; i < length; i++)
|
2015-04-06 15:02:29 +03:00
|
|
|
{
|
2016-11-03 20:57:46 +03:00
|
|
|
if ((unsigned char)text[i] == '\n')
|
2015-04-06 15:02:29 +03:00
|
|
|
{
|
2016-11-03 20:57:46 +03:00
|
|
|
// NOTE: Fixed line spacing of 1.5 lines
|
2018-02-18 20:07:57 +03:00
|
|
|
textOffsetY += (int)((font.baseSize + font.baseSize/2)*scaleFactor);
|
2016-11-03 20:57:46 +03:00
|
|
|
textOffsetX = 0;
|
2015-04-06 15:02:29 +03:00
|
|
|
}
|
2015-10-21 19:24:44 +03:00
|
|
|
else
|
|
|
|
{
|
2016-11-03 20:57:46 +03:00
|
|
|
if ((unsigned char)text[i] == 0xc2) // UTF-8 encoding identification HACK!
|
2015-10-21 19:24:44 +03:00
|
|
|
{
|
2016-11-03 20:57:46 +03:00
|
|
|
// Support UTF-8 encoded values from [0xc2 0x80] -> [0xc2 0xbf](¿)
|
|
|
|
letter = (unsigned char)text[i + 1];
|
2018-02-18 20:07:57 +03:00
|
|
|
index = GetGlyphIndex(font, (int)letter);
|
2016-11-03 20:57:46 +03:00
|
|
|
i++;
|
2015-10-21 19:24:44 +03:00
|
|
|
}
|
2016-11-03 20:57:46 +03:00
|
|
|
else if ((unsigned char)text[i] == 0xc3) // UTF-8 encoding identification HACK!
|
|
|
|
{
|
|
|
|
// Support UTF-8 encoded values from [0xc3 0x80](À) -> [0xc3 0xbf](ÿ)
|
|
|
|
letter = (unsigned char)text[i + 1];
|
2018-02-18 20:07:57 +03:00
|
|
|
index = GetGlyphIndex(font, (int)letter + 64);
|
2016-11-03 20:57:46 +03:00
|
|
|
i++;
|
|
|
|
}
|
2018-02-18 20:07:57 +03:00
|
|
|
else index = GetGlyphIndex(font, (unsigned char)text[i]);
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-02-12 13:25:00 +03:00
|
|
|
if ((unsigned char)text[i] != ' ')
|
|
|
|
{
|
2018-02-18 20:07:57 +03:00
|
|
|
DrawTexturePro(font.texture, font.chars[index].rec,
|
|
|
|
(Rectangle){ position.x + textOffsetX + font.chars[index].offsetX*scaleFactor,
|
|
|
|
position.y + textOffsetY + font.chars[index].offsetY*scaleFactor,
|
|
|
|
font.chars[index].rec.width*scaleFactor,
|
|
|
|
font.chars[index].rec.height*scaleFactor }, (Vector2){ 0, 0 }, 0.0f, tint);
|
2018-02-12 13:25:00 +03:00
|
|
|
}
|
2015-04-06 15:02:29 +03:00
|
|
|
|
2018-02-18 20:07:57 +03:00
|
|
|
if (font.chars[index].advanceX == 0) textOffsetX += (int)(font.chars[index].rec.width*scaleFactor + spacing);
|
|
|
|
else textOffsetX += (int)(font.chars[index].advanceX*scaleFactor + spacing);
|
2015-10-21 19:24:44 +03:00
|
|
|
}
|
2015-04-06 15:02:29 +03:00
|
|
|
}
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Measure string width for default font
|
2013-11-28 22:59:56 +04:00
|
|
|
int MeasureText(const char *text, int fontSize)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2016-10-09 14:09:08 +03:00
|
|
|
Vector2 vec = { 0.0f, 0.0f };
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2016-10-09 14:09:08 +03:00
|
|
|
// Check if default font has been loaded
|
2018-07-05 20:17:06 +03:00
|
|
|
if (GetFontDefault().texture.id != 0)
|
2016-10-09 14:09:08 +03:00
|
|
|
{
|
|
|
|
int defaultFontSize = 10; // Default Font chars height in pixel
|
|
|
|
if (fontSize < defaultFontSize) fontSize = defaultFontSize;
|
|
|
|
int spacing = fontSize/defaultFontSize;
|
2014-10-17 23:11:58 +04:00
|
|
|
|
2018-07-05 20:17:06 +03:00
|
|
|
vec = MeasureTextEx(GetFontDefault(), text, (float)fontSize, (float)spacing);
|
2016-10-09 14:09:08 +03:00
|
|
|
}
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
return (int)vec.x;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2018-05-04 17:59:48 +03:00
|
|
|
// Measure string size for Font
|
|
|
|
Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
int len = strlen(text);
|
2016-12-25 04:01:13 +03:00
|
|
|
int tempLen = 0; // Used to count longer text line num chars
|
2015-12-09 22:57:50 +03:00
|
|
|
int lenCounter = 0;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2018-10-18 17:00:11 +03:00
|
|
|
float textWidth = 0.0f;
|
|
|
|
float tempTextWidth = 0.0f; // Used to count longer text line width
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2018-02-18 20:07:57 +03:00
|
|
|
float textHeight = (float)font.baseSize;
|
|
|
|
float scaleFactor = fontSize/(float)font.baseSize;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
{
|
2015-12-09 22:57:50 +03:00
|
|
|
lenCounter++;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2015-12-09 22:57:50 +03:00
|
|
|
if (text[i] != '\n')
|
|
|
|
{
|
2018-02-18 20:07:57 +03:00
|
|
|
int index = GetGlyphIndex(font, (int)text[i]);
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2018-02-18 20:07:57 +03:00
|
|
|
if (font.chars[index].advanceX != 0) textWidth += font.chars[index].advanceX;
|
|
|
|
else textWidth += (font.chars[index].rec.width + font.chars[index].offsetX);
|
2015-12-09 22:57:50 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (tempTextWidth < textWidth) tempTextWidth = textWidth;
|
|
|
|
lenCounter = 0;
|
|
|
|
textWidth = 0;
|
2018-02-18 20:07:57 +03:00
|
|
|
textHeight += ((float)font.baseSize*1.5f); // NOTE: Fixed line spacing of 1.5 lines
|
2015-12-09 22:57:50 +03:00
|
|
|
}
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2015-12-09 22:57:50 +03:00
|
|
|
if (tempLen < lenCounter) tempLen = lenCounter;
|
2013-11-23 16:30:54 +04:00
|
|
|
}
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2015-12-09 22:57:50 +03:00
|
|
|
if (tempTextWidth < textWidth) tempTextWidth = textWidth;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
Vector2 vec;
|
2016-12-05 03:14:18 +03:00
|
|
|
vec.x = tempTextWidth*scaleFactor + (float)((tempLen - 1)*spacing); // Adds chars spacing to measure
|
|
|
|
vec.y = textHeight*scaleFactor;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
return vec;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2018-02-18 20:07:57 +03:00
|
|
|
// Returns index position for a unicode character on spritefont
|
2018-05-04 17:59:48 +03:00
|
|
|
int GetGlyphIndex(Font font, int character)
|
2018-02-18 20:07:57 +03:00
|
|
|
{
|
|
|
|
#define UNORDERED_CHARSET
|
|
|
|
#if defined(UNORDERED_CHARSET)
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < font.charsCount; i++)
|
|
|
|
{
|
|
|
|
if (font.chars[i].value == character)
|
|
|
|
{
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
#else
|
|
|
|
return (character - 32);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-10-23 16:43:19 +03:00
|
|
|
// Formatting of text with variables to 'embed'
|
|
|
|
const char *FormatText(const char *text, ...)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2018-10-23 16:43:19 +03:00
|
|
|
static char buffer[MAX_FORMATTEXT_LENGTH];
|
2017-03-05 21:17:00 +03:00
|
|
|
|
2018-10-23 16:43:19 +03:00
|
|
|
va_list args;
|
|
|
|
va_start(args, text);
|
|
|
|
vsprintf(buffer, text, args);
|
|
|
|
va_end(args);
|
2017-03-05 21:17:00 +03:00
|
|
|
|
2018-10-23 16:43:19 +03:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a piece of a text string
|
|
|
|
const char *SubText(const char *text, int position, int length)
|
|
|
|
{
|
|
|
|
static char buffer[MAX_SUBTEXT_LENGTH] = { 0 };
|
|
|
|
int textLength = strlen(text);
|
|
|
|
|
|
|
|
if (position >= textLength)
|
2017-03-05 21:17:00 +03:00
|
|
|
{
|
2018-10-23 16:43:19 +03:00
|
|
|
position = textLength - 1;
|
|
|
|
length = 0;
|
2017-03-05 21:17:00 +03:00
|
|
|
}
|
2018-10-23 16:43:19 +03:00
|
|
|
|
|
|
|
if (length >= textLength) length = textLength;
|
|
|
|
|
|
|
|
for (int c = 0 ; c < length ; c++)
|
|
|
|
{
|
|
|
|
*(buffer + c) = *(text + position);
|
|
|
|
text++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*(buffer + length) = '\0';
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split string into multiple strings
|
|
|
|
// NOTE: Files count is returned by parameters pointer
|
|
|
|
// NOTE: Allocated memory should be manually freed
|
|
|
|
char **SplitText(char *text, char delimiter, int *strCount)
|
|
|
|
{
|
|
|
|
#define MAX_SUBSTRING_LENGTH 128
|
|
|
|
|
|
|
|
char **strings = NULL;
|
|
|
|
int len = strlen(text);
|
|
|
|
char *strDup = (char *)malloc(len + 1);
|
|
|
|
strcpy(strDup, text);
|
|
|
|
int counter = 1;
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-23 16:43:19 +03:00
|
|
|
// Count how many substrings we have on string
|
|
|
|
for (int i = 0; i < len; i++) if (text[i] == delimiter) counter++;
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-23 16:43:19 +03:00
|
|
|
// Memory allocation for substrings
|
|
|
|
strings = (char **)malloc(sizeof(char *)*counter);
|
|
|
|
for (int i = 0; i < counter; i++) strings[i] = (char *)malloc(sizeof(char)*MAX_SUBSTRING_LENGTH);
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-23 16:43:19 +03:00
|
|
|
char *substrPtr = NULL;
|
|
|
|
char delimiters[1] = { delimiter }; // Only caring for one delimiter
|
|
|
|
substrPtr = strtok(strDup, delimiters);
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-23 16:43:19 +03:00
|
|
|
for (int i = 0; (i < counter) && (substrPtr != NULL); i++)
|
|
|
|
{
|
|
|
|
strcpy(strings[i], substrPtr);
|
|
|
|
substrPtr = strtok(NULL, delimiters);
|
|
|
|
}
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-23 16:43:19 +03:00
|
|
|
*strCount = counter;
|
|
|
|
free(strDup);
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-23 16:43:19 +03:00
|
|
|
return strings;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2018-10-29 18:18:06 +03:00
|
|
|
// Check if two text string are equal
|
|
|
|
bool IsEqualText(const char *text1, const char *text2)
|
|
|
|
{
|
|
|
|
bool result = false;
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-29 18:18:06 +03:00
|
|
|
if (strcmp(text1, text2) == 0) result = true;
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-10-29 18:18:06 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module specific Functions Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
2017-03-26 23:49:01 +03:00
|
|
|
#if defined(SUPPORT_FILEFORMAT_FNT)
|
2016-01-02 12:45:51 +03:00
|
|
|
// Load a BMFont file (AngelCode font file)
|
2018-05-04 17:59:48 +03:00
|
|
|
static Font LoadBMFont(const char *fileName)
|
2013-12-19 15:08:06 +04:00
|
|
|
{
|
2016-01-02 12:45:51 +03:00
|
|
|
#define MAX_BUFFER_SIZE 256
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2018-05-04 17:59:48 +03:00
|
|
|
Font font = { 0 };
|
2016-01-02 12:45:51 +03:00
|
|
|
font.texture.id = 0;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2018-10-18 17:00:11 +03:00
|
|
|
char buffer[MAX_BUFFER_SIZE] = { 0 };
|
2016-01-02 12:45:51 +03:00
|
|
|
char *searchPoint = NULL;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
int fontSize = 0;
|
2018-10-18 17:00:11 +03:00
|
|
|
int texWidth = 0;
|
|
|
|
int texHeight = 0;
|
2018-02-25 04:55:32 +03:00
|
|
|
char texFileName[129];
|
2017-02-05 04:59:39 +03:00
|
|
|
int charsCount = 0;
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2018-10-18 17:00:11 +03:00
|
|
|
int base = 0; // Useless data
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2018-10-18 17:00:11 +03:00
|
|
|
FILE *fntFile = NULL;
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
fntFile = fopen(fileName, "rt");
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
if (fntFile == NULL)
|
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_WARNING, "[%s] FNT file could not be opened", fileName);
|
2016-01-02 12:45:51 +03:00
|
|
|
return font;
|
|
|
|
}
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
// NOTE: We skip first line, it contains no useful information
|
|
|
|
fgets(buffer, MAX_BUFFER_SIZE, fntFile);
|
|
|
|
//searchPoint = strstr(buffer, "size");
|
|
|
|
//sscanf(searchPoint, "size=%i", &fontSize);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
fgets(buffer, MAX_BUFFER_SIZE, fntFile);
|
|
|
|
searchPoint = strstr(buffer, "lineHeight");
|
|
|
|
sscanf(searchPoint, "lineHeight=%i base=%i scaleW=%i scaleH=%i", &fontSize, &base, &texWidth, &texHeight);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_DEBUG, "[%s] Font size: %i", fileName, fontSize);
|
|
|
|
TraceLog(LOG_DEBUG, "[%s] Font texture scale: %ix%i", fileName, texWidth, texHeight);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
fgets(buffer, MAX_BUFFER_SIZE, fntFile);
|
|
|
|
searchPoint = strstr(buffer, "file");
|
|
|
|
sscanf(searchPoint, "file=\"%128[^\"]\"", texFileName);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_DEBUG, "[%s] Font texture filename: %s", fileName, texFileName);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
fgets(buffer, MAX_BUFFER_SIZE, fntFile);
|
|
|
|
searchPoint = strstr(buffer, "count");
|
2017-02-05 04:59:39 +03:00
|
|
|
sscanf(searchPoint, "count=%i", &charsCount);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_DEBUG, "[%s] Font num chars: %i", fileName, charsCount);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
// Compose correct path using route of .fnt file (fileName) and texFileName
|
|
|
|
char *texPath = NULL;
|
|
|
|
char *lastSlash = NULL;
|
|
|
|
|
2016-07-06 21:02:15 +03:00
|
|
|
lastSlash = strrchr(fileName, '/');
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-07-06 21:02:15 +03:00
|
|
|
// NOTE: We need some extra space to avoid memory corruption on next allocations!
|
|
|
|
texPath = malloc(strlen(fileName) - strlen(lastSlash) + strlen(texFileName) + 4);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-07-06 21:02:15 +03:00
|
|
|
// NOTE: strcat() and strncat() required a '\0' terminated string to work!
|
|
|
|
*texPath = '\0';
|
|
|
|
strncat(texPath, fileName, strlen(fileName) - strlen(lastSlash) + 1);
|
|
|
|
strncat(texPath, texFileName, strlen(texFileName));
|
2016-01-02 12:45:51 +03:00
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_DEBUG, "[%s] Font texture loading path: %s", fileName, texPath);
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-10-24 20:11:29 +03:00
|
|
|
Image imFont = LoadImage(texPath);
|
2017-01-29 01:02:30 +03:00
|
|
|
|
|
|
|
if (imFont.format == UNCOMPRESSED_GRAYSCALE)
|
2016-11-21 21:47:58 +03:00
|
|
|
{
|
|
|
|
Image imCopy = ImageCopy(imFont);
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2018-05-20 01:37:16 +03:00
|
|
|
for (int i = 0; i < imCopy.width*imCopy.height; i++) ((unsigned char *)imCopy.data)[i] = 0xff;
|
2016-10-24 20:11:29 +03:00
|
|
|
|
2016-11-21 21:47:58 +03:00
|
|
|
ImageAlphaMask(&imCopy, imFont);
|
|
|
|
font.texture = LoadTextureFromImage(imCopy);
|
|
|
|
UnloadImage(imCopy);
|
|
|
|
}
|
|
|
|
else font.texture = LoadTextureFromImage(imFont);
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2018-05-20 01:37:16 +03:00
|
|
|
UnloadImage(imFont);
|
|
|
|
free(texPath);
|
2018-11-06 17:10:50 +03:00
|
|
|
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2018-05-20 01:37:16 +03:00
|
|
|
// Fill font characters info data
|
2017-02-05 04:59:39 +03:00
|
|
|
font.baseSize = fontSize;
|
|
|
|
font.charsCount = charsCount;
|
|
|
|
font.chars = (CharInfo *)malloc(charsCount*sizeof(CharInfo));
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
int charId, charX, charY, charWidth, charHeight, charOffsetX, charOffsetY, charAdvanceX;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2017-02-05 04:59:39 +03:00
|
|
|
for (int i = 0; i < charsCount; i++)
|
2016-01-02 12:45:51 +03:00
|
|
|
{
|
|
|
|
fgets(buffer, MAX_BUFFER_SIZE, fntFile);
|
2016-08-16 12:09:55 +03:00
|
|
|
sscanf(buffer, "char id=%i x=%i y=%i width=%i height=%i xoffset=%i yoffset=%i xadvance=%i",
|
2016-01-02 12:45:51 +03:00
|
|
|
&charId, &charX, &charY, &charWidth, &charHeight, &charOffsetX, &charOffsetY, &charAdvanceX);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
// Save data properly in sprite font
|
2017-02-05 04:59:39 +03:00
|
|
|
font.chars[i].value = charId;
|
2018-08-06 21:49:47 +03:00
|
|
|
font.chars[i].rec = (Rectangle){ (float)charX, (float)charY, (float)charWidth, (float)charHeight };
|
2017-02-05 04:59:39 +03:00
|
|
|
font.chars[i].offsetX = charOffsetX;
|
|
|
|
font.chars[i].offsetY = charOffsetY;
|
|
|
|
font.chars[i].advanceX = charAdvanceX;
|
2016-01-02 12:45:51 +03:00
|
|
|
}
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
fclose(fntFile);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-11-01 02:58:21 +03:00
|
|
|
if (font.texture.id == 0)
|
2016-03-02 20:35:30 +03:00
|
|
|
{
|
2018-05-04 17:59:48 +03:00
|
|
|
UnloadFont(font);
|
2018-07-05 20:17:06 +03:00
|
|
|
font = GetFontDefault();
|
2016-03-02 20:35:30 +03:00
|
|
|
}
|
2018-05-04 17:59:48 +03:00
|
|
|
else TraceLog(LOG_INFO, "[%s] Font loaded successfully", fileName);
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2016-01-02 12:45:51 +03:00
|
|
|
return font;
|
|
|
|
}
|
2018-06-20 01:52:14 +03:00
|
|
|
#endif
|