Skip to content

Commit

Permalink
Fix cutting when render text through TTF_RenderText_Solid (#5269)
Browse files Browse the repository at this point in the history
  • Loading branch information
caiiiycuk authored and kripken committed Jun 13, 2017
1 parent 12c40a5 commit abb142b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/library_sdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3001,14 +3001,17 @@ var LibrarySDL = {
var w = SDL.estimateTextWidth(fontData, text);
var h = fontData.size;
var color = SDL.loadColorToCSSRGB(color); // XXX alpha breaks fonts?
var fontString = h + 'px ' + fontData.name;
var fontString = h + 'px ' + fontData.name + ', serif';
var surf = SDL.makeSurface(w, h, 0, false, 'text:' + text); // bogus numbers..
var surfData = SDL.surfaces[surf];
surfData.ctx.save();
surfData.ctx.fillStyle = color;
surfData.ctx.font = fontString;
surfData.ctx.textBaseline = 'top';
surfData.ctx.fillText(text, 0, 0);
// use bottom alligment, because it works
// same in all browsers, more info here:
// https://bugzilla.mozilla.org/show_bug.cgi?id=737852
surfData.ctx.textBaseline = 'bottom';
surfData.ctx.fillText(text, 0, h|0);
surfData.ctx.restore();
return surf;
},
Expand Down
19 changes: 19 additions & 0 deletions tests/sdl_ttf_render_text_solid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdio.h>
#include <SDL.h>
#include <SDL_ttf.h>

int main()
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface *screen = SDL_SetVideoMode(320, 32, 32, SDL_HWSURFACE);

TTF_Init();
TTF_Font *font = TTF_OpenFont("Arial", 32);

SDL_Color color = { 0xff, 0x99, 0x00, 0xff };
SDL_Surface *text = TTF_RenderText_Solid(font, "Play", color);
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 0, 0));
SDL_BlitSurface(text, NULL, screen, NULL);
return 0;
}

Binary file added tests/sdl_ttf_render_text_solid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions tests/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,9 @@ def test_sdl_canvas_palette_2(self):
self.btest('sdl_canvas_palette_2.c', reference='sdl_canvas_palette_g.png', args=['--pre-js', 'pre.js', '--pre-js', 'args-g.js', '-lSDL', '-lGL'])
self.btest('sdl_canvas_palette_2.c', reference='sdl_canvas_palette_b.png', args=['--pre-js', 'pre.js', '--pre-js', 'args-b.js', '-lSDL', '-lGL'])

def test_sdl_ttf_render_text_solid(self):
self.btest('sdl_ttf_render_text_solid.c', reference='sdl_ttf_render_text_solid.png', args=['-O2', '-s', 'TOTAL_MEMORY=16MB', '-lSDL', '-lGL'])

def test_sdl_alloctext(self):
self.btest('sdl_alloctext.c', expected='1', args=['-O2', '-s', 'TOTAL_MEMORY=16MB', '-lSDL', '-lGL'])

Expand Down

0 comments on commit abb142b

Please sign in to comment.