rtext.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578
  1. /**********************************************************************************************
  2. *
  3. * rtext - Basic functions to load fonts and draw text
  4. *
  5. * CONFIGURATION:
  6. * #define SUPPORT_MODULE_RTEXT
  7. * rtext module is included in the build
  8. *
  9. * #define SUPPORT_DEFAULT_FONT
  10. * Load default raylib font on initialization to be used by DrawText() and MeasureText().
  11. * If no default font loaded, DrawTextEx() and MeasureTextEx() are required.
  12. *
  13. * #define SUPPORT_FILEFORMAT_FNT
  14. * #define SUPPORT_FILEFORMAT_TTF
  15. * #define SUPPORT_FILEFORMAT_BDF
  16. * Selected desired fileformats to be supported for loading. Some of those formats are
  17. * supported by default, to remove support, just comment unrequired #define in this module
  18. *
  19. * #define SUPPORT_FONT_ATLAS_WHITE_REC
  20. * On font atlas image generation [GenImageFontAtlas()], add a 3x3 pixels white rectangle
  21. * at the bottom-right corner of the atlas. It can be useful to for shapes drawing, to allow
  22. * drawing text and shapes with a single draw call [SetShapesTexture()].
  23. *
  24. * #define TEXTSPLIT_MAX_TEXT_BUFFER_LENGTH
  25. * TextSplit() function static buffer max size
  26. *
  27. * #define MAX_TEXTSPLIT_COUNT
  28. * TextSplit() function static substrings pointers array (pointing to static buffer)
  29. *
  30. * DEPENDENCIES:
  31. * stb_truetype - Load TTF file and rasterize characters data
  32. * stb_rect_pack - Rectangles packing algorithms, required for font atlas generation
  33. *
  34. *
  35. * LICENSE: zlib/libpng
  36. *
  37. * Copyright (c) 2013-2025 Ramon Santamaria (@raysan5)
  38. *
  39. * This software is provided "as-is", without any express or implied warranty. In no event
  40. * will the authors be held liable for any damages arising from the use of this software.
  41. *
  42. * Permission is granted to anyone to use this software for any purpose, including commercial
  43. * applications, and to alter it and redistribute it freely, subject to the following restrictions:
  44. *
  45. * 1. The origin of this software must not be misrepresented; you must not claim that you
  46. * wrote the original software. If you use this software in a product, an acknowledgment
  47. * in the product documentation would be appreciated but is not required.
  48. *
  49. * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
  50. * as being the original software.
  51. *
  52. * 3. This notice may not be removed or altered from any source distribution.
  53. *
  54. **********************************************************************************************/
  55. #include "raylib.h" // Declares module functions
  56. // Check if config flags have been externally provided on compilation line
  57. #if !defined(EXTERNAL_CONFIG_FLAGS)
  58. #include "config.h" // Defines module configuration flags
  59. #endif
  60. #if defined(SUPPORT_MODULE_RTEXT)
  61. #include "utils.h" // Required for: LoadFile*()
  62. #include "rlgl.h" // OpenGL abstraction layer to OpenGL 1.1, 2.1, 3.3+ or ES2 -> Only DrawTextPro()
  63. #include <stdlib.h> // Required for: malloc(), free()
  64. #include <stdio.h> // Required for: vsprintf()
  65. #include <string.h> // Required for: strcmp(), strstr(), strcpy(), strncpy() [Used in TextReplace()], sscanf() [Used in LoadBMFont()]
  66. #include <stdarg.h> // Required for: va_list, va_start(), vsprintf(), va_end() [Used in TextFormat()]
  67. #include <ctype.h> // Required for: toupper(), tolower() [Used in TextToUpper(), TextToLower()]
  68. #if defined(SUPPORT_FILEFORMAT_TTF) || defined(SUPPORT_FILEFORMAT_BDF)
  69. #if defined(__GNUC__) // GCC and Clang
  70. #pragma GCC diagnostic push
  71. #pragma GCC diagnostic ignored "-Wunused-function"
  72. #endif
  73. #define STB_RECT_PACK_IMPLEMENTATION
  74. #include "external/stb_rect_pack.h" // Required for: ttf/bdf font rectangles packaging
  75. #include <math.h> // Required for: ttf/bdf font rectangles packaging
  76. #if defined(__GNUC__) // GCC and Clang
  77. #pragma GCC diagnostic pop
  78. #endif
  79. #endif
  80. #if defined(SUPPORT_FILEFORMAT_TTF)
  81. #if defined(__GNUC__) // GCC and Clang
  82. #pragma GCC diagnostic push
  83. #pragma GCC diagnostic ignored "-Wunused-function"
  84. #endif
  85. #define STBTT_STATIC
  86. #define STB_TRUETYPE_IMPLEMENTATION
  87. #include "external/stb_truetype.h" // Required for: ttf font data reading
  88. #if defined(__GNUC__) // GCC and Clang
  89. #pragma GCC diagnostic pop
  90. #endif
  91. #endif
  92. //----------------------------------------------------------------------------------
  93. // Defines and Macros
  94. //----------------------------------------------------------------------------------
  95. #ifndef MAX_TEXT_BUFFER_LENGTH
  96. #define MAX_TEXT_BUFFER_LENGTH 1024 // Size of internal static buffers used on some functions:
  97. // TextFormat(), TextSubtext(), TextToUpper(), TextToLower(), TextToPascal(), TextSplit()
  98. #endif
  99. #ifndef MAX_TEXT_UNICODE_CHARS
  100. #define MAX_TEXT_UNICODE_CHARS 512 // Maximum number of unicode codepoints: GetCodepoints()
  101. #endif
  102. #ifndef MAX_TEXTSPLIT_COUNT
  103. #define MAX_TEXTSPLIT_COUNT 128 // Maximum number of substrings to split: TextSplit()
  104. #endif
  105. //----------------------------------------------------------------------------------
  106. // Types and Structures Definition
  107. //----------------------------------------------------------------------------------
  108. //...
  109. //----------------------------------------------------------------------------------
  110. // Global variables
  111. //----------------------------------------------------------------------------------
  112. extern bool isGpuReady;
  113. #if defined(SUPPORT_DEFAULT_FONT)
  114. // Default font provided by raylib
  115. // NOTE: Default font is loaded on InitWindow() and disposed on CloseWindow() [module: core]
  116. static Font defaultFont = { 0 };
  117. #endif
  118. //----------------------------------------------------------------------------------
  119. // Other Modules Functions Declaration (required by text)
  120. //----------------------------------------------------------------------------------
  121. //...
  122. //----------------------------------------------------------------------------------
  123. // Module specific Functions Declaration
  124. //----------------------------------------------------------------------------------
  125. #if defined(SUPPORT_FILEFORMAT_FNT)
  126. static Font LoadBMFont(const char *fileName); // Load a BMFont file (AngelCode font file)
  127. #endif
  128. #if defined(SUPPORT_FILEFORMAT_BDF)
  129. static GlyphInfo *LoadFontDataBDF(const unsigned char *fileData, int dataSize, int *codepoints, int codepointCount, int *outFontSize);
  130. #endif
  131. static int textLineSpacing = 2; // Text vertical line spacing in pixels (between lines)
  132. #if defined(SUPPORT_DEFAULT_FONT)
  133. extern void LoadFontDefault(void);
  134. extern void UnloadFontDefault(void);
  135. #endif
  136. //----------------------------------------------------------------------------------
  137. // Module Functions Definition
  138. //----------------------------------------------------------------------------------
  139. #if defined(SUPPORT_DEFAULT_FONT)
  140. // Load raylib default font
  141. extern void LoadFontDefault(void)
  142. {
  143. #define BIT_CHECK(a,b) ((a) & (1u << (b)))
  144. // NOTE: Using UTF-8 encoding table for Unicode U+0000..U+00FF Basic Latin + Latin-1 Supplement
  145. // Ref: http://www.utf8-chartable.de/unicode-utf8-table.pl
  146. defaultFont.glyphCount = 224; // Number of chars included in our default font
  147. defaultFont.glyphPadding = 0; // Characters padding
  148. // Default font is directly defined here (data generated from a sprite font image)
  149. // This way, we reconstruct Font without creating large global variables
  150. // This data is automatically allocated to Stack and automatically deallocated at the end of this function
  151. unsigned int defaultFontData[512] = {
  152. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00200020, 0x0001b000, 0x00000000, 0x00000000, 0x8ef92520, 0x00020a00, 0x7dbe8000, 0x1f7df45f,
  153. 0x4a2bf2a0, 0x0852091e, 0x41224000, 0x10041450, 0x2e292020, 0x08220812, 0x41222000, 0x10041450, 0x10f92020, 0x3efa084c, 0x7d22103c, 0x107df7de,
  154. 0xe8a12020, 0x08220832, 0x05220800, 0x10450410, 0xa4a3f000, 0x08520832, 0x05220400, 0x10450410, 0xe2f92020, 0x0002085e, 0x7d3e0281, 0x107df41f,
  155. 0x00200000, 0x8001b000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  156. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0xc0000fbe, 0xfbf7e00f, 0x5fbf7e7d, 0x0050bee8, 0x440808a2, 0x0a142fe8, 0x50810285, 0x0050a048,
  157. 0x49e428a2, 0x0a142828, 0x40810284, 0x0048a048, 0x10020fbe, 0x09f7ebaf, 0xd89f3e84, 0x0047a04f, 0x09e48822, 0x0a142aa1, 0x50810284, 0x0048a048,
  158. 0x04082822, 0x0a142fa0, 0x50810285, 0x0050a248, 0x00008fbe, 0xfbf42021, 0x5f817e7d, 0x07d09ce8, 0x00008000, 0x00000fe0, 0x00000000, 0x00000000,
  159. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x000c0180,
  160. 0xdfbf4282, 0x0bfbf7ef, 0x42850505, 0x004804bf, 0x50a142c6, 0x08401428, 0x42852505, 0x00a808a0, 0x50a146aa, 0x08401428, 0x42852505, 0x00081090,
  161. 0x5fa14a92, 0x0843f7e8, 0x7e792505, 0x00082088, 0x40a15282, 0x08420128, 0x40852489, 0x00084084, 0x40a16282, 0x0842022a, 0x40852451, 0x00088082,
  162. 0xc0bf4282, 0xf843f42f, 0x7e85fc21, 0x3e0900bf, 0x00000000, 0x00000004, 0x00000000, 0x000c0180, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  163. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x04000402, 0x41482000, 0x00000000, 0x00000800,
  164. 0x04000404, 0x4100203c, 0x00000000, 0x00000800, 0xf7df7df0, 0x514bef85, 0xbefbefbe, 0x04513bef, 0x14414500, 0x494a2885, 0xa28a28aa, 0x04510820,
  165. 0xf44145f0, 0x474a289d, 0xa28a28aa, 0x04510be0, 0x14414510, 0x494a2884, 0xa28a28aa, 0x02910a00, 0xf7df7df0, 0xd14a2f85, 0xbefbe8aa, 0x011f7be0,
  166. 0x00000000, 0x00400804, 0x20080000, 0x00000000, 0x00000000, 0x00600f84, 0x20080000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  167. 0xac000000, 0x00000f01, 0x00000000, 0x00000000, 0x24000000, 0x00000f01, 0x00000000, 0x06000000, 0x24000000, 0x00000f01, 0x00000000, 0x09108000,
  168. 0x24fa28a2, 0x00000f01, 0x00000000, 0x013e0000, 0x2242252a, 0x00000f52, 0x00000000, 0x038a8000, 0x2422222a, 0x00000f29, 0x00000000, 0x010a8000,
  169. 0x2412252a, 0x00000f01, 0x00000000, 0x010a8000, 0x24fbe8be, 0x00000f01, 0x00000000, 0x0ebe8000, 0xac020000, 0x00000f01, 0x00000000, 0x00048000,
  170. 0x0003e000, 0x00000f00, 0x00000000, 0x00008000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000038, 0x8443b80e, 0x00203a03,
  171. 0x02bea080, 0xf0000020, 0xc452208a, 0x04202b02, 0xf8029122, 0x07f0003b, 0xe44b388e, 0x02203a02, 0x081e8a1c, 0x0411e92a, 0xf4420be0, 0x01248202,
  172. 0xe8140414, 0x05d104ba, 0xe7c3b880, 0x00893a0a, 0x283c0e1c, 0x04500902, 0xc4400080, 0x00448002, 0xe8208422, 0x04500002, 0x80400000, 0x05200002,
  173. 0x083e8e00, 0x04100002, 0x804003e0, 0x07000042, 0xf8008400, 0x07f00003, 0x80400000, 0x04000022, 0x00000000, 0x00000000, 0x80400000, 0x04000002,
  174. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00800702, 0x1848a0c2, 0x84010000, 0x02920921, 0x01042642, 0x00005121, 0x42023f7f, 0x00291002,
  175. 0xefc01422, 0x7efdfbf7, 0xefdfa109, 0x03bbbbf7, 0x28440f12, 0x42850a14, 0x20408109, 0x01111010, 0x28440408, 0x42850a14, 0x2040817f, 0x01111010,
  176. 0xefc78204, 0x7efdfbf7, 0xe7cf8109, 0x011111f3, 0x2850a932, 0x42850a14, 0x2040a109, 0x01111010, 0x2850b840, 0x42850a14, 0xefdfbf79, 0x03bbbbf7,
  177. 0x001fa020, 0x00000000, 0x00001000, 0x00000000, 0x00002070, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  178. 0x08022800, 0x00012283, 0x02430802, 0x01010001, 0x8404147c, 0x20000144, 0x80048404, 0x00823f08, 0xdfbf4284, 0x7e03f7ef, 0x142850a1, 0x0000210a,
  179. 0x50a14684, 0x528a1428, 0x142850a1, 0x03efa17a, 0x50a14a9e, 0x52521428, 0x142850a1, 0x02081f4a, 0x50a15284, 0x4a221428, 0xf42850a1, 0x03efa14b,
  180. 0x50a16284, 0x4a521428, 0x042850a1, 0x0228a17a, 0xdfbf427c, 0x7e8bf7ef, 0xf7efdfbf, 0x03efbd0b, 0x00000000, 0x04000000, 0x00000000, 0x00000008,
  181. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00200508, 0x00840400, 0x11458122, 0x00014210,
  182. 0x00514294, 0x51420800, 0x20a22a94, 0x0050a508, 0x00200000, 0x00000000, 0x00050000, 0x08000000, 0xfefbefbe, 0xfbefbefb, 0xfbeb9114, 0x00fbefbe,
  183. 0x20820820, 0x8a28a20a, 0x8a289114, 0x3e8a28a2, 0xfefbefbe, 0xfbefbe0b, 0x8a289114, 0x008a28a2, 0x228a28a2, 0x08208208, 0x8a289114, 0x088a28a2,
  184. 0xfefbefbe, 0xfbefbefb, 0xfa2f9114, 0x00fbefbe, 0x00000000, 0x00000040, 0x00000000, 0x00000000, 0x00000000, 0x00000020, 0x00000000, 0x00000000,
  185. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00210100, 0x00000004, 0x00000000, 0x00000000, 0x14508200, 0x00001402, 0x00000000, 0x00000000,
  186. 0x00000010, 0x00000020, 0x00000000, 0x00000000, 0xa28a28be, 0x00002228, 0x00000000, 0x00000000, 0xa28a28aa, 0x000022e8, 0x00000000, 0x00000000,
  187. 0xa28a28aa, 0x000022a8, 0x00000000, 0x00000000, 0xa28a28aa, 0x000022e8, 0x00000000, 0x00000000, 0xbefbefbe, 0x00003e2f, 0x00000000, 0x00000000,
  188. 0x00000004, 0x00002028, 0x00000000, 0x00000000, 0x80000000, 0x00003e0f, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  189. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  190. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  191. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  192. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  193. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  194. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 };
  195. int charsHeight = 10;
  196. int charsDivisor = 1; // Every char is separated from the consecutive by a 1 pixel divisor, horizontally and vertically
  197. 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,
  198. 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,
  199. 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,
  200. 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,
  201. 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,
  202. 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,
  203. 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 };
  204. // Re-construct image from defaultFontData and generate OpenGL texture
  205. //----------------------------------------------------------------------
  206. Image imFont = {
  207. .data = RL_CALLOC(128*128, 2), // 2 bytes per pixel (gray + alpha)
  208. .width = 128,
  209. .height = 128,
  210. .mipmaps = 1,
  211. .format = PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA
  212. };
  213. // Fill image.data with defaultFontData (convert from bit to pixel!)
  214. for (int i = 0, counter = 0; i < imFont.width*imFont.height; i += 32)
  215. {
  216. for (int j = 31; j >= 0; j--)
  217. {
  218. if (BIT_CHECK(defaultFontData[counter], j))
  219. {
  220. // NOTE: We are unreferencing data as short, so,
  221. // we must consider data as little-endian order (alpha + gray)
  222. ((unsigned short *)imFont.data)[i + j] = 0xffff;
  223. }
  224. else
  225. {
  226. ((unsigned char *)imFont.data)[(i + j)*sizeof(short)] = 0xFF;
  227. ((unsigned char *)imFont.data)[(i + j)*sizeof(short) + 1] = 0x00;
  228. }
  229. }
  230. counter++;
  231. }
  232. if (isGpuReady) defaultFont.texture = LoadTextureFromImage(imFont);
  233. // Reconstruct charSet using charsWidth[], charsHeight, charsDivisor, glyphCount
  234. //------------------------------------------------------------------------------
  235. // Allocate space for our characters info data
  236. // NOTE: This memory must be freed at end! --> Done by CloseWindow()
  237. defaultFont.glyphs = (GlyphInfo *)RL_CALLOC(defaultFont.glyphCount, sizeof(GlyphInfo));
  238. defaultFont.recs = (Rectangle *)RL_CALLOC(defaultFont.glyphCount, sizeof(Rectangle));
  239. int currentLine = 0;
  240. int currentPosX = charsDivisor;
  241. int testPosX = charsDivisor;
  242. for (int i = 0; i < defaultFont.glyphCount; i++)
  243. {
  244. defaultFont.glyphs[i].value = 32 + i; // First char is 32
  245. defaultFont.recs[i].x = (float)currentPosX;
  246. defaultFont.recs[i].y = (float)(charsDivisor + currentLine*(charsHeight + charsDivisor));
  247. defaultFont.recs[i].width = (float)charsWidth[i];
  248. defaultFont.recs[i].height = (float)charsHeight;
  249. testPosX += (int)(defaultFont.recs[i].width + (float)charsDivisor);
  250. if (testPosX >= defaultFont.texture.width)
  251. {
  252. currentLine++;
  253. currentPosX = 2*charsDivisor + charsWidth[i];
  254. testPosX = currentPosX;
  255. defaultFont.recs[i].x = (float)charsDivisor;
  256. defaultFont.recs[i].y = (float)(charsDivisor + currentLine*(charsHeight + charsDivisor));
  257. }
  258. else currentPosX = testPosX;
  259. // NOTE: On default font character offsets and xAdvance are not required
  260. defaultFont.glyphs[i].offsetX = 0;
  261. defaultFont.glyphs[i].offsetY = 0;
  262. defaultFont.glyphs[i].advanceX = 0;
  263. // Fill character image data from fontClear data
  264. defaultFont.glyphs[i].image = ImageFromImage(imFont, defaultFont.recs[i]);
  265. }
  266. UnloadImage(imFont);
  267. defaultFont.baseSize = (int)defaultFont.recs[0].height;
  268. TRACELOG(LOG_INFO, "FONT: Default font loaded successfully (%i glyphs)", defaultFont.glyphCount);
  269. }
  270. // Unload raylib default font
  271. extern void UnloadFontDefault(void)
  272. {
  273. for (int i = 0; i < defaultFont.glyphCount; i++) UnloadImage(defaultFont.glyphs[i].image);
  274. if (isGpuReady) UnloadTexture(defaultFont.texture);
  275. RL_FREE(defaultFont.glyphs);
  276. RL_FREE(defaultFont.recs);
  277. }
  278. #endif // SUPPORT_DEFAULT_FONT
  279. // Get the default font, useful to be used with extended parameters
  280. Font GetFontDefault()
  281. {
  282. #if defined(SUPPORT_DEFAULT_FONT)
  283. return defaultFont;
  284. #else
  285. Font font = { 0 };
  286. return font;
  287. #endif
  288. }
  289. // Load Font from file into GPU memory (VRAM)
  290. Font LoadFont(const char *fileName)
  291. {
  292. // Default values for ttf font generation
  293. #ifndef FONT_TTF_DEFAULT_SIZE
  294. #define FONT_TTF_DEFAULT_SIZE 32 // TTF font generation default char size (char-height)
  295. #endif
  296. #ifndef FONT_TTF_DEFAULT_NUMCHARS
  297. #define FONT_TTF_DEFAULT_NUMCHARS 95 // TTF font generation default charset: 95 glyphs (ASCII 32..126)
  298. #endif
  299. #ifndef FONT_TTF_DEFAULT_FIRST_CHAR
  300. #define FONT_TTF_DEFAULT_FIRST_CHAR 32 // TTF font generation default first char for image sprite font (32-Space)
  301. #endif
  302. #ifndef FONT_TTF_DEFAULT_CHARS_PADDING
  303. #define FONT_TTF_DEFAULT_CHARS_PADDING 4 // TTF font generation default chars padding
  304. #endif
  305. Font font = { 0 };
  306. #if defined(SUPPORT_FILEFORMAT_TTF)
  307. if (IsFileExtension(fileName, ".ttf") || IsFileExtension(fileName, ".otf")) font = LoadFontEx(fileName, FONT_TTF_DEFAULT_SIZE, NULL, FONT_TTF_DEFAULT_NUMCHARS);
  308. else
  309. #endif
  310. #if defined(SUPPORT_FILEFORMAT_FNT)
  311. if (IsFileExtension(fileName, ".fnt")) font = LoadBMFont(fileName);
  312. else
  313. #endif
  314. #if defined(SUPPORT_FILEFORMAT_BDF)
  315. if (IsFileExtension(fileName, ".bdf")) font = LoadFontEx(fileName, FONT_TTF_DEFAULT_SIZE, NULL, FONT_TTF_DEFAULT_NUMCHARS);
  316. else
  317. #endif
  318. {
  319. Image image = LoadImage(fileName);
  320. if (image.data != NULL) font = LoadFontFromImage(image, MAGENTA, FONT_TTF_DEFAULT_FIRST_CHAR);
  321. UnloadImage(image);
  322. }
  323. if (isGpuReady)
  324. {
  325. if (font.texture.id == 0) TRACELOG(LOG_WARNING, "FONT: [%s] Failed to load font texture -> Using default font", fileName);
  326. else
  327. {
  328. SetTextureFilter(font.texture, TEXTURE_FILTER_POINT); // By default, we set point filter (the best performance)
  329. TRACELOG(LOG_INFO, "FONT: Data loaded successfully (%i pixel size | %i glyphs)", FONT_TTF_DEFAULT_SIZE, FONT_TTF_DEFAULT_NUMCHARS);
  330. }
  331. }
  332. return font;
  333. }
  334. // Load Font from TTF or BDF font file with generation parameters
  335. // NOTE: You can pass an array with desired characters, those characters should be available in the font
  336. // if array is NULL, default char set is selected 32..126
  337. Font LoadFontEx(const char *fileName, int fontSize, int *codepoints, int codepointCount)
  338. {
  339. Font font = { 0 };
  340. // Loading file to memory
  341. int dataSize = 0;
  342. unsigned char *fileData = LoadFileData(fileName, &dataSize);
  343. if (fileData != NULL)
  344. {
  345. // Loading font from memory data
  346. font = LoadFontFromMemory(GetFileExtension(fileName), fileData, dataSize, fontSize, codepoints, codepointCount);
  347. UnloadFileData(fileData);
  348. }
  349. return font;
  350. }
  351. // Load an Image font file (XNA style)
  352. Font LoadFontFromImage(Image image, Color key, int firstChar)
  353. {
  354. #ifndef MAX_GLYPHS_FROM_IMAGE
  355. #define MAX_GLYPHS_FROM_IMAGE 256 // Maximum number of glyphs supported on image scan
  356. #endif
  357. #define COLOR_EQUAL(col1, col2) ((col1.r == col2.r) && (col1.g == col2.g) && (col1.b == col2.b) && (col1.a == col2.a))
  358. Font font = GetFontDefault();
  359. int charSpacing = 0;
  360. int lineSpacing = 0;
  361. int x = 0;
  362. int y = 0;
  363. // We allocate a temporal arrays for chars data measures,
  364. // once we get the actual number of chars, we copy data to a sized arrays
  365. int tempCharValues[MAX_GLYPHS_FROM_IMAGE] = { 0 };
  366. Rectangle tempCharRecs[MAX_GLYPHS_FROM_IMAGE] = { 0 };
  367. Color *pixels = LoadImageColors(image);
  368. // Parse image data to get charSpacing and lineSpacing
  369. for (y = 0; y < image.height; y++)
  370. {
  371. for (x = 0; x < image.width; x++)
  372. {
  373. if (!COLOR_EQUAL(pixels[y*image.width + x], key)) break;
  374. }
  375. if (!COLOR_EQUAL(pixels[y*image.width + x], key)) break;
  376. }
  377. if ((x == 0) || (y == 0)) return font; // Security check
  378. charSpacing = x;
  379. lineSpacing = y;
  380. int charHeight = 0;
  381. int j = 0;
  382. while (!COLOR_EQUAL(pixels[(lineSpacing + j)*image.width + charSpacing], key)) j++;
  383. charHeight = j;
  384. // Check array values to get characters: value, x, y, w, h
  385. int index = 0;
  386. int lineToRead = 0;
  387. int xPosToRead = charSpacing;
  388. // Parse image data to get rectangle sizes
  389. while ((lineSpacing + lineToRead*(charHeight + lineSpacing)) < image.height)
  390. {
  391. while ((xPosToRead < image.width) &&
  392. !COLOR_EQUAL((pixels[(lineSpacing + (charHeight+lineSpacing)*lineToRead)*image.width + xPosToRead]), key))
  393. {
  394. tempCharValues[index] = firstChar + index;
  395. tempCharRecs[index].x = (float)xPosToRead;
  396. tempCharRecs[index].y = (float)(lineSpacing + lineToRead*(charHeight + lineSpacing));
  397. tempCharRecs[index].height = (float)charHeight;
  398. int charWidth = 0;
  399. while (!COLOR_EQUAL(pixels[(lineSpacing + (charHeight+lineSpacing)*lineToRead)*image.width + xPosToRead + charWidth], key)) charWidth++;
  400. tempCharRecs[index].width = (float)charWidth;
  401. index++;
  402. xPosToRead += (charWidth + charSpacing);
  403. }
  404. lineToRead++;
  405. xPosToRead = charSpacing;
  406. }
  407. // NOTE: We need to remove key color borders from image to avoid weird
  408. // artifacts on texture scaling when using TEXTURE_FILTER_BILINEAR or TEXTURE_FILTER_TRILINEAR
  409. for (int i = 0; i < image.height*image.width; i++) if (COLOR_EQUAL(pixels[i], key)) pixels[i] = BLANK;
  410. // Create a new image with the processed color data (key color replaced by BLANK)
  411. Image fontClear = {
  412. .data = pixels,
  413. .width = image.width,
  414. .height = image.height,
  415. .mipmaps = 1,
  416. .format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8
  417. };
  418. // Set font with all data parsed from image
  419. if (isGpuReady) font.texture = LoadTextureFromImage(fontClear); // Convert processed image to OpenGL texture
  420. font.glyphCount = index;
  421. font.glyphPadding = 0;
  422. // We got tempCharValues and tempCharsRecs populated with chars data
  423. // Now we move temp data to sized charValues and charRecs arrays
  424. font.glyphs = (GlyphInfo *)RL_MALLOC(font.glyphCount*sizeof(GlyphInfo));
  425. font.recs = (Rectangle *)RL_MALLOC(font.glyphCount*sizeof(Rectangle));
  426. for (int i = 0; i < font.glyphCount; i++)
  427. {
  428. font.glyphs[i].value = tempCharValues[i];
  429. // Get character rectangle in the font atlas texture
  430. font.recs[i] = tempCharRecs[i];
  431. // NOTE: On image based fonts (XNA style), character offsets and xAdvance are not required (set to 0)
  432. font.glyphs[i].offsetX = 0;
  433. font.glyphs[i].offsetY = 0;
  434. font.glyphs[i].advanceX = 0;
  435. // Fill character image data from fontClear data
  436. font.glyphs[i].image = ImageFromImage(fontClear, tempCharRecs[i]);
  437. }
  438. UnloadImage(fontClear); // Unload processed image once converted to texture
  439. font.baseSize = (int)font.recs[0].height;
  440. return font;
  441. }
  442. // Load font from memory buffer, fileType refers to extension: i.e. ".ttf"
  443. Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int dataSize, int fontSize, int *codepoints, int codepointCount)
  444. {
  445. Font font = { 0 };
  446. char fileExtLower[16] = { 0 };
  447. strncpy(fileExtLower, TextToLower(fileType), 16 - 1);
  448. font.baseSize = fontSize;
  449. font.glyphCount = (codepointCount > 0)? codepointCount : 95;
  450. font.glyphPadding = 0;
  451. #if defined(SUPPORT_FILEFORMAT_TTF)
  452. if (TextIsEqual(fileExtLower, ".ttf") ||
  453. TextIsEqual(fileExtLower, ".otf"))
  454. {
  455. font.glyphs = LoadFontData(fileData, dataSize, font.baseSize, codepoints, font.glyphCount, FONT_DEFAULT);
  456. }
  457. else
  458. #endif
  459. #if defined(SUPPORT_FILEFORMAT_BDF)
  460. if (TextIsEqual(fileExtLower, ".bdf"))
  461. {
  462. font.glyphs = LoadFontDataBDF(fileData, dataSize, codepoints, font.glyphCount, &font.baseSize);
  463. }
  464. else
  465. #endif
  466. {
  467. font.glyphs = NULL;
  468. }
  469. #if defined(SUPPORT_FILEFORMAT_TTF) || defined(SUPPORT_FILEFORMAT_BDF)
  470. if (font.glyphs != NULL)
  471. {
  472. font.glyphPadding = FONT_TTF_DEFAULT_CHARS_PADDING;
  473. Image atlas = GenImageFontAtlas(font.glyphs, &font.recs, font.glyphCount, font.baseSize, font.glyphPadding, 0);
  474. if (isGpuReady) font.texture = LoadTextureFromImage(atlas);
  475. // Update glyphs[i].image to use alpha, required to be used on ImageDrawText()
  476. for (int i = 0; i < font.glyphCount; i++)
  477. {
  478. UnloadImage(font.glyphs[i].image);
  479. font.glyphs[i].image = ImageFromImage(atlas, font.recs[i]);
  480. }
  481. UnloadImage(atlas);
  482. TRACELOG(LOG_INFO, "FONT: Data loaded successfully (%i pixel size | %i glyphs)", font.baseSize, font.glyphCount);
  483. }
  484. else font = GetFontDefault();
  485. #else
  486. font = GetFontDefault();
  487. #endif
  488. return font;
  489. }
  490. // Check if a font is valid (font data loaded)
  491. // WARNING: GPU texture not checked
  492. bool IsFontValid(Font font)
  493. {
  494. return ((font.baseSize > 0) && // Validate font size
  495. (font.glyphCount > 0) && // Validate font contains some glyph
  496. (font.recs != NULL) && // Validate font recs defining glyphs on texture atlas
  497. (font.glyphs != NULL)); // Validate glyph data is loaded
  498. // NOTE: Further validations could be done to verify if recs and glyphs contain valid data (glyphs values, metrics...)
  499. }
  500. // Load font data for further use
  501. // NOTE: Requires TTF font memory data and can generate SDF data
  502. GlyphInfo *LoadFontData(const unsigned char *fileData, int dataSize, int fontSize, int *codepoints, int codepointCount, int type)
  503. {
  504. // NOTE: Using some SDF generation default values,
  505. // trades off precision with ability to handle *smaller* sizes
  506. #ifndef FONT_SDF_CHAR_PADDING
  507. #define FONT_SDF_CHAR_PADDING 4 // SDF font generation char padding
  508. #endif
  509. #ifndef FONT_SDF_ON_EDGE_VALUE
  510. #define FONT_SDF_ON_EDGE_VALUE 128 // SDF font generation on edge value
  511. #endif
  512. #ifndef FONT_SDF_PIXEL_DIST_SCALE
  513. #define FONT_SDF_PIXEL_DIST_SCALE 64.0f // SDF font generation pixel distance scale
  514. #endif
  515. #ifndef FONT_BITMAP_ALPHA_THRESHOLD
  516. #define FONT_BITMAP_ALPHA_THRESHOLD 80 // Bitmap (B&W) font generation alpha threshold
  517. #endif
  518. GlyphInfo *chars = NULL;
  519. #if defined(SUPPORT_FILEFORMAT_TTF)
  520. // Load font data (including pixel data) from TTF memory file
  521. // NOTE: Loaded information should be enough to generate font image atlas, using any packaging method
  522. if (fileData != NULL)
  523. {
  524. bool genFontChars = false;
  525. stbtt_fontinfo fontInfo = { 0 };
  526. if (stbtt_InitFont(&fontInfo, (unsigned char *)fileData, 0)) // Initialize font for data reading
  527. {
  528. // Calculate font scale factor
  529. float scaleFactor = stbtt_ScaleForPixelHeight(&fontInfo, (float)fontSize);
  530. // Calculate font basic metrics
  531. // NOTE: ascent is equivalent to font baseline
  532. int ascent, descent, lineGap;
  533. stbtt_GetFontVMetrics(&fontInfo, &ascent, &descent, &lineGap);
  534. // In case no chars count provided, default to 95
  535. codepointCount = (codepointCount > 0)? codepointCount : 95;
  536. // Fill fontChars in case not provided externally
  537. // NOTE: By default we fill glyphCount consecutively, starting at 32 (Space)
  538. if (codepoints == NULL)
  539. {
  540. codepoints = (int *)RL_MALLOC(codepointCount*sizeof(int));
  541. for (int i = 0; i < codepointCount; i++) codepoints[i] = i + 32;
  542. genFontChars = true;
  543. }
  544. chars = (GlyphInfo *)RL_CALLOC(codepointCount, sizeof(GlyphInfo));
  545. // NOTE: Using simple packaging, one char after another
  546. for (int i = 0; i < codepointCount; i++)
  547. {
  548. int chw = 0, chh = 0; // Character width and height (on generation)
  549. int ch = codepoints[i]; // Character value to get info for
  550. chars[i].value = ch;
  551. // Render a unicode codepoint to a bitmap
  552. // stbtt_GetCodepointBitmap() -- allocates and returns a bitmap
  553. // stbtt_GetCodepointBitmapBox() -- how big the bitmap must be
  554. // stbtt_MakeCodepointBitmap() -- renders into bitmap you provide
  555. // Check if a glyph is available in the font
  556. // WARNING: if (index == 0), glyph not found, it could fallback to default .notdef glyph (if defined in font)
  557. int index = stbtt_FindGlyphIndex(&fontInfo, ch);
  558. if (index > 0)
  559. {
  560. switch (type)
  561. {
  562. case FONT_DEFAULT:
  563. case FONT_BITMAP: chars[i].image.data = stbtt_GetCodepointBitmap(&fontInfo, scaleFactor, scaleFactor, ch, &chw, &chh, &chars[i].offsetX, &chars[i].offsetY); break;
  564. case FONT_SDF: if (ch != 32) chars[i].image.data = stbtt_GetCodepointSDF(&fontInfo, scaleFactor, ch, FONT_SDF_CHAR_PADDING, FONT_SDF_ON_EDGE_VALUE, FONT_SDF_PIXEL_DIST_SCALE, &chw, &chh, &chars[i].offsetX, &chars[i].offsetY); break;
  565. default: break;
  566. }
  567. if (chars[i].image.data != NULL) // Glyph data has been found in the font
  568. {
  569. stbtt_GetCodepointHMetrics(&fontInfo, ch, &chars[i].advanceX, NULL);
  570. chars[i].advanceX = (int)((float)chars[i].advanceX*scaleFactor);
  571. if (chh > fontSize) TRACELOG(LOG_WARNING, "FONT: Character [0x%08x] size is bigger than expected font size", ch);
  572. // Load characters images
  573. chars[i].image.width = chw;
  574. chars[i].image.height = chh;
  575. chars[i].image.mipmaps = 1;
  576. chars[i].image.format = PIXELFORMAT_UNCOMPRESSED_GRAYSCALE;
  577. chars[i].offsetY += (int)((float)ascent*scaleFactor);
  578. }
  579. // NOTE: We create an empty image for space character,
  580. // it could be further required for atlas packing
  581. if (ch == 32)
  582. {
  583. stbtt_GetCodepointHMetrics(&fontInfo, ch, &chars[i].advanceX, NULL);
  584. chars[i].advanceX = (int)((float)chars[i].advanceX*scaleFactor);
  585. Image imSpace = {
  586. .data = RL_CALLOC(chars[i].advanceX*fontSize, 2),
  587. .width = chars[i].advanceX,
  588. .height = fontSize,
  589. .mipmaps = 1,
  590. .format = PIXELFORMAT_UNCOMPRESSED_GRAYSCALE
  591. };
  592. chars[i].image = imSpace;
  593. }
  594. if (type == FONT_BITMAP)
  595. {
  596. // Aliased bitmap (black & white) font generation, avoiding anti-aliasing
  597. // NOTE: For optimum results, bitmap font should be generated at base pixel size
  598. for (int p = 0; p < chw*chh; p++)
  599. {
  600. if (((unsigned char *)chars[i].image.data)[p] < FONT_BITMAP_ALPHA_THRESHOLD) ((unsigned char *)chars[i].image.data)[p] = 0;
  601. else ((unsigned char *)chars[i].image.data)[p] = 255;
  602. }
  603. }
  604. }
  605. else
  606. {
  607. // TODO: Use some fallback glyph for codepoints not found in the font
  608. }
  609. }
  610. }
  611. else TRACELOG(LOG_WARNING, "FONT: Failed to process TTF font data");
  612. if (genFontChars) RL_FREE(codepoints);
  613. }
  614. #endif
  615. return chars;
  616. }
  617. // Generate image font atlas using chars info
  618. // NOTE: Packing method: 0-Default, 1-Skyline
  619. #if defined(SUPPORT_FILEFORMAT_TTF) || defined(SUPPORT_FILEFORMAT_BDF)
  620. Image GenImageFontAtlas(const GlyphInfo *glyphs, Rectangle **glyphRecs, int glyphCount, int fontSize, int padding, int packMethod)
  621. {
  622. Image atlas = { 0 };
  623. if (glyphs == NULL)
  624. {
  625. TRACELOG(LOG_WARNING, "FONT: Provided chars info not valid, returning empty image atlas");
  626. return atlas;
  627. }
  628. *glyphRecs = NULL;
  629. // In case no chars count provided we suppose default of 95
  630. glyphCount = (glyphCount > 0)? glyphCount : 95;
  631. // NOTE: Rectangles memory is loaded here!
  632. Rectangle *recs = (Rectangle *)RL_MALLOC(glyphCount*sizeof(Rectangle));
  633. // Calculate image size based on total glyph width and glyph row count
  634. int totalWidth = 0;
  635. int maxGlyphWidth = 0;
  636. for (int i = 0; i < glyphCount; i++)
  637. {
  638. if (glyphs[i].image.width > maxGlyphWidth) maxGlyphWidth = glyphs[i].image.width;
  639. totalWidth += glyphs[i].image.width + 2*padding;
  640. }
  641. //#define SUPPORT_FONT_ATLAS_SIZE_CONSERVATIVE
  642. #if defined(SUPPORT_FONT_ATLAS_SIZE_CONSERVATIVE)
  643. int rowCount = 0;
  644. int imageSize = 64; // Define minimum starting value to avoid unnecessary calculation steps for very small images
  645. // NOTE: maxGlyphWidth is maximum possible space left at the end of row
  646. while (totalWidth > (imageSize - maxGlyphWidth)*rowCount)
  647. {
  648. imageSize *= 2; // Double the size of image (to keep POT)
  649. rowCount = imageSize/(fontSize + 2*padding); // Calculate new row count for the new image size
  650. }
  651. atlas.width = imageSize; // Atlas bitmap width
  652. atlas.height = imageSize; // Atlas bitmap height
  653. #else
  654. int paddedFontSize = fontSize + 2*padding;
  655. // No need for a so-conservative atlas generation
  656. float totalArea = totalWidth*paddedFontSize*1.2f;
  657. float imageMinSize = sqrtf(totalArea);
  658. int imageSize = (int)powf(2, ceilf(logf(imageMinSize)/logf(2)));
  659. if (totalArea < ((imageSize*imageSize)/2))
  660. {
  661. atlas.width = imageSize; // Atlas bitmap width
  662. atlas.height = imageSize/2; // Atlas bitmap height
  663. }
  664. else
  665. {
  666. atlas.width = imageSize; // Atlas bitmap width
  667. atlas.height = imageSize; // Atlas bitmap height
  668. }
  669. #endif
  670. atlas.data = (unsigned char *)RL_CALLOC(1, atlas.width*atlas.height); // Create a bitmap to store characters (8 bpp)
  671. atlas.format = PIXELFORMAT_UNCOMPRESSED_GRAYSCALE;
  672. atlas.mipmaps = 1;
  673. // DEBUG: We can see padding in the generated image setting a gray background...
  674. //for (int i = 0; i < atlas.width*atlas.height; i++) ((unsigned char *)atlas.data)[i] = 100;
  675. if (packMethod == 0) // Use basic packing algorithm
  676. {
  677. int offsetX = padding;
  678. int offsetY = padding;
  679. // NOTE: Using simple packaging, one char after another
  680. for (int i = 0; i < glyphCount; i++)
  681. {
  682. // Check remaining space for glyph
  683. if (offsetX >= (atlas.width - glyphs[i].image.width - 2*padding))
  684. {
  685. offsetX = padding;
  686. // NOTE: Be careful on offsetY for SDF fonts, by default SDF
  687. // use an internal padding of 4 pixels, it means char rectangle
  688. // height is bigger than fontSize, it could be up to (fontSize + 8)
  689. offsetY += (fontSize + 2*padding);
  690. if (offsetY > (atlas.height - fontSize - padding))
  691. {
  692. for (int j = i + 1; j < glyphCount; j++)
  693. {
  694. TRACELOG(LOG_WARNING, "FONT: Failed to package character (%i)", j);
  695. // Make sure remaining recs contain valid data
  696. recs[j].x = 0;
  697. recs[j].y = 0;
  698. recs[j].width = 0;
  699. recs[j].height = 0;
  700. }
  701. break;
  702. }
  703. }
  704. // Copy pixel data from glyph image to atlas
  705. for (int y = 0; y < glyphs[i].image.height; y++)
  706. {
  707. for (int x = 0; x < glyphs[i].image.width; x++)
  708. {
  709. ((unsigned char *)atlas.data)[(offsetY + y)*atlas.width + (offsetX + x)] = ((unsigned char *)glyphs[i].image.data)[y*glyphs[i].image.width + x];
  710. }
  711. }
  712. // Fill chars rectangles in atlas info
  713. recs[i].x = (float)offsetX;
  714. recs[i].y = (float)offsetY;
  715. recs[i].width = (float)glyphs[i].image.width;
  716. recs[i].height = (float)glyphs[i].image.height;
  717. // Move atlas position X for next character drawing
  718. offsetX += (glyphs[i].image.width + 2*padding);
  719. }
  720. }
  721. else if (packMethod == 1) // Use Skyline rect packing algorithm (stb_pack_rect)
  722. {
  723. stbrp_context *context = (stbrp_context *)RL_MALLOC(sizeof(*context));
  724. stbrp_node *nodes = (stbrp_node *)RL_MALLOC(glyphCount*sizeof(*nodes));
  725. stbrp_init_target(context, atlas.width, atlas.height, nodes, glyphCount);
  726. stbrp_rect *rects = (stbrp_rect *)RL_MALLOC(glyphCount*sizeof(stbrp_rect));
  727. // Fill rectangles for packaging
  728. for (int i = 0; i < glyphCount; i++)
  729. {
  730. rects[i].id = i;
  731. rects[i].w = glyphs[i].image.width + 2*padding;
  732. rects[i].h = glyphs[i].image.height + 2*padding;
  733. }
  734. // Package rectangles into atlas
  735. stbrp_pack_rects(context, rects, glyphCount);
  736. for (int i = 0; i < glyphCount; i++)
  737. {
  738. // It returns char rectangles in atlas
  739. recs[i].x = rects[i].x + (float)padding;
  740. recs[i].y = rects[i].y + (float)padding;
  741. recs[i].width = (float)glyphs[i].image.width;
  742. recs[i].height = (float)glyphs[i].image.height;
  743. if (rects[i].was_packed)
  744. {
  745. // Copy pixel data from fc.data to atlas
  746. for (int y = 0; y < glyphs[i].image.height; y++)
  747. {
  748. for (int x = 0; x < glyphs[i].image.width; x++)
  749. {
  750. ((unsigned char *)atlas.data)[(rects[i].y + padding + y)*atlas.width + (rects[i].x + padding + x)] = ((unsigned char *)glyphs[i].image.data)[y*glyphs[i].image.width + x];
  751. }
  752. }
  753. }
  754. else TRACELOG(LOG_WARNING, "FONT: Failed to package character (%i)", i);
  755. }
  756. RL_FREE(rects);
  757. RL_FREE(nodes);
  758. RL_FREE(context);
  759. }
  760. #if defined(SUPPORT_FONT_ATLAS_WHITE_REC)
  761. // Add a 3x3 white rectangle at the bottom-right corner of the generated atlas,
  762. // useful to use as the white texture to draw shapes with raylib, using this rectangle
  763. // shapes and text can be backed into a single draw call: SetShapesTexture()
  764. for (int i = 0, k = atlas.width*atlas.height - 1; i < 3; i++)
  765. {
  766. ((unsigned char *)atlas.data)[k - 0] = 255;
  767. ((unsigned char *)atlas.data)[k - 1] = 255;
  768. ((unsigned char *)atlas.data)[k - 2] = 255;
  769. k -= atlas.width;
  770. }
  771. #endif
  772. // Convert image data from GRAYSCALE to GRAY_ALPHA
  773. unsigned char *dataGrayAlpha = (unsigned char *)RL_MALLOC(atlas.width*atlas.height*sizeof(unsigned char)*2); // Two channels
  774. for (int i = 0, k = 0; i < atlas.width*atlas.height; i++, k += 2)
  775. {
  776. dataGrayAlpha[k] = 255;
  777. dataGrayAlpha[k + 1] = ((unsigned char *)atlas.data)[i];
  778. }
  779. RL_FREE(atlas.data);
  780. atlas.data = dataGrayAlpha;
  781. atlas.format = PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA;
  782. *glyphRecs = recs;
  783. return atlas;
  784. }
  785. #endif
  786. // Unload font glyphs info data (RAM)
  787. void UnloadFontData(GlyphInfo *glyphs, int glyphCount)
  788. {
  789. if (glyphs != NULL)
  790. {
  791. for (int i = 0; i < glyphCount; i++) UnloadImage(glyphs[i].image);
  792. RL_FREE(glyphs);
  793. }
  794. }
  795. // Unload Font from GPU memory (VRAM)
  796. void UnloadFont(Font font)
  797. {
  798. // NOTE: Make sure font is not default font (fallback)
  799. if (font.texture.id != GetFontDefault().texture.id)
  800. {
  801. UnloadFontData(font.glyphs, font.glyphCount);
  802. if (isGpuReady) UnloadTexture(font.texture);
  803. RL_FREE(font.recs);
  804. TRACELOGD("FONT: Unloaded font data from RAM and VRAM");
  805. }
  806. }
  807. // Export font as code file, returns true on success
  808. bool ExportFontAsCode(Font font, const char *fileName)
  809. {
  810. bool success = false;
  811. #ifndef TEXT_BYTES_PER_LINE
  812. #define TEXT_BYTES_PER_LINE 20
  813. #endif
  814. #define MAX_FONT_DATA_SIZE 1024*1024 // 1 MB
  815. // Get file name from path
  816. char fileNamePascal[256] = { 0 };
  817. strncpy(fileNamePascal, TextToPascal(GetFileNameWithoutExt(fileName)), 256 - 1);
  818. // NOTE: Text data buffer size is estimated considering image data size in bytes
  819. // and requiring 6 char bytes for every byte: "0x00, "
  820. char *txtData = (char *)RL_CALLOC(MAX_FONT_DATA_SIZE, sizeof(char));
  821. int byteCount = 0;
  822. byteCount += sprintf(txtData + byteCount, "////////////////////////////////////////////////////////////////////////////////////////\n");
  823. byteCount += sprintf(txtData + byteCount, "// //\n");
  824. byteCount += sprintf(txtData + byteCount, "// FontAsCode exporter v1.0 - Font data exported as an array of bytes //\n");
  825. byteCount += sprintf(txtData + byteCount, "// //\n");
  826. byteCount += sprintf(txtData + byteCount, "// more info and bugs-report: github.com/raysan5/raylib //\n");
  827. byteCount += sprintf(txtData + byteCount, "// feedback and support: ray[at]raylib.com //\n");
  828. byteCount += sprintf(txtData + byteCount, "// //\n");
  829. byteCount += sprintf(txtData + byteCount, "// Copyright (c) 2018-2025 Ramon Santamaria (@raysan5) //\n");
  830. byteCount += sprintf(txtData + byteCount, "// //\n");
  831. byteCount += sprintf(txtData + byteCount, "// ---------------------------------------------------------------------------------- //\n");
  832. byteCount += sprintf(txtData + byteCount, "// //\n");
  833. byteCount += sprintf(txtData + byteCount, "// TODO: Fill the information and license of the exported font here: //\n");
  834. byteCount += sprintf(txtData + byteCount, "// //\n");
  835. byteCount += sprintf(txtData + byteCount, "// Font name: .... //\n");
  836. byteCount += sprintf(txtData + byteCount, "// Font creator: .... //\n");
  837. byteCount += sprintf(txtData + byteCount, "// Font LICENSE: .... //\n");
  838. byteCount += sprintf(txtData + byteCount, "// //\n");
  839. byteCount += sprintf(txtData + byteCount, "////////////////////////////////////////////////////////////////////////////////////////\n\n");
  840. // Support font export and initialization
  841. // NOTE: This mechanism is highly coupled to raylib
  842. Image image = LoadImageFromTexture(font.texture);
  843. if (image.format != PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA) TRACELOG(LOG_WARNING, "Font export as code: Font image format is not GRAY+ALPHA!");
  844. int imageDataSize = GetPixelDataSize(image.width, image.height, image.format);
  845. // Image data is usually GRAYSCALE + ALPHA and can be reduced to GRAYSCALE
  846. //ImageFormat(&image, PIXELFORMAT_UNCOMPRESSED_GRAYSCALE);
  847. #define SUPPORT_COMPRESSED_FONT_ATLAS
  848. #if defined(SUPPORT_COMPRESSED_FONT_ATLAS)
  849. // WARNING: Data is compressed using raylib CompressData() DEFLATE,
  850. // it requires to be decompressed with raylib DecompressData(), that requires
  851. // compiling raylib with SUPPORT_COMPRESSION_API config flag enabled
  852. // Compress font image data
  853. int compDataSize = 0;
  854. unsigned char *compData = CompressData((const unsigned char *)image.data, imageDataSize, &compDataSize);
  855. // Save font image data (compressed)
  856. byteCount += sprintf(txtData + byteCount, "#define COMPRESSED_DATA_SIZE_FONT_%s %i\n\n", TextToUpper(fileNamePascal), compDataSize);
  857. byteCount += sprintf(txtData + byteCount, "// Font image pixels data compressed (DEFLATE)\n");
  858. byteCount += sprintf(txtData + byteCount, "// NOTE: Original pixel data simplified to GRAYSCALE\n");
  859. byteCount += sprintf(txtData + byteCount, "static unsigned char fontData_%s[COMPRESSED_DATA_SIZE_FONT_%s] = { ", fileNamePascal, TextToUpper(fileNamePascal));
  860. for (int i = 0; i < compDataSize - 1; i++) byteCount += sprintf(txtData + byteCount, ((i%TEXT_BYTES_PER_LINE == 0)? "0x%02x,\n " : "0x%02x, "), compData[i]);
  861. byteCount += sprintf(txtData + byteCount, "0x%02x };\n\n", compData[compDataSize - 1]);
  862. RL_FREE(compData);
  863. #else
  864. // Save font image data (uncompressed)
  865. byteCount += sprintf(txtData + byteCount, "// Font image pixels data\n");
  866. byteCount += sprintf(txtData + byteCount, "// NOTE: 2 bytes per pixel, GRAY + ALPHA channels\n");
  867. byteCount += sprintf(txtData + byteCount, "static unsigned char fontImageData_%s[%i] = { ", fileNamePascal, imageDataSize);
  868. for (int i = 0; i < imageDataSize - 1; i++) byteCount += sprintf(txtData + byteCount, ((i%TEXT_BYTES_PER_LINE == 0)? "0x%02x,\n " : "0x%02x, "), ((unsigned char *)imFont.data)[i]);
  869. byteCount += sprintf(txtData + byteCount, "0x%02x };\n\n", ((unsigned char *)imFont.data)[imageDataSize - 1]);
  870. #endif
  871. // Save font recs data
  872. byteCount += sprintf(txtData + byteCount, "// Font characters rectangles data\n");
  873. byteCount += sprintf(txtData + byteCount, "static Rectangle fontRecs_%s[%i] = {\n", fileNamePascal, font.glyphCount);
  874. for (int i = 0; i < font.glyphCount; i++)
  875. {
  876. byteCount += sprintf(txtData + byteCount, " { %1.0f, %1.0f, %1.0f , %1.0f },\n", font.recs[i].x, font.recs[i].y, font.recs[i].width, font.recs[i].height);
  877. }
  878. byteCount += sprintf(txtData + byteCount, "};\n\n");
  879. // Save font glyphs data
  880. // NOTE: Glyphs image data not saved (grayscale pixels),
  881. // it could be generated from image and recs
  882. byteCount += sprintf(txtData + byteCount, "// Font glyphs info data\n");
  883. byteCount += sprintf(txtData + byteCount, "// NOTE: No glyphs.image data provided\n");
  884. byteCount += sprintf(txtData + byteCount, "static GlyphInfo fontGlyphs_%s[%i] = {\n", fileNamePascal, font.glyphCount);
  885. for (int i = 0; i < font.glyphCount; i++)
  886. {
  887. byteCount += sprintf(txtData + byteCount, " { %i, %i, %i, %i, { 0 }},\n", font.glyphs[i].value, font.glyphs[i].offsetX, font.glyphs[i].offsetY, font.glyphs[i].advanceX);
  888. }
  889. byteCount += sprintf(txtData + byteCount, "};\n\n");
  890. // Custom font loading function
  891. byteCount += sprintf(txtData + byteCount, "// Font loading function: %s\n", fileNamePascal);
  892. byteCount += sprintf(txtData + byteCount, "static Font LoadFont_%s(void)\n{\n", fileNamePascal);
  893. byteCount += sprintf(txtData + byteCount, " Font font = { 0 };\n\n");
  894. byteCount += sprintf(txtData + byteCount, " font.baseSize = %i;\n", font.baseSize);
  895. byteCount += sprintf(txtData + byteCount, " font.glyphCount = %i;\n", font.glyphCount);
  896. byteCount += sprintf(txtData + byteCount, " font.glyphPadding = %i;\n\n", font.glyphPadding);
  897. byteCount += sprintf(txtData + byteCount, " // Custom font loading\n");
  898. #if defined(SUPPORT_COMPRESSED_FONT_ATLAS)
  899. byteCount += sprintf(txtData + byteCount, " // NOTE: Compressed font image data (DEFLATE), it requires DecompressData() function\n");
  900. byteCount += sprintf(txtData + byteCount, " int fontDataSize_%s = 0;\n", fileNamePascal);
  901. byteCount += sprintf(txtData + byteCount, " unsigned char *data = DecompressData(fontData_%s, COMPRESSED_DATA_SIZE_FONT_%s, &fontDataSize_%s);\n", fileNamePascal, TextToUpper(fileNamePascal), fileNamePascal);
  902. byteCount += sprintf(txtData + byteCount, " Image imFont = { data, %i, %i, 1, %i };\n\n", image.width, image.height, image.format);
  903. #else
  904. byteCount += sprintf(txtData + byteCount, " Image imFont = { fontImageData_%s, %i, %i, 1, %i };\n\n", styleName, image.width, image.height, image.format);
  905. #endif
  906. byteCount += sprintf(txtData + byteCount, " // Load texture from image\n");
  907. byteCount += sprintf(txtData + byteCount, " if (isGpuReady) font.texture = LoadTextureFromImage(imFont);\n");
  908. #if defined(SUPPORT_COMPRESSED_FONT_ATLAS)
  909. byteCount += sprintf(txtData + byteCount, " UnloadImage(imFont); // Uncompressed data can be unloaded from memory\n\n");
  910. #endif
  911. // We have two possible mechanisms to assign font.recs and font.glyphs data,
  912. // that data is already available as global arrays, we two options to assign that data:
  913. // - 1. Data copy. This option consumes more memory and Font MUST be unloaded by user, requiring additional code
  914. // - 2. Data assignment. This option consumes less memory and Font MUST NOT be unloaded by user because data is on protected DATA segment
  915. //#define SUPPORT_FONT_DATA_COPY
  916. #if defined(SUPPORT_FONT_DATA_COPY)
  917. byteCount += sprintf(txtData + byteCount, " // Copy glyph recs data from global fontRecs\n");
  918. byteCount += sprintf(txtData + byteCount, " // NOTE: Required to avoid issues if trying to free font\n");
  919. byteCount += sprintf(txtData + byteCount, " font.recs = (Rectangle *)malloc(font.glyphCount*sizeof(Rectangle));\n");
  920. byteCount += sprintf(txtData + byteCount, " memcpy(font.recs, fontRecs_%s, font.glyphCount*sizeof(Rectangle));\n\n", fileNamePascal);
  921. byteCount += sprintf(txtData + byteCount, " // Copy font glyph info data from global fontChars\n");
  922. byteCount += sprintf(txtData + byteCount, " // NOTE: Required to avoid issues if trying to free font\n");
  923. byteCount += sprintf(txtData + byteCount, " font.glyphs = (GlyphInfo *)malloc(font.glyphCount*sizeof(GlyphInfo));\n");
  924. byteCount += sprintf(txtData + byteCount, " memcpy(font.glyphs, fontGlyphs_%s, font.glyphCount*sizeof(GlyphInfo));\n\n", fileNamePascal);
  925. #else
  926. byteCount += sprintf(txtData + byteCount, " // Assign glyph recs and info data directly\n");
  927. byteCount += sprintf(txtData + byteCount, " // WARNING: This font data must not be unloaded\n");
  928. byteCount += sprintf(txtData + byteCount, " font.recs = fontRecs_%s;\n", fileNamePascal);
  929. byteCount += sprintf(txtData + byteCount, " font.glyphs = fontGlyphs_%s;\n\n", fileNamePascal);
  930. #endif
  931. byteCount += sprintf(txtData + byteCount, " return font;\n");
  932. byteCount += sprintf(txtData + byteCount, "}\n");
  933. UnloadImage(image);
  934. // NOTE: Text data size exported is determined by '\0' (NULL) character
  935. success = SaveFileText(fileName, txtData);
  936. RL_FREE(txtData);
  937. if (success != 0) TRACELOG(LOG_INFO, "FILEIO: [%s] Font as code exported successfully", fileName);
  938. else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to export font as code", fileName);
  939. return success;
  940. }
  941. // Draw current FPS
  942. // NOTE: Uses default font
  943. void DrawFPS(int posX, int posY)
  944. {
  945. Color color = LIME; // Good FPS
  946. int fps = GetFPS();
  947. if ((fps < 30) && (fps >= 15)) color = ORANGE; // Warning FPS
  948. else if (fps < 15) color = RED; // Low FPS
  949. DrawText(TextFormat("%2i FPS", fps), posX, posY, 20, color);
  950. }
  951. // Draw text (using default font)
  952. // NOTE: fontSize work like in any drawing program but if fontSize is lower than font-base-size, then font-base-size is used
  953. // NOTE: chars spacing is proportional to fontSize
  954. void DrawText(const char *text, int posX, int posY, int fontSize, Color color)
  955. {
  956. // Check if default font has been loaded
  957. if (GetFontDefault().texture.id != 0)
  958. {
  959. Vector2 position = { (float)posX, (float)posY };
  960. int defaultFontSize = 10; // Default Font chars height in pixel
  961. if (fontSize < defaultFontSize) fontSize = defaultFontSize;
  962. int spacing = fontSize/defaultFontSize;
  963. DrawTextEx(GetFontDefault(), text, position, (float)fontSize, (float)spacing, color);
  964. }
  965. }
  966. // Draw text using Font
  967. // NOTE: chars spacing is NOT proportional to fontSize
  968. void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, float spacing, Color tint)
  969. {
  970. if (font.texture.id == 0) font = GetFontDefault(); // Security check in case of not valid font
  971. int size = TextLength(text); // Total size in bytes of the text, scanned by codepoints in loop
  972. float textOffsetY = 0; // Offset between lines (on linebreak '\n')
  973. float textOffsetX = 0.0f; // Offset X to next character to draw
  974. float scaleFactor = fontSize/font.baseSize; // Character quad scaling factor
  975. for (int i = 0; i < size;)
  976. {
  977. // Get next codepoint from byte string and glyph index in font
  978. int codepointByteCount = 0;
  979. int codepoint = GetCodepointNext(&text[i], &codepointByteCount);
  980. int index = GetGlyphIndex(font, codepoint);
  981. if (codepoint == '\n')
  982. {
  983. // NOTE: Line spacing is a global variable, use SetTextLineSpacing() to setup
  984. textOffsetY += (fontSize + textLineSpacing);
  985. textOffsetX = 0.0f;
  986. }
  987. else
  988. {
  989. if ((codepoint != ' ') && (codepoint != '\t'))
  990. {
  991. DrawTextCodepoint(font, codepoint, (Vector2){ position.x + textOffsetX, position.y + textOffsetY }, fontSize, tint);
  992. }
  993. if (font.glyphs[index].advanceX == 0) textOffsetX += ((float)font.recs[index].width*scaleFactor + spacing);
  994. else textOffsetX += ((float)font.glyphs[index].advanceX*scaleFactor + spacing);
  995. }
  996. i += codepointByteCount; // Move text bytes counter to next codepoint
  997. }
  998. }
  999. // Draw text using Font and pro parameters (rotation)
  1000. void DrawTextPro(Font font, const char *text, Vector2 position, Vector2 origin, float rotation, float fontSize, float spacing, Color tint)
  1001. {
  1002. rlPushMatrix();
  1003. rlTranslatef(position.x, position.y, 0.0f);
  1004. rlRotatef(rotation, 0.0f, 0.0f, 1.0f);
  1005. rlTranslatef(-origin.x, -origin.y, 0.0f);
  1006. DrawTextEx(font, text, (Vector2){ 0.0f, 0.0f }, fontSize, spacing, tint);
  1007. rlPopMatrix();
  1008. }
  1009. // Draw one character (codepoint)
  1010. void DrawTextCodepoint(Font font, int codepoint, Vector2 position, float fontSize, Color tint)
  1011. {
  1012. // Character index position in sprite font
  1013. // NOTE: In case a codepoint is not available in the font, index returned points to '?'
  1014. int index = GetGlyphIndex(font, codepoint);
  1015. float scaleFactor = fontSize/font.baseSize; // Character quad scaling factor
  1016. // Character destination rectangle on screen
  1017. // NOTE: We consider glyphPadding on drawing
  1018. Rectangle dstRec = { position.x + font.glyphs[index].offsetX*scaleFactor - (float)font.glyphPadding*scaleFactor,
  1019. position.y + font.glyphs[index].offsetY*scaleFactor - (float)font.glyphPadding*scaleFactor,
  1020. (font.recs[index].width + 2.0f*font.glyphPadding)*scaleFactor,
  1021. (font.recs[index].height + 2.0f*font.glyphPadding)*scaleFactor };
  1022. // Character source rectangle from font texture atlas
  1023. // NOTE: We consider chars padding when drawing, it could be required for outline/glow shader effects
  1024. Rectangle srcRec = { font.recs[index].x - (float)font.glyphPadding, font.recs[index].y - (float)font.glyphPadding,
  1025. font.recs[index].width + 2.0f*font.glyphPadding, font.recs[index].height + 2.0f*font.glyphPadding };
  1026. // Draw the character texture on the screen
  1027. DrawTexturePro(font.texture, srcRec, dstRec, (Vector2){ 0, 0 }, 0.0f, tint);
  1028. }
  1029. // Draw multiple character (codepoints)
  1030. void DrawTextCodepoints(Font font, const int *codepoints, int codepointCount, Vector2 position, float fontSize, float spacing, Color tint)
  1031. {
  1032. float textOffsetY = 0; // Offset between lines (on linebreak '\n')
  1033. float textOffsetX = 0.0f; // Offset X to next character to draw
  1034. float scaleFactor = fontSize/font.baseSize; // Character quad scaling factor
  1035. for (int i = 0; i < codepointCount; i++)
  1036. {
  1037. int index = GetGlyphIndex(font, codepoints[i]);
  1038. if (codepoints[i] == '\n')
  1039. {
  1040. // NOTE: Line spacing is a global variable, use SetTextLineSpacing() to setup
  1041. textOffsetY += (fontSize + textLineSpacing);
  1042. textOffsetX = 0.0f;
  1043. }
  1044. else
  1045. {
  1046. if ((codepoints[i] != ' ') && (codepoints[i] != '\t'))
  1047. {
  1048. DrawTextCodepoint(font, codepoints[i], (Vector2){ position.x + textOffsetX, position.y + textOffsetY }, fontSize, tint);
  1049. }
  1050. if (font.glyphs[index].advanceX == 0) textOffsetX += ((float)font.recs[index].width*scaleFactor + spacing);
  1051. else textOffsetX += ((float)font.glyphs[index].advanceX*scaleFactor + spacing);
  1052. }
  1053. }
  1054. }
  1055. // Set vertical line spacing when drawing with line-breaks
  1056. void SetTextLineSpacing(int spacing)
  1057. {
  1058. textLineSpacing = spacing;
  1059. }
  1060. // Measure string width for default font
  1061. int MeasureText(const char *text, int fontSize)
  1062. {
  1063. Vector2 textSize = { 0.0f, 0.0f };
  1064. // Check if default font has been loaded
  1065. if (GetFontDefault().texture.id != 0)
  1066. {
  1067. int defaultFontSize = 10; // Default Font chars height in pixel
  1068. if (fontSize < defaultFontSize) fontSize = defaultFontSize;
  1069. int spacing = fontSize/defaultFontSize;
  1070. textSize = MeasureTextEx(GetFontDefault(), text, (float)fontSize, (float)spacing);
  1071. }
  1072. return (int)textSize.x;
  1073. }
  1074. // Measure string size for Font
  1075. Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing)
  1076. {
  1077. Vector2 textSize = { 0 };
  1078. if ((isGpuReady && (font.texture.id == 0)) ||
  1079. (text == NULL) || (text[0] == '\0')) return textSize; // Security check
  1080. int size = TextLength(text); // Get size in bytes of text
  1081. int tempByteCounter = 0; // Used to count longer text line num chars
  1082. int byteCounter = 0;
  1083. float textWidth = 0.0f;
  1084. float tempTextWidth = 0.0f; // Used to count longer text line width
  1085. float textHeight = fontSize;
  1086. float scaleFactor = fontSize/(float)font.baseSize;
  1087. int letter = 0; // Current character
  1088. int index = 0; // Index position in sprite font
  1089. for (int i = 0; i < size;)
  1090. {
  1091. byteCounter++;
  1092. int codepointByteCount = 0;
  1093. letter = GetCodepointNext(&text[i], &codepointByteCount);
  1094. index = GetGlyphIndex(font, letter);
  1095. i += codepointByteCount;
  1096. if (letter != '\n')
  1097. {
  1098. if (font.glyphs[index].advanceX > 0) textWidth += font.glyphs[index].advanceX;
  1099. else textWidth += (font.recs[index].width + font.glyphs[index].offsetX);
  1100. }
  1101. else
  1102. {
  1103. if (tempTextWidth < textWidth) tempTextWidth = textWidth;
  1104. byteCounter = 0;
  1105. textWidth = 0;
  1106. // NOTE: Line spacing is a global variable, use SetTextLineSpacing() to setup
  1107. textHeight += (fontSize + textLineSpacing);
  1108. }
  1109. if (tempByteCounter < byteCounter) tempByteCounter = byteCounter;
  1110. }
  1111. if (tempTextWidth < textWidth) tempTextWidth = textWidth;
  1112. textSize.x = tempTextWidth*scaleFactor + (float)((tempByteCounter - 1)*spacing);
  1113. textSize.y = textHeight;
  1114. return textSize;
  1115. }
  1116. // Get index position for a unicode character on font
  1117. // NOTE: If codepoint is not found in the font it fallbacks to '?'
  1118. int GetGlyphIndex(Font font, int codepoint)
  1119. {
  1120. int index = 0;
  1121. if (!IsFontValid(font)) return index;
  1122. #define SUPPORT_UNORDERED_CHARSET
  1123. #if defined(SUPPORT_UNORDERED_CHARSET)
  1124. int fallbackIndex = 0; // Get index of fallback glyph '?'
  1125. // Look for character index in the unordered charset
  1126. for (int i = 0; i < font.glyphCount; i++)
  1127. {
  1128. if (font.glyphs[i].value == 63) fallbackIndex = i;
  1129. if (font.glyphs[i].value == codepoint)
  1130. {
  1131. index = i;
  1132. break;
  1133. }
  1134. }
  1135. if ((index == 0) && (font.glyphs[0].value != codepoint)) index = fallbackIndex;
  1136. #else
  1137. index = codepoint - 32;
  1138. #endif
  1139. return index;
  1140. }
  1141. // Get glyph font info data for a codepoint (unicode character)
  1142. // NOTE: If codepoint is not found in the font it fallbacks to '?'
  1143. GlyphInfo GetGlyphInfo(Font font, int codepoint)
  1144. {
  1145. GlyphInfo info = { 0 };
  1146. info = font.glyphs[GetGlyphIndex(font, codepoint)];
  1147. return info;
  1148. }
  1149. // Get glyph rectangle in font atlas for a codepoint (unicode character)
  1150. // NOTE: If codepoint is not found in the font it fallbacks to '?'
  1151. Rectangle GetGlyphAtlasRec(Font font, int codepoint)
  1152. {
  1153. Rectangle rec = { 0 };
  1154. rec = font.recs[GetGlyphIndex(font, codepoint)];
  1155. return rec;
  1156. }
  1157. //----------------------------------------------------------------------------------
  1158. // Text strings management functions
  1159. //----------------------------------------------------------------------------------
  1160. // Get text length in bytes, check for \0 character
  1161. unsigned int TextLength(const char *text)
  1162. {
  1163. unsigned int length = 0;
  1164. if (text != NULL)
  1165. {
  1166. // NOTE: Alternative: use strlen(text)
  1167. while (*text++) length++;
  1168. }
  1169. return length;
  1170. }
  1171. // Formatting of text with variables to 'embed'
  1172. // WARNING: String returned will expire after this function is called MAX_TEXTFORMAT_BUFFERS times
  1173. const char *TextFormat(const char *text, ...)
  1174. {
  1175. #ifndef MAX_TEXTFORMAT_BUFFERS
  1176. #define MAX_TEXTFORMAT_BUFFERS 4 // Maximum number of static buffers for text formatting
  1177. #endif
  1178. // We create an array of buffers so strings don't expire until MAX_TEXTFORMAT_BUFFERS invocations
  1179. static char buffers[MAX_TEXTFORMAT_BUFFERS][MAX_TEXT_BUFFER_LENGTH] = { 0 };
  1180. static int index = 0;
  1181. char *currentBuffer = buffers[index];
  1182. memset(currentBuffer, 0, MAX_TEXT_BUFFER_LENGTH); // Clear buffer before using
  1183. va_list args;
  1184. va_start(args, text);
  1185. int requiredByteCount = vsnprintf(currentBuffer, MAX_TEXT_BUFFER_LENGTH, text, args);
  1186. va_end(args);
  1187. // If requiredByteCount is larger than the MAX_TEXT_BUFFER_LENGTH, then overflow occured
  1188. if (requiredByteCount >= MAX_TEXT_BUFFER_LENGTH)
  1189. {
  1190. // Inserting "..." at the end of the string to mark as truncated
  1191. char *truncBuffer = buffers[index] + MAX_TEXT_BUFFER_LENGTH - 4; // Adding 4 bytes = "...\0"
  1192. sprintf(truncBuffer, "...");
  1193. }
  1194. index += 1; // Move to next buffer for next function call
  1195. if (index >= MAX_TEXTFORMAT_BUFFERS) index = 0;
  1196. return currentBuffer;
  1197. }
  1198. // Get integer value from text
  1199. // NOTE: This function replaces atoi() [stdlib.h]
  1200. int TextToInteger(const char *text)
  1201. {
  1202. int value = 0;
  1203. int sign = 1;
  1204. if ((text[0] == '+') || (text[0] == '-'))
  1205. {
  1206. if (text[0] == '-') sign = -1;
  1207. text++;
  1208. }
  1209. for (int i = 0; ((text[i] >= '0') && (text[i] <= '9')); i++) value = value*10 + (int)(text[i] - '0');
  1210. return value*sign;
  1211. }
  1212. // Get float value from text
  1213. // NOTE: This function replaces atof() [stdlib.h]
  1214. // WARNING: Only '.' character is understood as decimal point
  1215. float TextToFloat(const char *text)
  1216. {
  1217. float value = 0.0f;
  1218. float sign = 1.0f;
  1219. if ((text[0] == '+') || (text[0] == '-'))
  1220. {
  1221. if (text[0] == '-') sign = -1.0f;
  1222. text++;
  1223. }
  1224. int i = 0;
  1225. for (; ((text[i] >= '0') && (text[i] <= '9')); i++) value = value*10.0f + (float)(text[i] - '0');
  1226. if (text[i++] == '.')
  1227. {
  1228. float divisor = 10.0f;
  1229. for (; ((text[i] >= '0') && (text[i] <= '9')); i++)
  1230. {
  1231. value += ((float)(text[i] - '0'))/divisor;
  1232. divisor = divisor*10.0f;
  1233. }
  1234. }
  1235. return value*sign;
  1236. }
  1237. #if defined(SUPPORT_TEXT_MANIPULATION)
  1238. // Copy one string to another, returns bytes copied
  1239. int TextCopy(char *dst, const char *src)
  1240. {
  1241. int bytes = 0;
  1242. if ((src != NULL) && (dst != NULL))
  1243. {
  1244. // NOTE: Alternative: use strcpy(dst, src)
  1245. while (*src != '\0')
  1246. {
  1247. *dst = *src;
  1248. dst++;
  1249. src++;
  1250. bytes++;
  1251. }
  1252. *dst = '\0';
  1253. }
  1254. return bytes;
  1255. }
  1256. // Check if two text string are equal
  1257. // REQUIRES: strcmp()
  1258. bool TextIsEqual(const char *text1, const char *text2)
  1259. {
  1260. bool result = false;
  1261. if ((text1 != NULL) && (text2 != NULL))
  1262. {
  1263. if (strcmp(text1, text2) == 0) result = true;
  1264. }
  1265. return result;
  1266. }
  1267. // Get a piece of a text string
  1268. const char *TextSubtext(const char *text, int position, int length)
  1269. {
  1270. static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 };
  1271. memset(buffer, 0, MAX_TEXT_BUFFER_LENGTH);
  1272. int textLength = TextLength(text);
  1273. if (position >= textLength)
  1274. {
  1275. position = textLength - 1;
  1276. length = 0;
  1277. }
  1278. if (length >= textLength) length = textLength;
  1279. // NOTE: Alternative: memcpy(buffer, text + position, length)
  1280. for (int c = 0 ; c < length ; c++)
  1281. {
  1282. *(buffer + c) = *(text + position);
  1283. text++;
  1284. }
  1285. *(buffer + length) = '\0';
  1286. return buffer;
  1287. }
  1288. // Replace text string
  1289. // REQUIRES: strlen(), strstr(), strncpy(), strcpy()
  1290. // WARNING: Allocated memory must be manually freed
  1291. char *TextReplace(const char *text, const char *replace, const char *by)
  1292. {
  1293. // Sanity checks and initialization
  1294. if (!text || !replace || !by) return NULL;
  1295. char *result = NULL;
  1296. char *insertPoint = NULL; // Next insert point
  1297. char *temp = NULL; // Temp pointer
  1298. int replaceLen = 0; // Replace string length of (the string to remove)
  1299. int byLen = 0; // Replacement length (the string to replace by)
  1300. int lastReplacePos = 0; // Distance between replace and end of last replace
  1301. int count = 0; // Number of replacements
  1302. replaceLen = TextLength(replace);
  1303. if (replaceLen == 0) return NULL; // Empty replace causes infinite loop during count
  1304. byLen = TextLength(by);
  1305. // Count the number of replacements needed
  1306. insertPoint = (char *)text;
  1307. for (count = 0; (temp = strstr(insertPoint, replace)); count++) insertPoint = temp + replaceLen;
  1308. // Allocate returning string and point temp to it
  1309. temp = result = (char *)RL_MALLOC(TextLength(text) + (byLen - replaceLen)*count + 1);
  1310. if (!result) return NULL; // Memory could not be allocated
  1311. // First time through the loop, all the variable are set correctly from here on,
  1312. // - 'temp' points to the end of the result string
  1313. // - 'insertPoint' points to the next occurrence of replace in text
  1314. // - 'text' points to the remainder of text after "end of replace"
  1315. while (count--)
  1316. {
  1317. insertPoint = strstr(text, replace);
  1318. lastReplacePos = (int)(insertPoint - text);
  1319. temp = strncpy(temp, text, lastReplacePos) + lastReplacePos;
  1320. temp = strcpy(temp, by) + byLen;
  1321. text += lastReplacePos + replaceLen; // Move to next "end of replace"
  1322. }
  1323. // Copy remaind text part after replacement to result (pointed by moving temp)
  1324. strcpy(temp, text);
  1325. return result;
  1326. }
  1327. // Insert text in a specific position, moves all text forward
  1328. // WARNING: Allocated memory must be manually freed
  1329. char *TextInsert(const char *text, const char *insert, int position)
  1330. {
  1331. int textLen = TextLength(text);
  1332. int insertLen = TextLength(insert);
  1333. char *result = (char *)RL_MALLOC(textLen + insertLen + 1);
  1334. for (int i = 0; i < position; i++) result[i] = text[i];
  1335. for (int i = position; i < insertLen + position; i++) result[i] = insert[i];
  1336. for (int i = (insertLen + position); i < (textLen + insertLen); i++) result[i] = text[i];
  1337. result[textLen + insertLen] = '\0'; // Make sure text string is valid!
  1338. return result;
  1339. }
  1340. // Join text strings with delimiter
  1341. // REQUIRES: memset(), memcpy()
  1342. const char *TextJoin(const char **textList, int count, const char *delimiter)
  1343. {
  1344. static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 };
  1345. memset(buffer, 0, MAX_TEXT_BUFFER_LENGTH);
  1346. char *textPtr = buffer;
  1347. int totalLength = 0;
  1348. int delimiterLen = TextLength(delimiter);
  1349. for (int i = 0; i < count; i++)
  1350. {
  1351. int textLength = TextLength(textList[i]);
  1352. // Make sure joined text could fit inside MAX_TEXT_BUFFER_LENGTH
  1353. if ((totalLength + textLength) < MAX_TEXT_BUFFER_LENGTH)
  1354. {
  1355. memcpy(textPtr, textList[i], textLength);
  1356. totalLength += textLength;
  1357. textPtr += textLength;
  1358. if ((delimiterLen > 0) && (i < (count - 1)))
  1359. {
  1360. memcpy(textPtr, delimiter, delimiterLen);
  1361. totalLength += delimiterLen;
  1362. textPtr += delimiterLen;
  1363. }
  1364. }
  1365. }
  1366. return buffer;
  1367. }
  1368. // Split string into multiple strings
  1369. // REQUIRES: memset()
  1370. const char **TextSplit(const char *text, char delimiter, int *count)
  1371. {
  1372. // NOTE: Current implementation returns a copy of the provided string with '\0' (string end delimiter)
  1373. // inserted between strings defined by "delimiter" parameter. No memory is dynamically allocated,
  1374. // all used memory is static... it has some limitations:
  1375. // 1. Maximum number of possible split strings is set by MAX_TEXTSPLIT_COUNT
  1376. // 2. Maximum size of text to split is MAX_TEXT_BUFFER_LENGTH
  1377. static const char *result[MAX_TEXTSPLIT_COUNT] = { NULL };
  1378. static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 };
  1379. memset(buffer, 0, MAX_TEXT_BUFFER_LENGTH);
  1380. result[0] = buffer;
  1381. int counter = 0;
  1382. if (text != NULL)
  1383. {
  1384. counter = 1;
  1385. // Count how many substrings we have on text and point to every one
  1386. for (int i = 0; i < MAX_TEXT_BUFFER_LENGTH; i++)
  1387. {
  1388. buffer[i] = text[i];
  1389. if (buffer[i] == '\0') break;
  1390. else if (buffer[i] == delimiter)
  1391. {
  1392. buffer[i] = '\0'; // Set an end of string at this point
  1393. result[counter] = buffer + i + 1;
  1394. counter++;
  1395. if (counter == MAX_TEXTSPLIT_COUNT) break;
  1396. }
  1397. }
  1398. }
  1399. *count = counter;
  1400. return result;
  1401. }
  1402. // Append text at specific position and move cursor
  1403. // WARNING: It's up to the user to make sure appended text does not overflow the buffer!
  1404. // REQUIRES: strcpy()
  1405. void TextAppend(char *text, const char *append, int *position)
  1406. {
  1407. strcpy(text + *position, append);
  1408. *position += TextLength(append);
  1409. }
  1410. // Find first text occurrence within a string
  1411. // REQUIRES: strstr()
  1412. int TextFindIndex(const char *text, const char *find)
  1413. {
  1414. int position = -1;
  1415. char *ptr = strstr(text, find);
  1416. if (ptr != NULL) position = (int)(ptr - text);
  1417. return position;
  1418. }
  1419. // Get upper case version of provided string
  1420. // WARNING: Limited functionality, only basic characters set
  1421. // TODO: Support UTF-8 diacritics to upper-case, check codepoints
  1422. const char *TextToUpper(const char *text)
  1423. {
  1424. static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 };
  1425. memset(buffer, 0, MAX_TEXT_BUFFER_LENGTH);
  1426. if (text != NULL)
  1427. {
  1428. for (int i = 0; (i < MAX_TEXT_BUFFER_LENGTH - 1) && (text[i] != '\0'); i++)
  1429. {
  1430. if ((text[i] >= 'a') && (text[i] <= 'z')) buffer[i] = text[i] - 32;
  1431. else buffer[i] = text[i];
  1432. }
  1433. }
  1434. return buffer;
  1435. }
  1436. // Get lower case version of provided string
  1437. // WARNING: Limited functionality, only basic characters set
  1438. const char *TextToLower(const char *text)
  1439. {
  1440. static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 };
  1441. memset(buffer, 0, MAX_TEXT_BUFFER_LENGTH);
  1442. if (text != NULL)
  1443. {
  1444. for (int i = 0; (i < MAX_TEXT_BUFFER_LENGTH - 1) && (text[i] != '\0'); i++)
  1445. {
  1446. if ((text[i] >= 'A') && (text[i] <= 'Z')) buffer[i] = text[i] + 32;
  1447. else buffer[i] = text[i];
  1448. }
  1449. }
  1450. return buffer;
  1451. }
  1452. // Get Pascal case notation version of provided string
  1453. // WARNING: Limited functionality, only basic characters set
  1454. const char *TextToPascal(const char *text)
  1455. {
  1456. static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 };
  1457. memset(buffer, 0, MAX_TEXT_BUFFER_LENGTH);
  1458. if (text != NULL)
  1459. {
  1460. // Upper case first character
  1461. if ((text[0] >= 'a') && (text[0] <= 'z')) buffer[0] = text[0] - 32;
  1462. else buffer[0] = text[0];
  1463. // Check for next separator to upper case another character
  1464. for (int i = 1, j = 1; (i < MAX_TEXT_BUFFER_LENGTH - 1) && (text[j] != '\0'); i++, j++)
  1465. {
  1466. if (text[j] != '_') buffer[i] = text[j];
  1467. else
  1468. {
  1469. j++;
  1470. if ((text[j] >= 'a') && (text[j] <= 'z')) buffer[i] = text[j] - 32;
  1471. }
  1472. }
  1473. }
  1474. return buffer;
  1475. }
  1476. // Get snake case notation version of provided string
  1477. // WARNING: Limited functionality, only basic characters set
  1478. const char *TextToSnake(const char *text)
  1479. {
  1480. static char buffer[MAX_TEXT_BUFFER_LENGTH] = {0};
  1481. memset(buffer, 0, MAX_TEXT_BUFFER_LENGTH);
  1482. if (text != NULL)
  1483. {
  1484. // Check for next separator to upper case another character
  1485. for (int i = 0, j = 0; (i < MAX_TEXT_BUFFER_LENGTH - 1) && (text[j] != '\0'); i++, j++)
  1486. {
  1487. if ((text[j] >= 'A') && (text[j] <= 'Z'))
  1488. {
  1489. if (i >= 1)
  1490. {
  1491. buffer[i] = '_';
  1492. i++;
  1493. }
  1494. buffer[i] = text[j] + 32;
  1495. }
  1496. else buffer[i] = text[j];
  1497. }
  1498. }
  1499. return buffer;
  1500. }
  1501. // Get Camel case notation version of provided string
  1502. // WARNING: Limited functionality, only basic characters set
  1503. const char *TextToCamel(const char *text)
  1504. {
  1505. static char buffer[MAX_TEXT_BUFFER_LENGTH] = {0};
  1506. memset(buffer, 0, MAX_TEXT_BUFFER_LENGTH);
  1507. if (text != NULL)
  1508. {
  1509. // Lower case first character
  1510. if ((text[0] >= 'A') && (text[0] <= 'Z')) buffer[0] = text[0] + 32;
  1511. else buffer[0] = text[0];
  1512. // Check for next separator to upper case another character
  1513. for (int i = 1, j = 1; (i < MAX_TEXT_BUFFER_LENGTH - 1) && (text[j] != '\0'); i++, j++)
  1514. {
  1515. if (text[j] != '_') buffer[i] = text[j];
  1516. else
  1517. {
  1518. j++;
  1519. if ((text[j] >= 'a') && (text[j] <= 'z')) buffer[i] = text[j] - 32;
  1520. }
  1521. }
  1522. }
  1523. return buffer;
  1524. }
  1525. // Encode text codepoint into UTF-8 text
  1526. // REQUIRES: memcpy()
  1527. // WARNING: Allocated memory must be manually freed
  1528. char *LoadUTF8(const int *codepoints, int length)
  1529. {
  1530. // We allocate enough memory to fit all possible codepoints
  1531. // NOTE: 5 bytes for every codepoint should be enough
  1532. char *text = (char *)RL_CALLOC(length*5, 1);
  1533. const char *utf8 = NULL;
  1534. int size = 0;
  1535. for (int i = 0, bytes = 0; i < length; i++)
  1536. {
  1537. utf8 = CodepointToUTF8(codepoints[i], &bytes);
  1538. memcpy(text + size, utf8, bytes);
  1539. size += bytes;
  1540. }
  1541. // Resize memory to text length + string NULL terminator
  1542. void *ptr = RL_REALLOC(text, size + 1);
  1543. if (ptr != NULL) text = (char *)ptr;
  1544. return text;
  1545. }
  1546. // Unload UTF-8 text encoded from codepoints array
  1547. void UnloadUTF8(char *text)
  1548. {
  1549. RL_FREE(text);
  1550. }
  1551. // Load all codepoints from a UTF-8 text string, codepoints count returned by parameter
  1552. int *LoadCodepoints(const char *text, int *count)
  1553. {
  1554. int textLength = TextLength(text);
  1555. int codepointSize = 0;
  1556. int codepointCount = 0;
  1557. // Allocate a big enough buffer to store as many codepoints as text bytes
  1558. int *codepoints = (int *)RL_CALLOC(textLength, sizeof(int));
  1559. for (int i = 0; i < textLength; codepointCount++)
  1560. {
  1561. codepoints[codepointCount] = GetCodepointNext(text + i, &codepointSize);
  1562. i += codepointSize;
  1563. }
  1564. // Re-allocate buffer to the actual number of codepoints loaded
  1565. codepoints = (int *)RL_REALLOC(codepoints, codepointCount*sizeof(int));
  1566. *count = codepointCount;
  1567. return codepoints;
  1568. }
  1569. // Unload codepoints data from memory
  1570. void UnloadCodepoints(int *codepoints)
  1571. {
  1572. RL_FREE(codepoints);
  1573. }
  1574. // Get total number of characters(codepoints) in a UTF-8 encoded text, until '\0' is found
  1575. // NOTE: If an invalid UTF-8 sequence is encountered a '?'(0x3f) codepoint is counted instead
  1576. int GetCodepointCount(const char *text)
  1577. {
  1578. unsigned int length = 0;
  1579. char *ptr = (char *)&text[0];
  1580. while (*ptr != '\0')
  1581. {
  1582. int next = 0;
  1583. GetCodepointNext(ptr, &next);
  1584. ptr += next;
  1585. length++;
  1586. }
  1587. return length;
  1588. }
  1589. // Encode codepoint into utf8 text (char array length returned as parameter)
  1590. // NOTE: It uses a static array to store UTF-8 bytes
  1591. const char *CodepointToUTF8(int codepoint, int *utf8Size)
  1592. {
  1593. static char utf8[6] = { 0 };
  1594. memset(utf8, 0, 6); // Clear static array
  1595. int size = 0; // Byte size of codepoint
  1596. if (codepoint <= 0x7f)
  1597. {
  1598. utf8[0] = (char)codepoint;
  1599. size = 1;
  1600. }
  1601. else if (codepoint <= 0x7ff)
  1602. {
  1603. utf8[0] = (char)(((codepoint >> 6) & 0x1f) | 0xc0);
  1604. utf8[1] = (char)((codepoint & 0x3f) | 0x80);
  1605. size = 2;
  1606. }
  1607. else if (codepoint <= 0xffff)
  1608. {
  1609. utf8[0] = (char)(((codepoint >> 12) & 0x0f) | 0xe0);
  1610. utf8[1] = (char)(((codepoint >> 6) & 0x3f) | 0x80);
  1611. utf8[2] = (char)((codepoint & 0x3f) | 0x80);
  1612. size = 3;
  1613. }
  1614. else if (codepoint <= 0x10ffff)
  1615. {
  1616. utf8[0] = (char)(((codepoint >> 18) & 0x07) | 0xf0);
  1617. utf8[1] = (char)(((codepoint >> 12) & 0x3f) | 0x80);
  1618. utf8[2] = (char)(((codepoint >> 6) & 0x3f) | 0x80);
  1619. utf8[3] = (char)((codepoint & 0x3f) | 0x80);
  1620. size = 4;
  1621. }
  1622. *utf8Size = size;
  1623. return utf8;
  1624. }
  1625. #endif // SUPPORT_TEXT_MANIPULATION
  1626. // Get next codepoint in a UTF-8 encoded text, scanning until '\0' is found
  1627. // When an invalid UTF-8 byte is encountered we exit as soon as possible and a '?'(0x3f) codepoint is returned
  1628. // Total number of bytes processed are returned as a parameter
  1629. // NOTE: The standard says U+FFFD should be returned in case of errors
  1630. // but that character is not supported by the default font in raylib
  1631. int GetCodepoint(const char *text, int *codepointSize)
  1632. {
  1633. /*
  1634. UTF-8 specs from https://www.ietf.org/rfc/rfc3629.txt
  1635. Char. number range | UTF-8 octet sequence
  1636. (hexadecimal) | (binary)
  1637. --------------------+---------------------------------------------
  1638. 0000 0000-0000 007F | 0xxxxxxx
  1639. 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  1640. 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  1641. 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  1642. */
  1643. // NOTE: on decode errors we return as soon as possible
  1644. int codepoint = 0x3f; // Codepoint (defaults to '?')
  1645. int octet = (unsigned char)(text[0]); // The first UTF8 octet
  1646. *codepointSize = 1;
  1647. if (octet <= 0x7f)
  1648. {
  1649. // Only one octet (ASCII range x00-7F)
  1650. codepoint = text[0];
  1651. }
  1652. else if ((octet & 0xe0) == 0xc0)
  1653. {
  1654. // Two octets
  1655. // [0]xC2-DF [1]UTF8-tail(x80-BF)
  1656. unsigned char octet1 = text[1];
  1657. if ((octet1 == '\0') || ((octet1 >> 6) != 2)) { *codepointSize = 2; return codepoint; } // Unexpected sequence
  1658. if ((octet >= 0xc2) && (octet <= 0xdf))
  1659. {
  1660. codepoint = ((octet & 0x1f) << 6) | (octet1 & 0x3f);
  1661. *codepointSize = 2;
  1662. }
  1663. }
  1664. else if ((octet & 0xf0) == 0xe0)
  1665. {
  1666. // Three octets
  1667. unsigned char octet1 = text[1];
  1668. unsigned char octet2 = '\0';
  1669. if ((octet1 == '\0') || ((octet1 >> 6) != 2)) { *codepointSize = 2; return codepoint; } // Unexpected sequence
  1670. octet2 = text[2];
  1671. if ((octet2 == '\0') || ((octet2 >> 6) != 2)) { *codepointSize = 3; return codepoint; } // Unexpected sequence
  1672. // [0]xE0 [1]xA0-BF [2]UTF8-tail(x80-BF)
  1673. // [0]xE1-EC [1]UTF8-tail [2]UTF8-tail(x80-BF)
  1674. // [0]xED [1]x80-9F [2]UTF8-tail(x80-BF)
  1675. // [0]xEE-EF [1]UTF8-tail [2]UTF8-tail(x80-BF)
  1676. if (((octet == 0xe0) && !((octet1 >= 0xa0) && (octet1 <= 0xbf))) ||
  1677. ((octet == 0xed) && !((octet1 >= 0x80) && (octet1 <= 0x9f)))) { *codepointSize = 2; return codepoint; }
  1678. if ((octet >= 0xe0) && (octet <= 0xef))
  1679. {
  1680. codepoint = ((octet & 0xf) << 12) | ((octet1 & 0x3f) << 6) | (octet2 & 0x3f);
  1681. *codepointSize = 3;
  1682. }
  1683. }
  1684. else if ((octet & 0xf8) == 0xf0)
  1685. {
  1686. // Four octets
  1687. if (octet > 0xf4) return codepoint;
  1688. unsigned char octet1 = text[1];
  1689. unsigned char octet2 = '\0';
  1690. unsigned char octet3 = '\0';
  1691. if ((octet1 == '\0') || ((octet1 >> 6) != 2)) { *codepointSize = 2; return codepoint; } // Unexpected sequence
  1692. octet2 = text[2];
  1693. if ((octet2 == '\0') || ((octet2 >> 6) != 2)) { *codepointSize = 3; return codepoint; } // Unexpected sequence
  1694. octet3 = text[3];
  1695. if ((octet3 == '\0') || ((octet3 >> 6) != 2)) { *codepointSize = 4; return codepoint; } // Unexpected sequence
  1696. // [0]xF0 [1]x90-BF [2]UTF8-tail [3]UTF8-tail
  1697. // [0]xF1-F3 [1]UTF8-tail [2]UTF8-tail [3]UTF8-tail
  1698. // [0]xF4 [1]x80-8F [2]UTF8-tail [3]UTF8-tail
  1699. if (((octet == 0xf0) && !((octet1 >= 0x90) && (octet1 <= 0xbf))) ||
  1700. ((octet == 0xf4) && !((octet1 >= 0x80) && (octet1 <= 0x8f)))) { *codepointSize = 2; return codepoint; } // Unexpected sequence
  1701. if (octet >= 0xf0)
  1702. {
  1703. codepoint = ((octet & 0x7) << 18) | ((octet1 & 0x3f) << 12) | ((octet2 & 0x3f) << 6) | (octet3 & 0x3f);
  1704. *codepointSize = 4;
  1705. }
  1706. }
  1707. if (codepoint > 0x10ffff) codepoint = 0x3f; // Codepoints after U+10ffff are invalid
  1708. return codepoint;
  1709. }
  1710. // Get next codepoint in a byte sequence and bytes processed
  1711. int GetCodepointNext(const char *text, int *codepointSize)
  1712. {
  1713. const char *ptr = text;
  1714. int codepoint = 0x3f; // Codepoint (defaults to '?')
  1715. *codepointSize = 1;
  1716. // Get current codepoint and bytes processed
  1717. if (0xf0 == (0xf8 & ptr[0]))
  1718. {
  1719. // 4 byte UTF-8 codepoint
  1720. if (((ptr[1] & 0xC0) ^ 0x80) || ((ptr[2] & 0xC0) ^ 0x80) || ((ptr[3] & 0xC0) ^ 0x80)) { return codepoint; } // 10xxxxxx checks
  1721. codepoint = ((0x07 & ptr[0]) << 18) | ((0x3f & ptr[1]) << 12) | ((0x3f & ptr[2]) << 6) | (0x3f & ptr[3]);
  1722. *codepointSize = 4;
  1723. }
  1724. else if (0xe0 == (0xf0 & ptr[0]))
  1725. {
  1726. // 3 byte UTF-8 codepoint */
  1727. if (((ptr[1] & 0xC0) ^ 0x80) || ((ptr[2] & 0xC0) ^ 0x80)) { return codepoint; } // 10xxxxxx checks
  1728. codepoint = ((0x0f & ptr[0]) << 12) | ((0x3f & ptr[1]) << 6) | (0x3f & ptr[2]);
  1729. *codepointSize = 3;
  1730. }
  1731. else if (0xc0 == (0xe0 & ptr[0]))
  1732. {
  1733. // 2 byte UTF-8 codepoint
  1734. if ((ptr[1] & 0xC0) ^ 0x80) { return codepoint; } // 10xxxxxx checks
  1735. codepoint = ((0x1f & ptr[0]) << 6) | (0x3f & ptr[1]);
  1736. *codepointSize = 2;
  1737. }
  1738. else if (0x00 == (0x80 & ptr[0]))
  1739. {
  1740. // 1 byte UTF-8 codepoint
  1741. codepoint = ptr[0];
  1742. *codepointSize = 1;
  1743. }
  1744. return codepoint;
  1745. }
  1746. // Get previous codepoint in a byte sequence and bytes processed
  1747. int GetCodepointPrevious(const char *text, int *codepointSize)
  1748. {
  1749. const char *ptr = text;
  1750. int codepoint = 0x3f; // Codepoint (defaults to '?')
  1751. int cpSize = 0;
  1752. *codepointSize = 0;
  1753. // Move to previous codepoint
  1754. do ptr--;
  1755. while (((0x80 & ptr[0]) != 0) && ((0xc0 & ptr[0]) == 0x80));
  1756. codepoint = GetCodepointNext(ptr, &cpSize);
  1757. if (codepoint != 0) *codepointSize = cpSize;
  1758. return codepoint;
  1759. }
  1760. //----------------------------------------------------------------------------------
  1761. // Module specific Functions Definition
  1762. //----------------------------------------------------------------------------------
  1763. #if defined(SUPPORT_FILEFORMAT_FNT) || defined(SUPPORT_FILEFORMAT_BDF)
  1764. // Read a line from memory
  1765. // REQUIRES: memcpy()
  1766. // NOTE: Returns the number of bytes read
  1767. static int GetLine(const char *origin, char *buffer, int maxLength)
  1768. {
  1769. int count = 0;
  1770. for (; count < maxLength - 1; count++) if (origin[count] == '\n') break;
  1771. memcpy(buffer, origin, count);
  1772. buffer[count] = '\0';
  1773. return count;
  1774. }
  1775. #endif
  1776. #if defined(SUPPORT_FILEFORMAT_FNT)
  1777. // Load a BMFont file (AngelCode font file)
  1778. // REQUIRES: strstr(), sscanf(), strrchr(), memcpy()
  1779. static Font LoadBMFont(const char *fileName)
  1780. {
  1781. #define MAX_BUFFER_SIZE 256
  1782. #define MAX_FONT_IMAGE_PAGES 8
  1783. Font font = { 0 };
  1784. char buffer[MAX_BUFFER_SIZE] = { 0 };
  1785. char *searchPoint = NULL;
  1786. int fontSize = 0;
  1787. int glyphCount = 0;
  1788. int imWidth = 0;
  1789. int imHeight = 0;
  1790. int pageCount = 1;
  1791. char imFileName[MAX_FONT_IMAGE_PAGES][129] = { 0 };
  1792. int base = 0; // Useless data
  1793. int readBytes = 0; // Data bytes read
  1794. int readVars = 0; // Variables filled by sscanf()
  1795. char *fileText = LoadFileText(fileName);
  1796. if (fileText == NULL) return font;
  1797. char *fileTextPtr = fileText;
  1798. // NOTE: We skip first line, it contains no useful information
  1799. readBytes = GetLine(fileTextPtr, buffer, MAX_BUFFER_SIZE);
  1800. fileTextPtr += (readBytes + 1);
  1801. // Read line data
  1802. readBytes = GetLine(fileTextPtr, buffer, MAX_BUFFER_SIZE);
  1803. searchPoint = strstr(buffer, "lineHeight");
  1804. readVars = sscanf(searchPoint, "lineHeight=%i base=%i scaleW=%i scaleH=%i pages=%i", &fontSize, &base, &imWidth, &imHeight, &pageCount);
  1805. fileTextPtr += (readBytes + 1);
  1806. if (readVars < 4) { UnloadFileText(fileText); return font; } // Some data not available, file malformed
  1807. if (pageCount > MAX_FONT_IMAGE_PAGES)
  1808. {
  1809. TRACELOG(LOG_WARNING, "FONT: [%s] Font defines more pages than supported: %i/%i", fileName, pageCount, MAX_FONT_IMAGE_PAGES);
  1810. pageCount = MAX_FONT_IMAGE_PAGES;
  1811. }
  1812. for (int i = 0; i < pageCount; i++)
  1813. {
  1814. readBytes = GetLine(fileTextPtr, buffer, MAX_BUFFER_SIZE);
  1815. searchPoint = strstr(buffer, "file");
  1816. readVars = sscanf(searchPoint, "file=\"%128[^\"]\"", imFileName[i]);
  1817. fileTextPtr += (readBytes + 1);
  1818. if (readVars < 1) { UnloadFileText(fileText); return font; } // No fileName read
  1819. }
  1820. readBytes = GetLine(fileTextPtr, buffer, MAX_BUFFER_SIZE);
  1821. searchPoint = strstr(buffer, "count");
  1822. readVars = sscanf(searchPoint, "count=%i", &glyphCount);
  1823. fileTextPtr += (readBytes + 1);
  1824. if (readVars < 1) { UnloadFileText(fileText); return font; } // No glyphCount read
  1825. // Load all required images for further compose
  1826. Image *imFonts = (Image *)RL_CALLOC(pageCount, sizeof(Image)); // Font atlases, multiple images
  1827. for (int i = 0; i < pageCount; i++)
  1828. {
  1829. imFonts[i] = LoadImage(TextFormat("%s/%s", GetDirectoryPath(fileName), imFileName[i]));
  1830. if (imFonts[i].format == PIXELFORMAT_UNCOMPRESSED_GRAYSCALE)
  1831. {
  1832. // Convert image to GRAYSCALE + ALPHA, using the mask as the alpha channel
  1833. Image imFontAlpha = {
  1834. .data = RL_CALLOC(imFonts[i].width*imFonts[i].height, 2),
  1835. .width = imFonts[i].width,
  1836. .height = imFonts[i].height,
  1837. .mipmaps = 1,
  1838. .format = PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA
  1839. };
  1840. for (int p = 0, pi = 0; p < (imFonts[i].width*imFonts[i].height*2); p += 2, pi++)
  1841. {
  1842. ((unsigned char *)(imFontAlpha.data))[p] = 0xff;
  1843. ((unsigned char *)(imFontAlpha.data))[p + 1] = ((unsigned char *)imFonts[i].data)[pi];
  1844. }
  1845. UnloadImage(imFonts[i]);
  1846. imFonts[i] = imFontAlpha;
  1847. }
  1848. }
  1849. Image fullFont = imFonts[0];
  1850. for (int i = 1; i < pageCount; i++) UnloadImage(imFonts[i]);
  1851. // If multiple atlas, then merge atlas
  1852. // NOTE: WARNING: This process could be really slow!
  1853. if (pageCount > 1)
  1854. {
  1855. // Resize font atlas to draw additional images
  1856. ImageResizeCanvas(&fullFont, imWidth, imHeight*pageCount, 0, 0, BLACK);
  1857. for (int i = 1; i < pageCount; i++)
  1858. {
  1859. Rectangle srcRec = { 0.0f, 0.0f, (float)imWidth, (float)imHeight };
  1860. Rectangle destRec = { 0.0f, (float)imHeight*(float)i, (float)imWidth, (float)imHeight };
  1861. ImageDraw(&fullFont, imFonts[i], srcRec, destRec, WHITE);
  1862. }
  1863. }
  1864. RL_FREE(imFonts);
  1865. if (isGpuReady) font.texture = LoadTextureFromImage(fullFont);
  1866. // Fill font characters info data
  1867. font.baseSize = fontSize;
  1868. font.glyphCount = glyphCount;
  1869. font.glyphPadding = 0;
  1870. font.glyphs = (GlyphInfo *)RL_MALLOC(glyphCount*sizeof(GlyphInfo));
  1871. font.recs = (Rectangle *)RL_MALLOC(glyphCount*sizeof(Rectangle));
  1872. int charId, charX, charY, charWidth, charHeight, charOffsetX, charOffsetY, charAdvanceX, pageID;
  1873. for (int i = 0; i < glyphCount; i++)
  1874. {
  1875. readBytes = GetLine(fileTextPtr, buffer, MAX_BUFFER_SIZE);
  1876. readVars = sscanf(buffer, "char id=%i x=%i y=%i width=%i height=%i xoffset=%i yoffset=%i xadvance=%i page=%i",
  1877. &charId, &charX, &charY, &charWidth, &charHeight, &charOffsetX, &charOffsetY, &charAdvanceX, &pageID);
  1878. fileTextPtr += (readBytes + 1);
  1879. if (readVars == 9) // Make sure all char data has been properly read
  1880. {
  1881. // Get character rectangle in the font atlas texture
  1882. font.recs[i] = (Rectangle){ (float)charX, (float)charY + (float)imHeight*pageID, (float)charWidth, (float)charHeight };
  1883. // Save data properly in sprite font
  1884. font.glyphs[i].value = charId;
  1885. font.glyphs[i].offsetX = charOffsetX;
  1886. font.glyphs[i].offsetY = charOffsetY;
  1887. font.glyphs[i].advanceX = charAdvanceX;
  1888. // Fill character image data from full font data
  1889. font.glyphs[i].image = ImageFromImage(fullFont, font.recs[i]);
  1890. }
  1891. else
  1892. {
  1893. font.glyphs[i].image = GenImageColor((int)font.recs[i].width, (int)font.recs[i].height, BLACK);
  1894. TRACELOG(LOG_WARNING, "FONT: [%s] Some characters data not correctly provided", fileName);
  1895. }
  1896. }
  1897. UnloadImage(fullFont);
  1898. UnloadFileText(fileText);
  1899. if (isGpuReady && (font.texture.id == 0))
  1900. {
  1901. UnloadFont(font);
  1902. font = GetFontDefault();
  1903. TRACELOG(LOG_WARNING, "FONT: [%s] Failed to load texture, reverted to default font", fileName);
  1904. }
  1905. else TRACELOG(LOG_INFO, "FONT: [%s] Font loaded successfully (%i glyphs)", fileName, font.glyphCount);
  1906. return font;
  1907. }
  1908. #endif
  1909. #if defined(SUPPORT_FILEFORMAT_BDF)
  1910. // Convert hexadecimal to decimal (single digit)
  1911. static unsigned char HexToInt(char hex)
  1912. {
  1913. if ((hex >= '0') && (hex <= '9')) return hex - '0';
  1914. else if ((hex >= 'a') && (hex <= 'f')) return hex - 'a' + 10;
  1915. else if ((hex >= 'A') && (hex <= 'F')) return hex - 'A' + 10;
  1916. else return 0;
  1917. }
  1918. // Load font data for further use
  1919. // NOTE: Requires BDF font memory data
  1920. static GlyphInfo *LoadFontDataBDF(const unsigned char *fileData, int dataSize, int *codepoints, int codepointCount, int *outFontSize)
  1921. {
  1922. #define MAX_BUFFER_SIZE 256
  1923. char buffer[MAX_BUFFER_SIZE] = { 0 };
  1924. GlyphInfo *glyphs = NULL;
  1925. bool genFontChars = false;
  1926. int totalReadBytes = 0; // Data bytes read (total)
  1927. int readBytes = 0; // Data bytes read (line)
  1928. int readVars = 0; // Variables filled by sscanf()
  1929. const char *fileText = (const char *)fileData;
  1930. const char *fileTextPtr = fileText;
  1931. bool fontMalformed = false; // Is the font malformed
  1932. bool fontStarted = false; // Has font started (STARTFONT)
  1933. int fontBBw = 0; // Font base character bounding box width
  1934. int fontBBh = 0; // Font base character bounding box height
  1935. int fontBBxoff0 = 0; // Font base character bounding box X0 offset
  1936. int fontBByoff0 = 0; // Font base character bounding box Y0 offset
  1937. int fontAscent = 0; // Font ascent
  1938. bool charStarted = false; // Has character started (STARTCHAR)
  1939. bool charBitmapStarted = false; // Has bitmap data started (BITMAP)
  1940. int charBitmapNextRow = 0; // Y position for the next row of bitmap data
  1941. int charEncoding = -1; // The unicode value of the character (-1 if not set)
  1942. int charBBw = 0; // Character bounding box width
  1943. int charBBh = 0; // Character bounding box height
  1944. int charBBxoff0 = 0; // Character bounding box X0 offset
  1945. int charBByoff0 = 0; // Character bounding box Y0 offset
  1946. int charDWidthX = 0; // Character advance X
  1947. int charDWidthY = 0; // Character advance Y (unused)
  1948. GlyphInfo *charGlyphInfo = NULL; // Pointer to output glyph info (NULL if not set)
  1949. if (fileData == NULL) return glyphs;
  1950. // In case no chars count provided, default to 95
  1951. codepointCount = (codepointCount > 0)? codepointCount : 95;
  1952. // Fill fontChars in case not provided externally
  1953. // NOTE: By default we fill glyphCount consecutively, starting at 32 (Space)
  1954. if (codepoints == NULL)
  1955. {
  1956. codepoints = (int *)RL_MALLOC(codepointCount*sizeof(int));
  1957. for (int i = 0; i < codepointCount; i++) codepoints[i] = i + 32;
  1958. genFontChars = true;
  1959. }
  1960. glyphs = (GlyphInfo *)RL_CALLOC(codepointCount, sizeof(GlyphInfo));
  1961. while (totalReadBytes <= dataSize)
  1962. {
  1963. readBytes = GetLine(fileTextPtr, buffer, MAX_BUFFER_SIZE);
  1964. totalReadBytes += (readBytes + 1);
  1965. fileTextPtr += (readBytes + 1);
  1966. // Line: COMMENT
  1967. if (strstr(buffer, "COMMENT") != NULL) continue; // Ignore line
  1968. if (charStarted)
  1969. {
  1970. // Line: ENDCHAR
  1971. if (strstr(buffer, "ENDCHAR") != NULL)
  1972. {
  1973. charStarted = false;
  1974. continue;
  1975. }
  1976. if (charBitmapStarted)
  1977. {
  1978. if (charGlyphInfo != NULL)
  1979. {
  1980. int pixelY = charBitmapNextRow++;
  1981. if (pixelY >= charGlyphInfo->image.height) break;
  1982. for (int x = 0; x < readBytes; x++)
  1983. {
  1984. unsigned char byte = HexToInt(buffer[x]);
  1985. for (int bitX = 0; bitX < 4; bitX++)
  1986. {
  1987. int pixelX = ((x*4) + bitX);
  1988. if (pixelX >= charGlyphInfo->image.width) break;
  1989. if ((byte & (8 >> bitX)) > 0) ((unsigned char *)charGlyphInfo->image.data)[(pixelY*charGlyphInfo->image.width) + pixelX] = 255;
  1990. }
  1991. }
  1992. }
  1993. continue;
  1994. }
  1995. // Line: ENCODING
  1996. if (strstr(buffer, "ENCODING") != NULL)
  1997. {
  1998. readVars = sscanf(buffer, "ENCODING %i", &charEncoding);
  1999. continue;
  2000. }
  2001. // Line: BBX
  2002. if (strstr(buffer, "BBX") != NULL)
  2003. {
  2004. readVars = sscanf(buffer, "BBX %i %i %i %i", &charBBw, &charBBh, &charBBxoff0, &charBByoff0);
  2005. continue;
  2006. }
  2007. // Line: DWIDTH
  2008. if (strstr(buffer, "DWIDTH") != NULL)
  2009. {
  2010. readVars = sscanf(buffer, "DWIDTH %i %i", &charDWidthX, &charDWidthY);
  2011. continue;
  2012. }
  2013. // Line: BITMAP
  2014. if (strstr(buffer, "BITMAP") != NULL)
  2015. {
  2016. // Search for glyph index in codepoints
  2017. charGlyphInfo = NULL;
  2018. for (int codepointIndex = 0; codepointIndex < codepointCount; codepointIndex++)
  2019. {
  2020. if (codepoints[codepointIndex] == charEncoding)
  2021. {
  2022. charGlyphInfo = &glyphs[codepointIndex];
  2023. break;
  2024. }
  2025. }
  2026. // Init glyph info
  2027. if (charGlyphInfo != NULL)
  2028. {
  2029. charGlyphInfo->value = charEncoding;
  2030. charGlyphInfo->offsetX = charBBxoff0 + fontBByoff0;
  2031. charGlyphInfo->offsetY = fontBBh - (charBBh + charBByoff0 + fontBByoff0 + fontAscent);
  2032. charGlyphInfo->advanceX = charDWidthX;
  2033. charGlyphInfo->image.data = RL_CALLOC(charBBw*charBBh, 1);
  2034. charGlyphInfo->image.width = charBBw;
  2035. charGlyphInfo->image.height = charBBh;
  2036. charGlyphInfo->image.mipmaps = 1;
  2037. charGlyphInfo->image.format = PIXELFORMAT_UNCOMPRESSED_GRAYSCALE;
  2038. }
  2039. charBitmapStarted = true;
  2040. charBitmapNextRow = 0;
  2041. continue;
  2042. }
  2043. }
  2044. else if (fontStarted)
  2045. {
  2046. // Line: ENDFONT
  2047. if (strstr(buffer, "ENDFONT") != NULL)
  2048. {
  2049. fontStarted = false;
  2050. break;
  2051. }
  2052. // Line: SIZE
  2053. if (strstr(buffer, "SIZE") != NULL)
  2054. {
  2055. if (outFontSize != NULL) readVars = sscanf(buffer, "SIZE %i", outFontSize);
  2056. continue;
  2057. }
  2058. // PIXEL_SIZE
  2059. if (strstr(buffer, "PIXEL_SIZE") != NULL)
  2060. {
  2061. if (outFontSize != NULL) readVars = sscanf(buffer, "PIXEL_SIZE %i", outFontSize);
  2062. continue;
  2063. }
  2064. // FONTBOUNDINGBOX
  2065. if (strstr(buffer, "FONTBOUNDINGBOX") != NULL)
  2066. {
  2067. readVars = sscanf(buffer, "FONTBOUNDINGBOX %i %i %i %i", &fontBBw, &fontBBh, &fontBBxoff0, &fontBByoff0);
  2068. continue;
  2069. }
  2070. // FONT_ASCENT
  2071. if (strstr(buffer, "FONT_ASCENT") != NULL)
  2072. {
  2073. readVars = sscanf(buffer, "FONT_ASCENT %i", &fontAscent);
  2074. continue;
  2075. }
  2076. // STARTCHAR
  2077. if (strstr(buffer, "STARTCHAR") != NULL)
  2078. {
  2079. charStarted = true;
  2080. charEncoding = -1;
  2081. charGlyphInfo = NULL;
  2082. charBBw = 0;
  2083. charBBh = 0;
  2084. charBBxoff0 = 0;
  2085. charBByoff0 = 0;
  2086. charDWidthX = 0;
  2087. charDWidthY = 0;
  2088. charGlyphInfo = NULL;
  2089. charBitmapStarted = false;
  2090. charBitmapNextRow = 0;
  2091. continue;
  2092. }
  2093. }
  2094. else
  2095. {
  2096. // STARTFONT
  2097. if (strstr(buffer, "STARTFONT") != NULL)
  2098. {
  2099. if (fontStarted)
  2100. {
  2101. fontMalformed = true;
  2102. break;
  2103. }
  2104. else
  2105. {
  2106. fontStarted = true;
  2107. continue;
  2108. }
  2109. }
  2110. }
  2111. }
  2112. if (genFontChars) RL_FREE(codepoints);
  2113. if (fontMalformed)
  2114. {
  2115. RL_FREE(glyphs);
  2116. glyphs = NULL;
  2117. }
  2118. return glyphs;
  2119. }
  2120. #endif // SUPPORT_FILEFORMAT_BDF
  2121. #endif // SUPPORT_MODULE_RTEXT