Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cutting when render text through TTF_RenderText_Solid #5269

Merged
merged 1 commit into from
Jun 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I know little about this too, but it does sound like your change is a good one.

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 @@ -1825,6 +1825,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