Magic compiles silenced.
Attempted Text tool font load under pango. Made pango optional. TuxPaint_Font is aware of both panfgo contexts & sdl_ttf ttf fonts.
This commit is contained in:
parent
3d1718a2b7
commit
0eecea9581
8 changed files with 261 additions and 103 deletions
|
|
@ -30,10 +30,6 @@
|
|||
void loadfont_callback(SDL_Surface * screen, const char *restrict const dir,
|
||||
unsigned dirlen, tp_ftw_str * files, unsigned i)
|
||||
{
|
||||
/* FIXME */
|
||||
|
||||
#ifdef NO_SDLPANGO
|
||||
|
||||
dirlen = dirlen;
|
||||
|
||||
while (i--)
|
||||
|
|
@ -78,13 +74,14 @@ void loadfont_callback(SDL_Surface * screen, const char *restrict const dir,
|
|||
const char *restrict const family = TuxPaint_Font_FontFaceFamilyName(font);
|
||||
const char *restrict const style = TuxPaint_Font_FontFaceStyleName(font);
|
||||
|
||||
printf("success: 0x%x -> 0x%x\n", font, font->ttf_font);
|
||||
|
||||
#ifdef DEBUG
|
||||
int numfaces = TTF_FontFaces(font);
|
||||
//#ifdef DEBUG
|
||||
int numfaces = TTF_FontFaces(font->ttf_font);
|
||||
if (numfaces != 1)
|
||||
printf("Found %d faces in %s, %s, %s\n", numfaces, files[i].str,
|
||||
family, style);
|
||||
#endif
|
||||
//#endif
|
||||
|
||||
// First, the blacklist. We list font families that can crash Tux Paint
|
||||
// via bugs in the SDL_ttf library. We also test fonts to be sure that
|
||||
|
|
@ -140,7 +137,7 @@ void loadfont_callback(SDL_Surface * screen, const char *restrict const dir,
|
|||
style);
|
||||
#endif
|
||||
}
|
||||
TTF_CloseFont(font);
|
||||
TuxPaint_Font_CloseFont(font);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -153,9 +150,6 @@ void loadfont_callback(SDL_Surface * screen, const char *restrict const dir,
|
|||
free(files[i].str);
|
||||
}
|
||||
free(files);
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
106
src/fonts.c
106
src/fonts.c
|
|
@ -144,7 +144,7 @@ TuxPaint_Font *try_alternate_font(int size)
|
|||
*p = 0;
|
||||
snprintf(str, sizeof(str), "%sfonts/locale/%s.ttf", DATA_PREFIX, prefix);
|
||||
|
||||
return TTF_OpenFont(str, size);
|
||||
return TuxPaint_Font_OpenFont("", str, size);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
@ -169,25 +169,25 @@ TuxPaint_Font *load_locale_font(TuxPaint_Font * fallback, int size)
|
|||
snprintf(str, sizeof(str), "%sfonts/locale/%s.ttf",
|
||||
DATA_PREFIX, lang_prefix);
|
||||
|
||||
ret = TuxPaint_OpenFont(str, size);
|
||||
ret = TuxPaint_Font_OpenFont("", str, size);
|
||||
|
||||
#ifdef __APPLE__
|
||||
if (ret == NULL)
|
||||
{
|
||||
snprintf(str, sizeof(str), "%sfonts/%s.ttf", DATA_PREFIX, lang_prefix);
|
||||
ret = TuxPaint_Font_OpenFont(str, size);
|
||||
ret = TuxPaint_Font_OpenFont("", str, size);
|
||||
}
|
||||
|
||||
if (ret == NULL)
|
||||
{
|
||||
snprintf(str, sizeof(str), "/Library/Fonts/%s.ttf", lang_prefix);
|
||||
ret = TuxPaint_Font_OpenFont(str, size);
|
||||
ret = TuxPaint_Font_OpenFont("", str, size);
|
||||
}
|
||||
|
||||
if (ret == NULL)
|
||||
{
|
||||
snprintf(str, sizeof(str), "%s/%s.ttf", macosx.fontsPath, lang_prefix);
|
||||
ret = TuxPaint_Font_OpenFont(str, size);
|
||||
ret = TuxPaint_Font_OpenFont("", str, size);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
@ -224,38 +224,66 @@ TuxPaint_Font *load_locale_font(TuxPaint_Font * fallback, int size)
|
|||
void TuxPaint_Font_CloseFont(TuxPaint_Font * tpf)
|
||||
{
|
||||
#ifndef NO_SDLPANGO
|
||||
SDLPango_FreeContext(tpf->pango_context);
|
||||
tpf->pango_context = NULL;
|
||||
free(tpf);
|
||||
#else
|
||||
TTF_CloseFont(tpf->ttf_font);
|
||||
tpf->ttf_font = NULL;
|
||||
free(tpf);
|
||||
if (tpf->typ == FONT_TYPE_PANGO)
|
||||
{
|
||||
SDLPango_FreeContext(tpf->pango_context);
|
||||
tpf->pango_context = NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (tpf->typ == FONT_TYPE_TTF)
|
||||
{
|
||||
TTF_CloseFont(tpf->ttf_font);
|
||||
tpf->ttf_font = NULL;
|
||||
}
|
||||
free(tpf);
|
||||
}
|
||||
|
||||
TuxPaint_Font * TuxPaint_Font_OpenFont(const char * pangodesc, const char * ttffilename, int size)
|
||||
{
|
||||
TuxPaint_Font * tpf = (TuxPaint_Font *) malloc(sizeof(TuxPaint_Font));
|
||||
TuxPaint_Font * tpf = NULL;
|
||||
|
||||
printf("OpenFont(%s, %s)\n", pangodesc, ttffilename);
|
||||
|
||||
#ifndef NO_SDLPANGO
|
||||
|
||||
char desc[1024];
|
||||
|
||||
snprintf(desc, sizeof(desc), "%s %d", pangodesc, size - 2);
|
||||
if (pangodesc != NULL && pangodesc[0] != '\0')
|
||||
{
|
||||
tpf = (TuxPaint_Font *) malloc(sizeof(TuxPaint_Font));
|
||||
tpf->typ = FONT_TYPE_PANGO;
|
||||
snprintf(desc, sizeof(desc), "%s %d", pangodesc, (size * 3) / 4);
|
||||
|
||||
tpf->pango_context = SDLPango_CreateContext_GivenFontDesc(desc);
|
||||
tpf->height = size - 2; /* FIXME: Is this accurate!? -bjk 2007.07.12 */
|
||||
|
||||
(void)(ttffilename);
|
||||
#else
|
||||
|
||||
tpf->ttf_font = TTF_OpenFont(ttffilename, size);
|
||||
tpf->height = TTF_FontHeight(getfonthandle(tpf->font)));
|
||||
|
||||
(void)(pangodesc);
|
||||
tpf->pango_context = SDLPango_CreateContext_GivenFontDesc(desc);
|
||||
if (tpf->pango_context == NULL)
|
||||
{
|
||||
printf("Failed to load %s\n", desc);
|
||||
free(tpf);
|
||||
tpf = NULL;
|
||||
}
|
||||
else
|
||||
tpf->height = size; /* FIXME: Is this accurate!? -bjk 2007.07.12 */
|
||||
|
||||
return(tpf);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (ttffilename != NULL && ttffilename[0] != '\0')
|
||||
{
|
||||
tpf = (TuxPaint_Font *) malloc(sizeof(TuxPaint_Font));
|
||||
tpf->typ = FONT_TYPE_TTF;
|
||||
tpf->ttf_font = TTF_OpenFont(ttffilename, size);
|
||||
if (tpf->ttf_font == NULL)
|
||||
{
|
||||
printf("Failed to load %s: %s\n", ttffilename, SDL_GetError());
|
||||
free(tpf);
|
||||
tpf = NULL;
|
||||
}
|
||||
else
|
||||
tpf->height = TTF_FontHeight(tpf->ttf_font);
|
||||
}
|
||||
|
||||
return(tpf);
|
||||
}
|
||||
|
||||
|
|
@ -1138,6 +1166,9 @@ TuxPaint_Font *getfonthandle(int desire)
|
|||
|
||||
pathname = (char *) "";
|
||||
|
||||
(void)(name);
|
||||
(void)(missing);
|
||||
|
||||
printf("getfonthandle(%d) asking SDL_Pango for %s\n", desire, description);
|
||||
#else
|
||||
|
||||
|
|
@ -1166,7 +1197,7 @@ TuxPaint_Font *getfonthandle(int desire)
|
|||
// if the font doesn't load, we die -- it did load OK before though
|
||||
|
||||
#ifdef NO_SDLPANGO
|
||||
TTF_SetFontStyle(fi->handle, missing);
|
||||
TTF_SetFontStyle(fi->handle->ttf_font, missing);
|
||||
#endif
|
||||
|
||||
return fi->handle;
|
||||
|
|
@ -1252,7 +1283,7 @@ int surfcmp(const void *s1, const void *s2)
|
|||
int charset_works(TuxPaint_Font * font, const char *s)
|
||||
{
|
||||
SDL_Color black = { 0, 0, 0, 0 };
|
||||
#ifndef SDL_NOPANGO
|
||||
#ifndef NO_SDLPANGO
|
||||
SDLPango_Matrix pango_color;
|
||||
#endif
|
||||
SDL_Surface **surfs = malloc(strlen(s) * sizeof surfs[0]);
|
||||
|
|
@ -1262,21 +1293,28 @@ int charset_works(TuxPaint_Font * font, const char *s)
|
|||
{
|
||||
char c[8];
|
||||
unsigned offset = 0;
|
||||
SDL_Surface *tmp_surf;
|
||||
SDL_Surface *tmp_surf = NULL;
|
||||
|
||||
do
|
||||
c[offset++] = *s++;
|
||||
while ((*s & 0xc0u) == 0x80u); // assume safe input
|
||||
c[offset++] = '\0';
|
||||
|
||||
#ifndef NO_SDLPANGO
|
||||
sdl_color_to_pango_color(black, &pango_color);
|
||||
SDLPango_SetDefaultColor(font->pango_context, &pango_color);
|
||||
SDLPango_SetText(font->pango_context, c, -1);
|
||||
tmp_surf = SDLPango_CreateSurfaceDraw(font->pango_context);
|
||||
#else
|
||||
tmp_surf = TTF_RenderUTF8_Blended(font, c, black);
|
||||
if (font->typ == FONT_TYPE_PANGO)
|
||||
{
|
||||
sdl_color_to_pango_color(black, &pango_color);
|
||||
SDLPango_SetDefaultColor(font->pango_context, &pango_color);
|
||||
SDLPango_SetText(font->pango_context, c, -1);
|
||||
tmp_surf = SDLPango_CreateSurfaceDraw(font->pango_context);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (font->typ == FONT_TYPE_TTF)
|
||||
{
|
||||
tmp_surf = TTF_RenderUTF8_Blended(font->ttf_font, c, black);
|
||||
}
|
||||
|
||||
if (!tmp_surf)
|
||||
{
|
||||
#if 0
|
||||
|
|
@ -1314,6 +1352,7 @@ const char * TuxPaint_Font_FontFaceFamilyName(TuxPaint_Font * tpf)
|
|||
{
|
||||
#ifndef NO_SDLPANGO
|
||||
/* FIXME */
|
||||
(void)(tpf);
|
||||
return("");
|
||||
#else
|
||||
return (TTF_FontFaceFamilyName(tpf->ttf_font));
|
||||
|
|
@ -1324,6 +1363,7 @@ const char * TuxPaint_Font_FontFaceStyleName(TuxPaint_Font * tpf)
|
|||
{
|
||||
#ifndef NO_SDLPANGO
|
||||
/* FIXME */
|
||||
(void)(tpf);
|
||||
return("");
|
||||
#else
|
||||
return (TTF_FontFaceStyleName(tpf->ttf_font));
|
||||
|
|
|
|||
10
src/fonts.h
10
src/fonts.h
|
|
@ -64,13 +64,19 @@ TTF_Font *BUGFIX_TTF_OpenFont206(const char *const file, int ptsize);
|
|||
|
||||
/* Stuff that wraps either SDL_Pango or SDL_TTF for font rendering: */
|
||||
|
||||
enum {
|
||||
#ifndef NO_SDLPANGO
|
||||
FONT_TYPE_PANGO,
|
||||
#endif
|
||||
FONT_TYPE_TTF
|
||||
};
|
||||
|
||||
typedef struct TuxPaint_Font_s {
|
||||
#ifndef NO_SDLPANGO
|
||||
SDLPango_Context * pango_context;
|
||||
#else
|
||||
TTF_Font * ttf_font;
|
||||
#endif
|
||||
int typ;
|
||||
TTF_Font * ttf_font;
|
||||
int height;
|
||||
} TuxPaint_Font;
|
||||
|
||||
|
|
|
|||
110
src/tuxpaint.c
110
src/tuxpaint.c
|
|
@ -978,10 +978,6 @@ static SDL_Surface *render_text(TuxPaint_Font * restrict font,
|
|||
// will substitute a rectangle without telling us. Sometimes it returns NULL.
|
||||
// Probably we should use FreeType directly. For now though...
|
||||
|
||||
/*
|
||||
height = TuxPaint_Font_FontHeight(font);
|
||||
if (height < 2)
|
||||
*/
|
||||
height = 2;
|
||||
|
||||
return thumbnail(img_title_large_off, height * strlen(str) / 2, height, 0);
|
||||
|
|
@ -1015,35 +1011,107 @@ static SDL_Surface *render_text_w(TuxPaint_Font * restrict font,
|
|||
const wchar_t * restrict str,
|
||||
SDL_Color color)
|
||||
{
|
||||
SDL_Surface *ret;
|
||||
SDL_Surface *ret = NULL;
|
||||
int height;
|
||||
Uint16 *ustr;
|
||||
#ifndef NO_SDLPANGO
|
||||
unsigned int i, j;
|
||||
int utfstr_max;
|
||||
char * utfstr;
|
||||
SDLPango_Matrix pango_color;
|
||||
#endif
|
||||
|
||||
ustr = wcstou16(str); // FIXME: For SDL_Pango, too? -bjk 2007.07.12
|
||||
#ifndef NO_SDLPANGO
|
||||
if (font->typ == FONT_TYPE_PANGO)
|
||||
{
|
||||
sdl_color_to_pango_color(color, &pango_color);
|
||||
|
||||
#ifdef NO_SDLPANGO
|
||||
ret = TTF_RenderUNICODE_Blended(font, ustr, color);
|
||||
#else
|
||||
sdl_color_to_pango_color(color, &pango_color);
|
||||
SDLPango_SetDefaultColor(font->pango_context, &pango_color);
|
||||
|
||||
SDLPango_SetDefaultColor(font->pango_context, &pango_color);
|
||||
SDLPango_SetText(font->pango_context, (char *) ustr, -1); // char * cast ok for SDL_Pango? -bjk 2007.07.12
|
||||
ret = SDLPango_CreateSurfaceDraw(font->pango_context);
|
||||
/* Convert from 16-bit UNICODE to UTF-8 encoded for SDL_Pango: */
|
||||
|
||||
utfstr_max = (sizeof(char) * 4 * (wcslen(str) + 1));
|
||||
utfstr = (char *) malloc(utfstr_max);
|
||||
memset(utfstr, utfstr_max, 0);
|
||||
|
||||
j = 0;
|
||||
for (i = 0; i < wcslen(str); i++)
|
||||
{
|
||||
if (str[i] <= 0x0000007F)
|
||||
{
|
||||
/* 0x00000000 - 0x0000007F:
|
||||
0xxxxxxx */
|
||||
|
||||
utfstr[j++] = (str[i] & 0x7F);
|
||||
}
|
||||
else if (str[i] <= 0x000007FF)
|
||||
{
|
||||
/* 0x00000080 - 0x000007FF:
|
||||
|
||||
00000abc defghijk
|
||||
110abcde 10fghijk */
|
||||
|
||||
utfstr[j++] = (((str[i] & 0x0700) >> 6) | /* -----abc -------- to ---abc-- */
|
||||
((str[i] & 0x00C0) >> 6) | /* -------- de------ to ------de */
|
||||
(0xC0)); /* add 110----- */
|
||||
|
||||
utfstr[j++] = (((str[i] & 0x003F)) | /* -------- --fghijk to --fghijk */
|
||||
(0x80)); /* add 10------ */
|
||||
}
|
||||
else if (str[i] <= 0x0000FFFF)
|
||||
{
|
||||
/* 0x00000800 - 0x0000FFFF:
|
||||
|
||||
abcdefgh ijklmnop
|
||||
1110abcd 10efghij 10klmnop */
|
||||
|
||||
utfstr[j++] = (((str[i] & 0xF000) >> 12) | /* abcd---- -------- to ----abcd */
|
||||
(0xE0)); /* add 1110---- */
|
||||
utfstr[j++] = (((str[i] & 0x0FC0) >> 6) | /* ----efgh ij------ to --efghij */
|
||||
(0x80)); /* add 10------ */
|
||||
utfstr[j++] = (((str[i] & 0x003F)) | /* -------- --klmnop to --klmnop */
|
||||
(0x80)); /* add 10------ */
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 0x00010000 - 0x001FFFFF:
|
||||
11110abc 10defghi 10jklmno 10pqrstu */
|
||||
|
||||
utfstr[j++] = (((str[i] & 0x1C0000) >> 18) | /* ---abc-- -------- -------- to -----abc */
|
||||
(0xF0)); /* add 11110000 */
|
||||
utfstr[j++] = (((str[i] & 0x030000) >> 12) | /* ------de -------- -------- to --de---- */
|
||||
((str[i] & 0x00F000) >> 12) | /* -------- fghi---- -------- to ----fghi */
|
||||
(0x80)); /* add 10------ */
|
||||
utfstr[j++] = (((str[i] & 0x000F00) >> 6) | /* -------- ----jklm -------- to --jklm-- */
|
||||
((str[i] & 0x0000C0) >> 6) | /* -------- -------- no------ to ------no */
|
||||
(0x80)); /* add 10------ */
|
||||
utfstr[j++] = ((str[i] & 0x00003F) | /* -------- -------- --pqrstu to --prqstu */
|
||||
(0x80)); /* add 10------ */
|
||||
}
|
||||
}
|
||||
utfstr[j] = '\0';
|
||||
|
||||
|
||||
SDLPango_SetText(font->pango_context, utfstr, -1);
|
||||
ret = SDLPango_CreateSurfaceDraw(font->pango_context);
|
||||
}
|
||||
#endif
|
||||
|
||||
free(ustr); // FIXME: For SDL_Pango, too? -bjk 2007.07.12
|
||||
if (font->typ == FONT_TYPE_TTF)
|
||||
{
|
||||
ustr = wcstou16(str);
|
||||
ret = TTF_RenderUNICODE_Blended(font->ttf_font, ustr, color);
|
||||
free(ustr);
|
||||
}
|
||||
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
// Sometimes a font will be missing a character we need. Sometimes the library
|
||||
// will substitute a rectangle without telling us. Sometimes it returns NULL.
|
||||
// Probably we should use FreeType directly. For now though...
|
||||
height = TuxPaint_Font_FontHeight(font);
|
||||
if (height < 2)
|
||||
height = 2;
|
||||
|
||||
height = 2;
|
||||
return thumbnail(img_title_large_off, height * wcslen(str) / 2, height, 0);
|
||||
}
|
||||
|
||||
|
|
@ -2715,6 +2783,8 @@ static void mainloop(void)
|
|||
if (cur_tool == TOOL_TEXT) // Huh? It had better be!
|
||||
{
|
||||
// need to invalidate all the cached user fonts, causing reload on demand
|
||||
|
||||
#ifdef NO_SDLPANGO
|
||||
int i;
|
||||
for (i = 0; i < num_font_families; i++)
|
||||
{
|
||||
|
|
@ -2725,7 +2795,7 @@ static void mainloop(void)
|
|||
user_font_families[i]->handle = NULL;
|
||||
}
|
||||
}
|
||||
// FIXME: is setting do_draw enough?
|
||||
#endif
|
||||
draw_fonts();
|
||||
update_screen_rect(&r_toolopt);
|
||||
}
|
||||
|
|
@ -7337,7 +7407,7 @@ static void draw_toolbar(void)
|
|||
|
||||
|
||||
dest.x = ((i % 2) * 48) + 4 + (40 - img_tool_names[i]->w) / 2;
|
||||
dest.y = ((i / 2) * 48) + 40 + 2 + (48 - img_tool_names[i]->h);
|
||||
dest.y = ((i / 2) * 48) + 40 + 2 + (44 - img_tool_names[i]->h);
|
||||
|
||||
SDL_BlitSurface(img_tool_names[i], NULL, screen, &dest);
|
||||
}
|
||||
|
|
@ -7630,6 +7700,8 @@ static void draw_fonts(void)
|
|||
if (disable_stamp_controls)
|
||||
most = 14;
|
||||
|
||||
printf("there are %d font families\n", num_font_families);
|
||||
|
||||
/* Do we need scrollbars? */
|
||||
|
||||
if (num_font_families > most + TOOLOFFSET)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue