CNeutrinoFonts: Fix segfault, when fontfile changed

- use std::string for font and style names
This commit is contained in:
Michael Liebmann
2013-07-03 01:15:28 +02:00
committed by Thilo Graf
parent 0a0035c2ae
commit aca97e210e
3 changed files with 38 additions and 25 deletions

View File

@@ -52,7 +52,14 @@ const font_sizes_struct signal_font = {LOCALE_FONTSIZE_INFOBAR_SMALL, 14, CNeutr
CNeutrinoFonts::CNeutrinoFonts()
{
useDigitOffset = false;
memset(&fontDescr, 0, sizeof(neutrino_font_descr_struct));
fontDescr.name = "";
fontDescr.filename = "";
fontDescr.size_offset = 0;
old_fontDescr.name = "";
old_fontDescr.filename = "";
old_fontDescr.size_offset = 0;
InitDynFonts();
}
@@ -100,36 +107,37 @@ void CNeutrinoFonts::SetupNeutrinoFonts()
delete g_fontRenderer;
g_fontRenderer = new FBFontRenderClass(72 * g_settings.screen_xres / 100, 72 * g_settings.screen_yres / 100);
if (fontDescr.filename != NULL)
free((void *)fontDescr.filename);
old_fontDescr.size_offset = fontDescr.size_offset;
old_fontDescr.filename = fontDescr.filename;
fontDescr.filename = "";
printf("[neutrino] settings font file %s\n", g_settings.font_file);
if (access(g_settings.font_file, F_OK)) {
if (!access(FONTDIR"/neutrino.ttf", F_OK)) {
fontDescr.filename = strdup(FONTDIR"/neutrino.ttf");
strcpy(g_settings.font_file, fontDescr.filename);
fontDescr.filename = FONTDIR"/neutrino.ttf";
strcpy(g_settings.font_file, fontDescr.filename.c_str());
} else {
fprintf( stderr,"[neutrino] font file [%s] not found\n neutrino exit\n",FONTDIR"/neutrino.ttf");
_exit(0);
}
} else
fontDescr.filename = strdup(g_settings.font_file);
fontStyle[0] = g_fontRenderer->AddFont(fontDescr.filename);
fontDescr.filename = g_settings.font_file;
fontStyle[0] = g_fontRenderer->AddFont(fontDescr.filename.c_str());
if (fontDescr.name != NULL)
free((void *)fontDescr.name);
fontDescr.name = strdup(g_fontRenderer->getFamily(fontDescr.filename).c_str());
printf("[neutrino] font family %s\n", fontDescr.name);
old_fontDescr.name = fontDescr.name;
fontDescr.name = "";
fontDescr.name = g_fontRenderer->getFamily(fontDescr.filename.c_str());
printf("[neutrino] font family %s\n", fontDescr.name.c_str());
fontStyle[1] = "Bold Regular";
g_fontRenderer->AddFont(fontDescr.filename, true); // make italics
g_fontRenderer->AddFont(fontDescr.filename.c_str(), true); // make italics
fontStyle[2] = "Italic";
for (int i = 0; i < SNeutrinoSettings::FONT_TYPE_COUNT; i++) {
if (g_Font[i]) delete g_Font[i];
g_Font[i] = g_fontRenderer->getFont(fontDescr.name, fontStyle[neutrino_font[i].style], CNeutrinoApp::getInstance()->getConfigFile()->getInt32(locale_real_names[neutrino_font[i].name], neutrino_font[i].defaultsize) + neutrino_font[i].size_offset * fontDescr.size_offset);
g_Font[i] = g_fontRenderer->getFont(fontDescr.name.c_str(), fontStyle[neutrino_font[i].style].c_str(), CNeutrinoApp::getInstance()->getConfigFile()->getInt32(locale_real_names[neutrino_font[i].name], neutrino_font[i].defaultsize) + neutrino_font[i].size_offset * fontDescr.size_offset);
}
g_SignalFont = g_fontRenderer->getFont(fontDescr.name, fontStyle[signal_font.style], signal_font.defaultsize + signal_font.size_offset * fontDescr.size_offset);
g_SignalFont = g_fontRenderer->getFont(fontDescr.name.c_str(), fontStyle[signal_font.style].c_str(), signal_font.defaultsize + signal_font.size_offset * fontDescr.size_offset);
}
void CNeutrinoFonts::refreshDynFonts()
@@ -160,12 +168,16 @@ void CNeutrinoFonts::refreshDynFont(int dx, int dy, std::string text, int style,
useDigitOffset = dyn_font->useDigitOffset;
int dynSize = getDynFontSize(dx, dy, text, style);
useDigitOffset = tmp;
if (dyn_font->size == dynSize)
if ((dyn_font->size == dynSize) && (old_fontDescr.name == fontDescr.name) && (old_fontDescr.filename == fontDescr.filename))
return;
old_fontDescr.filename = fontDescr.filename;
old_fontDescr.name = fontDescr.name;
old_fontDescr.size_offset = fontDescr.size_offset;
if (dyn_font->font != NULL)
delete dyn_font->font;
Font *dynFont = g_fontRenderer->getFont(fontDescr.name, fontStyle[style], dynSize);
Font *dynFont = g_fontRenderer->getFont(fontDescr.name.c_str(), fontStyle[style].c_str(), dynSize);
dyn_font->font = dynFont;
dyn_font->size = dynSize;
printf("##### [%s] change %s_font size old %d to new %d, index: %u\n", __FUNCTION__, (isShare)?"share":"dyn", oldSize, dyn_font->size, index);
@@ -191,7 +203,7 @@ int CNeutrinoFonts::getDynFontSize(int dx, int dy, std::string text, int style)
while (1) {
if (dynFont != NULL)
delete dynFont;
dynFont = g_fontRenderer->getFont(fontDescr.name, fontStyle[style], dynSize);
dynFont = g_fontRenderer->getFont(fontDescr.name.c_str(), fontStyle[style].c_str(), dynSize);
// calculate height & width
_height = getFontHeight(dynFont);
std::string tmpText = text;
@@ -258,7 +270,7 @@ Font **CNeutrinoFonts::getDynFontWithID(int &dx, int &dy, std::string text, int
{
if ((dx <= 0) && (dy <= 0))
return NULL;
if ((fontDescr.name == NULL) || (fontDescr.filename == NULL) || (g_fontRenderer == NULL))
if ((fontDescr.name == "") || (fontDescr.filename == "") || (g_fontRenderer == NULL))
SetupNeutrinoFonts();
int dynSize = getDynFontSize(dx, dy, text, style);
@@ -273,7 +285,7 @@ Font **CNeutrinoFonts::getDynFontWithID(int &dx, int &dy, std::string text, int
return &(v_dyn_fonts[f_id].font);
}
dynFont = g_fontRenderer->getFont(fontDescr.name, fontStyle[style], dynSize);
dynFont = g_fontRenderer->getFont(fontDescr.name.c_str(), fontStyle[style].c_str(), dynSize);
if (v_dyn_fonts[f_id].font != NULL)
delete v_dyn_fonts[f_id].font;
v_dyn_fonts[f_id].dx = dx;
@@ -299,7 +311,7 @@ Font **CNeutrinoFonts::getDynFontShare(int &dx, int &dy, std::string text, int s
{
if ((dx <= 0) && (dy <= 0))
return NULL;
if ((fontDescr.name == NULL) || (fontDescr.filename == NULL) || (g_fontRenderer == NULL))
if ((fontDescr.name == "") || (fontDescr.filename == "") || (g_fontRenderer == NULL))
SetupNeutrinoFonts();
int dynSize = getDynFontSize(dx, dy, text, style);
@@ -327,7 +339,7 @@ Font **CNeutrinoFonts::getDynFontShare(int &dx, int &dy, std::string text, int s
ret = &(v_share_fonts[i].font);
}
else {
dynFont = g_fontRenderer->getFont(fontDescr.name, fontStyle[style], dynSize);
dynFont = g_fontRenderer->getFont(fontDescr.name.c_str(), fontStyle[style].c_str(), dynSize);
dyn_font_t dyn_font;
dyn_font.dx = dx;
dyn_font.dy = dy;