1
/* vim:expandtab:ts=2 sw=2:
3
/* Grafx2 - The Ultimate 256-color bitmap paint program
5
Copyright 2008 Yves Rizoud
6
Copyright 2008 Franck Charlet
7
Copyright 2008 Adrien Destugues
8
Copyright 1996-2001 Sunset Design (Guillaume Dorme & Karl Maritaud)
10
Grafx2 is free software; you can redistribute it and/or
11
modify it under the terms of the GNU General Public License
12
as published by the Free Software Foundation; version 2
15
Grafx2 is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
GNU General Public License for more details.
20
You should have received a copy of the GNU General Public License
21
along with Grafx2; if not, see <http://www.gnu.org/licenses/>
24
// Pour d�sactiver le support TrueType, d�finir NOTTF
25
// To disable TrueType support, define NOTTF
29
#include <ctype.h> // tolower()
33
#if defined(__macosx__)
34
#include <SDL_ttf/SDL_ttf.h>
39
#if defined(__linux__)
40
#if defined(__macosx__)
41
#include <Carbon/Carbon.h>
42
#import <corefoundation/corefoundation.h>
50
#include <SDL_image.h>
56
#include "sdlscreen.h"
67
// Liste chain�e simple
69
struct T_Font * Previous;
71
// Liste chain�e des polices de texte
72
T_Font * font_list_start;
75
// Inspir� par Allegro
76
#define EXTID(a,b,c) ((((a)&255)<<16) | (((b)&255)<<8) | (((c)&255)))
77
#define EXTID4(a,b,c,d) ((((a)&255)<<24) | (((b)&255)<<16) | (((c)&255)<<8) | (((d)&255)))
79
int Compare_fonts(T_Font * font_1, T_Font * font_2)
81
if (font_1->Is_bitmap && !font_2->Is_bitmap)
83
if (font_2->Is_bitmap && !font_1->Is_bitmap)
85
return strcmp(font_1->Label, font_2->Label);
88
// Ajout d'une fonte � la liste.
89
void Add_font(const char *name)
93
int size=strlen(name)+1;
96
// D�termination du type:
98
#if defined(__macosx__)
100
if (size < 6) return;
102
char strFontName[512];
103
CFStringRef CFSFontName;// = CFSTR(name);
105
CFSFontName = CFStringCreateWithBytes(NULL, (UInt8 *) name, size - 1, kCFStringEncodingASCII, false);
106
// Fix some funny names
107
CFStringGetCString(CFSFontName, strFontName, 512, kCFStringEncodingASCII);
109
// Now we have a printable font name, use it
118
font = (T_Font *)malloc(sizeof(T_Font));
120
switch (EXTID(tolower(name[size-4]), tolower(name[size-3]), tolower(name[size-2])))
122
case EXTID('t','t','f'):
123
case EXTID('f','o','n'):
124
case EXTID('o','t','f'):
125
case EXTID('p','f','b'):
126
font->Is_truetype = 1;
129
case EXTID('b','m','p'):
130
case EXTID('g','i','f'):
131
case EXTID('j','p','g'):
132
case EXTID('l','b','m'):
133
case EXTID('p','c','x'):
134
case EXTID('p','n','g'):
135
case EXTID('t','g','a'):
136
case EXTID('t','i','f'):
137
case EXTID('x','c','f'):
138
case EXTID('x','p','m'):
139
case EXTID('.','x','v'):
140
font->Is_truetype = 0;
144
#if defined(__macosx__)
145
if(strcasecmp(&name[size-6], "dfont") == 0)
147
font->Is_truetype = 1;
161
font->Name = (char *)malloc(size);
162
strcpy(font->Name, name);
164
strcpy(font->Label, " ");
165
if (font->Is_truetype)
166
font->Label[17]=font->Label[18]='T'; // Logo TT
167
font_name=Find_last_slash(font->Name);
169
font_name=font->Name;
172
for (index=0; index < 17 && font_name[index]!='\0' && font_name[index]!='.'; index++)
173
font->Label[index]=font_name[index];
177
font->Previous = NULL;
178
if (font_list_start==NULL)
180
// Premiere (liste vide)
181
font_list_start = font;
187
compare = Compare_fonts(font, font_list_start);
190
if (compare==0 && !strcmp(font->Name, font_list_start->Name))
198
font->Next=font_list_start;
199
font_list_start=font;
204
T_Font *searched_font;
205
searched_font=font_list_start;
206
while (searched_font->Next && (compare=Compare_fonts(font, searched_font->Next))>0)
207
searched_font=searched_font->Next;
208
// Apr�s searched_font
209
if (compare==0 && strcmp(font->Name, searched_font->Next->Name)==0)
216
font->Next=searched_font->Next;
217
searched_font->Next=font;
224
// Trouve le nom d'une fonte par son num�ro
225
char * Font_name(int index)
227
T_Font *font = font_list_start;
228
if (index<0 ||index>=Nb_fonts)
236
// Trouve le libell� d'affichage d'une fonte par son num�ro
237
// Renvoie un pointeur sur un buffer statique de 20 caracteres.
238
char * Font_label(int index)
241
static char label[20];
245
// Recherche de la fonte
246
font = font_list_start;
247
if (index<0 ||index>=Nb_fonts)
253
strcpy(label, font->Label);
258
// V�rifie si une fonte donn�e est TrueType
259
int TrueType_font(int index)
261
T_Font *font = font_list_start;
262
if (index<0 ||index>=Nb_fonts)
266
return font->Is_truetype;
271
// Initialisation � faire une fois au d�but du programme
274
char directory_name[MAX_PATH_CHARACTERS];
276
// Initialisation de TTF
280
// Initialisation des fontes
281
font_list_start = NULL;
283
// Parcours du r�pertoire "fonts"
284
strcpy(directory_name, Data_directory);
285
strcat(directory_name, "fonts");
286
For_each_file(directory_name, Add_font);
288
#if defined(__WIN32__)
289
// Parcours du r�pertoire systeme windows "fonts"
292
char * WindowsPath=getenv("windir");
295
sprintf(directory_name, "%s\\FONTS", WindowsPath);
296
For_each_file(directory_name, Add_font);
300
#elif defined(__macosx__)
301
// R�cup�ration de la liste des fonts avec fontconfig
306
char home_dir[MAXPATHLEN];
307
char *font_path_list[3] = {
308
"/System/Library/Fonts",
312
// Make sure we also search into the user's fonts directory
313
CFURLRef url = (CFURLRef) CFCopyHomeDirectoryURLForUser(NULL);
314
CFURLGetFileSystemRepresentation(url, true, (UInt8 *) home_dir, MAXPATHLEN);
315
strcat(home_dir, "/Library/Fonts");
316
font_path_list[2] = home_dir;
318
for(i=0;i<number;i++)
319
For_each_file(*(font_path_list+i),Add_font);
324
#elif defined(__linux__)
331
Display* dpy = XOpenDisplay(NULL);
332
char** font_path_list = XGetFontPath(dpy,&number);
335
for(i=0;i<number;i++)
336
For_each_file(*(font_path_list+i),Add_font);
338
XFreeFontPath(font_path_list);
342
#elif defined(__amigaos4__) || defined(__amigaos__)
344
For_each_file( "FONTS:_TrueType", Add_font );
346
#elif defined(__BEOS__) || defined(__HAIKU__)
348
For_each_file("/etc/fonts/ttfonts", Add_font);
351
#elif defined(__SKYOS__)
353
For_each_file("/boot/system/fonts", Add_font);
359
// Informe si texte.c a �t� compil� avec l'option de support TrueType ou pas.
360
int TrueType_is_supported()
371
byte *Render_text_TTF(const char *str, int font_number, int size, int antialias, int bold, int italic, int *width, int *height)
374
SDL_Surface * TexteColore;
375
SDL_Surface * Texte8Bit;
380
SDL_Color Couleur_Avant;
381
SDL_Color Couleur_Arriere;
383
// Chargement de la fonte
384
font=TTF_OpenFont(Font_name(font_number), size);
392
style|=TTF_STYLE_ITALIC;
394
style|=TTF_STYLE_BOLD;
395
TTF_SetFontStyle(font, style);
399
Couleur_Avant = Color_to_SDL_color(Fore_color);
400
Couleur_Arriere = Color_to_SDL_color(Back_color);
404
Couleur_Avant = Color_to_SDL_color(MC_White);
405
Couleur_Arriere = Color_to_SDL_color(MC_Black);
409
// Rendu du texte: cr�e une surface SDL RGB 24bits
411
TexteColore=TTF_RenderText_Shaded(font, str, Couleur_Avant, Couleur_Arriere );
413
TexteColore=TTF_RenderText_Solid(font, str, Couleur_Avant);
420
Texte8Bit=SDL_DisplayFormat(TexteColore);
422
SDL_FreeSurface(TexteColore);
424
new_brush=Surface_to_bytefield(Texte8Bit, NULL);
427
SDL_FreeSurface(TexteColore);
428
SDL_FreeSurface(Texte8Bit);
434
// Mappage des couleurs
435
for (index=0; index < Texte8Bit->w * Texte8Bit->h; index++)
437
if (*(new_brush+index) == MC_Black)
438
*(new_brush+index)=Back_color;
439
else if (*(new_brush+index) == MC_White)
440
*(new_brush+index)=Fore_color;
444
*height=Texte8Bit->h;
445
SDL_FreeSurface(Texte8Bit);
452
byte *Render_text_SFont(const char *str, int font_number, int *width, int *height)
455
SDL_Surface * TexteColore;
456
SDL_Surface * Texte8Bit;
457
SDL_Surface *Surface_fonte;
461
// Chargement de la fonte
462
Surface_fonte=IMG_Load(Font_name(font_number));
465
DEBUG("Font loading failed",0);
468
font=SFont_InitFont(Surface_fonte);
471
DEBUG("Font init failed",1);
475
// Calcul des dimensions
476
*height=SFont_TextHeight(font);
477
*width=SFont_TextWidth(font, str);
478
// Allocation d'une surface SDL
479
TexteColore=SDL_CreateRGBSurface(SDL_SWSURFACE, *width, *height, 24, 0, 0, 0, 0);
480
// Fill with backcolor
485
SDL_FillRect(TexteColore, &rectangle, SDL_MapRGB(
487
Main_palette[Back_color].R,
488
Main_palette[Back_color].G,
489
Main_palette[Back_color].B
492
SFont_Write(TexteColore, font, 0, 0, str);
495
DEBUG("Rendering failed",2);
496
SFont_FreeFont(font);
500
Texte8Bit=SDL_DisplayFormat(TexteColore);
501
SDL_FreeSurface(TexteColore);
503
new_brush=Surface_to_bytefield(Texte8Bit, NULL);
506
DEBUG("Converting failed",3);
507
SDL_FreeSurface(TexteColore);
508
SDL_FreeSurface(Texte8Bit);
509
SFont_FreeFont(font);
512
SDL_FreeSurface(Texte8Bit);
513
SFont_FreeFont(font);
519
#define TTFONLY __attribute__((unused))
524
// Cr�e une brosse � partir des param�tres de texte demand�s.
525
// Si cela r�ussit, la fonction place les dimensions dans width et height,
526
// et retourne l'adresse du bloc d'octets.
527
byte *Render_text(const char *str, int font_number, TTFONLY int size, int TTFONLY antialias, TTFONLY int bold, TTFONLY int italic, int *width, int *height)
529
T_Font *font = font_list_start;
530
int index=font_number;
532
// Verification type de la fonte
533
if (font_number<0 ||font_number>=Nb_fonts)
538
if (font->Is_truetype)
541
return Render_text_TTF(str, font_number, size, antialias, bold, italic, width, height);
548
return Render_text_SFont(str, font_number, width, height);