From aca97e210e4ebde189d9f048363bd31dba661cb3 Mon Sep 17 00:00:00 2001 From: Michael Liebmann Date: Wed, 3 Jul 2013 01:15:28 +0200 Subject: [PATCH] CNeutrinoFonts: Fix segfault, when fontfile changed - use std::string for font and style names --- src/driver/neutrinofonts.cpp | 54 ++++++++++++++++++++++-------------- src/driver/neutrinofonts.h | 7 +++-- src/neutrino.cpp | 2 +- 3 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/driver/neutrinofonts.cpp b/src/driver/neutrinofonts.cpp index 93e34f32d..a20d03024 100644 --- a/src/driver/neutrinofonts.cpp +++ b/src/driver/neutrinofonts.cpp @@ -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; diff --git a/src/driver/neutrinofonts.h b/src/driver/neutrinofonts.h index 1aefa4fcd..959e7b9e5 100644 --- a/src/driver/neutrinofonts.h +++ b/src/driver/neutrinofonts.h @@ -28,8 +28,8 @@ #define __neutrinofonts__ typedef struct neutrino_font_descr { - const char *name; - const char *filename; + std::string name; + std::string filename; int size_offset; } neutrino_font_descr_struct; @@ -51,7 +51,7 @@ typedef struct font_sizes_groups { class CNeutrinoFonts { private: - const char * fontStyle[3]; + std::string fontStyle[3]; typedef struct dyn_font_t { @@ -97,6 +97,7 @@ class CNeutrinoFonts static CNeutrinoFonts* getInstance(); neutrino_font_descr_struct fontDescr; + neutrino_font_descr_struct old_fontDescr; void SetupNeutrinoFonts(); void refreshDynFonts(); diff --git a/src/neutrino.cpp b/src/neutrino.cpp index a37f5d23e..cdcdfed51 100644 --- a/src/neutrino.cpp +++ b/src/neutrino.cpp @@ -1742,7 +1742,7 @@ TIMER_START(); CHintBox * hintBox = new CHintBox(LOCALE_MESSAGEBOX_INFO, g_Locale->getText(LOCALE_NEUTRINO_STARTING)); hintBox->paint(); - CVFD::getInstance()->init(neutrinoFonts->fontDescr.filename, neutrinoFonts->fontDescr.name); + CVFD::getInstance()->init(neutrinoFonts->fontDescr.filename.c_str(), neutrinoFonts->fontDescr.name.c_str()); CVFD::getInstance()->Clear(); CVFD::getInstance()->ShowText(g_Locale->getText(LOCALE_NEUTRINO_STARTING)); CVFD::getInstance()->setBacklight(g_settings.backlight_tv);