Another string map.

This time the font tables.
This commit is contained in:
2025-08-11 04:01:27 -04:00
parent aa6fdaddb3
commit 06a2b527a6
2 changed files with 15 additions and 23 deletions

View File

@ -52,9 +52,7 @@ container::typed::HashTable <dw::core::style::FontAttrs,
new container::typed::HashTable <dw::core::style::FontAttrs, new container::typed::HashTable <dw::core::style::FontAttrs,
FltkFont> (false, false); FltkFont> (false, false);
container::typed::HashTable <lout::object::ConstString, std::optional< std::map< std::string, std::unique_ptr< FltkFont::FontFamily > > > FltkFont::systemFonts;
FltkFont::FontFamily> *FltkFont::systemFonts =
NULL;
FltkFont::FontFamily FltkFont::standardFontFamily (FL_HELVETICA, FltkFont::FontFamily FltkFont::standardFontFamily (FL_HELVETICA,
FL_HELVETICA_BOLD, FL_HELVETICA_BOLD,
@ -108,10 +106,12 @@ FltkFont::FltkFont (core::style::FontAttrs *attrs)
if (style != core::style::FONT_STYLE_NORMAL) if (style != core::style::FONT_STYLE_NORMAL)
fa |= FL_ITALIC; fa |= FL_ITALIC;
object::ConstString nameString (name); FontFamily *family= nullptr;
FontFamily *family = systemFonts->get (&nameString); if ( not systemFonts->contains( name ) )
if (!family)
family = &standardFontFamily; family = &standardFontFamily;
else
family= systemFonts->at( name ).get();
assert( family );
font = family->get (fa); font = family->get (fa);
@ -141,8 +141,7 @@ static void strstrip(char *big, const char *little)
void FltkFont::initSystemFonts () void FltkFont::initSystemFonts ()
{ {
systemFonts = new container::typed::HashTable systemFonts.emplace();
<lout::object::ConstString, FontFamily> (true, true);
int k = Fl::set_fonts ("-*-iso10646-1"); int k = Fl::set_fonts ("-*-iso10646-1");
for (int i = 0; i < k; i++) { for (int i = 0; i < k; i++) {
@ -158,19 +157,16 @@ void FltkFont::initSystemFonts ()
_MSG("Found font: %s%s%s\n", name, t & FL_BOLD ? " bold" : "", _MSG("Found font: %s%s%s\n", name, t & FL_BOLD ? " bold" : "",
t & FL_ITALIC ? " italic" : ""); t & FL_ITALIC ? " italic" : "");
object::String *familyName = new object::String(name);
free (name); free (name);
FontFamily *family = systemFonts->get (familyName);
if (family) { auto &family = (*systemFonts)[ name ];
if( family ) {
family->set ((Fl_Font) i, t); family->set ((Fl_Font) i, t);
delete familyName;
} else { } else {
// set first font of family also as normal font in case there // set first font of family also as normal font in case there
// is no normal (non-bold, non-italic) font // is no normal (non-bold, non-italic) font
family = new FontFamily ((Fl_Font) i, -1, -1, -1); family = std::make_unique< FontFamily >( (Fl_Font) i, -1, -1, -1 );
family->set ((Fl_Font) i, t); family->set( (Fl_Font) i, t );
systemFonts->put (familyName, family);
} }
} }
} }
@ -180,8 +176,7 @@ FltkFont::fontExists (const char *name)
{ {
if (!systemFonts) if (!systemFonts)
initSystemFonts (); initSystemFonts ();
object::ConstString familyName (name); return systemFonts->contains(name);
return systemFonts->get (&familyName) != NULL;
} }
Fl_Font Fl_Font
@ -189,10 +184,8 @@ FltkFont::get (const char *name, int attrs)
{ {
if (!systemFonts) if (!systemFonts)
initSystemFonts (); initSystemFonts ();
object::ConstString familyName (name); if( systemFonts->contains( name ) )
FontFamily *family = systemFonts->get (&familyName); return systemFonts->at( name )->get (attrs);
if (family)
return family->get (attrs);
else else
return FL_HELVETICA; return FL_HELVETICA;
} }

View File

@ -25,8 +25,7 @@ class FltkFont: public core::style::Font
static FontFamily standardFontFamily; static FontFamily standardFontFamily;
static lout::container::typed::HashTable <lout::object::ConstString, static std::optional< std::map< std::string, std::unique_ptr< FontFamily > > > systemFonts;
FontFamily> *systemFonts;
static lout::container::typed::HashTable <dw::core::style::FontAttrs, static lout::container::typed::HashTable <dw::core::style::FontAttrs,
FltkFont> *fontsTable; FltkFont> *fontsTable;