mirror of
git://projects.qi-hardware.com/gmenu2x.git
synced 2024-11-17 23:28:27 +02:00
Do not construct Font objects that crash gmenu2x if font loading fails.
This commit ensures that a fully constructed Font object will not crash gmenu2x when it is used, even if loading the font or initialising SDL_ttf altogether has failed. Instead, it will render no text, but icons and images are still drawn. The proper way to signal an error would be to throw an exception and fail to construct the Font object. However, gmenu2x does not use exceptions.
This commit is contained in:
parent
738c296c67
commit
4474a42767
27
src/font.cpp
27
src/font.cpp
@ -21,6 +21,9 @@ Font *Font::defaultFont()
|
|||||||
|
|
||||||
Font::Font(const std::string &path, unsigned int size)
|
Font::Font(const std::string &path, unsigned int size)
|
||||||
{
|
{
|
||||||
|
font = nullptr;
|
||||||
|
fontheight = 1;
|
||||||
|
|
||||||
/* Note: TTF_Init and TTF_Quit perform reference counting, so call them
|
/* Note: TTF_Init and TTF_Quit perform reference counting, so call them
|
||||||
* both unconditionally for each font. */
|
* both unconditionally for each font. */
|
||||||
if (TTF_Init() < 0) {
|
if (TTF_Init() < 0) {
|
||||||
@ -31,6 +34,7 @@ Font::Font(const std::string &path, unsigned int size)
|
|||||||
font = TTF_OpenFont(path.c_str(), size);
|
font = TTF_OpenFont(path.c_str(), size);
|
||||||
if (!font) {
|
if (!font) {
|
||||||
ERROR("Unable to open font\n");
|
ERROR("Unable to open font\n");
|
||||||
|
TTF_Quit();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,20 +43,29 @@ Font::Font(const std::string &path, unsigned int size)
|
|||||||
|
|
||||||
Font::~Font()
|
Font::~Font()
|
||||||
{
|
{
|
||||||
TTF_CloseFont(font);
|
if (font) {
|
||||||
TTF_Quit();
|
TTF_CloseFont(font);
|
||||||
|
TTF_Quit();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int Font::getTextWidth(const char *text)
|
int Font::getTextWidth(const char *text)
|
||||||
{
|
{
|
||||||
int w, h;
|
if (font) {
|
||||||
TTF_SizeUTF8(font, text, &w, &h);
|
int w, h;
|
||||||
return w;
|
TTF_SizeUTF8(font, text, &w, &h);
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
else return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Font::write(Surface *surface, const string &text,
|
void Font::write(Surface *surface, const string &text,
|
||||||
int x, int y, HAlign halign, VAlign valign)
|
int x, int y, HAlign halign, VAlign valign)
|
||||||
{
|
{
|
||||||
|
if (!font) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (text.find("\n", 0) == string::npos) {
|
if (text.find("\n", 0) == string::npos) {
|
||||||
writeLine(surface, text.c_str(), x, y, halign, valign);
|
writeLine(surface, text.c_str(), x, y, halign, valign);
|
||||||
return;
|
return;
|
||||||
@ -70,6 +83,10 @@ void Font::write(Surface *surface, const string &text,
|
|||||||
void Font::writeLine(Surface *surface, const char *text,
|
void Font::writeLine(Surface *surface, const char *text,
|
||||||
int x, int y, HAlign halign, VAlign valign)
|
int x, int y, HAlign halign, VAlign valign)
|
||||||
{
|
{
|
||||||
|
if (!font) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
switch (halign) {
|
switch (halign) {
|
||||||
case HAlignLeft:
|
case HAlignLeft:
|
||||||
break;
|
break;
|
||||||
|
Loading…
Reference in New Issue
Block a user