messed something up with git running it back to an older version

This commit is contained in:
DissolveDZ 2023-09-30 19:08:57 +02:00
parent 5c85cbaebd
commit 4a1f239c2a

View File

@ -215,8 +215,7 @@ extern void LoadFontDefault(void)
.width = 128,
.height = 128,
.mipmaps = 1,
.format = PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA
};
.format = PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA};
// Fill image.data with defaultFontData (convert from bit to pixel!)
for (int i = 0, counter = 0; i < imFont.width * imFont.height; i += 32)
@ -229,7 +228,8 @@ extern void LoadFontDefault(void)
// we must consider data as little-endian order (alpha + gray)
((unsigned short *)imFont.data)[i + j] = 0xffff;
}
else ((unsigned short *)imFont.data)[i + j] = 0x00ff;
else
((unsigned short *)imFont.data)[i + j] = 0x00ff;
}
counter++;
@ -269,7 +269,8 @@ extern void LoadFontDefault(void)
defaultFont.recs[i].x = (float)charsDivisor;
defaultFont.recs[i].y = (float)(charsDivisor + currentLine * (charsHeight + charsDivisor));
}
else currentPosX = testPosX;
else
currentPosX = testPosX;
// NOTE: On default font character offsets and xAdvance are not required
defaultFont.glyphs[i].offsetX = 0;
@ -290,7 +291,8 @@ extern void LoadFontDefault(void)
// Unload raylib default font
extern void UnloadFontDefault(void)
{
for (int i = 0; i < defaultFont.glyphCount; i++) UnloadImage(defaultFont.glyphs[i].image);
for (int i = 0; i < defaultFont.glyphCount; i++)
UnloadImage(defaultFont.glyphs[i].image);
UnloadTexture(defaultFont.texture);
RL_FREE(defaultFont.glyphs);
RL_FREE(defaultFont.recs);
@ -328,16 +330,19 @@ Font LoadFont(const char *fileName)
Font font = {0};
#if defined(SUPPORT_FILEFORMAT_TTF)
if (IsFileExtension(fileName, ".ttf") || IsFileExtension(fileName, ".otf")) font = LoadFontEx(fileName, FONT_TTF_DEFAULT_SIZE, NULL, FONT_TTF_DEFAULT_NUMCHARS);
if (IsFileExtension(fileName, ".ttf") || IsFileExtension(fileName, ".otf"))
font = LoadFontEx(fileName, FONT_TTF_DEFAULT_SIZE, NULL, FONT_TTF_DEFAULT_NUMCHARS);
else
#endif
#if defined(SUPPORT_FILEFORMAT_FNT)
if (IsFileExtension(fileName, ".fnt")) font = LoadBMFont(fileName);
if (IsFileExtension(fileName, ".fnt"))
font = LoadBMFont(fileName);
else
#endif
{
Image image = LoadImage(fileName);
if (image.data != NULL) font = LoadFontFromImage(image, MAGENTA, FONT_TTF_DEFAULT_FIRST_CHAR);
if (image.data != NULL)
font = LoadFontFromImage(image, MAGENTA, FONT_TTF_DEFAULT_FIRST_CHAR);
UnloadImage(image);
}
@ -373,7 +378,8 @@ Font LoadFontEx(const char *fileName, int fontSize, int *codepoints, int codepoi
UnloadFileData(fileData);
}
else font = GetFontDefault();
else
font = GetFontDefault();
return font;
}
@ -407,13 +413,16 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
{
for (x = 0; x < image.width; x++)
{
if (!COLOR_EQUAL(pixels[y*image.width + x], key)) break;
if (!COLOR_EQUAL(pixels[y * image.width + x], key))
break;
}
if (!COLOR_EQUAL(pixels[y*image.width + x], key)) break;
if (!COLOR_EQUAL(pixels[y * image.width + x], key))
break;
}
if ((x == 0) || (y == 0)) return font;
if ((x == 0) || (y == 0))
return font;
charSpacing = x;
lineSpacing = y;
@ -421,7 +430,8 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
int charHeight = 0;
int j = 0;
while (!COLOR_EQUAL(pixels[(lineSpacing + j)*image.width + charSpacing], key)) j++;
while (!COLOR_EQUAL(pixels[(lineSpacing + j) * image.width + charSpacing], key))
j++;
charHeight = j;
@ -444,7 +454,8 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
int charWidth = 0;
while (!COLOR_EQUAL(pixels[(lineSpacing + (charHeight+lineSpacing)*lineToRead)*image.width + xPosToRead + charWidth], key)) charWidth++;
while (!COLOR_EQUAL(pixels[(lineSpacing + (charHeight + lineSpacing) * lineToRead) * image.width + xPosToRead + charWidth], key))
charWidth++;
tempCharRecs[index].width = (float)charWidth;
@ -459,7 +470,9 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
// NOTE: We need to remove key color borders from image to avoid weird
// artifacts on texture scaling when using TEXTURE_FILTER_BILINEAR or TEXTURE_FILTER_TRILINEAR
for (int i = 0; i < image.height*image.width; i++) if (COLOR_EQUAL(pixels[i], key)) pixels[i] = BLANK;
for (int i = 0; i < image.height * image.width; i++)
if (COLOR_EQUAL(pixels[i], key))
pixels[i] = BLANK;
// Create a new image with the processed color data (key color replaced by BLANK)
Image fontClear = {
@ -467,8 +480,7 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
.width = image.width,
.height = image.height,
.mipmaps = 1,
.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8
};
.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8};
// Set font with all data parsed from image
font.texture = LoadTextureFromImage(fontClear); // Convert processed image to OpenGL texture
@ -538,7 +550,8 @@ Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int
TRACELOG(LOG_INFO, "FONT: Data loaded successfully (%i pixel size | %i glyphs)", font.baseSize, font.glyphCount);
}
else font = GetFontDefault();
else
font = GetFontDefault();
}
#else
font = GetFontDefault();
@ -608,7 +621,8 @@ GlyphInfo *LoadFontData(const unsigned char *fileData, int dataSize, int fontSiz
if (codepoints == NULL)
{
codepoints = (int *)RL_MALLOC(codepointCount * sizeof(int));
for (int i = 0; i < codepointCount; i++) codepoints[i] = i + 32;
for (int i = 0; i < codepointCount; i++)
codepoints[i] = i + 32;
genFontChars = true;
}
@ -626,9 +640,12 @@ GlyphInfo *LoadFontData(const unsigned char *fileData, int dataSize, int fontSiz
// stbtt_GetCodepointBitmapBox() -- how big the bitmap must be
// stbtt_MakeCodepointBitmap() -- renders into bitmap you provide
if (type != FONT_SDF) chars[i].image.data = stbtt_GetCodepointBitmap(&fontInfo, scaleFactor, scaleFactor, ch, &chw, &chh, &chars[i].offsetX, &chars[i].offsetY);
else 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);
else chars[i].image.data = NULL;
if (type != FONT_SDF)
chars[i].image.data = stbtt_GetCodepointBitmap(&fontInfo, scaleFactor, scaleFactor, ch, &chw, &chh, &chars[i].offsetX, &chars[i].offsetY);
else 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);
else
chars[i].image.data = NULL;
stbtt_GetCodepointHMetrics(&fontInfo, ch, &chars[i].advanceX, NULL);
chars[i].advanceX = (int)((float)chars[i].advanceX * scaleFactor);
@ -649,8 +666,7 @@ GlyphInfo *LoadFontData(const unsigned char *fileData, int dataSize, int fontSiz
.width = chars[i].advanceX,
.height = fontSize,
.mipmaps = 1,
.format = PIXELFORMAT_UNCOMPRESSED_GRAYSCALE
};
.format = PIXELFORMAT_UNCOMPRESSED_GRAYSCALE};
chars[i].image = imSpace;
}
@ -661,8 +677,10 @@ GlyphInfo *LoadFontData(const unsigned char *fileData, int dataSize, int fontSiz
// NOTE: For optimum results, bitmap font should be generated at base pixel size
for (int p = 0; p < chw * chh; p++)
{
if (((unsigned char *)chars[i].image.data)[p] < FONT_BITMAP_ALPHA_THRESHOLD) ((unsigned char *)chars[i].image.data)[p] = 0;
else ((unsigned char *)chars[i].image.data)[p] = 255;
if (((unsigned char *)chars[i].image.data)[p] < FONT_BITMAP_ALPHA_THRESHOLD)
((unsigned char *)chars[i].image.data)[p] = 0;
else
((unsigned char *)chars[i].image.data)[p] = 255;
}
}
@ -676,9 +694,11 @@ GlyphInfo *LoadFontData(const unsigned char *fileData, int dataSize, int fontSiz
*/
}
}
else TRACELOG(LOG_WARNING, "FONT: Failed to process TTF font data");
else
TRACELOG(LOG_WARNING, "FONT: Failed to process TTF font data");
if (genFontChars) RL_FREE(codepoints);
if (genFontChars)
RL_FREE(codepoints);
}
#endif
@ -712,7 +732,8 @@ Image GenImageFontAtlas(const GlyphInfo *glyphs, Rectangle **glyphRecs, int glyp
for (int i = 0; i < glyphCount; i++)
{
if (glyphs[i].image.width > maxGlyphWidth) maxGlyphWidth = glyphs[i].image.width;
if (glyphs[i].image.width > maxGlyphWidth)
maxGlyphWidth = glyphs[i].image.width;
totalWidth += glyphs[i].image.width + 4 * padding;
}
@ -845,7 +866,8 @@ Image GenImageFontAtlas(const GlyphInfo *glyphs, Rectangle **glyphRecs, int glyp
}
}
}
else TRACELOG(LOG_WARNING, "FONT: Failed to package character (%i)", i);
else
TRACELOG(LOG_WARNING, "FONT: Failed to package character (%i)", i);
}
RL_FREE(rects);
@ -890,7 +912,8 @@ void UnloadFontData(GlyphInfo *glyphs, int glyphCount)
{
if (glyphs != NULL)
{
for (int i = 0; i < glyphCount; i++) UnloadImage(glyphs[i].image);
for (int i = 0; i < glyphCount; i++)
UnloadImage(glyphs[i].image);
RL_FREE(glyphs);
}
@ -952,7 +975,8 @@ bool ExportFontAsCode(Font font, const char *fileName)
// Support font export and initialization
// NOTE: This mechanism is highly coupled to raylib
Image image = LoadImageFromTexture(font.texture);
if (image.format != PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA) TRACELOG(LOG_WARNING, "Font export as code: Font image format is not GRAY+ALPHA!");
if (image.format != PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA)
TRACELOG(LOG_WARNING, "Font export as code: Font image format is not GRAY+ALPHA!");
int imageDataSize = GetPixelDataSize(image.width, image.height, image.format);
// Image data is usually GRAYSCALE + ALPHA and can be reduced to GRAYSCALE
@ -973,7 +997,8 @@ bool ExportFontAsCode(Font font, const char *fileName)
byteCount += sprintf(txtData + byteCount, "// Font image pixels data compressed (DEFLATE)\n");
byteCount += sprintf(txtData + byteCount, "// NOTE: Original pixel data simplified to GRAYSCALE\n");
byteCount += sprintf(txtData + byteCount, "static unsigned char fontData_%s[COMPRESSED_DATA_SIZE_FONT_%s] = { ", fileNamePascal, TextToUpper(fileNamePascal));
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]);
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]);
byteCount += sprintf(txtData + byteCount, "0x%02x };\n\n", compData[compDataSize - 1]);
RL_FREE(compData);
#else
@ -981,7 +1006,8 @@ bool ExportFontAsCode(Font font, const char *fileName)
byteCount += sprintf(txtData + byteCount, "// Font image pixels data\n");
byteCount += sprintf(txtData + byteCount, "// NOTE: 2 bytes per pixel, GRAY + ALPHA channels\n");
byteCount += sprintf(txtData + byteCount, "static unsigned char fontImageData_%s[%i] = { ", fileNamePascal, imageDataSize);
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]);
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]);
byteCount += sprintf(txtData + byteCount, "0x%02x };\n\n", ((unsigned char *)imFont.data)[imageDataSize - 1]);
#endif
@ -1058,13 +1084,14 @@ bool ExportFontAsCode(Font font, const char *fileName)
RL_FREE(txtData);
if (success != 0) TRACELOG(LOG_INFO, "FILEIO: [%s] Font as code exported successfully", fileName);
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to export font as code", fileName);
if (success != 0)
TRACELOG(LOG_INFO, "FILEIO: [%s] Font as code exported successfully", fileName);
else
TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to export font as code", fileName);
return success;
}
// Draw current FPS
// NOTE: Uses default font
void DrawFPS(int posX, int posY)
@ -1072,8 +1099,10 @@ void DrawFPS(int posX, int posY)
Color color = LIME; // Good FPS
int fps = GetFPS();
if ((fps < 30) && (fps >= 15)) color = ORANGE; // Warning FPS
else if (fps < 15) color = RED; // Low FPS
if ((fps < 30) && (fps >= 15))
color = ORANGE; // Warning FPS
else if (fps < 15)
color = RED; // Low FPS
DrawText(TextFormat("%2i FPS", fps), posX, posY, 20, color);
}
@ -1089,7 +1118,8 @@ void DrawText(const char *text, int posX, int posY, int fontSize, Color color)
Vector2 position = {(float)posX, (float)posY};
int defaultFontSize = 10; // Default Font chars height in pixel
if (fontSize < defaultFontSize) fontSize = defaultFontSize;
if (fontSize < defaultFontSize)
fontSize = defaultFontSize;
int spacing = fontSize / defaultFontSize;
DrawTextEx(GetFontDefault(), text, position, (float)fontSize, (float)spacing, color);
@ -1100,7 +1130,8 @@ void DrawText(const char *text, int posX, int posY, int fontSize, Color color)
// NOTE: chars spacing is NOT proportional to fontSize
void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, float spacing, Color tint)
{
if (font.texture.id == 0) font = GetFontDefault(); // Security check in case of not valid font
if (font.texture.id == 0)
font = GetFontDefault(); // Security check in case of not valid font
int size = TextLength(text); // Total size in bytes of the text, scanned by codepoints in loop
@ -1129,8 +1160,10 @@ void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, f
DrawTextCodepoint(font, codepoint, (Vector2){position.x + textOffsetX, position.y + textOffsetY}, fontSize, tint);
}
if (font.glyphs[index].advanceX == 0) textOffsetX += ((float)font.recs[index].width*scaleFactor + spacing);
else textOffsetX += ((float)font.glyphs[index].advanceX*scaleFactor + spacing);
if (font.glyphs[index].advanceX == 0)
textOffsetX += ((float)font.recs[index].width * scaleFactor + spacing);
else
textOffsetX += ((float)font.glyphs[index].advanceX * scaleFactor + spacing);
}
i += codepointByteCount; // Move text bytes counter to next codepoint
@ -1200,8 +1233,10 @@ void DrawTextCodepoints(Font font, const int *codepoints, int codepointCount, Ve
DrawTextCodepoint(font, codepoints[i], (Vector2){position.x + textOffsetX, position.y + textOffsetY}, fontSize, tint);
}
if (font.glyphs[index].advanceX == 0) textOffsetX += ((float)font.recs[index].width*scaleFactor + spacing);
else textOffsetX += ((float)font.glyphs[index].advanceX*scaleFactor + spacing);
if (font.glyphs[index].advanceX == 0)
textOffsetX += ((float)font.recs[index].width * scaleFactor + spacing);
else
textOffsetX += ((float)font.glyphs[index].advanceX * scaleFactor + spacing);
}
}
}
@ -1221,7 +1256,8 @@ int MeasureText(const char *text, int fontSize)
if (GetFontDefault().texture.id != 0)
{
int defaultFontSize = 10; // Default Font chars height in pixel
if (fontSize < defaultFontSize) fontSize = defaultFontSize;
if (fontSize < defaultFontSize)
fontSize = defaultFontSize;
int spacing = fontSize / defaultFontSize;
textSize = MeasureTextEx(GetFontDefault(), text, (float)fontSize, (float)spacing);
@ -1235,7 +1271,8 @@ Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing
{
Vector2 textSize = {0};
if ((font.texture.id == 0) || (text == NULL)) return textSize;
if ((font.texture.id == 0) || (text == NULL))
return textSize;
int size = TextLength(text); // Get size in bytes of text
int tempByteCounter = 0; // Used to count longer text line num chars
@ -1262,12 +1299,15 @@ Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing
if (letter != '\n')
{
if (font.glyphs[index].advanceX != 0) textWidth += font.glyphs[index].advanceX;
else textWidth += (font.recs[index].width + font.glyphs[index].offsetX);
if (font.glyphs[index].advanceX != 0)
textWidth += font.glyphs[index].advanceX;
else
textWidth += (font.recs[index].width + font.glyphs[index].offsetX);
}
else
{
if (tempTextWidth < textWidth) tempTextWidth = textWidth;
if (tempTextWidth < textWidth)
tempTextWidth = textWidth;
byteCounter = 0;
textWidth = 0;
@ -1275,10 +1315,12 @@ Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing
textHeight += (float)textLineSpacing;
}
if (tempByteCounter < byteCounter) tempByteCounter = byteCounter;
if (tempByteCounter < byteCounter)
tempByteCounter = byteCounter;
}
if (tempTextWidth < textWidth) tempTextWidth = textWidth;
if (tempTextWidth < textWidth)
tempTextWidth = textWidth;
textSize.x = tempTextWidth * scaleFactor + (float)((tempByteCounter - 1) * spacing);
textSize.y = textHeight * scaleFactor;
@ -1299,7 +1341,8 @@ int GetGlyphIndex(Font font, int codepoint)
// Look for character index in the unordered charset
for (int i = 0; i < font.glyphCount; i++)
{
if (font.glyphs[i].value == 63) fallbackIndex = i;
if (font.glyphs[i].value == 63)
fallbackIndex = i;
if (font.glyphs[i].value == codepoint)
{
@ -1308,7 +1351,8 @@ int GetGlyphIndex(Font font, int codepoint)
}
}
if ((index == 0) && (font.glyphs[0].value != codepoint)) index = fallbackIndex;
if ((index == 0) && (font.glyphs[0].value != codepoint))
index = fallbackIndex;
#else
index = codepoint - 32;
#endif
@ -1344,40 +1388,44 @@ Rectangle GetGlyphAtlasRec(Font font, int codepoint)
// Get text length in bytes, check for \0 character
unsigned int TextLength(const char *text)
{
unsigned int length = 0; //strlen(text)
if (!text)
return 0;
if (text != NULL)
{
while (*text++) length++;
}
// use strlen since it uses vector operations
unsigned int length = strlen(text);
return length;
}
// Formatting of text with variables to 'embed'
// WARNING: String returned will expire after this function is called MAX_TEXTFORMAT_BUFFERS times
const char *TextFormat(const char *text, ...)
const char *TextFormat(const char *format, ...)
{
#ifndef MAX_TEXTFORMAT_BUFFERS
#define MAX_TEXTFORMAT_BUFFERS 4 // Maximum number of static buffers for text formatting
#endif
// We create an array of buffers so strings don't expire until MAX_TEXTFORMAT_BUFFERS invocations
static char buffers[MAX_TEXTFORMAT_BUFFERS][MAX_TEXT_BUFFER_LENGTH] = { 0 };
static int index = 0;
char *currentBuffer = buffers[index];
memset(currentBuffer, 0, MAX_TEXT_BUFFER_LENGTH); // Clear buffer before using
va_list args;
va_start(args, text);
vsnprintf(currentBuffer, MAX_TEXT_BUFFER_LENGTH, text, args);
va_start(args, format);
int length = vsnprintf(NULL, 0, format, args); // Determine the length of the formatted string
if (length < 0)
{
// Handle error
va_end(args);
return NULL;
}
char *result = (char *)malloc(length + 1); // Allocate memory for the formatted string (+1 for null terminator)
if (result == NULL)
{
// Handle memory allocation failure
va_end(args);
return NULL;
}
va_end(args); // Reset the va_list
va_start(args, format); // Start again for the actual formatting
vsnprintf(result, length + 1, format, args); // Format the string and copy it to the result buffer
va_end(args);
index += 1; // Move to next buffer for next function call
if (index >= MAX_TEXTFORMAT_BUFFERS) index = 0;
return currentBuffer;
return result;
}
// Get integer value from text
@ -1389,11 +1437,13 @@ int TextToInteger(const char *text)
if ((text[0] == '+') || (text[0] == '-'))
{
if (text[0] == '-') sign = -1;
if (text[0] == '-')
sign = -1;
text++;
}
for (int i = 0; ((text[i] >= '0') && (text[i] <= '9')); i++) value = value*10 + (int)(text[i] - '0');
for (int i = 0; ((text[i] >= '0') && (text[i] <= '9')); i++)
value = value * 10 + (int)(text[i] - '0');
return value * sign;
}
@ -1427,10 +1477,8 @@ bool TextIsEqual(const char *text1, const char *text2)
{
bool result = false;
if ((text1 != NULL) && (text2 != NULL))
{
if (strcmp(text1, text2) == 0) result = true;
}
if (text1 && text2 && !strcmp(text1, text2))
result = true;
return result;
}
@ -1438,8 +1486,8 @@ bool TextIsEqual(const char *text1, const char *text2)
// Get a piece of a text string
const char *TextSubtext(const char *text, int position, int length)
{
static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 };
memset(buffer, 0, MAX_TEXT_BUFFER_LENGTH);
if (!length || !text)
return "";
int textLength = TextLength(text);
@ -1449,15 +1497,12 @@ const char *TextSubtext(const char *text, int position, int length)
length = 0;
}
if (length >= textLength) length = textLength;
if (length > textLength)
length = textLength;
for (int c = 0 ; c < length ; c++)
{
*(buffer + c) = *(text + position);
text++;
}
*(buffer + length) = '\0';
char *buffer = malloc(length + 1);
memcpy(buffer, text + position, length);
buffer[length] = '\0';
return buffer;
}
@ -1468,7 +1513,8 @@ const char *TextSubtext(const char *text, int position, int length)
char *TextReplace(char *text, const char *replace, const char *by)
{
// Sanity checks and initialization
if (!text || !replace || !by) return NULL;
if (!text || !replace || !by)
return NULL;
char *result = NULL;
@ -1480,18 +1526,21 @@ char *TextReplace(char *text, const char *replace, const char *by)
int count = 0; // Number of replacements
replaceLen = TextLength(replace);
if (replaceLen == 0) return NULL; // Empty replace causes infinite loop during count
if (replaceLen == 0)
return NULL; // Empty replace causes infinite loop during count
byLen = TextLength(by);
// Count the number of replacements needed
insertPoint = text;
for (count = 0; (temp = strstr(insertPoint, replace)); count++) insertPoint = temp + replaceLen;
for (count = 0; (temp = strstr(insertPoint, replace)); count++)
insertPoint = temp + replaceLen;
// Allocate returning string and point temp to it
temp = result = (char *)RL_MALLOC(TextLength(text) + (byLen - replaceLen) * count + 1);
if (!result) return NULL; // Memory could not be allocated
if (!result)
return NULL; // Memory could not be allocated
// First time through the loop, all the variable are set correctly from here on,
// - 'temp' points to the end of the result string
@ -1521,9 +1570,12 @@ char *TextInsert(const char *text, const char *insert, int position)
char *result = (char *)RL_MALLOC(textLen + insertLen + 1);
for (int i = 0; i < position; i++) result[i] = text[i];
for (int i = position; i < insertLen + position; i++) result[i] = insert[i];
for (int i = (insertLen + position); i < (textLen + insertLen); i++) result[i] = text[i];
for (int i = 0; i < position; i++)
result[i] = text[i];
for (int i = position; i < insertLen + position; i++)
result[i] = insert[i];
for (int i = (insertLen + position); i < (textLen + insertLen); i++)
result[i] = text[i];
result[textLen + insertLen] = '\0'; // Make sure text string is valid!
@ -1589,14 +1641,16 @@ const char **TextSplit(const char *text, char delimiter, int *count)
for (int i = 0; i < MAX_TEXT_BUFFER_LENGTH; i++)
{
buffer[i] = text[i];
if (buffer[i] == '\0') break;
if (buffer[i] == '\0')
break;
else if (buffer[i] == delimiter)
{
buffer[i] = '\0'; // Set an end of string at this point
result[counter] = buffer + i + 1;
counter++;
if (counter == MAX_TEXTSPLIT_COUNT) break;
if (counter == MAX_TEXTSPLIT_COUNT)
break;
}
}
}
@ -1621,7 +1675,8 @@ int TextFindIndex(const char *text, const char *find)
char *ptr = strstr(text, find);
if (ptr != NULL) position = (int)(ptr - text);
if (ptr != NULL)
position = (int)(ptr - text);
return position;
}
@ -1638,8 +1693,10 @@ const char *TextToUpper(const char *text)
{
for (int i = 0; (i < MAX_TEXT_BUFFER_LENGTH - 1) && (text[i] != '\0'); i++)
{
if ((text[i] >= 'a') && (text[i] <= 'z')) buffer[i] = text[i] - 32;
else buffer[i] = text[i];
if ((text[i] >= 'a') && (text[i] <= 'z'))
buffer[i] = text[i] - 32;
else
buffer[i] = text[i];
}
}
@ -1657,8 +1714,10 @@ const char *TextToLower(const char *text)
{
for (int i = 0; (i < MAX_TEXT_BUFFER_LENGTH - 1) && (text[i] != '\0'); i++)
{
if ((text[i] >= 'A') && (text[i] <= 'Z')) buffer[i] = text[i] + 32;
else buffer[i] = text[i];
if ((text[i] >= 'A') && (text[i] <= 'Z'))
buffer[i] = text[i] + 32;
else
buffer[i] = text[i];
}
}
@ -1675,17 +1734,21 @@ const char *TextToPascal(const char *text)
if (text != NULL)
{
// Upper case first character
if ((text[0] >= 'a') && (text[0] <= 'z')) buffer[0] = text[0] - 32;
else buffer[0] = text[0];
if ((text[0] >= 'a') && (text[0] <= 'z'))
buffer[0] = text[0] - 32;
else
buffer[0] = text[0];
// Check for next separator to upper case another character
for (int i = 1, j = 1; (i < MAX_TEXT_BUFFER_LENGTH - 1) && (text[j] != '\0'); i++, j++)
{
if (text[j] != '_') buffer[i] = text[j];
if (text[j] != '_')
buffer[i] = text[j];
else
{
j++;
if ((text[j] >= 'a') && (text[j] <= 'z')) buffer[i] = text[j] - 32;
if ((text[j] >= 'a') && (text[j] <= 'z'))
buffer[i] = text[j] - 32;
}
}
}
@ -1714,7 +1777,8 @@ char *LoadUTF8(const int *codepoints, int length)
// Resize memory to text length + string NULL terminator
void *ptr = RL_REALLOC(text, size + 1);
if (ptr != NULL) text = (char *)ptr;
if (ptr != NULL)
text = (char *)ptr;
return text;
}
@ -1744,7 +1808,8 @@ int *LoadCodepoints(const char *text, int *count)
// Re-allocate buffer to the actual number of codepoints loaded
int *temp = (int *)RL_REALLOC(codepoints, codepointCount * sizeof(int));
if (temp != NULL) codepoints = temp;
if (temp != NULL)
codepoints = temp;
*count = codepointCount;
@ -1853,7 +1918,11 @@ int GetCodepoint(const char *text, int *codepointSize)
// [0]xC2-DF [1]UTF8-tail(x80-BF)
unsigned char octet1 = text[1];
if ((octet1 == '\0') || ((octet1 >> 6) != 2)) { *codepointSize = 2; return codepoint; } // Unexpected sequence
if ((octet1 == '\0') || ((octet1 >> 6) != 2))
{
*codepointSize = 2;
return codepoint;
} // Unexpected sequence
if ((octet >= 0xc2) && (octet <= 0xdf))
{
@ -1867,11 +1936,19 @@ int GetCodepoint(const char *text, int *codepointSize)
unsigned char octet1 = text[1];
unsigned char octet2 = '\0';
if ((octet1 == '\0') || ((octet1 >> 6) != 2)) { *codepointSize = 2; return codepoint; } // Unexpected sequence
if ((octet1 == '\0') || ((octet1 >> 6) != 2))
{
*codepointSize = 2;
return codepoint;
} // Unexpected sequence
octet2 = text[2];
if ((octet2 == '\0') || ((octet2 >> 6) != 2)) { *codepointSize = 3; return codepoint; } // Unexpected sequence
if ((octet2 == '\0') || ((octet2 >> 6) != 2))
{
*codepointSize = 3;
return codepoint;
} // Unexpected sequence
// [0]xE0 [1]xA0-BF [2]UTF8-tail(x80-BF)
// [0]xE1-EC [1]UTF8-tail [2]UTF8-tail(x80-BF)
@ -1879,7 +1956,11 @@ int GetCodepoint(const char *text, int *codepointSize)
// [0]xEE-EF [1]UTF8-tail [2]UTF8-tail(x80-BF)
if (((octet == 0xe0) && !((octet1 >= 0xa0) && (octet1 <= 0xbf))) ||
((octet == 0xed) && !((octet1 >= 0x80) && (octet1 <= 0x9f)))) { *codepointSize = 2; return codepoint; }
((octet == 0xed) && !((octet1 >= 0x80) && (octet1 <= 0x9f))))
{
*codepointSize = 2;
return codepoint;
}
if ((octet >= 0xe0) && (octet <= 0xef))
{
@ -1890,28 +1971,45 @@ int GetCodepoint(const char *text, int *codepointSize)
else if ((octet & 0xf8) == 0xf0)
{
// Four octets
if (octet > 0xf4) return codepoint;
if (octet > 0xf4)
return codepoint;
unsigned char octet1 = text[1];
unsigned char octet2 = '\0';
unsigned char octet3 = '\0';
if ((octet1 == '\0') || ((octet1 >> 6) != 2)) { *codepointSize = 2; return codepoint; } // Unexpected sequence
if ((octet1 == '\0') || ((octet1 >> 6) != 2))
{
*codepointSize = 2;
return codepoint;
} // Unexpected sequence
octet2 = text[2];
if ((octet2 == '\0') || ((octet2 >> 6) != 2)) { *codepointSize = 3; return codepoint; } // Unexpected sequence
if ((octet2 == '\0') || ((octet2 >> 6) != 2))
{
*codepointSize = 3;
return codepoint;
} // Unexpected sequence
octet3 = text[3];
if ((octet3 == '\0') || ((octet3 >> 6) != 2)) { *codepointSize = 4; return codepoint; } // Unexpected sequence
if ((octet3 == '\0') || ((octet3 >> 6) != 2))
{
*codepointSize = 4;
return codepoint;
} // Unexpected sequence
// [0]xF0 [1]x90-BF [2]UTF8-tail [3]UTF8-tail
// [0]xF1-F3 [1]UTF8-tail [2]UTF8-tail [3]UTF8-tail
// [0]xF4 [1]x80-8F [2]UTF8-tail [3]UTF8-tail
if (((octet == 0xf0) && !((octet1 >= 0x90) && (octet1 <= 0xbf))) ||
((octet == 0xf4) && !((octet1 >= 0x80) && (octet1 <= 0x8f)))) { *codepointSize = 2; return codepoint; } // Unexpected sequence
((octet == 0xf4) && !((octet1 >= 0x80) && (octet1 <= 0x8f))))
{
*codepointSize = 2;
return codepoint;
} // Unexpected sequence
if (octet >= 0xf0)
{
@ -1920,7 +2018,8 @@ int GetCodepoint(const char *text, int *codepointSize)
}
}
if (codepoint > 0x10ffff) codepoint = 0x3f; // Codepoints after U+10ffff are invalid
if (codepoint > 0x10ffff)
codepoint = 0x3f; // Codepoints after U+10ffff are invalid
return codepoint;
}
@ -1936,21 +2035,30 @@ int GetCodepointNext(const char *text, int *codepointSize)
if (0xf0 == (0xf8 & ptr[0]))
{
// 4 byte UTF-8 codepoint
if(((ptr[1] & 0xC0) ^ 0x80) || ((ptr[2] & 0xC0) ^ 0x80) || ((ptr[3] & 0xC0) ^ 0x80)) { return codepoint; } //10xxxxxx checks
if (((ptr[1] & 0xC0) ^ 0x80) || ((ptr[2] & 0xC0) ^ 0x80) || ((ptr[3] & 0xC0) ^ 0x80))
{
return codepoint;
} // 10xxxxxx checks
codepoint = ((0x07 & ptr[0]) << 18) | ((0x3f & ptr[1]) << 12) | ((0x3f & ptr[2]) << 6) | (0x3f & ptr[3]);
*codepointSize = 4;
}
else if (0xe0 == (0xf0 & ptr[0]))
{
// 3 byte UTF-8 codepoint */
if(((ptr[1] & 0xC0) ^ 0x80) || ((ptr[2] & 0xC0) ^ 0x80)) { return codepoint; } //10xxxxxx checks
if (((ptr[1] & 0xC0) ^ 0x80) || ((ptr[2] & 0xC0) ^ 0x80))
{
return codepoint;
} // 10xxxxxx checks
codepoint = ((0x0f & ptr[0]) << 12) | ((0x3f & ptr[1]) << 6) | (0x3f & ptr[2]);
*codepointSize = 3;
}
else if (0xc0 == (0xe0 & ptr[0]))
{
// 2 byte UTF-8 codepoint
if((ptr[1] & 0xC0) ^ 0x80) { return codepoint; } //10xxxxxx checks
if ((ptr[1] & 0xC0) ^ 0x80)
{
return codepoint;
} // 10xxxxxx checks
codepoint = ((0x1f & ptr[0]) << 6) | (0x3f & ptr[1]);
*codepointSize = 2;
}
@ -1973,12 +2081,14 @@ int GetCodepointPrevious(const char *text, int *codepointSize)
*codepointSize = 0;
// Move to previous codepoint
do ptr--;
do
ptr--;
while (((0x80 & ptr[0]) != 0) && ((0xc0 & ptr[0]) == 0x80));
codepoint = GetCodepointNext(ptr, &cpSize);
if (codepoint != 0) *codepointSize = cpSize;
if (codepoint != 0)
*codepointSize = cpSize;
return codepoint;
}
@ -1994,7 +2104,9 @@ int GetCodepointPrevious(const char *text, int *codepointSize)
static int GetLine(const char *origin, char *buffer, int maxLength)
{
int count = 0;
for (; count < maxLength; count++) if (origin[count] == '\n') break;
for (; count < maxLength; count++)
if (origin[count] == '\n')
break;
memcpy(buffer, origin, count);
return count;
}
@ -2021,7 +2133,8 @@ static Font LoadBMFont(const char *fileName)
char *fileText = LoadFileText(fileName);
if (fileText == NULL) return font;
if (fileText == NULL)
return font;
char *fileTextPtr = fileText;
@ -2058,7 +2171,8 @@ static Font LoadBMFont(const char *fileName)
char *lastSlash = NULL;
lastSlash = strrchr(fileName, '/');
if (lastSlash == NULL) lastSlash = strrchr(fileName, '\\');
if (lastSlash == NULL)
lastSlash = strrchr(fileName, '\\');
if (lastSlash != NULL)
{
@ -2067,7 +2181,8 @@ static Font LoadBMFont(const char *fileName)
memcpy(imPath, fileName, TextLength(fileName) - TextLength(lastSlash) + 1);
memcpy(imPath + TextLength(fileName) - TextLength(lastSlash) + 1, imFileName, TextLength(imFileName));
}
else imPath = imFileName;
else
imPath = imFileName;
TRACELOGD(" > Image loading path: %s", imPath);
@ -2081,8 +2196,7 @@ static Font LoadBMFont(const char *fileName)
.width = imFont.width,
.height = imFont.height,
.mipmaps = 1,
.format = PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA
};
.format = PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA};
for (int p = 0, i = 0; p < (imFont.width * imFont.height * 2); p += 2, i++)
{
@ -2096,7 +2210,8 @@ static Font LoadBMFont(const char *fileName)
font.texture = LoadTextureFromImage(imFont);
if (lastSlash != NULL) RL_FREE(imPath);
if (lastSlash != NULL)
RL_FREE(imPath);
// Fill font characters info data
font.baseSize = fontSize;
@ -2136,7 +2251,8 @@ static Font LoadBMFont(const char *fileName)
font = GetFontDefault();
TRACELOG(LOG_WARNING, "FONT: [%s] Failed to load texture, reverted to default font", fileName);
}
else TRACELOG(LOG_INFO, "FONT: [%s] Font loaded successfully (%i glyphs)", fileName, font.glyphCount);
else
TRACELOG(LOG_INFO, "FONT: [%s] Font loaded successfully (%i glyphs)", fileName, font.glyphCount);
return font;
}