Lagrange [release]

Text: Workaround for glyph rasterization problem with older SDL versions

d4174c624c6927e1fc54b37d6530ca718bef02c6
diff --git a/src/ui/text.c b/src/ui/text.c
index 7143a4dc..dd5c5e4c 100644
--- a/src/ui/text.c
+++ b/src/ui/text.c
@@ -41,8 +41,17 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
 
 #include 
 #include 
+#include 
 #include 
 
+#if SDL_VERSION_ATLEAST(2, 0, 10)
+#   define LAGRANGE_RASTER_DEPTH    8
+#   define LAGRANGE_RASTER_FORMAT   SDL_PIXELFORMAT_INDEX8
+#else
+#   define LAGRANGE_RASTER_DEPTH    32
+#   define LAGRANGE_RASTER_FORMAT   SDL_PIXELFORMAT_RGBA8888
+#endif
+
 iDeclareType(Font)
 iDeclareType(Glyph)
 iDeclareTypeConstructionArgs(Glyph, iChar ch)
@@ -475,7 +484,15 @@ static SDL_Surface *rasterizeGlyph_Font_(const iFont *d, uint32_t glyphIndex, fl
     SDL_Surface *surface8 =
         SDL_CreateRGBSurfaceWithFormatFrom(bmp, w, h, 8, w, SDL_PIXELFORMAT_INDEX8);
     SDL_SetSurfacePalette(surface8, text_.grayscale);
+#if LAGRANGE_RASTER_DEPTH != 8
+    /* Convert to the cache format. */
+    SDL_Surface *surf = SDL_ConvertSurfaceFormat(surface8, LAGRANGE_RASTER_FORMAT, 0);
+    free(bmp);
+    SDL_FreeSurface(surface8);
+    return surf;
+#else
     return surface8;
+#endif
 }
 
 #if 0
@@ -710,7 +727,9 @@ void cacheTextGlyphs_Font_(iFont *d, const iRangecc text) {
                 if (buf == NULL) {
                     rasters = new_Array(sizeof(iRasterGlyph));
                     buf     = SDL_CreateRGBSurfaceWithFormat(
-                                0, bufSize.x, bufSize.y, 8, SDL_PIXELFORMAT_INDEX8);
+                                0, bufSize.x, bufSize.y,
+                                LAGRANGE_RASTER_DEPTH,
+                                LAGRANGE_RASTER_FORMAT);
                     SDL_SetSurfacePalette(buf, text_.grayscale);
                 }
                 SDL_Surface *surfaces[2] = {
@@ -741,7 +760,9 @@ void cacheTextGlyphs_Font_(iFont *d, const iRangecc text) {
                 }
                 iForIndices(i, surfaces) {
                     if (surfaces[i]) {
-                        free(surfaces[i]->pixels);
+                        if (surfaces[i]->flags & SDL_PREALLOC) {
+                            free(surfaces[i]->pixels);
+                        }
                         SDL_FreeSurface(surfaces[i]);
                     }
                 }