[GB.SDL]
* NEW: Add Font.Bold, Font.Italic, Font.Underline and Font.Strikeout properties. git-svn-id: svn://localhost/gambas/trunk@3219 867c0c6c-44f3-4631-809d-bfa615b0a4ec
This commit is contained in:
parent
cac73831c9
commit
496d61f955
@ -31,7 +31,7 @@
|
||||
|
||||
static CDRAW draw_stack[DRAW_STACK_MAX];
|
||||
static CDRAW *draw_current = 0;
|
||||
static CFONT *default_font = 0;
|
||||
static GB_CLASS font_id = NULL;
|
||||
|
||||
#define THIS (draw_current)
|
||||
#define GFX (THIS->graphic)
|
||||
@ -56,10 +56,7 @@ static bool check_graphic(void)
|
||||
|
||||
void DRAW_begin(void *device)
|
||||
{
|
||||
static GB_CLASS font_id = NULL;
|
||||
|
||||
if (THIS >= &draw_stack[DRAW_STACK_MAX - 1])
|
||||
{
|
||||
if (THIS >= &draw_stack[DRAW_STACK_MAX - 1]) {
|
||||
GB.Error("Too many nested drawings");
|
||||
return;
|
||||
}
|
||||
@ -84,8 +81,7 @@ void DRAW_begin(void *device)
|
||||
GB.New(POINTER(&FONT), font_id, NULL, NULL);
|
||||
GB.Ref(FONT);
|
||||
|
||||
if (GB.Is(device, CLASS_Window))
|
||||
{
|
||||
if (GB.Is(device, CLASS_Window)) {
|
||||
THIS->device = device;
|
||||
THIS->graphic = new SDLgfx(WINDOWID(device));
|
||||
GB.Ref(THIS->device);
|
||||
|
@ -128,6 +128,42 @@ BEGIN_PROPERTY(CFONT_size)
|
||||
|
||||
END_PROPERTY
|
||||
|
||||
BEGIN_PROPERTY(CFONT_bold)
|
||||
|
||||
if (READ_PROPERTY)
|
||||
GB.ReturnBoolean(FONT->IsFontBold());
|
||||
else
|
||||
FONT->SetFontBold(VPROP(GB_BOOLEAN));
|
||||
|
||||
END_PROPERTY
|
||||
|
||||
BEGIN_PROPERTY(CFONT_italic)
|
||||
|
||||
if (READ_PROPERTY)
|
||||
GB.ReturnBoolean(FONT->IsFontItalic());
|
||||
else
|
||||
FONT->SetFontItalic(VPROP(GB_BOOLEAN));
|
||||
|
||||
END_PROPERTY
|
||||
|
||||
BEGIN_PROPERTY(CFONT_strikeout)
|
||||
|
||||
if (READ_PROPERTY)
|
||||
GB.ReturnBoolean(FONT->IsFontStrikeout());
|
||||
else
|
||||
FONT->SetFontStrikeout(VPROP(GB_BOOLEAN));
|
||||
|
||||
END_PROPERTY
|
||||
|
||||
BEGIN_PROPERTY(CFONT_underline)
|
||||
|
||||
if (READ_PROPERTY)
|
||||
GB.ReturnBoolean(FONT->IsFontUnderline());
|
||||
else
|
||||
FONT->SetFontUnderline(VPROP(GB_BOOLEAN));
|
||||
|
||||
END_PROPERTY
|
||||
|
||||
BEGIN_PROPERTY(CFONT_ascent)
|
||||
|
||||
GB.ReturnInteger(FONT->GetFontAscent());
|
||||
@ -174,30 +210,22 @@ GB_DESC CFont[] =
|
||||
|
||||
GB_PROPERTY("Name", "s", CFONT_name),
|
||||
GB_PROPERTY("Size", "i", CFONT_size),
|
||||
GB_PROPERTY("Bold", "b", CFONT_bold),
|
||||
GB_PROPERTY("Italic", "b", CFONT_italic),
|
||||
GB_PROPERTY("StrikeOut", "b", CFONT_strikeout),
|
||||
GB_PROPERTY("Underline", "b", CFONT_underline),
|
||||
|
||||
GB_PROPERTY_READ("Ascent", "i", CFONT_ascent),
|
||||
GB_PROPERTY_READ("Descent", "i", CFONT_descent),
|
||||
GB_PROPERTY_READ("Fixed", "b", CFONT_fixed),
|
||||
GB_PROPERTY_READ("Scalable", "b", CFONT_scalable),
|
||||
|
||||
|
||||
GB_METHOD("Width", "i", CFONT_width, "(Text)s"),
|
||||
GB_METHOD("Height", "i", CFONT_height, "(Text)s"),
|
||||
GB_METHOD("Image", "Image", CFONT_image, "(Text)s"),
|
||||
|
||||
/*
|
||||
GB_STATIC_METHOD("_init", NULL, CFONT_init, NULL),
|
||||
GB_STATIC_METHOD("_exit", NULL, CFONT_exit, NULL),
|
||||
|
||||
GB_PROPERTY("Grade", "i", CFONT_grade),
|
||||
GB_PROPERTY("Bold", "b", CFONT_bold),
|
||||
GB_PROPERTY("Italic", "b", CFONT_italic),
|
||||
GB_PROPERTY("Underline", "b", CFONT_underline),
|
||||
GB_PROPERTY("StrikeOut", "b", CFONT_strikeout),
|
||||
|
||||
GB_METHOD("ToString", "s", CFONT_to_string, NULL),
|
||||
GB_STATIC_METHOD("_get", "Font", CFONT_get, "(Font)s"),
|
||||
|
||||
GB_PROPERTY_READ("Styles", "String[]", CFONT_styles),
|
||||
*/
|
||||
GB_END_DECLARE
|
||||
};
|
||||
|
@ -93,7 +93,6 @@ SDLapplication::~SDLapplication()
|
||||
return;
|
||||
}
|
||||
|
||||
SDLfont::Exit();
|
||||
TTF_Quit();
|
||||
Uint32 sysInit = SDL_WasInit(SDL_INIT_EVERYTHING);
|
||||
|
||||
|
@ -38,10 +38,7 @@ typedef struct {
|
||||
} fontdesc;
|
||||
|
||||
static std::vector<fontdesc> fontDB;
|
||||
|
||||
static StringList _FontList;
|
||||
Display *SDLfont::display = 0;
|
||||
int SDLfont::screen = 0;
|
||||
|
||||
#define DEFAULT_FONT_SIZE 20
|
||||
#define DEFAULT_DPI 72 /* Default DPI size in SDL_TTF */
|
||||
@ -56,13 +53,17 @@ inline bool cmp_db_nocase(const fontdesc x, const fontdesc y)
|
||||
|
||||
StringList SDLfont::GetFontList(void )
|
||||
{
|
||||
if (LIKELY(_FontList.empty() == 0))
|
||||
return _FontList;
|
||||
return _FontList;
|
||||
}
|
||||
|
||||
Display *disp = SDLapp->X11appDisplay();
|
||||
XftFontSet *FntnameSet = XftListFonts(disp, DefaultScreen(disp), NULL,
|
||||
XFT_FAMILY, /*XFT_FILE, XFT_FOUNDRY,*/ NULL);
|
||||
void SDLfont::Init()
|
||||
{
|
||||
Display *disp = XOpenDisplay(NULL);
|
||||
int scr = XDefaultScreen(disp);
|
||||
int i;
|
||||
|
||||
XftFontSet *FntnameSet = XftListFonts(disp, scr, 0,
|
||||
XFT_FAMILY, NULL);
|
||||
|
||||
// Get the fonts name
|
||||
for (i = 0; i < FntnameSet->nfont; i++) {
|
||||
@ -76,7 +77,7 @@ StringList SDLfont::GetFontList(void )
|
||||
if (res!=XftResultMatch)
|
||||
continue;
|
||||
|
||||
XftFontSet *Fntdetail = XftListFonts(disp, DefaultScreen(disp),
|
||||
XftFontSet *Fntdetail = XftListFonts(disp, scr,
|
||||
XFT_FAMILY, XftTypeString, name[0], 0,
|
||||
XFT_FOUNDRY, NULL);
|
||||
j = Fntdetail->nfont;
|
||||
@ -118,7 +119,7 @@ StringList SDLfont::GetFontList(void )
|
||||
while (i<int(fontDB.size())) {
|
||||
char *res[255];
|
||||
|
||||
XftFontSet *Fntdetail = XftListFonts(disp, DefaultScreen(disp),
|
||||
XftFontSet *Fntdetail = XftListFonts(disp, scr,
|
||||
XFT_FAMILY, XftTypeString, fontDB[i].realname.c_str(),
|
||||
XFT_FOUNDRY, XftTypeString, fontDB[i].foundry.c_str(), 0,
|
||||
XFT_FILE, NULL);
|
||||
@ -128,18 +129,8 @@ StringList SDLfont::GetFontList(void )
|
||||
XFree(Fntdetail);
|
||||
i++;
|
||||
}
|
||||
|
||||
return _FontList;
|
||||
}
|
||||
|
||||
void SDLfont::Init()
|
||||
{
|
||||
display = SDLapp->X11appDisplay();
|
||||
screen = DefaultScreen(display);
|
||||
}
|
||||
|
||||
void SDLfont::Exit()
|
||||
{
|
||||
|
||||
XCloseDisplay(disp);
|
||||
}
|
||||
|
||||
SDLfont::SDLfont()
|
||||
@ -148,7 +139,6 @@ SDLfont::SDLfont()
|
||||
hfontsize = DEFAULT_FONT_SIZE;
|
||||
hfontindex = 0;
|
||||
|
||||
SDLfont::GetFontList();
|
||||
hfontname = fontDB[hfontindex].path;
|
||||
hSDLfont = TTF_OpenFont(hfontname.c_str(), hfontsize);
|
||||
|
||||
@ -180,35 +170,77 @@ void SDLfont::SetFontName(char* name)
|
||||
|
||||
const char* SDLfont::GetFontName(void )
|
||||
{
|
||||
if (hfonttype == SDLTTF_font) {
|
||||
std::string name;
|
||||
name = hfontname.substr((hfontname.find_last_of("/"))+1);
|
||||
return name.c_str();
|
||||
}
|
||||
if (hfonttype == SDLTTF_font)
|
||||
return hfontname.substr((hfontname.find_last_of("/"))+1).c_str();
|
||||
else
|
||||
return fontDB[hfontindex].name.c_str();
|
||||
}
|
||||
|
||||
void SDLfont::SetFontSize(int size)
|
||||
{
|
||||
int style = TTF_GetFontStyle(hSDLfont);
|
||||
hfontsize = size;
|
||||
|
||||
if (hSDLfont)
|
||||
TTF_CloseFont(hSDLfont);
|
||||
|
||||
TTF_CloseFont(hSDLfont);
|
||||
hSDLfont = TTF_OpenFont(hfontname.c_str(), hfontsize);
|
||||
|
||||
if (UNLIKELY(hSDLfont == NULL))
|
||||
SDLerror::RaiseError(TTF_GetError());
|
||||
|
||||
TTF_SetFontStyle(hSDLfont, style);
|
||||
}
|
||||
|
||||
void SDLfont::SetFontBold(bool state)
|
||||
{
|
||||
if (state == (TTF_GetFontStyle(hSDLfont) & TTF_STYLE_BOLD))
|
||||
return;
|
||||
|
||||
TTF_SetFontStyle(hSDLfont, (TTF_GetFontStyle(hSDLfont) ^ TTF_STYLE_BOLD));
|
||||
}
|
||||
|
||||
bool SDLfont::IsFontBold(void )
|
||||
{
|
||||
return (TTF_GetFontStyle(hSDLfont) & TTF_STYLE_BOLD);
|
||||
}
|
||||
|
||||
|
||||
void SDLfont::SetFontItalic(bool state)
|
||||
{
|
||||
if (state == (TTF_GetFontStyle(hSDLfont) & TTF_STYLE_ITALIC))
|
||||
return;
|
||||
|
||||
TTF_SetFontStyle(hSDLfont, (TTF_GetFontStyle(hSDLfont) ^ TTF_STYLE_ITALIC));
|
||||
}
|
||||
|
||||
bool SDLfont::IsFontItalic(void )
|
||||
{
|
||||
return (TTF_GetFontStyle(hSDLfont) & TTF_STYLE_ITALIC);
|
||||
}
|
||||
|
||||
void SDLfont::SetFontStrikeout(bool state)
|
||||
{
|
||||
if (state == (TTF_GetFontStyle(hSDLfont) & TTF_STYLE_STRIKETHROUGH))
|
||||
return;
|
||||
|
||||
TTF_SetFontStyle(hSDLfont, (TTF_GetFontStyle(hSDLfont) ^ TTF_STYLE_STRIKETHROUGH));
|
||||
}
|
||||
|
||||
bool SDLfont::IsFontStrikeout(void )
|
||||
{
|
||||
return (TTF_GetFontStyle(hSDLfont) & TTF_STYLE_STRIKETHROUGH);
|
||||
}
|
||||
|
||||
void SDLfont::SetFontUnderline(bool state)
|
||||
{
|
||||
if (state == (TTF_GetFontStyle(hSDLfont) & TTF_STYLE_UNDERLINE))
|
||||
return;
|
||||
|
||||
TTF_SetFontStyle(hSDLfont, (TTF_GetFontStyle(hSDLfont) ^ TTF_STYLE_UNDERLINE));
|
||||
}
|
||||
|
||||
bool SDLfont::IsFontUnderlined(void )
|
||||
bool SDLfont::IsFontUnderline(void )
|
||||
{
|
||||
return false;
|
||||
return (TTF_GetFontStyle(hSDLfont) & TTF_STYLE_UNDERLINE);
|
||||
}
|
||||
|
||||
bool SDLfont::IsFontScalable(void )
|
||||
@ -233,7 +265,7 @@ bool SDLfont::IsFontFixed(void )
|
||||
|
||||
void SDLfont::SizeText(const char *text, int *width, int *height)
|
||||
{
|
||||
TTF_SizeText(hSDLfont, text, width, height);
|
||||
TTF_SizeUTF8(hSDLfont, text, width, height);
|
||||
}
|
||||
|
||||
SDLsurface* SDLfont::RenderText(const char* text)
|
||||
|
@ -48,10 +48,12 @@ public:
|
||||
|
||||
static StringList GetFontList(void );
|
||||
static void Init(void );
|
||||
static void Exit(void );
|
||||
|
||||
void SetFontName(char* name);
|
||||
void SetFontSize(int size);
|
||||
void SetFontBold(bool state);
|
||||
void SetFontItalic(bool state);
|
||||
void SetFontStrikeout(bool state);
|
||||
void SetFontUnderline(bool state);
|
||||
|
||||
const char* GetFontName(void );
|
||||
@ -60,7 +62,10 @@ public:
|
||||
int GetFontDescent(void );
|
||||
|
||||
bool IsFontFixed(void );
|
||||
bool IsFontUnderlined(void );
|
||||
bool IsFontBold(void );
|
||||
bool IsFontItalic(void );
|
||||
bool IsFontStrikeout(void );
|
||||
bool IsFontUnderline(void );
|
||||
bool IsFontScalable(void );
|
||||
|
||||
void SizeText(const char* text, int *width, int *height);
|
||||
@ -69,16 +74,13 @@ public:
|
||||
private:
|
||||
int hfonttype;
|
||||
int hfontsize;
|
||||
int hfontindex;
|
||||
std::string hfontname;
|
||||
|
||||
/* SDL_TTF font */
|
||||
TTF_Font *hSDLfont;
|
||||
int hSDLfontstyle;
|
||||
|
||||
/* System font */
|
||||
static Display *display;
|
||||
static int screen;
|
||||
int hfontindex;
|
||||
};
|
||||
|
||||
#endif /* _SDLFONT_H */
|
||||
|
Loading…
x
Reference in New Issue
Block a user